Skip to content

Commit 060393f

Browse files
committed
Fix pg_dump's errno checking for zlib I/O
Some error reports were reporting strerror(errno), which for some error conditions coming from zlib are wrong, resulting in confusing reports such as pg_restore: [compress_io] could not read from input file: Success which makes no sense. To correctly extract the error message we need to use gzerror(), so let's do that. This isn't as comprehensive or as neat as I would like, but at least it should improve things in many common cases. The zlib abstraction in compress_io does not seem to be applied consistently enough; we could perhaps improve that, but it seems master-only material, not a bug fix for back-patching. This problem goes back all the way, but I decided to apply back to 9.4 only, because older branches don't contain commit 14ea893 which this change depends on. Authors: Vladimir Kunschikov, Álvaro Herrera Discussion: https://postgr.es/m/1498120508308.9826@infotecs.ru
1 parent e5c87d5 commit 060393f

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

src/bin/pg_dump/compress_io.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,14 @@ cfread(void *ptr, int size, cfp *fp)
592592
{
593593
ret = gzread(fp->compressedfp, ptr, size);
594594
if (ret != size && !gzeof(fp->compressedfp))
595+
{
596+
int errnum;
597+
const char *errmsg = gzerror(fp->compressedfp, &errnum);
598+
595599
exit_horribly(modulename,
596-
"could not read from input file: %s\n", strerror(errno));
600+
"could not read from input file: %s\n",
601+
errnum == Z_ERRNO ? strerror(errno) : errmsg);
602+
}
597603
}
598604
else
599605
#endif
@@ -695,6 +701,22 @@ cfeof(cfp *fp)
695701
return feof(fp->uncompressedfp);
696702
}
697703

704+
const char *
705+
get_cfp_error(cfp *fp)
706+
{
707+
#ifdef HAVE_LIBZ
708+
if (fp->compressedfp)
709+
{
710+
int errnum;
711+
const char *errmsg = gzerror(fp->compressedfp, &errnum);
712+
713+
if (errnum != Z_ERRNO)
714+
return errmsg;
715+
}
716+
#endif
717+
return strerror(errno);
718+
}
719+
698720
#ifdef HAVE_LIBZ
699721
static int
700722
hasSuffix(const char *filename, const char *suffix)

src/bin/pg_dump/compress_io.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ extern int cfgetc(cfp *fp);
6565
extern char *cfgets(cfp *fp, char *buf, int len);
6666
extern int cfclose(cfp *fp);
6767
extern int cfeof(cfp *fp);
68+
extern const char *get_cfp_error(cfp *fp);
6869

6970
#endif

src/bin/pg_dump/pg_backup_directory.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,9 @@ _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
357357
lclContext *ctx = (lclContext *) AH->formatData;
358358

359359
if (dLen > 0 && cfwrite(data, dLen, ctx->dataFH) != dLen)
360-
WRITE_ERROR_EXIT;
360+
exit_horribly(modulename, "could not write to output file: %s\n",
361+
get_cfp_error(ctx->dataFH));
362+
361363

362364
return;
363365
}
@@ -495,7 +497,8 @@ _WriteByte(ArchiveHandle *AH, const int i)
495497
lclContext *ctx = (lclContext *) AH->formatData;
496498

497499
if (cfwrite(&c, 1, ctx->dataFH) != 1)
498-
WRITE_ERROR_EXIT;
500+
exit_horribly(modulename, "could not write to output file: %s\n",
501+
get_cfp_error(ctx->dataFH));
499502

500503
return 1;
501504
}
@@ -524,7 +527,8 @@ _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
524527
lclContext *ctx = (lclContext *) AH->formatData;
525528

526529
if (cfwrite(buf, len, ctx->dataFH) != len)
527-
WRITE_ERROR_EXIT;
530+
exit_horribly(modulename, "could not write to output file: %s\n",
531+
get_cfp_error(ctx->dataFH));
528532

529533
return;
530534
}

src/bin/pg_dump/pg_backup_tar.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,14 @@ _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh)
557557
{
558558
res = GZREAD(&((char *) buf)[used], 1, len, th->zFH);
559559
if (res != len && !GZEOF(th->zFH))
560+
{
561+
int errnum;
562+
const char *errmsg = gzerror(th->zFH, &errnum);
563+
560564
exit_horribly(modulename,
561-
"could not read from input file: %s\n", strerror(errno));
565+
"could not read from input file: %s\n",
566+
errnum == Z_ERRNO ? strerror(errno) : errmsg);
567+
}
562568
}
563569
else
564570
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy