| 1420 | } |
| 1421 | |
| 1422 | static int |
| 1423 | BufFileLoadCompressedBuffer(BufFile *file, void *buffer, size_t bufsize) |
| 1424 | { |
| 1425 | ZSTD_outBuffer output; |
| 1426 | size_t ret; |
| 1427 | bool eof = false; |
| 1428 | off_t pos = 0; |
| 1429 | |
| 1430 | if (file->decompression_finished) |
| 1431 | return 0; |
| 1432 | |
| 1433 | /* Initialize Zstd output buffer. */ |
| 1434 | output.dst = buffer; |
| 1435 | output.size = bufsize; |
| 1436 | output.pos = 0; |
| 1437 | |
| 1438 | do |
| 1439 | { |
| 1440 | /* No more compressed input? Load some. */ |
| 1441 | if (file->compressed_buffer.pos == file->compressed_buffer.size) |
| 1442 | { |
| 1443 | int nb; |
| 1444 | |
| 1445 | nb = FileRead(file->files[0], (char *) file->compressed_buffer.src, BLCKSZ, file->curOffset + file->pos + pos, WAIT_EVENT_BUFFILE_READ); |
| 1446 | if (nb < 0) |
| 1447 | { |
| 1448 | elog(ERROR, "could not read from temporary file: %m"); |
| 1449 | } |
| 1450 | pos += nb; |
| 1451 | file->compressed_buffer.size = nb; |
| 1452 | file->compressed_buffer.pos = 0; |
| 1453 | |
| 1454 | if (nb == 0) |
| 1455 | eof = true; |
| 1456 | } |
| 1457 | |
| 1458 | /* Decompress, and check result */ |
| 1459 | ret = ZSTD_decompressStream(file->zstd_context->dctx, &output, &file->compressed_buffer); |
| 1460 | if (ZSTD_isError(ret)) |
| 1461 | elog(ERROR, "zstd decompression failed: %s", ZSTD_getErrorName(ret)); |
| 1462 | |
| 1463 | if (ret == 0) |
| 1464 | { |
| 1465 | /* End of compressed data. */ |
| 1466 | Assert (file->compressed_buffer.pos == file->compressed_buffer.size); |
| 1467 | file->decompression_finished = true; |
| 1468 | break; |
| 1469 | } |
| 1470 | |
| 1471 | if (ret > 0 && eof && output.pos < output.size) |
| 1472 | { |
| 1473 | /* |
| 1474 | * We ran out of compressed input, but Zstd expects more. File was |
| 1475 | * truncated on disk after we wrote it? |
| 1476 | */ |
| 1477 | elog(ERROR, "unexpected end of compressed temporary file"); |
| 1478 | } |
| 1479 | } |
no test coverage detected