8000 Fix pg_dump's errno checking for zlib I/O · dirbacke/postgres@330e687 · GitHub
[go: up one dir, main page]

Skip to content

Commit 330e687

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 c006309 commit 330e687

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
@@ -66,5 +66,6 @@ extern int cfgetc(cfp *fp);
6666
extern char *cfgets(cfp *fp, char *buf, int len);
6767
extern int cfclose(cfp *fp);
6868
extern int cfeof(cfp *fp);
69+
extern const char *get_cfp_error(cfp *fp);
6970

7071
#endif

src/bin/pg_dump/pg_backup_directory.c

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

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

361363
return;
362364
}
@@ -494,7 +496,8 @@ _WriteByte(ArchiveHandle *AH, const int i)
494496
lclContext *ctx = (lclContext *) AH->formatData;
495497

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

499502
return 1;
500503
}
@@ -523,7 +526,8 @@ _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
523526
lclContext *ctx = (lclContext *) AH->formatData;
524527

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

528532
return;
529533
}

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)
0