1212 *-------------------------------------------------------------------------
1313 */
1414#include "postgres_fe.h"
15+ #include <fcntl.h>
1516#include <unistd.h>
1617
1718#include "compress_gzip.h"
19+ #include "fe_utils/gzip.h"
1820#include "pg_backup_utils.h"
1921
2022#ifdef HAVE_LIBZ
2123#include <zlib.h>
2224
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
25+ #define GZIP_BUFFER_SIZE 8192
3126
3227/*----------------------
3328 * Compressor API
@@ -261,45 +256,103 @@ InitCompressorGzip(CompressorState *cs,
261256 * Compress File API
262257 *----------------------
263258 */
259+ typedef struct GzipFileState
260+ {
261+ bool reading ;
262+ union
263+ {
264+ gzFile gzfp ;
265+ struct
266+ {
267+ int fd ;
268+ GzipDecompressor decoder ;
269+ char * input ;
270+ size_t input_len ;
271+ size_t input_pos ;
272+ char * output ;
273+ size_t output_len ;
274+ size_t output_pos ;
275+ bool eof ;
276+ bool done ;
277+ } read ;
278+ };
279+ } GzipFileState ;
264280
281+ /*
282+ * Use strict gzip parsing: empty physical files, plain input, and trailing
283+ * non-member bytes are errors. Valid empty and concatenated members work.
284+ */
265285static size_t
266286Gzip_read (void * ptr , size_t size , CompressFileHandle * CFH )
267287{
268- gzFile gzfp = (gzFile ) CFH -> private_data ;
269- int gzret ;
288+ GzipFileState * state = (GzipFileState * ) CFH -> private_data ;
289+ size_t copied = 0 ;
270290
271- /* Reading zero bytes must be a no-op */
272- if ( size == 0 )
273- return 0 ;
291+ while ( copied < size )
292+ {
293+ size_t bytes ;
274294
275- gzret = gzread (gzfp , ptr , size );
295+ if (state -> read .output_pos == state -> read .output_len )
296+ {
297+ /*
298+ * No buffered output remains; refill it unless decoding is
299+ * complete.
300+ */
301+ size_t consumed ;
302+ GzipDecompressResult result ;
276303
277- /*
278- * gzread returns zero on EOF as well as some error conditions, and less
279- * than zero on other error conditions, so we need to inspect for EOF on
280- * zero.
281- */
282- if (gzret <= 0 )
283- {
284- int errnum ;
285- const char * errmsg ;
304+ if (state -> read .done )
305+ break ;
286306
287- if (gzret == 0 && gzeof (gzfp ))
288- return 0 ;
307+ if (state -> read .input_pos == state -> read .input_len && !state -> read .eof )
308+ {
309+ /* The input buffer is empty and EOF has not been reached */
310+ ssize_t readlen ;
311+
312+ do
313+ {
314+ readlen = read (state -> read .fd , state -> read .input , GZIP_BUFFER_SIZE );
315+ /* retry if a signal interrupts the read */
316+ } while (readlen < 0 && errno == EINTR );
317+
318+ if (readlen < 0 )
319+ pg_fatal ("could not read from input file: %m" );
320+ state -> read .input_pos = 0 ;
321+ state -> read .input_len = readlen ;
322+ state -> read .eof = readlen == 0 ;
323+ }
289324
290- errmsg = gzerror (gzfp , & errnum );
325+ /* Produce more decompressed output */
326+ state -> read .output_pos = 0 ;
327+ result = gzip_decompressor_read (& state -> read .decoder ,
328+ state -> read .input + state -> read .input_pos ,
329+ state -> read .input_len - state -> read .input_pos ,
330+ state -> read .output , DEFAULT_IO_BUFFER_SIZE ,
331+ state -> read .eof , & consumed ,
332+ & state -> read .output_len );
333+ state -> read .input_pos += consumed ;
334+ if (result == GZIP_DECOMPRESS_ERROR )
335+ pg_fatal ("could not decompress data: %s" , state -> read .decoder .error );
336+ state -> read .done = result == GZIP_DECOMPRESS_DONE ;
337+
338+ /* Headers and empty members may produce no output. */
339+ if (state -> read .output_len == 0 )
340+ continue ;
341+ }
291342
292- pg_fatal ("could not read from input file: %s" ,
293- errnum == Z_ERRNO ? strerror (errno ) : errmsg );
343+ bytes = Min (size - copied , state -> read .output_len - state -> read .output_pos );
344+ memcpy ((char * ) ptr + copied , state -> read .output + state -> read .output_pos , bytes );
345+ state -> read .output_pos += bytes ;
346+ copied += bytes ;
294347 }
295-
296- return (size_t ) gzret ;
348+ return copied ;
297349}
298350
299351static void
300352Gzip_write (const void * ptr , size_t size , CompressFileHandle * CFH )
301353{
302- gzFile gzfp = (gzFile ) CFH -> private_data ;
354+ GzipFileState * state = (GzipFileState * ) CFH -> private_data ;
355+ gzFile gzfp = state -> gzfp ;
303356 int errnum ;
304357 const char * errmsg ;
305358
@@ -314,56 +367,82 @@ Gzip_write(const void *ptr, size_t size, CompressFileHandle *CFH)
314367static int
315368Gzip_getc (CompressFileHandle * CFH )
316369{
317- gzFile gzfp = (gzFile ) CFH -> private_data ;
318- int ret ;
370+ unsigned char ret ;
319371
320- errno = 0 ;
321- ret = gzgetc (gzfp );
322- if (ret == EOF )
323- {
324- if (!gzeof (gzfp ))
325- pg_fatal ("could not read from input file: %m" );
326- else
327- pg_fatal ("could not read from input file: end of file" );
328- }
372+ if (Gzip_read (& ret , 1 , CFH ) != 1 )
373+ pg_fatal ("could not read from input file: end of file" );
329374
330375 return ret ;
331376}
332377
333378static char *
334379Gzip_gets (char * ptr , int size , CompressFileHandle * CFH )
335380{
336- gzFile gzfp = ( gzFile ) CFH -> private_data ;
381+ int i ;
337382
338- return gzgets (gzfp , ptr , size );
383+ Assert (size > 0 );
384+ for (i = 0 ; i < size - 1 ; i ++ )
385+ {
386+ if (Gzip_read (& ptr [i ], 1 , CFH ) != 1 )
387+ break ;
388+ if (ptr [i ] == '\n' )
389+ {
390+ i ++ ;
391+ break ;
392+ }
393+ }
394+ ptr [i ] = '\0' ;
395+ return i > 0 ? ptr : NULL ;
339396}
340397
341398static bool
342399Gzip_close (CompressFileHandle * CFH )
343400{
344- gzFile gzfp = (gzFile ) CFH -> private_data ;
401+ GzipFileState * state = (GzipFileState * ) CFH -> private_data ;
402+ bool ok = true;
345403
404+ if (state -> reading )
405+ {
406+ char buf [GZIP_BUFFER_SIZE ];
407+
408+ /* Consume and validate any input the caller did not read. */
409+ while (Gzip_read (buf , sizeof (buf ), CFH ) > 0 )
410+ ;
411+ if (!gzip_decompressor_end (& state -> read .decoder ))
412+ pg_fatal ("could not decompress data: %s" , state -> read .decoder .error );
413+ pg_free (state -> read .input );
414+ pg_free (state -> read .output );
415+ if (close (state -> read .fd ) != 0 )
416+ ok = false;
417+ }
418+ else if (gzclose (state -> gzfp ) != Z_OK )
419+ ok = false;
346420 CFH -> private_data = NULL ;
347-
348- return gzclose ( gzfp ) == Z_OK ;
421+ pg_free ( state );
422+ return ok ;
349423}
350424
351425static bool
352426Gzip_eof (CompressFileHandle * CFH )
353427{
354- gzFile gzfp = (gzFile ) CFH -> private_data ;
428+ GzipFileState * state = (GzipFileState * ) CFH -> private_data ;
355429
356- return gzeof (gzfp ) == 1 ;
430+ return state -> reading ?
431+ (state -> read .done && state -> read .output_pos == state -> read .output_len ) :
432+ gzeof (state -> gzfp ) == 1 ;
357433}
358434
359435static const char *
360436Gzip_get_error (CompressFileHandle * CFH )
361437{
362- gzFile gzfp = (gzFile ) CFH -> private_data ;
438+ GzipFileState * state = (GzipFileState * ) CFH -> private_data ;
363439 const char * errmsg ;
364440 int errnum ;
365441
366- errmsg = gzerror (gzfp , & errnum );
442+ if (state -> reading )
443+ return state -> read .decoder .error ? state -> read .decoder .error : "" ;
444+
445+ errmsg = gzerror (state -> gzfp , & errnum );
367446 if (errnum == Z_ERRNO )
368447 errmsg = strerror (errno );
369448
@@ -373,7 +452,8 @@ Gzip_get_error(CompressFileHandle *CFH)
373452static bool
374453Gzip_open (const char * path , int fd , const char * mode , CompressFileHandle * CFH )
375454{
376- gzFile gzfp ;
455+ GzipFileState * state ;
456+ gzFile gzfp = NULL ;
377457 char mode_compression [32 ];
378458
379459 if (CFH -> compression_spec .level != Z_DEFAULT_COMPRESSION )
@@ -387,27 +467,53 @@ Gzip_open(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
387467 else
388468 strcpy (mode_compression , mode );
389469
390- if (fd >= 0 )
470+ state = pg_malloc0_object (GzipFileState );
471+ state -> reading = mode [0 ] == 'r' ;
472+ if (state -> reading )
473+ {
474+ state -> read .fd = fd >= 0 ? dup (fd ) : open (path , O_RDONLY | PG_BINARY , 0 );
475+ if (state -> read .fd < 0 )
476+ {
477+ pg_free (state );
478+ return false;
479+ }
480+ if (!gzip_decompressor_init (& state -> read .decoder ))
481+ pg_fatal ("could not initialize compression library: %s" ,
482+ state -> read .decoder .error );
483+ state -> read .input = pg_malloc (GZIP_BUFFER_SIZE );
484+ state -> read .output = pg_malloc (DEFAULT_IO_BUFFER_SIZE );
485+ }
486+ else if (fd >= 0 )
391487 {
392488 int dup_fd = dup (fd );
393489
394490 if (dup_fd < 0 )
491+ {
492+ pg_free (state );
395493 return false;
494+ }
396495 gzfp = gzdopen (dup_fd , mode_compression );
397496 if (gzfp == NULL )
398497 {
399498 close (dup_fd );
499+ pg_free (state );
400500 return false;
401501 }
402502 }
403503 else
404504 {
405505 gzfp = gzopen (path , mode_compression );
406506 if (gzfp == NULL )
507+ {
508+ pg_free (state );
407509 return false;
510+ }
408511 }
409512
410- CFH -> private_data = gzfp ;
513+ if (!state -> reading )
514+ state -> gzfp = gzfp ;
515+
516+ CFH -> private_data = state ;
411517
412518 return true;
413519}
0 commit comments