@@ -455,13 +455,25 @@ struct cfp
455
455
static int hasSuffix (const char * filename , const char * suffix );
456
456
#endif
457
457
458
+ /* free() without changing errno; useful in several places below */
459
+ static void
460
+ free_keep_errno (void * p )
461
+ {
462
+ int save_errno = errno ;
463
+
464
+ free (p );
465
+ errno = save_errno ;
466
+ }
467
+
458
468
/*
459
469
* Open a file for reading. 'path' is the file to open, and 'mode' should
460
470
* be either "r" or "rb".
461
471
*
462
472
* If the file at 'path' does not exist, we append the ".gz" suffix (if 'path'
463
473
* doesn't already have it) and try again. So if you pass "foo" as 'path',
464
474
* this will open either "foo" or "foo.gz".
475
+ *
476
+ * On failure, return NULL with an error code in errno.
465
477
*/
466
478
cfp *
467
479
cfopen_read (const char * path , const char * mode )
@@ -483,7 +495,7 @@ cfopen_read(const char *path, const char *mode)
483
495
484
496
snprintf (fname , fnamelen , "%s%s" , path , ".gz" );
485
497
fp = cfopen (fname , mode , 1 );
486
- free (fname );
498
+ free_keep_errno (fname );
487
499
}
488
500
#endif
489
501
}
@@ -496,8 +508,10 @@ cfopen_read(const char *path, const char *mode)
496
508
* ("w", "wb", "a", or "ab").
497
509
*
498
510
* If 'compression' is non-zero, a gzip compressed stream is opened, and
499
- * and 'compression' indicates the compression level used. The ".gz" suffix
511
+ * 'compression' indicates the compression level used. The ".gz" suffix
500
512
* is automatically added to 'path' in that case.
513
+ *
514
+ * On failure, return NULL with an error code in errno.
501
515
*/
502
516
cfp *
503
517
cfopen_write (const char * path , const char * mode , int compression )
@@ -513,8 +527,8 @@ cfopen_write(const char *path, const char *mode, int compression)
513
527
char * fname = pg_malloc (fnamelen );
514
528
515
529
snprintf (fname , fnamelen , "%s%s" , path , ".gz" );
516
- fp = cfopen (fname , mode , 1 );
517
- free (fname );
530
+ fp = cfopen (fname , mode , compression );
531
+ free_keep_errno (fname );
518
532
#else
519
533
exit_horribly (modulename , "not built with zlib support\n" );
520
534
fp = NULL ; /* keep compiler quiet */
@@ -525,7 +539,9 @@ cfopen_write(const char *path, const char *mode, int compression)
525
539
526
540
/*
527
541
* Opens file 'path' in 'mode'. If 'compression' is non-zero, the file
528
- * is opened with libz gzopen(), otherwise with plain fopen()
542
+ * is opened with libz gzopen(), otherwise with plain fopen().
543
+ *
544
+ * On failure, return NULL with an error code in errno.
529
545
*/
530
546
cfp *
531
547
cfopen (const char * path , const char * mode , int compression )
@@ -535,11 +551,15 @@ cfopen(const char *path, const char *mode, int compression)
535
551
if (compression != 0 )
536
552
{
537
553
#ifdef HAVE_LIBZ
538
- fp -> compressedfp = gzopen (path , mode );
554
+ char mode_compression [32 ];
555
+
556
+ snprintf (mode_compression , sizeof (mode_compression ), "%s%d" ,
557
+ mode , compression );
558
+ fp -> compressedfp = gzopen (path , mode_compression );
539
559
fp -> uncompressedfp = NULL ;
540
560
if (fp -> compressedfp == NULL )
541
561
{
542
- free (fp );
562
+ free_keep_errno (fp );
543
563
fp = NULL ;
544
564
}
545
565
#else
@@ -554,7 +574,7 @@ cfopen(const char *path, const char *mode, int compression)
554
574
fp -> uncompressedfp = fopen (path , mode );
555
575
if (fp -> uncompressedfp == NULL )
556
576
{
557
- free (fp );
577
+ free_keep_errno (fp );
558
578
fp = NULL ;
559
579
}
560
580
}
@@ -629,7 +649,7 @@ cfclose(cfp *fp)
629
649
result = fclose (fp -> uncompressedfp );
630
650
fp -> uncompressedfp = NULL ;
631
651
}
632
- free (fp );
652
+ free_keep_errno (fp );
633
653
634
654
return result ;
635
655
}
0 commit comments