LCOV - differential code coverage report
Current view: top level - src/bin/pg_dump - compress_zstd.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 84.3 % 248 209 39 209
Current Date: 2025-09-06 07:49:51 +0900 Functions: 88.9 % 18 16 2 16
Baseline: lcov-20250906-005545-baseline Branches: 61.5 % 122 75 47 75
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 49.0 % 51 25 26 25
(30,360] days: 100.0 % 2 2 2
(360..) days: 93.3 % 195 182 13 182
Function coverage date bins:
(7,30] days: 100.0 % 2 2 2
(360..) days: 87.5 % 16 14 2 14
Branch coverage date bins:
(7,30] days: 38.5 % 26 10 16 10
(360..) days: 67.7 % 96 65 31 65

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * compress_zstd.c
                                  4                 :                :  *   Routines for archivers to write a Zstd compressed data stream.
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  * IDENTIFICATION
                                 10                 :                :  *     src/bin/pg_dump/compress_zstd.c
                                 11                 :                :  *
                                 12                 :                :  *-------------------------------------------------------------------------
                                 13                 :                :  */
                                 14                 :                : 
                                 15                 :                : #include "postgres_fe.h"
                                 16                 :                : #include <unistd.h>
                                 17                 :                : 
                                 18                 :                : #include "compress_zstd.h"
                                 19                 :                : #include "pg_backup_utils.h"
                                 20                 :                : 
                                 21                 :                : #ifndef USE_ZSTD
                                 22                 :                : 
                                 23                 :                : void
                                 24                 :                : InitCompressorZstd(CompressorState *cs, const pg_compress_specification compression_spec)
                                 25                 :                : {
                                 26                 :                :     pg_fatal("this build does not support compression with %s", "ZSTD");
                                 27                 :                : }
                                 28                 :                : 
                                 29                 :                : void
                                 30                 :                : InitCompressFileHandleZstd(CompressFileHandle *CFH, const pg_compress_specification compression_spec)
                                 31                 :                : {
                                 32                 :                :     pg_fatal("this build does not support compression with %s", "ZSTD");
                                 33                 :                : }
                                 34                 :                : 
                                 35                 :                : #else
                                 36                 :                : 
                                 37                 :                : #include <zstd.h>
                                 38                 :                : 
                                 39                 :                : typedef struct ZstdCompressorState
                                 40                 :                : {
                                 41                 :                :     /* This is a normal file to which we read/write compressed data */
                                 42                 :                :     FILE       *fp;
                                 43                 :                : 
                                 44                 :                :     ZSTD_CStream *cstream;
                                 45                 :                :     ZSTD_DStream *dstream;
                                 46                 :                :     ZSTD_outBuffer output;
                                 47                 :                :     ZSTD_inBuffer input;
                                 48                 :                : 
                                 49                 :                :     /* pointer to a static string like from strerror(), for Zstd_write() */
                                 50                 :                :     const char *zstderror;
                                 51                 :                : } ZstdCompressorState;
                                 52                 :                : 
                                 53                 :                : static ZSTD_CStream *_ZstdCStreamParams(pg_compress_specification compress);
                                 54                 :                : static void EndCompressorZstd(ArchiveHandle *AH, CompressorState *cs);
                                 55                 :                : static void WriteDataToArchiveZstd(ArchiveHandle *AH, CompressorState *cs,
                                 56                 :                :                                    const void *data, size_t dLen);
                                 57                 :                : static void ReadDataFromArchiveZstd(ArchiveHandle *AH, CompressorState *cs);
                                 58                 :                : 
                                 59                 :                : static void
  885 tomas.vondra@postgre       60                 :CBC          81 : _Zstd_CCtx_setParam_or_die(ZSTD_CStream *cstream,
                                 61                 :                :                            ZSTD_cParameter param, int value, char *paramname)
                                 62                 :                : {
                                 63                 :                :     size_t      res;
                                 64                 :                : 
                                 65                 :             81 :     res = ZSTD_CCtx_setParameter(cstream, param, value);
                                 66         [ -  + ]:             81 :     if (ZSTD_isError(res))
  843 alvherre@alvh.no-ip.       67                 :UBC           0 :         pg_fatal("could not set compression parameter \"%s\": %s",
                                 68                 :                :                  paramname, ZSTD_getErrorName(res));
  885 tomas.vondra@postgre       69                 :CBC          81 : }
                                 70                 :                : 
                                 71                 :                : /* Return a compression stream with parameters set per argument */
                                 72                 :                : static ZSTD_CStream *
                                 73                 :             80 : _ZstdCStreamParams(pg_compress_specification compress)
                                 74                 :                : {
                                 75                 :                :     ZSTD_CStream *cstream;
                                 76                 :                : 
                                 77                 :             80 :     cstream = ZSTD_createCStream();
                                 78         [ -  + ]:             80 :     if (cstream == NULL)
  885 tomas.vondra@postgre       79                 :UBC           0 :         pg_fatal("could not initialize compression library");
                                 80                 :                : 
  885 tomas.vondra@postgre       81                 :CBC          80 :     _Zstd_CCtx_setParam_or_die(cstream, ZSTD_c_compressionLevel,
                                 82                 :                :                                compress.level, "level");
                                 83                 :                : 
  884                            84         [ +  + ]:             80 :     if (compress.options & PG_COMPRESSION_OPTION_LONG_DISTANCE)
                                 85                 :              1 :         _Zstd_CCtx_setParam_or_die(cstream,
                                 86                 :                :                                    ZSTD_c_enableLongDistanceMatching,
  841 tgl@sss.pgh.pa.us          87                 :              1 :                                    compress.long_distance, "long");
                                 88                 :                : 
  885 tomas.vondra@postgre       89                 :             80 :     return cstream;
                                 90                 :                : }
                                 91                 :                : 
                                 92                 :                : /* Helper function for WriteDataToArchiveZstd and EndCompressorZstd */
                                 93                 :                : static void
                                 94                 :            209 : _ZstdWriteCommon(ArchiveHandle *AH, CompressorState *cs, bool flush)
                                 95                 :                : {
                                 96                 :            209 :     ZstdCompressorState *zstdcs = (ZstdCompressorState *) cs->private_data;
                                 97                 :            209 :     ZSTD_inBuffer *input = &zstdcs->input;
                                 98                 :            209 :     ZSTD_outBuffer *output = &zstdcs->output;
                                 99                 :                : 
                                100                 :                :     /* Loop while there's any input or until flushed */
                                101   [ +  +  +  - ]:            209 :     while (input->pos != input->size || flush)
                                102                 :                :     {
                                103                 :                :         size_t      res;
                                104                 :                : 
                                105                 :            209 :         output->pos = 0;
                                106         [ +  + ]:            209 :         res = ZSTD_compressStream2(zstdcs->cstream, output,
                                107                 :                :                                    input, flush ? ZSTD_e_end : ZSTD_e_continue);
                                108                 :                : 
                                109         [ -  + ]:            209 :         if (ZSTD_isError(res))
  885 tomas.vondra@postgre      110                 :UBC           0 :             pg_fatal("could not compress data: %s", ZSTD_getErrorName(res));
                                111                 :                : 
                                112                 :                :         /*
                                113                 :                :          * Extra paranoia: avoid zero-length chunks, since a zero length chunk
                                114                 :                :          * is the EOF marker in the custom format. This should never happen
                                115                 :                :          * but...
                                116                 :                :          */
  885 tomas.vondra@postgre      117         [ +  + ]:CBC         209 :         if (output->pos > 0)
                                118                 :             40 :             cs->writeF(AH, output->dst, output->pos);
                                119                 :                : 
                                120         [ +  - ]:            209 :         if (res == 0)
                                121                 :            209 :             break;              /* End of frame or all input consumed */
                                122                 :                :     }
                                123                 :            209 : }
                                124                 :                : 
                                125                 :                : static void
                                126                 :             80 : EndCompressorZstd(ArchiveHandle *AH, CompressorState *cs)
                                127                 :                : {
                                128                 :             80 :     ZstdCompressorState *zstdcs = (ZstdCompressorState *) cs->private_data;
                                129                 :                : 
                                130         [ +  + ]:             80 :     if (cs->readF != NULL)
                                131                 :                :     {
                                132         [ -  + ]:             40 :         Assert(zstdcs->cstream == NULL);
                                133                 :             40 :         ZSTD_freeDStream(zstdcs->dstream);
                                134                 :             40 :         pg_free(unconstify(void *, zstdcs->input.src));
                                135                 :                :     }
                                136         [ +  - ]:             40 :     else if (cs->writeF != NULL)
                                137                 :                :     {
                                138         [ -  + ]:             40 :         Assert(zstdcs->dstream == NULL);
                                139                 :             40 :         _ZstdWriteCommon(AH, cs, true);
                                140                 :             40 :         ZSTD_freeCStream(zstdcs->cstream);
                                141                 :                :     }
                                142                 :                : 
                                143                 :                :     /* output buffer may be allocated in either mode */
  263 tgl@sss.pgh.pa.us         144                 :             80 :     pg_free(zstdcs->output.dst);
  885 tomas.vondra@postgre      145                 :             80 :     pg_free(zstdcs);
  142 dgustafsson@postgres      146                 :             80 :     cs->private_data = NULL;
  885 tomas.vondra@postgre      147                 :             80 : }
                                148                 :                : 
                                149                 :                : static void
                                150                 :            169 : WriteDataToArchiveZstd(ArchiveHandle *AH, CompressorState *cs,
                                151                 :                :                        const void *data, size_t dLen)
                                152                 :                : {
                                153                 :            169 :     ZstdCompressorState *zstdcs = (ZstdCompressorState *) cs->private_data;
                                154                 :                : 
                                155                 :            169 :     zstdcs->input.src = data;
                                156                 :            169 :     zstdcs->input.size = dLen;
                                157                 :            169 :     zstdcs->input.pos = 0;
                                158                 :                : 
                                159                 :            169 :     _ZstdWriteCommon(AH, cs, false);
                                160                 :            169 : }
                                161                 :                : 
                                162                 :                : static void
                                163                 :             40 : ReadDataFromArchiveZstd(ArchiveHandle *AH, CompressorState *cs)
                                164                 :                : {
                                165                 :             40 :     ZstdCompressorState *zstdcs = (ZstdCompressorState *) cs->private_data;
                                166                 :             40 :     ZSTD_outBuffer *output = &zstdcs->output;
                                167                 :             40 :     ZSTD_inBuffer *input = &zstdcs->input;
                                168                 :             40 :     size_t      input_allocated_size = ZSTD_DStreamInSize();
                                169                 :                :     size_t      res;
                                170                 :                : 
                                171                 :                :     for (;;)
                                172                 :             40 :     {
                                173                 :                :         size_t      cnt;
                                174                 :                : 
                                175                 :                :         /*
                                176                 :                :          * Read compressed data.  Note that readF can resize the buffer; the
                                177                 :                :          * new size is tracked and used for future loops.
                                178                 :                :          */
                                179                 :             80 :         input->size = input_allocated_size;
                                180                 :             80 :         cnt = cs->readF(AH, (char **) unconstify(void **, &input->src), &input->size);
                                181                 :                : 
                                182                 :                :         /* ensure that readF didn't *shrink* the buffer */
                                183         [ -  + ]:             80 :         Assert(input->size >= input_allocated_size);
                                184                 :             80 :         input_allocated_size = input->size;
                                185                 :             80 :         input->size = cnt;
                                186                 :             80 :         input->pos = 0;
                                187                 :                : 
                                188         [ +  + ]:             80 :         if (cnt == 0)
                                189                 :             40 :             break;
                                190                 :                : 
                                191                 :                :         /* Now decompress */
                                192         [ +  - ]:             40 :         while (input->pos < input->size)
                                193                 :                :         {
                                194                 :             40 :             output->pos = 0;
                                195                 :             40 :             res = ZSTD_decompressStream(zstdcs->dstream, output, input);
                                196         [ -  + ]:             40 :             if (ZSTD_isError(res))
  885 tomas.vondra@postgre      197                 :UBC           0 :                 pg_fatal("could not decompress data: %s", ZSTD_getErrorName(res));
                                198                 :                : 
                                199                 :                :             /*
                                200                 :                :              * then write the decompressed data to the output handle
                                201                 :                :              */
  885 tomas.vondra@postgre      202                 :CBC          40 :             ((char *) output->dst)[output->pos] = '\0';
                                203                 :             40 :             ahwrite(output->dst, 1, output->pos, AH);
                                204                 :                : 
                                205         [ +  - ]:             40 :             if (res == 0)
                                206                 :             40 :                 break;          /* End of frame */
                                207                 :                :         }
                                208                 :                :     }
                                209                 :             40 : }
                                210                 :                : 
                                211                 :                : /* Public routine that supports Zstd compressed data I/O */
                                212                 :                : void
                                213                 :             80 : InitCompressorZstd(CompressorState *cs,
                                214                 :                :                    const pg_compress_specification compression_spec)
                                215                 :                : {
                                216                 :                :     ZstdCompressorState *zstdcs;
                                217                 :                : 
                                218                 :             80 :     cs->readData = ReadDataFromArchiveZstd;
                                219                 :             80 :     cs->writeData = WriteDataToArchiveZstd;
                                220                 :             80 :     cs->end = EndCompressorZstd;
                                221                 :                : 
                                222                 :             80 :     cs->compression_spec = compression_spec;
                                223                 :                : 
                                224                 :             80 :     zstdcs = (ZstdCompressorState *) pg_malloc0(sizeof(*zstdcs));
                                225                 :             80 :     cs->private_data = zstdcs;
                                226                 :                : 
                                227                 :                :     /* We expect that exactly one of readF/writeF is specified */
                                228         [ -  + ]:             80 :     Assert((cs->readF == NULL) != (cs->writeF == NULL));
                                229                 :                : 
                                230         [ +  + ]:             80 :     if (cs->readF != NULL)
                                231                 :                :     {
                                232                 :             40 :         zstdcs->dstream = ZSTD_createDStream();
                                233         [ -  + ]:             40 :         if (zstdcs->dstream == NULL)
  885 tomas.vondra@postgre      234                 :UBC           0 :             pg_fatal("could not initialize compression library");
                                235                 :                : 
  885 tomas.vondra@postgre      236                 :CBC          40 :         zstdcs->input.size = ZSTD_DStreamInSize();
                                237                 :             40 :         zstdcs->input.src = pg_malloc(zstdcs->input.size);
                                238                 :                : 
                                239                 :                :         /*
                                240                 :                :          * output.size is the buffer size we tell zstd it can output to.
                                241                 :                :          * Allocate an additional byte such that ReadDataFromArchiveZstd() can
                                242                 :                :          * call ahwrite() with a null-terminated string, which is an optimized
                                243                 :                :          * case in ExecuteSqlCommandBuf().
                                244                 :                :          */
                                245                 :             40 :         zstdcs->output.size = ZSTD_DStreamOutSize();
                                246                 :             40 :         zstdcs->output.dst = pg_malloc(zstdcs->output.size + 1);
                                247                 :                :     }
                                248         [ +  - ]:             40 :     else if (cs->writeF != NULL)
                                249                 :                :     {
                                250                 :             40 :         zstdcs->cstream = _ZstdCStreamParams(cs->compression_spec);
                                251                 :                : 
                                252                 :             40 :         zstdcs->output.size = ZSTD_CStreamOutSize();
                                253                 :             40 :         zstdcs->output.dst = pg_malloc(zstdcs->output.size);
                                254                 :             40 :         zstdcs->output.pos = 0;
                                255                 :                :     }
                                256                 :             80 : }
                                257                 :                : 
                                258                 :                : /*
                                259                 :                :  * Compressed stream API
                                260                 :                :  */
                                261                 :                : 
                                262                 :                : static size_t
    8 dgustafsson@postgres      263                 :            126 : Zstd_read_internal(void *ptr, size_t size, CompressFileHandle *CFH, bool exit_on_error)
                                264                 :                : {
  885 tomas.vondra@postgre      265                 :            126 :     ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->private_data;
                                266                 :            126 :     ZSTD_inBuffer *input = &zstdcs->input;
                                267                 :            126 :     ZSTD_outBuffer *output = &zstdcs->output;
                                268                 :            126 :     size_t      input_allocated_size = ZSTD_DStreamInSize();
                                269                 :                :     size_t      res,
                                270                 :                :                 cnt;
                                271                 :                : 
                                272                 :                :     /*
                                273                 :                :      * If this is the first call to the reading function, initialize the
                                274                 :                :      * required datastructures.
                                275                 :                :      */
    8 dgustafsson@postgres      276         [ +  + ]:            126 :     if (zstdcs->dstream == NULL)
                                277                 :                :     {
                                278                 :             42 :         zstdcs->input.src = pg_malloc0(input_allocated_size);
                                279                 :             42 :         zstdcs->dstream = ZSTD_createDStream();
                                280         [ -  + ]:             42 :         if (zstdcs->dstream == NULL)
                                281                 :                :         {
    8 dgustafsson@postgres      282         [ #  # ]:UBC           0 :             if (exit_on_error)
                                283                 :              0 :                 pg_fatal("could not initialize compression library");
                                284                 :              0 :             return -1;
                                285                 :                :         }
                                286                 :                :     }
                                287                 :                : 
  885 tomas.vondra@postgre      288                 :CBC         126 :     output->size = size;
                                289                 :            126 :     output->dst = ptr;
                                290                 :            126 :     output->pos = 0;
                                291                 :                : 
                                292                 :                :     for (;;)
                                293                 :                :     {
                                294         [ -  + ]:            165 :         Assert(input->pos <= input->size);
                                295         [ -  + ]:            165 :         Assert(input->size <= input_allocated_size);
                                296                 :                : 
                                297                 :                :         /*
                                298                 :                :          * If the input is completely consumed, start back at the beginning
                                299                 :                :          */
                                300         [ +  + ]:            165 :         if (input->pos == input->size)
                                301                 :                :         {
                                302                 :                :             /* input->size is size produced by "fread" */
                                303                 :            122 :             input->size = 0;
                                304                 :                :             /* input->pos is position consumed by decompress */
                                305                 :            122 :             input->pos = 0;
                                306                 :                :         }
                                307                 :                : 
                                308                 :                :         /* read compressed data if we must produce more input */
                                309         [ +  + ]:            165 :         if (input->pos == input->size)
                                310                 :                :         {
                                311                 :            122 :             cnt = fread(unconstify(void *, input->src), 1, input_allocated_size, zstdcs->fp);
    8 dgustafsson@postgres      312         [ -  + ]:            122 :             if (ferror(zstdcs->fp))
                                313                 :                :             {
    8 dgustafsson@postgres      314         [ #  # ]:UBC           0 :                 if (exit_on_error)
                                315                 :              0 :                     pg_fatal("could not read from input file: %m");
                                316                 :              0 :                 return -1;
                                317                 :                :             }
                                318                 :                : 
  885 tomas.vondra@postgre      319                 :CBC         122 :             input->size = cnt;
                                320                 :                : 
                                321         [ -  + ]:            122 :             Assert(cnt <= input_allocated_size);
                                322                 :                : 
                                323                 :                :             /* If we have no more input to consume, we're done */
                                324         [ +  + ]:            122 :             if (cnt == 0)
                                325                 :             81 :                 break;
                                326                 :                :         }
                                327                 :                : 
                                328         [ +  - ]:             84 :         while (input->pos < input->size)
                                329                 :                :         {
                                330                 :                :             /* now decompress */
                                331                 :             84 :             res = ZSTD_decompressStream(zstdcs->dstream, output, input);
                                332                 :                : 
                                333         [ -  + ]:             84 :             if (ZSTD_isError(res))
                                334                 :                :             {
    8 dgustafsson@postgres      335         [ #  # ]:UBC           0 :                 if (exit_on_error)
                                336                 :              0 :                     pg_fatal("could not decompress data: %s", ZSTD_getErrorName(res));
                                337                 :              0 :                 return -1;
                                338                 :                :             }
                                339                 :                : 
  885 tomas.vondra@postgre      340         [ +  + ]:CBC          84 :             if (output->pos == output->size)
                                341                 :             45 :                 break;          /* No more room for output */
                                342                 :                : 
                                343         [ +  - ]:             39 :             if (res == 0)
                                344                 :             39 :                 break;          /* End of frame */
                                345                 :                :         }
                                346                 :                : 
                                347         [ +  + ]:             84 :         if (output->pos == output->size)
                                348                 :             45 :             break;              /* We read all the data that fits */
                                349                 :                :     }
                                350                 :                : 
    8 dgustafsson@postgres      351                 :            126 :     return output->pos;
                                352                 :                : }
                                353                 :                : 
                                354                 :                : static void
  885 tomas.vondra@postgre      355                 :           2377 : Zstd_write(const void *ptr, size_t size, CompressFileHandle *CFH)
                                356                 :                : {
                                357                 :           2377 :     ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->private_data;
                                358                 :           2377 :     ZSTD_inBuffer *input = &zstdcs->input;
                                359                 :           2377 :     ZSTD_outBuffer *output = &zstdcs->output;
                                360                 :                :     size_t      res,
                                361                 :                :                 cnt;
                                362                 :                : 
                                363                 :           2377 :     input->src = ptr;
                                364                 :           2377 :     input->size = size;
                                365                 :           2377 :     input->pos = 0;
                                366                 :                : 
    8 dgustafsson@postgres      367         [ +  + ]:           2377 :     if (zstdcs->cstream == NULL)
                                368                 :                :     {
                                369                 :             40 :         zstdcs->output.size = ZSTD_CStreamOutSize();
                                370                 :             40 :         zstdcs->output.dst = pg_malloc0(zstdcs->output.size);
                                371                 :             40 :         zstdcs->cstream = _ZstdCStreamParams(CFH->compression_spec);
                                372         [ -  + ]:             40 :         if (zstdcs->cstream == NULL)
    8 dgustafsson@postgres      373                 :UBC           0 :             pg_fatal("could not initialize compression library");
                                374                 :                :     }
                                375                 :                : 
                                376                 :                :     /* Consume all input, to be flushed later */
  885 tomas.vondra@postgre      377         [ +  + ]:CBC        4754 :     while (input->pos != input->size)
                                378                 :                :     {
                                379                 :           2377 :         output->pos = 0;
                                380                 :           2377 :         res = ZSTD_compressStream2(zstdcs->cstream, output, input, ZSTD_e_continue);
                                381         [ -  + ]:           2377 :         if (ZSTD_isError(res))
    8 dgustafsson@postgres      382                 :UBC           0 :             pg_fatal("could not write to file: %s", ZSTD_getErrorName(res));
                                383                 :                : 
    8 dgustafsson@postgres      384                 :CBC        2377 :         errno = 0;
  885 tomas.vondra@postgre      385                 :           2377 :         cnt = fwrite(output->dst, 1, output->pos, zstdcs->fp);
                                386         [ -  + ]:           2377 :         if (cnt != output->pos)
                                387                 :                :         {
    8 dgustafsson@postgres      388         [ #  # ]:UBC           0 :             errno = (errno) ? errno : ENOSPC;
                                389                 :              0 :             pg_fatal("could not write to file: %m");
                                390                 :                :         }
                                391                 :                :     }
  885 tomas.vondra@postgre      392                 :CBC        2377 : }
                                393                 :                : 
                                394                 :                : static int
  885 tomas.vondra@postgre      395                 :UBC           0 : Zstd_getc(CompressFileHandle *CFH)
                                396                 :                : {
                                397                 :                :     unsigned char ret;
                                398                 :                : 
    8 dgustafsson@postgres      399         [ #  # ]:              0 :     if (CFH->read_func(&ret, 1, CFH) != 1)
                                400                 :              0 :         pg_fatal("could not read from input file: end of file");
  885 tomas.vondra@postgre      401                 :              0 :     return ret;
                                402                 :                : }
                                403                 :                : 
                                404                 :                : static char *
  885 tomas.vondra@postgre      405                 :CBC           4 : Zstd_gets(char *buf, int len, CompressFileHandle *CFH)
                                406                 :                : {
                                407                 :                :     int         i;
                                408                 :                : 
                                409         [ -  + ]:              4 :     Assert(len > 0);
                                410                 :                : 
                                411                 :                :     /*
                                412                 :                :      * Read one byte at a time until newline or EOF. This is only used to read
                                413                 :                :      * the list of LOs, and the I/O is buffered anyway.
                                414                 :                :      */
                                415         [ +  - ]:             44 :     for (i = 0; i < len - 1; ++i)
                                416                 :                :     {
    8 dgustafsson@postgres      417         [ +  + ]:             44 :         if (Zstd_read_internal(&buf[i], 1, CFH, false) != 1)
  885 tomas.vondra@postgre      418                 :              2 :             break;
                                419         [ +  + ]:             42 :         if (buf[i] == '\n')
                                420                 :                :         {
                                421                 :              2 :             ++i;
                                422                 :              2 :             break;
                                423                 :                :         }
                                424                 :                :     }
                                425                 :              4 :     buf[i] = '\0';
                                426         [ +  + ]:              4 :     return i > 0 ? buf : NULL;
                                427                 :                : }
                                428                 :                : 
                                429                 :                : static size_t
    8 dgustafsson@postgres      430                 :             82 : Zstd_read(void *ptr, size_t size, CompressFileHandle *CFH)
                                431                 :                : {
                                432                 :             82 :     return Zstd_read_internal(ptr, size, CFH, true);
                                433                 :                : }
                                434                 :                : 
                                435                 :                : static bool
  885 tomas.vondra@postgre      436                 :             83 : Zstd_close(CompressFileHandle *CFH)
                                437                 :                : {
                                438                 :             83 :     ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->private_data;
    8 dgustafsson@postgres      439                 :             83 :     bool        success = true;
                                440                 :                : 
  885 tomas.vondra@postgre      441         [ +  + ]:             83 :     if (zstdcs->cstream)
                                442                 :                :     {
                                443                 :                :         size_t      res,
                                444                 :                :                     cnt;
                                445                 :             40 :         ZSTD_inBuffer *input = &zstdcs->input;
                                446                 :             40 :         ZSTD_outBuffer *output = &zstdcs->output;
                                447                 :                : 
                                448                 :                :         /* Loop until the compression buffers are fully consumed */
                                449                 :                :         for (;;)
                                450                 :                :         {
                                451                 :             40 :             output->pos = 0;
                                452                 :             40 :             res = ZSTD_compressStream2(zstdcs->cstream, output, input, ZSTD_e_end);
                                453         [ -  + ]:             40 :             if (ZSTD_isError(res))
                                454                 :                :             {
  885 tomas.vondra@postgre      455                 :UBC           0 :                 zstdcs->zstderror = ZSTD_getErrorName(res);
    8 dgustafsson@postgres      456                 :              0 :                 success = false;
                                457                 :              0 :                 break;
                                458                 :                :             }
                                459                 :                : 
    8 dgustafsson@postgres      460                 :CBC          40 :             errno = 0;
  885 tomas.vondra@postgre      461                 :             40 :             cnt = fwrite(output->dst, 1, output->pos, zstdcs->fp);
                                462         [ -  + ]:             40 :             if (cnt != output->pos)
                                463                 :                :             {
    8 dgustafsson@postgres      464         [ #  # ]:UBC           0 :                 errno = (errno) ? errno : ENOSPC;
  885 tomas.vondra@postgre      465                 :              0 :                 zstdcs->zstderror = strerror(errno);
    8 dgustafsson@postgres      466                 :              0 :                 success = false;
                                467                 :              0 :                 break;
                                468                 :                :             }
                                469                 :                : 
  885 tomas.vondra@postgre      470         [ +  - ]:CBC          40 :             if (res == 0)
                                471                 :             40 :                 break;          /* End of frame */
                                472                 :                :         }
                                473                 :                : 
                                474                 :             40 :         ZSTD_freeCStream(zstdcs->cstream);
                                475                 :             40 :         pg_free(zstdcs->output.dst);
                                476                 :                :     }
                                477                 :                : 
                                478         [ +  + ]:             83 :     if (zstdcs->dstream)
                                479                 :                :     {
                                480                 :             42 :         ZSTD_freeDStream(zstdcs->dstream);
                                481                 :             42 :         pg_free(unconstify(void *, zstdcs->input.src));
                                482                 :                :     }
                                483                 :                : 
    8 dgustafsson@postgres      484                 :             83 :     errno = 0;
  885 tomas.vondra@postgre      485         [ -  + ]:             83 :     if (fclose(zstdcs->fp) != 0)
                                486                 :                :     {
    8 dgustafsson@postgres      487                 :UBC           0 :         zstdcs->zstderror = strerror(errno);
                                488                 :              0 :         success = false;
                                489                 :                :     }
                                490                 :                : 
  885 tomas.vondra@postgre      491                 :CBC          83 :     pg_free(zstdcs);
    8 dgustafsson@postgres      492                 :             83 :     CFH->private_data = NULL;
                                493                 :             83 :     return success;
                                494                 :                : }
                                495                 :                : 
                                496                 :                : static bool
  885 tomas.vondra@postgre      497                 :              2 : Zstd_eof(CompressFileHandle *CFH)
                                498                 :                : {
                                499                 :              2 :     ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->private_data;
                                500                 :                : 
                                501                 :              2 :     return feof(zstdcs->fp);
                                502                 :                : }
                                503                 :                : 
                                504                 :                : static bool
                                505                 :             83 : Zstd_open(const char *path, int fd, const char *mode,
                                506                 :                :           CompressFileHandle *CFH)
                                507                 :                : {
                                508                 :                :     FILE       *fp;
                                509                 :                :     ZstdCompressorState *zstdcs;
                                510                 :                : 
                                511                 :                :     /*
                                512                 :                :      * Clear state storage to avoid having the fd point to non-NULL memory on
                                513                 :                :      * error return.
                                514                 :                :      */
    8 dgustafsson@postgres      515                 :             83 :     CFH->private_data = NULL;
                                516                 :                : 
                                517                 :             83 :     zstdcs = (ZstdCompressorState *) pg_malloc_extended(sizeof(*zstdcs),
                                518                 :                :                                                         MCXT_ALLOC_NO_OOM | MCXT_ALLOC_ZERO);
                                519         [ -  + ]:             83 :     if (!zstdcs)
                                520                 :                :     {
    8 dgustafsson@postgres      521                 :UBC           0 :         errno = ENOMEM;
                                522                 :              0 :         return false;
                                523                 :                :     }
                                524                 :                : 
  885 tomas.vondra@postgre      525         [ -  + ]:CBC          83 :     if (fd >= 0)
    8 dgustafsson@postgres      526                 :UBC           0 :         fp = fdopen(dup(fd), mode);
                                527                 :                :     else
  885 tomas.vondra@postgre      528                 :CBC          83 :         fp = fopen(path, mode);
                                529                 :                : 
                                530         [ -  + ]:             83 :     if (fp == NULL)
                                531                 :                :     {
    8 dgustafsson@postgres      532                 :UBC           0 :         pg_free(zstdcs);
  885 tomas.vondra@postgre      533                 :              0 :         return false;
                                534                 :                :     }
                                535                 :                : 
  885 tomas.vondra@postgre      536                 :CBC          83 :     zstdcs->fp = fp;
    8 dgustafsson@postgres      537                 :             83 :     CFH->private_data = zstdcs;
                                538                 :                : 
  885 tomas.vondra@postgre      539                 :             83 :     return true;
                                540                 :                : }
                                541                 :                : 
                                542                 :                : static bool
                                543                 :             40 : Zstd_open_write(const char *path, const char *mode, CompressFileHandle *CFH)
                                544                 :                : {
                                545                 :                :     char        fname[MAXPGPATH];
                                546                 :                : 
                                547                 :             40 :     sprintf(fname, "%s.zst", path);
                                548                 :             40 :     return CFH->open_func(fname, -1, mode, CFH);
                                549                 :                : }
                                550                 :                : 
                                551                 :                : static const char *
  885 tomas.vondra@postgre      552                 :UBC           0 : Zstd_get_error(CompressFileHandle *CFH)
                                553                 :                : {
                                554                 :              0 :     ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->private_data;
                                555                 :                : 
                                556                 :              0 :     return zstdcs->zstderror;
                                557                 :                : }
                                558                 :                : 
                                559                 :                : void
  885 tomas.vondra@postgre      560                 :CBC          83 : InitCompressFileHandleZstd(CompressFileHandle *CFH,
                                561                 :                :                            const pg_compress_specification compression_spec)
                                562                 :                : {
                                563                 :             83 :     CFH->open_func = Zstd_open;
                                564                 :             83 :     CFH->open_write_func = Zstd_open_write;
                                565                 :             83 :     CFH->read_func = Zstd_read;
                                566                 :             83 :     CFH->write_func = Zstd_write;
                                567                 :             83 :     CFH->gets_func = Zstd_gets;
                                568                 :             83 :     CFH->getc_func = Zstd_getc;
                                569                 :             83 :     CFH->close_func = Zstd_close;
                                570                 :             83 :     CFH->eof_func = Zstd_eof;
                                571                 :             83 :     CFH->get_error_func = Zstd_get_error;
                                572                 :                : 
                                573                 :             83 :     CFH->compression_spec = compression_spec;
                                574                 :                : 
                                575                 :             83 :     CFH->private_data = NULL;
                                576                 :             83 : }
                                577                 :                : 
                                578                 :                : #endif                          /* USE_ZSTD */
        

Generated by: LCOV version 2.4-beta