Skip to content

Commit 5b52008

Browse files
committed
Ensure write failure reports no-disk-space
A few places calling fwrite and gzwrite were not setting errno to ENOSPC when reporting errors, as is customary; this led to some failures being reported as "could not write file: Success" which makes us look silly. Make a few of these places in pg_dump and pg_basebackup use our customary pattern. Backpatch-to: 9.5 Author: Justin Pryzby <pryzby@telsasoft.com> Author: Tom Lane <tgl@sss.pgh.pa.us> Author: Álvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://postgr.es/m/20200611153753.GU14879@telsasoft.com
1 parent b22ca76 commit 5b52008

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/bin/pg_basebackup/pg_basebackup.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,8 +892,12 @@ writeTarData(
892892
#ifdef HAVE_LIBZ
893893
if (ztarfile != NULL)
894894
{
895+
errno = 0;
895896
if (gzwrite(ztarfile, buf, r) != r)
896897
{
898+
/* if write didn't set errno, assume problem is no disk space */
899+
if (errno == 0)
900+
errno = ENOSPC;
897901
pg_log_error("could not write to compressed file \"%s\": %s",
898902
current_file, get_gz_error(ztarfile));
899903
exit(1);
@@ -902,8 +906,12 @@ writeTarData(
902906
else
903907
#endif
904908
{
909+
errno = 0;
905910
if (fwrite(buf, r, 1, tarfile) != 1)
906911
{
912+
/* if write didn't set errno, assume problem is no disk space */
913+
if (errno == 0)
914+
errno = ENOSPC;
907915
pg_log_error("could not write to file \"%s\": %m", current_file);
908916
exit(1);
909917
}
@@ -1596,8 +1604,12 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum)
15961604
continue;
15971605
}
15981606

1607+
errno = 0;
15991608
if (fwrite(copybuf, r, 1, file) != 1)
16001609
{
1610+
/* if write didn't set errno, assume problem is no disk space */
1611+
if (errno == 0)
1612+
errno = ENOSPC;
16011613
pg_log_error("could not write to file \"%s\": %m", filename);
16021614
exit(1);
16031615
}

src/bin/pg_dump/pg_backup_directory.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,15 @@ _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
346346
{
347347
lclContext *ctx = (lclContext *) AH->formatData;
348348

349+
errno = 0;
349350
if (dLen > 0 && cfwrite(data, dLen, ctx->dataFH) != dLen)
351+
{
352+
/* if write didn't set errno, assume problem is no disk space */
353+
if (errno == 0)
354+
errno = ENOSPC;
350355
fatal("could not write to output file: %s",
351356
get_cfp_error(ctx->dataFH));
352-
357+
}
353358

354359
return;
355360
}
@@ -484,9 +489,15 @@ _WriteByte(ArchiveHandle *AH, const int i)
484489
unsigned char c = (unsigned char) i;
485490
lclContext *ctx = (lclContext *) AH->formatData;
486491

492+
errno = 0;
487493
if (cfwrite(&c, 1, ctx->dataFH) != 1)
494+
{
495+
/* if write didn't set errno, assume problem is no disk space */
496+
if (errno == 0)
497+
errno = ENOSPC;
488498
fatal("could not write to output file: %s",
489499
get_cfp_error(ctx->dataFH));
500+
}
490501

491502
return 1;
492503
}
@@ -514,9 +525,15 @@ _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
514525
{
515526
lclContext *ctx = (lclContext *) AH->formatData;
516527

528+
errno = 0;
517529
if (cfwrite(buf, len, ctx->dataFH) != len)
530+
{
531+
/* if write didn't set errno, assume problem is no disk space */
532+
if (errno == 0)
533+
errno = ENOSPC;
518534
fatal("could not write to output file: %s",
519535
get_cfp_error(ctx->dataFH));
536+
}
520537

521538
return;
522539
}

0 commit comments

Comments
 (0)