Skip to content

Commit 41ed5bb

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 b755133 commit 41ed5bb

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
@@ -546,11 +546,21 @@ cfopen(const char *path, const char *mode, int compression)
546546
if (compression != 0)
547547
{
548548
#ifdef HAVE_LIBZ
549-
char mode_compression[32];
549+
if (compression != Z_DEFAULT_COMPRESSION)
550+
{
551+
/* user has specified a compression level, so tell zlib to use it */
552+
char mode_compression[32];
553+
554+
snprintf(mode_compression, sizeof(mode_compression), "%s%d",
555+
mode, compression);
556+
fp->compressedfp = gzopen(path, mode_compression);
557+
}
558+
else
559+
{
560+
/* don't specify a level, just use the zlib default */
561+
fp->compressedfp = gzopen(path, mode);
562+
}
550563

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

src/bin/pg_dump/pg_backup_tar.c

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

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

212-
if (AH->compression < 0 || AH->compression > 9)
213-
AH->compression = Z_DEFAULT_COMPRESSION;
214-
215-
/* Don't compress into tar files unless asked to do so */
216-
if (AH->compression == Z_DEFAULT_COMPRESSION)
217-
AH->compression = 0;
218-
219212
/*
220213
* We don't support compression because reading the files back is not
221214
* 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
@@ -513,6 +513,11 @@ main(int argc, char **argv)
513513

514514
case 'Z': /* Compression Level */
515515
compressLevel = atoi(optarg);
516+
if (compressLevel < 0 || compressLevel > 9)
517+
{
518+
write_msg(NULL, "compression level must be in range 0..9\n");
519+
exit_nicely(1);
520+
}
516521
break;
517522

518523
case 0:

0 commit comments

Comments
 (0)