Skip to content

Commit caef94d

Browse files
committed
Restore use of zlib default compression in pg_dump directory mode.
This was broken by commit 0e7e355 and friends, which ignored the fact that gzopen() will treat "-1" in the mode argument as an invalid character, which it ignores, and a flag for compression level 1. Now, when this value is encountered no compression level flag is passed to gzopen, leaving it to use the zlib default. Also, enforce the documented allowed range for pg_dump's -Z option, namely 0 .. 9, and remove some consequently dead code from pg_backup_tar.c. Problem reported by Marc Mamin. Backpatch to 9.1, like the patch that introduced the bug.
1 parent c879d51 commit caef94d

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

src/bin/pg_dump/compress_io.c

+14-4
Original file line numberDiff line numberDiff line change
@@ -547,11 +547,21 @@ cfopen(const char *path, const char *mode, int compression)
547547
if (compression != 0)
548548
{
549549
#ifdef HAVE_LIBZ
550-
char mode_compression[32];
550+
if (compression != Z_DEFAULT_COMPRESSION)
551+
{
552+
/* user has specified a compression level, so tell zlib to use it */
553+
char mode_compression[32];
554+
555+
snprintf(mode_compression, sizeof(mode_compression), "%s%d",
556+
mode, compression);
557+
fp->compressedfp = gzopen(path, mode_compression);
558+
}
559+
else
560+
{
561+
/* don't specify a level, just use the zlib default */
562+
fp->compressedfp = gzopen(path, mode);
563+
}
551564

552-
snprintf(mode_compression, sizeof(mode_compression), "%s%d",
553-
mode, compression);
554-
fp->compressedfp = gzopen(path, mode_compression);
555565
fp->uncompressedfp = NULL;
556566
if (fp->compressedfp == NULL)
557567
{

src/bin/pg_dump/pg_backup_tar.c

-7
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,6 @@ InitArchiveFmt_Tar(ArchiveHandle *AH)
208208

209209
ctx->hasSeek = checkSeek(ctx->tarFH);
210210

211-
if (AH->compression < 0 || AH->compression > 9)
212-
AH->compression = Z_DEFAULT_COMPRESSION;
213-
214-
/* Don't compress into tar files unless asked to do so */
215-
if (AH->compression == Z_DEFAULT_COMPRESSION)
216-
AH->compression = 0;
217-
218211
/*
219212
* We don't support compression because reading the files back is not
220213
* possible since gzdopen uses buffered IO which totally screws file

src/bin/pg_dump/pg_dump.c

+5
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,11 @@ main(int argc, char **argv)
485485

486486
case 'Z': /* Compression Level */
487487
compressLevel = atoi(optarg);
488+
if (compressLevel < 0 || compressLevel > 9)
489+
{
490+
write_msg(NULL, "compression level must be in range 0..9\n");
491+
exit_nicely(1);
492+
}
488493
break;
489494

490495
case 0:

0 commit comments

Comments
 (0)