LCOV - code coverage report
Current view: top level - src/bin/pg_dump - compress_gzip.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 81.3 % 182 148
Test Date: 2026-07-11 05:15:30 Functions: 94.1 % 17 16
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 56.6 % 76 43

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * compress_gzip.c
       4                 :             :  *   Routines for archivers to read or write a gzip compressed data stream.
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  * IDENTIFICATION
      10                 :             :  *     src/bin/pg_dump/compress_gzip.c
      11                 :             :  *
      12                 :             :  *-------------------------------------------------------------------------
      13                 :             :  */
      14                 :             : #include "postgres_fe.h"
      15                 :             : #include <unistd.h>
      16                 :             : 
      17                 :             : #include "compress_gzip.h"
      18                 :             : #include "pg_backup_utils.h"
      19                 :             : 
      20                 :             : #ifdef HAVE_LIBZ
      21                 :             : #include <zlib.h>
      22                 :             : 
      23                 :             : /*
      24                 :             :  * We don't use the gzgetc() macro, because zlib's configuration logic is not
      25                 :             :  * robust enough to guarantee that the macro will have the same ideas about
      26                 :             :  * struct field layout as the library itself does; see for example
      27                 :             :  * https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=59711
      28                 :             :  * Instead, #undef the macro and fall back to the underlying function.
      29                 :             :  */
      30                 :             : #undef gzgetc
      31                 :             : 
      32                 :             : /*----------------------
      33                 :             :  * Compressor API
      34                 :             :  *----------------------
      35                 :             :  */
      36                 :             : typedef struct GzipCompressorState
      37                 :             : {
      38                 :             :     z_streamp   zp;
      39                 :             : 
      40                 :             :     void       *outbuf;
      41                 :             :     size_t      outsize;
      42                 :             : } GzipCompressorState;
      43                 :             : 
      44                 :             : /* Private routines that support gzip compressed data I/O */
      45                 :             : static void DeflateCompressorInit(CompressorState *cs);
      46                 :             : static void DeflateCompressorEnd(ArchiveHandle *AH, CompressorState *cs);
      47                 :             : static void DeflateCompressorCommon(ArchiveHandle *AH, CompressorState *cs,
      48                 :             :                                     bool flush);
      49                 :             : static void EndCompressorGzip(ArchiveHandle *AH, CompressorState *cs);
      50                 :             : static void WriteDataToArchiveGzip(ArchiveHandle *AH, CompressorState *cs,
      51                 :             :                                    const void *data, size_t dLen);
      52                 :             : static void ReadDataFromArchiveGzip(ArchiveHandle *AH, CompressorState *cs);
      53                 :             : 
      54                 :             : static void
      55                 :         262 : DeflateCompressorInit(CompressorState *cs)
      56                 :             : {
      57                 :             :     GzipCompressorState *gzipcs;
      58                 :             :     z_streamp   zp;
      59                 :             : 
      60                 :         262 :     gzipcs = pg_malloc0_object(GzipCompressorState);
      61                 :         262 :     zp = gzipcs->zp = pg_malloc_object(z_stream);
      62                 :         262 :     zp->zalloc = Z_NULL;
      63                 :         262 :     zp->zfree = Z_NULL;
      64                 :         262 :     zp->opaque = Z_NULL;
      65                 :             : 
      66                 :             :     /*
      67                 :             :      * outsize is the buffer size we tell zlib it can output to.  We actually
      68                 :             :      * allocate one extra byte because some routines want to append a trailing
      69                 :             :      * zero byte to the zlib output.
      70                 :             :      */
      71                 :         262 :     gzipcs->outsize = DEFAULT_IO_BUFFER_SIZE;
      72                 :         262 :     gzipcs->outbuf = pg_malloc(gzipcs->outsize + 1);
      73                 :             : 
      74                 :             :     /* -Z 0 uses the "None" compressor -- not zlib with no compression */
      75                 :             :     Assert(cs->compression_spec.level != 0);
      76                 :             : 
      77         [ -  + ]:         262 :     if (deflateInit(zp, cs->compression_spec.level) != Z_OK)
      78                 :           0 :         pg_fatal("could not initialize compression library: %s", zp->msg);
      79                 :             : 
      80                 :             :     /* Just be paranoid - maybe End is called after Start, with no Write */
      81                 :         262 :     zp->next_out = gzipcs->outbuf;
      82                 :         262 :     zp->avail_out = gzipcs->outsize;
      83                 :             : 
      84                 :             :     /* Keep track of gzipcs */
      85                 :         262 :     cs->private_data = gzipcs;
      86                 :         262 : }
      87                 :             : 
      88                 :             : static void
      89                 :         262 : DeflateCompressorEnd(ArchiveHandle *AH, CompressorState *cs)
      90                 :             : {
      91                 :         262 :     GzipCompressorState *gzipcs = (GzipCompressorState *) cs->private_data;
      92                 :             :     z_streamp   zp;
      93                 :             : 
      94                 :         262 :     zp = gzipcs->zp;
      95                 :         262 :     zp->next_in = NULL;
      96                 :         262 :     zp->avail_in = 0;
      97                 :             : 
      98                 :             :     /* Flush any remaining data from zlib buffer */
      99                 :         262 :     DeflateCompressorCommon(AH, cs, true);
     100                 :             : 
     101         [ -  + ]:         262 :     if (deflateEnd(zp) != Z_OK)
     102                 :           0 :         pg_fatal("could not close compression stream: %s", zp->msg);
     103                 :             : 
     104                 :         262 :     pg_free(gzipcs->outbuf);
     105                 :         262 :     pg_free(gzipcs->zp);
     106                 :         262 :     pg_free(gzipcs);
     107                 :         262 :     cs->private_data = NULL;
     108                 :         262 : }
     109                 :             : 
     110                 :             : static void
     111                 :        1681 : DeflateCompressorCommon(ArchiveHandle *AH, CompressorState *cs, bool flush)
     112                 :             : {
     113                 :        1681 :     GzipCompressorState *gzipcs = (GzipCompressorState *) cs->private_data;
     114                 :        1681 :     z_streamp   zp = gzipcs->zp;
     115                 :        1681 :     void       *out = gzipcs->outbuf;
     116                 :        1681 :     int         res = Z_OK;
     117                 :             : 
     118   [ +  +  +  + ]:        3100 :     while (gzipcs->zp->avail_in != 0 || flush)
     119                 :             :     {
     120         [ +  + ]:        1681 :         res = deflate(zp, flush ? Z_FINISH : Z_NO_FLUSH);
     121         [ -  + ]:        1681 :         if (res == Z_STREAM_ERROR)
     122                 :           0 :             pg_fatal("could not compress data: %s", zp->msg);
     123   [ +  +  -  + ]:        1681 :         if ((flush && (zp->avail_out < gzipcs->outsize))
     124         [ +  - ]:        1419 :             || (zp->avail_out == 0)
     125         [ -  + ]:        1419 :             || (zp->avail_in != 0)
     126                 :             :             )
     127                 :             :         {
     128                 :             :             /*
     129                 :             :              * Extra paranoia: avoid zero-length chunks, since a zero length
     130                 :             :              * chunk is the EOF marker in the custom format. This should never
     131                 :             :              * happen but ...
     132                 :             :              */
     133         [ +  - ]:         262 :             if (zp->avail_out < gzipcs->outsize)
     134                 :             :             {
     135                 :             :                 /*
     136                 :             :                  * Any write function should do its own error checking but to
     137                 :             :                  * make sure we do a check here as well ...
     138                 :             :                  */
     139                 :         262 :                 size_t      len = gzipcs->outsize - zp->avail_out;
     140                 :             : 
     141                 :         262 :                 cs->writeF(AH, out, len);
     142                 :             :             }
     143                 :         262 :             zp->next_out = out;
     144                 :         262 :             zp->avail_out = gzipcs->outsize;
     145                 :             :         }
     146                 :             : 
     147         [ +  + ]:        1681 :         if (res == Z_STREAM_END)
     148                 :         262 :             break;
     149                 :             :     }
     150                 :        1681 : }
     151                 :             : 
     152                 :             : static void
     153                 :         429 : EndCompressorGzip(ArchiveHandle *AH, CompressorState *cs)
     154                 :             : {
     155                 :             :     /* If deflation was initialized, finalize it */
     156         [ +  + ]:         429 :     if (cs->private_data)
     157                 :         262 :         DeflateCompressorEnd(AH, cs);
     158                 :         429 : }
     159                 :             : 
     160                 :             : static void
     161                 :        1419 : WriteDataToArchiveGzip(ArchiveHandle *AH, CompressorState *cs,
     162                 :             :                        const void *data, size_t dLen)
     163                 :             : {
     164                 :        1419 :     GzipCompressorState *gzipcs = (GzipCompressorState *) cs->private_data;
     165                 :             : 
     166                 :        1419 :     gzipcs->zp->next_in = data;
     167                 :        1419 :     gzipcs->zp->avail_in = dLen;
     168                 :        1419 :     DeflateCompressorCommon(AH, cs, false);
     169                 :        1419 : }
     170                 :             : 
     171                 :             : static void
     172                 :         167 : ReadDataFromArchiveGzip(ArchiveHandle *AH, CompressorState *cs)
     173                 :             : {
     174                 :             :     z_streamp   zp;
     175                 :             :     char       *out;
     176                 :         167 :     int         res = Z_OK;
     177                 :             :     size_t      cnt;
     178                 :             :     char       *buf;
     179                 :             :     size_t      buflen;
     180                 :             : 
     181                 :         167 :     zp = pg_malloc_object(z_stream);
     182                 :         167 :     zp->zalloc = Z_NULL;
     183                 :         167 :     zp->zfree = Z_NULL;
     184                 :         167 :     zp->opaque = Z_NULL;
     185                 :             : 
     186                 :         167 :     buflen = DEFAULT_IO_BUFFER_SIZE;
     187                 :         167 :     buf = pg_malloc(buflen);
     188                 :             : 
     189                 :         167 :     out = pg_malloc(DEFAULT_IO_BUFFER_SIZE + 1);
     190                 :             : 
     191         [ -  + ]:         167 :     if (inflateInit(zp) != Z_OK)
     192                 :           0 :         pg_fatal("could not initialize compression library: %s",
     193                 :             :                  zp->msg);
     194                 :             : 
     195                 :             :     /* no minimal chunk size for zlib */
     196         [ +  + ]:         334 :     while ((cnt = cs->readF(AH, &buf, &buflen)))
     197                 :             :     {
     198                 :         167 :         zp->next_in = (void *) buf;
     199                 :         167 :         zp->avail_in = cnt;
     200                 :             : 
     201         [ +  + ]:         336 :         while (zp->avail_in > 0)
     202                 :             :         {
     203                 :         169 :             zp->next_out = (void *) out;
     204                 :         169 :             zp->avail_out = DEFAULT_IO_BUFFER_SIZE;
     205                 :             : 
     206                 :         169 :             res = inflate(zp, 0);
     207   [ +  +  -  + ]:         169 :             if (res != Z_OK && res != Z_STREAM_END)
     208                 :           0 :                 pg_fatal("could not uncompress data: %s", zp->msg);
     209                 :             : 
     210                 :         169 :             out[DEFAULT_IO_BUFFER_SIZE - zp->avail_out] = '\0';
     211                 :         169 :             ahwrite(out, 1, DEFAULT_IO_BUFFER_SIZE - zp->avail_out, AH);
     212                 :             :         }
     213                 :             :     }
     214                 :             : 
     215                 :         167 :     zp->next_in = NULL;
     216                 :         167 :     zp->avail_in = 0;
     217         [ -  + ]:         167 :     while (res != Z_STREAM_END)
     218                 :             :     {
     219                 :           0 :         zp->next_out = (void *) out;
     220                 :           0 :         zp->avail_out = DEFAULT_IO_BUFFER_SIZE;
     221                 :           0 :         res = inflate(zp, 0);
     222   [ #  #  #  # ]:           0 :         if (res != Z_OK && res != Z_STREAM_END)
     223                 :           0 :             pg_fatal("could not uncompress data: %s", zp->msg);
     224                 :             : 
     225                 :           0 :         out[DEFAULT_IO_BUFFER_SIZE - zp->avail_out] = '\0';
     226                 :           0 :         ahwrite(out, 1, DEFAULT_IO_BUFFER_SIZE - zp->avail_out, AH);
     227                 :             :     }
     228                 :             : 
     229         [ -  + ]:         167 :     if (inflateEnd(zp) != Z_OK)
     230                 :           0 :         pg_fatal("could not close compression library: %s", zp->msg);
     231                 :             : 
     232                 :         167 :     pg_free(buf);
     233                 :         167 :     pg_free(out);
     234                 :         167 :     pg_free(zp);
     235                 :         167 : }
     236                 :             : 
     237                 :             : /* Public routines that support gzip compressed data I/O */
     238                 :             : void
     239                 :         429 : InitCompressorGzip(CompressorState *cs,
     240                 :             :                    const pg_compress_specification compression_spec)
     241                 :             : {
     242                 :         429 :     cs->readData = ReadDataFromArchiveGzip;
     243                 :         429 :     cs->writeData = WriteDataToArchiveGzip;
     244                 :         429 :     cs->end = EndCompressorGzip;
     245                 :             : 
     246                 :         429 :     cs->compression_spec = compression_spec;
     247                 :             : 
     248                 :             :     /*
     249                 :             :      * If the caller has defined a write function, prepare the necessary
     250                 :             :      * state.  Note that if the data is empty, End may be called immediately
     251                 :             :      * after Init, without ever calling Write.
     252                 :             :      */
     253         [ +  + ]:         429 :     if (cs->writeF)
     254                 :         262 :         DeflateCompressorInit(cs);
     255                 :         429 : }
     256                 :             : 
     257                 :             : 
     258                 :             : /*----------------------
     259                 :             :  * Compress File API
     260                 :             :  *----------------------
     261                 :             :  */
     262                 :             : 
     263                 :             : static size_t
     264                 :         381 : Gzip_read(void *ptr, size_t size, CompressFileHandle *CFH)
     265                 :             : {
     266                 :         381 :     gzFile      gzfp = (gzFile) CFH->private_data;
     267                 :             :     int         gzret;
     268                 :             : 
     269                 :             :     /* Reading zero bytes must be a no-op */
     270         [ +  + ]:         381 :     if (size == 0)
     271                 :          16 :         return 0;
     272                 :             : 
     273                 :         365 :     gzret = gzread(gzfp, ptr, size);
     274                 :             : 
     275                 :             :     /*
     276                 :             :      * gzread returns zero on EOF as well as some error conditions, and less
     277                 :             :      * than zero on other error conditions, so we need to inspect for EOF on
     278                 :             :      * zero.
     279                 :             :      */
     280         [ +  + ]:         365 :     if (gzret <= 0)
     281                 :             :     {
     282                 :             :         int         errnum;
     283                 :             :         const char *errmsg;
     284                 :             : 
     285   [ +  -  +  - ]:         111 :         if (gzret == 0 && gzeof(gzfp))
     286                 :         111 :             return 0;
     287                 :             : 
     288                 :           0 :         errmsg = gzerror(gzfp, &errnum);
     289                 :             : 
     290         [ #  # ]:           0 :         pg_fatal("could not read from input file: %s",
     291                 :             :                  errnum == Z_ERRNO ? strerror(errno) : errmsg);
     292                 :             :     }
     293                 :             : 
     294                 :         254 :     return (size_t) gzret;
     295                 :             : }
     296                 :             : 
     297                 :             : static void
     298                 :       24657 : Gzip_write(const void *ptr, size_t size, CompressFileHandle *CFH)
     299                 :             : {
     300                 :       24657 :     gzFile      gzfp = (gzFile) CFH->private_data;
     301                 :             :     int         errnum;
     302                 :             :     const char *errmsg;
     303                 :             : 
     304         [ -  + ]:       24657 :     if (gzwrite(gzfp, ptr, size) != size)
     305                 :             :     {
     306                 :           0 :         errmsg = gzerror(gzfp, &errnum);
     307         [ #  # ]:           0 :         pg_fatal("could not write to file: %s",
     308                 :             :                  errnum == Z_ERRNO ? strerror(errno) : errmsg);
     309                 :             :     }
     310                 :       24657 : }
     311                 :             : 
     312                 :             : static int
     313                 :        1572 : Gzip_getc(CompressFileHandle *CFH)
     314                 :             : {
     315                 :        1572 :     gzFile      gzfp = (gzFile) CFH->private_data;
     316                 :             :     int         ret;
     317                 :             : 
     318                 :        1572 :     errno = 0;
     319                 :        1572 :     ret = gzgetc(gzfp);
     320         [ -  + ]:        1572 :     if (ret == EOF)
     321                 :             :     {
     322         [ #  # ]:           0 :         if (!gzeof(gzfp))
     323                 :           0 :             pg_fatal("could not read from input file: %m");
     324                 :             :         else
     325                 :           0 :             pg_fatal("could not read from input file: end of file");
     326                 :             :     }
     327                 :             : 
     328                 :        1572 :     return ret;
     329                 :             : }
     330                 :             : 
     331                 :             : static char *
     332                 :           2 : Gzip_gets(char *ptr, int size, CompressFileHandle *CFH)
     333                 :             : {
     334                 :           2 :     gzFile      gzfp = (gzFile) CFH->private_data;
     335                 :             : 
     336                 :           2 :     return gzgets(gzfp, ptr, size);
     337                 :             : }
     338                 :             : 
     339                 :             : static bool
     340                 :         224 : Gzip_close(CompressFileHandle *CFH)
     341                 :             : {
     342                 :         224 :     gzFile      gzfp = (gzFile) CFH->private_data;
     343                 :             : 
     344                 :         224 :     CFH->private_data = NULL;
     345                 :             : 
     346                 :         224 :     return gzclose(gzfp) == Z_OK;
     347                 :             : }
     348                 :             : 
     349                 :             : static bool
     350                 :           1 : Gzip_eof(CompressFileHandle *CFH)
     351                 :             : {
     352                 :           1 :     gzFile      gzfp = (gzFile) CFH->private_data;
     353                 :             : 
     354                 :           1 :     return gzeof(gzfp) == 1;
     355                 :             : }
     356                 :             : 
     357                 :             : static const char *
     358                 :           0 : Gzip_get_error(CompressFileHandle *CFH)
     359                 :             : {
     360                 :           0 :     gzFile      gzfp = (gzFile) CFH->private_data;
     361                 :             :     const char *errmsg;
     362                 :             :     int         errnum;
     363                 :             : 
     364                 :           0 :     errmsg = gzerror(gzfp, &errnum);
     365         [ #  # ]:           0 :     if (errnum == Z_ERRNO)
     366                 :           0 :         errmsg = strerror(errno);
     367                 :             : 
     368                 :           0 :     return errmsg;
     369                 :             : }
     370                 :             : 
     371                 :             : static bool
     372                 :         224 : Gzip_open(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
     373                 :             : {
     374                 :             :     gzFile      gzfp;
     375                 :             :     char        mode_compression[32];
     376                 :             : 
     377         [ +  + ]:         224 :     if (CFH->compression_spec.level != Z_DEFAULT_COMPRESSION)
     378                 :             :     {
     379                 :             :         /*
     380                 :             :          * user has specified a compression level, so tell zlib to use it
     381                 :             :          */
     382                 :         117 :         snprintf(mode_compression, sizeof(mode_compression), "%s%d",
     383                 :             :                  mode, CFH->compression_spec.level);
     384                 :             :     }
     385                 :             :     else
     386                 :         107 :         strcpy(mode_compression, mode);
     387                 :             : 
     388         [ -  + ]:         224 :     if (fd >= 0)
     389                 :             :     {
     390                 :           0 :         int         dup_fd = dup(fd);
     391                 :             : 
     392         [ #  # ]:           0 :         if (dup_fd < 0)
     393                 :           0 :             return false;
     394                 :           0 :         gzfp = gzdopen(dup_fd, mode_compression);
     395         [ #  # ]:           0 :         if (gzfp == NULL)
     396                 :             :         {
     397                 :           0 :             close(dup_fd);
     398                 :           0 :             return false;
     399                 :             :         }
     400                 :             :     }
     401                 :             :     else
     402                 :             :     {
     403                 :         224 :         gzfp = gzopen(path, mode_compression);
     404         [ -  + ]:         224 :         if (gzfp == NULL)
     405                 :           0 :             return false;
     406                 :             :     }
     407                 :             : 
     408                 :         224 :     CFH->private_data = gzfp;
     409                 :             : 
     410                 :         224 :     return true;
     411                 :             : }
     412                 :             : 
     413                 :             : static bool
     414                 :         110 : Gzip_open_write(const char *path, const char *mode, CompressFileHandle *CFH)
     415                 :             : {
     416                 :             :     char       *fname;
     417                 :             :     bool        ret;
     418                 :             :     int         save_errno;
     419                 :             : 
     420                 :         110 :     fname = psprintf("%s.gz", path);
     421                 :         110 :     ret = CFH->open_func(fname, -1, mode, CFH);
     422                 :             : 
     423                 :         110 :     save_errno = errno;
     424                 :         110 :     pfree(fname);
     425                 :         110 :     errno = save_errno;
     426                 :             : 
     427                 :         110 :     return ret;
     428                 :             : }
     429                 :             : 
     430                 :             : void
     431                 :         224 : InitCompressFileHandleGzip(CompressFileHandle *CFH,
     432                 :             :                            const pg_compress_specification compression_spec)
     433                 :             : {
     434                 :         224 :     CFH->open_func = Gzip_open;
     435                 :         224 :     CFH->open_write_func = Gzip_open_write;
     436                 :         224 :     CFH->read_func = Gzip_read;
     437                 :         224 :     CFH->write_func = Gzip_write;
     438                 :         224 :     CFH->gets_func = Gzip_gets;
     439                 :         224 :     CFH->getc_func = Gzip_getc;
     440                 :         224 :     CFH->close_func = Gzip_close;
     441                 :         224 :     CFH->eof_func = Gzip_eof;
     442                 :         224 :     CFH->get_error_func = Gzip_get_error;
     443                 :             : 
     444                 :         224 :     CFH->compression_spec = compression_spec;
     445                 :             : 
     446                 :         224 :     CFH->private_data = NULL;
     447                 :         224 : }
     448                 :             : #else                           /* HAVE_LIBZ */
     449                 :             : void
     450                 :             : InitCompressorGzip(CompressorState *cs,
     451                 :             :                    const pg_compress_specification compression_spec)
     452                 :             : {
     453                 :             :     pg_fatal("this build does not support compression with %s", "gzip");
     454                 :             : }
     455                 :             : 
     456                 :             : void
     457                 :             : InitCompressFileHandleGzip(CompressFileHandle *CFH,
     458                 :             :                            const pg_compress_specification compression_spec)
     459                 :             : {
     460                 :             :     pg_fatal("this build does not support compression with %s", "gzip");
     461                 :             : }
     462                 :             : #endif                          /* HAVE_LIBZ */
        

Generated by: LCOV version 2.0-1