Skip to content

Commit 4d57e83

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 8021515 commit 4d57e83

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

src/bin/pg_dump/compress_io.c

+23-1
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

+1
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

+7-3
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,9 @@ _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
352352
lclContext *ctx = (lclContext *) AH->formatData;
353353

354354
if (dLen > 0 && cfwrite(data, dLen, ctx->dataFH) != dLen)
355-
WRITE_ERROR_EXIT;
355+
exit_horribly(modulename, "could not write to output file: %s\n",
356+
get_cfp_error(ctx->dataFH));
357+
356358

357359
return;
358360
}
@@ -490,7 +492,8 @@ _WriteByte(ArchiveHandle *AH, const int i)
490492
lclContext *ctx = (lclContext *) AH->formatData;
491493

492494
if (cfwrite(&c, 1, ctx->dataFH) != 1)
493-
WRITE_ERROR_EXIT;
495+
exit_horribly(modulename, "could not write to output file: %s\n",
496+
get_cfp_error(ctx->dataFH));
494497

495498
return 1;
496499
}
@@ -519,7 +522,8 @@ _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
519522
lclContext *ctx = (lclContext *) AH->formatData;
520523

521524
if (cfwrite(buf, len, ctx->dataFH) != len)
522-
WRITE_ERROR_EXIT;
525+
exit_horribly(modulename, "could not write to output file: %s\n",
526+
get_cfp_error(ctx->dataFH));
523527

524528
return;
525529
}

src/bin/pg_dump/pg_backup_tar.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,14 @@ _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh)
555555
{
556556
res = GZREAD(&((char *) buf)[used], 1, len, th->zFH);
557557
if (res != len && !GZEOF(th->zFH))
558+
{
559+
int errnum;
560+
const char *errmsg = gzerror(th->zFH, &errnum);
561+
558562
exit_horribly(modulename,
559-
"could not read from input file: %s\n", strerror(errno));
563+
"could not read from input file: %s\n",
564+
errnum == Z_ERRNO ? strerror(errno) : errmsg);
565+
}
560566
}
561567
else
562568
{

0 commit comments

Comments
 (0)