aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2015-02-18 11:43:00 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2015-02-18 11:43:00 -0500
commita7ad5cf0cfcfab8418000d652fa4f0c6ad6c8911 (patch)
tree7948849407c319470c4d56fe01ac04592d9c7f74 /src
parent4ea2d2ddbe247d529e9d51a362704d67c56f4e48 (diff)
downloadpostgresql-a7ad5cf0cfcfab8418000d652fa4f0c6ad6c8911.tar.gz
postgresql-a7ad5cf0cfcfab8418000d652fa4f0c6ad6c8911.zip
Fix failure to honor -Z compression level option in pg_dump -Fd.
cfopen() and cfopen_write() failed to pass the compression level through to zlib, so that you always got the default compression level if you got any at all. In passing, also fix these and related functions so that the correct errno is reliably returned on failure; the original coding supposes that free() cannot change errno, which is untrue on at least some platforms. Per bug #12779 from Christoph Berg. Back-patch to 9.1 where the faulty code was introduced. Michael Paquier
Diffstat (limited to 'src')
-rw-r--r--src/bin/pg_dump/compress_io.c38
1 files changed, 29 insertions, 9 deletions
diff --git a/src/bin/pg_dump/compress_io.c b/src/bin/pg_dump/compress_io.c
index 1dd31fbe2c6..a5f550399fd 100644
--- a/src/bin/pg_dump/compress_io.c
+++ b/src/bin/pg_dump/compress_io.c
@@ -464,6 +464,16 @@ struct cfp
static int hasSuffix(const char *filename, const char *suffix);
#endif
+/* free() without changing errno; useful in several places below */
+static void
+free_keep_errno(void *p)
+{
+ int save_errno = errno;
+
+ free(p);
+ errno = save_errno;
+}
+
/*
* Open a file for reading. 'path' is the file to open, and 'mode' should
* be either "r" or "rb".
@@ -471,6 +481,8 @@ static int hasSuffix(const char *filename, const char *suffix);
* If the file at 'path' does not exist, we append the ".gz" suffix (if 'path'
* doesn't already have it) and try again. So if you pass "foo" as 'path',
* this will open either "foo" or "foo.gz".
+ *
+ * On failure, return NULL with an error code in errno.
*/
cfp *
cfopen_read(const char *path, const char *mode)
@@ -492,7 +504,7 @@ cfopen_read(const char *path, const char *mode)
snprintf(fname, fnamelen, "%s%s", path, ".gz");
fp = cfopen(fname, mode, 1);
- free(fname);
+ free_keep_errno(fname);
}
#endif
}
@@ -505,8 +517,10 @@ cfopen_read(const char *path, const char *mode)
* ("w", "wb", "a", or "ab").
*
* If 'compression' is non-zero, a gzip compressed stream is opened, and
- * and 'compression' indicates the compression level used. The ".gz" suffix
+ * 'compression' indicates the compression level used. The ".gz" suffix
* is automatically added to 'path' in that case.
+ *
+ * On failure, return NULL with an error code in errno.
*/
cfp *
cfopen_write(const char *path, const char *mode, int compression)
@@ -522,8 +536,8 @@ cfopen_write(const char *path, const char *mode, int compression)
char *fname = pg_malloc(fnamelen);
snprintf(fname, fnamelen, "%s%s", path, ".gz");
- fp = cfopen(fname, mode, 1);
- free(fname);
+ fp = cfopen(fname, mode, compression);
+ free_keep_errno(fname);
#else
exit_horribly(modulename, "not built with zlib support\n");
fp = NULL; /* keep compiler quiet */
@@ -534,7 +548,9 @@ cfopen_write(const char *path, const char *mode, int compression)
/*
* Opens file 'path' in 'mode'. If 'compression' is non-zero, the file
- * is opened with libz gzopen(), otherwise with plain fopen()
+ * is opened with libz gzopen(), otherwise with plain fopen().
+ *
+ * On failure, return NULL with an error code in errno.
*/
cfp *
cfopen(const char *path, const char *mode, int compression)
@@ -544,11 +560,15 @@ cfopen(const char *path, const char *mode, int compression)
if (compression != 0)
{
#ifdef HAVE_LIBZ
- fp->compressedfp = gzopen(path, mode);
+ char mode_compression[32];
+
+ snprintf(mode_compression, sizeof(mode_compression), "%s%d",
+ mode, compression);
+ fp->compressedfp = gzopen(path, mode_compression);
fp->uncompressedfp = NULL;
if (fp->compressedfp == NULL)
{
- free(fp);
+ free_keep_errno(fp);
fp = NULL;
}
#else
@@ -563,7 +583,7 @@ cfopen(const char *path, const char *mode, int compression)
fp->uncompressedfp = fopen(path, mode);
if (fp->uncompressedfp == NULL)
{
- free(fp);
+ free_keep_errno(fp);
fp = NULL;
}
}
@@ -638,7 +658,7 @@ cfclose(cfp *fp)
result = fclose(fp->uncompressedfp);
fp->uncompressedfp = NULL;
}
- free(fp);
+ free_keep_errno(fp);
return result;
}