@@ -464,13 +464,25 @@ struct cfp
464
464
static int hasSuffix (const char * filename , const char * suffix );
465
465
#endif
466
466
467
+ /* free() without changing errno; useful in several places below */
468
+ static void
469
+ free_keep_errno (void * p )
470
+ {
471
+ int save_errno = errno ;
472
+
473
+ free (p );
474
+ errno = save_errno ;
475
+ }
476
+
467
477
/*
468
478
* Open a file for reading. 'path' is the file to open, and 'mode' should
469
479
* be either "r" or "rb".
470
480
*
471
481
* If the file at 'path' does not exist, we append the ".gz" suffix (if 'path'
472
482
* doesn't already have it) and try again. So if you pass "foo" as 'path',
473
483
* this will open either "foo" or "foo.gz".
484
+ *
485
+ * On failure, return NULL with an error code in errno.
474
486
*/
475
487
cfp *
476
488
cfopen_read (const char * path , const char * mode )
@@ -492,7 +504,7 @@ cfopen_read(const char *path, const char *mode)
492
504
493
505
snprintf (fname , fnamelen , "%s%s" , path , ".gz" );
494
506
fp = cfopen (fname , mode , 1 );
495
- free (fname );
507
+ free_keep_errno (fname );
496
508
}
497
509
#endif
498
510
}
@@ -505,8 +517,10 @@ cfopen_read(const char *path, const char *mode)
505
517
* ("w", "wb", "a", or "ab").
506
518
*
507
519
* If 'compression' is non-zero, a gzip compressed stream is opened, and
508
- * and 'compression' indicates the compression level used. The ".gz" suffix
520
+ * 'compression' indicates the compression level used. The ".gz" suffix
509
521
* is automatically added to 'path' in that case.
522
+ *
523
+ * On failure, return NULL with an error code in errno.
510
524
*/
511
525
cfp *
512
526
cfopen_write (const char * path , const char * mode , int compression )
@@ -522,8 +536,8 @@ cfopen_write(const char *path, const char *mode, int compression)
522
536
char * fname = pg_malloc (fnamelen );
523
537
524
538
snprintf (fname , fnamelen , "%s%s" , path , ".gz" );
525
- fp = cfopen (fname , mode , 1 );
526
- free (fname );
539
+ fp = cfopen (fname , mode , compression );
540
+ free_keep_errno (fname );
527
541
#else
528
542
exit_horribly (modulename , "not built with zlib support\n" );
529
543
fp = NULL ; /* keep compiler quiet */
@@ -534,7 +548,9 @@ cfopen_write(const char *path, const char *mode, int compression)
534
548
535
549
/*
536
550
* Opens file 'path' in 'mode'. If 'compression' is non-zero, the file
537
- * is opened with libz gzopen(), otherwise with plain fopen()
551
+ * is opened with libz gzopen(), otherwise with plain fopen().
552
+ *
553
+ * On failure, return NULL with an error code in errno.
538
554
*/
539
555
cfp *
540
556
cfopen (const char * path , const char * mode , int compression )
@@ -544,11 +560,15 @@ cfopen(const char *path, const char *mode, int compression)
544
560
if (compression != 0 )
545
561
{
546
562
#ifdef HAVE_LIBZ
547
- fp -> compressedfp = gzopen (path , mode );
563
+ char mode_compression [32 ];
564
+
565
+ snprintf (mode_compression , sizeof (mode_compression ), "%s%d" ,
566
+ mode , compression );
567
+ fp -> compressedfp = gzopen (path , mode_compression );
548
568
fp -> uncompressedfp = NULL ;
549
569
if (fp -> compressedfp == NULL )
550
570
{
551
- free (fp );
571
+ free_keep_errno (fp );
552
572
fp = NULL ;
553
573
}
554
574
#else
@@ -563,7 +583,7 @@ cfopen(const char *path, const char *mode, int compression)
563
583
fp -> uncompressedfp = fopen (path , mode );
564
584
if (fp -> uncompressedfp == NULL )
565
585
{
566
- free (fp );
586
+ free_keep_errno (fp );
567
587
fp = NULL ;
568
588
}
569
589
}
@@ -638,7 +658,7 @@ cfclose(cfp *fp)
638
658
result = fclose (fp -> uncompressedfp );
639
659
fp -> uncompressedfp = NULL ;
640
660
}
641
- free (fp );
661
+ free_keep_errno (fp );
642
662
643
663
return result ;
644
664
}
0 commit comments