Skip to content

Commit 14ea893

Browse files
committed
Properly detect read and write errors in pg_dump/dumpall, and pg_restore
Previously some I/O errors were ignored.
1 parent 768fb00 commit 14ea893

File tree

10 files changed

+237
-207
lines changed

10 files changed

+237
-207
lines changed

src/bin/pg_dump/compress_io.c

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ static void InitCompressorZlib(CompressorState *cs, int level);
8686
static void DeflateCompressorZlib(ArchiveHandle *AH, CompressorState *cs,
8787
bool flush);
8888
static void ReadDataFromArchiveZlib(ArchiveHandle *AH, ReadFunc readF);
89-
static size_t WriteDataToArchiveZlib(ArchiveHandle *AH, CompressorState *cs,
89+
static void WriteDataToArchiveZlib(ArchiveHandle *AH, CompressorState *cs,
9090
const char *data, size_t dLen);
9191
static void EndCompressorZlib(ArchiveHandle *AH, CompressorState *cs);
9292
#endif
9393

9494
/* Routines that support uncompressed data I/O */
9595
static void ReadDataFromArchiveNone(ArchiveHandle *AH, ReadFunc readF);
96-
static size_t WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
96+
static void WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
9797
const char *data, size_t dLen);
9898

9999
/*
@@ -179,7 +179,7 @@ ReadDataFromArchive(ArchiveHandle *AH, int compression, ReadFunc readF)
179179
/*
180180
* Compress and write data to the output stream (via writeF).
181181
*/
182-
size_t
182+
void
183183
WriteDataToArchive(ArchiveHandle *AH, CompressorState *cs,
184184
const void *data, size_t dLen)
185185
{
@@ -190,14 +190,16 @@ WriteDataToArchive(ArchiveHandle *AH, CompressorState *cs,
190190
{
191191
case COMPR_ALG_LIBZ:
192192
#ifdef HAVE_LIBZ
193-
return WriteDataToArchiveZlib(AH, cs, data, dLen);
193+
WriteDataToArchiveZlib(AH, cs, data, dLen);
194194
#else
195195
exit_horribly(modulename, "not built with zlib support\n");
196196
#endif
197+
break;
197198
case COMPR_ALG_NONE:
198-
return WriteDataToArchiveNone(AH, cs, data, dLen);
199+
WriteDataToArchiveNone(AH, cs, data, dLen);
200+
break;
199201
}
200-
return 0; /* keep compiler quiet */
202+
return;
201203
}
202204

203205
/*
@@ -298,10 +300,7 @@ DeflateCompressorZlib(ArchiveHandle *AH, CompressorState *cs, bool flush)
298300
*/
299301
size_t len = cs->zlibOutSize - zp->avail_out;
300302

301-
if (cs->writeF(AH, out, len) != len)
302-
exit_horribly(modulename,
303-
"could not write to output file: %s\n",
304-
strerror(errno));
303+
cs->writeF(AH, out, len);
305304
}
306305
zp->next_out = (void *) out;
307306
zp->avail_out = cs->zlibOutSize;
@@ -312,19 +311,15 @@ DeflateCompressorZlib(ArchiveHandle *AH, CompressorState *cs, bool flush)
312311
}
313312
}
314313

315-
static size_t
314+
static void
316315
WriteDataToArchiveZlib(ArchiveHandle *AH, CompressorState *cs,
317316
const char *data, size_t dLen)
318317
{
319318
cs->zp->next_in = (void *) data;
320319
cs->zp->avail_in = dLen;
321320
DeflateCompressorZlib(AH, cs, false);
322321

323-
/*
324-
* we have either succeeded in writing dLen bytes or we have called
325-
* exit_horribly()
326-
*/
327-
return dLen;
322+
return;
328323
}
329324

330325
static void
@@ -427,19 +422,12 @@ ReadDataFromArchiveNone(ArchiveHandle *AH, ReadFunc readF)
427422
free(buf);
428423
}
429424

430-
static size_t
425+
static void
431426
WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
432427
const char *data, size_t dLen)
433428
{
434-
/*
435-
* Any write function should do its own error checking but to make sure we
436-
* do a check here as well...
437-
*/
438-
if (cs->writeF(AH, data, dLen) != dLen)
439-
exit_horribly(modulename,
440-
"could not write to output file: %s\n",
441-
strerror(errno));
442-
return dLen;
429+
cs->writeF(AH, data, dLen);
430+
return;
443431
}
444432

445433

@@ -573,12 +561,27 @@ cfopen(const char *path, const char *mode, int compression)
573561
int
574562
cfread(void *ptr, int size, cfp *fp)
575563
{
564+
int ret;
565+
566+
if (size == 0)
567+
return 0;
568+
576569
#ifdef HAVE_LIBZ
577570
if (fp->compressedfp)
578-
return gzread(fp->compressedfp, ptr, size);
571+
{
572+
ret = gzread(fp->compressedfp, ptr, size);
573+
if (ret != size && !gzeof(fp->compressedfp))
574+
exit_horribly(modulename,
575+
"could not read from input file: %s\n", strerror(errno));
576+
}
579577
else
580578
#endif
581-
return fread(ptr, 1, size, fp->uncompressedfp);
579+
{
580+
ret = fread(ptr, 1, size, fp->uncompressedfp);
581+
if (ret != size && !feof(fp->uncompressedfp))
582+
READ_ERROR_EXIT(fp->uncompressedfp);
583+
}
584+
return ret;
582585
}
583586

584587
int
@@ -595,12 +598,31 @@ cfwrite(const void *ptr, int size, cfp *fp)
595598
int
596599
cfgetc(cfp *fp)
597600
{
601+
int ret;
602+
598603
#ifdef HAVE_LIBZ
599604
if (fp->compressedfp)
600-
return gzgetc(fp->compressedfp);
605+
{
606+
ret = gzgetc(fp->compressedfp);
607+
if (ret == EOF)
608+
{
609+
if (!gzeof(fp->compressedfp))
610+
exit_horribly(modulename,
611+
"could not read from input file: %s\n", strerror(errno));
612+
else
613+
exit_horribly(modulename,
614+
"could not read from input file: end of file\n");
615+
}
616+
}
601617
else
602618
#endif
603-
return fgetc(fp->uncompressedfp);
619+
{
620+
ret = fgetc(fp->uncompressedfp);
621+
if (ret == EOF)
622+
READ_ERROR_EXIT(fp->uncompressedfp);
623+
}
624+
625+
return ret;
604626
}
605627

606628
char *

src/bin/pg_dump/compress_io.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef enum
2929
} CompressionAlgorithm;
3030

3131
/* Prototype for callback function to WriteDataToArchive() */
32-
typedef size_t (*WriteFunc) (ArchiveHandle *AH, const char *buf, size_t len);
32+
typedef void (*WriteFunc) (ArchiveHandle *AH, const char *buf, size_t len);
3333

3434
/*
3535
* Prototype for callback function to ReadDataFromArchive()
@@ -50,7 +50,7 @@ typedef struct CompressorState CompressorState;
5050
extern CompressorState *AllocateCompressor(int compression, WriteFunc writeF);
5151
extern void ReadDataFromArchive(ArchiveHandle *AH, int compression,
5252
ReadFunc readF);
53-
extern size_t WriteDataToArchive(ArchiveHandle *AH, CompressorState *cs,
53+
extern void WriteDataToArchive(ArchiveHandle *AH, CompressorState *cs,
5454
const void *data, size_t dLen);
5555
extern void EndCompressor(ArchiveHandle *AH, CompressorState *cs);
5656

src/bin/pg_dump/pg_backup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ extern void ArchiveEntry(Archive *AHX,
180180
DataDumperPtr dumpFn, void *dumpArg);
181181

182182
/* Called to write *data* to the archive */
183-
extern size_t WriteData(Archive *AH, const void *data, size_t dLen);
183+
extern void WriteData(Archive *AH, const void *data, size_t dLen);
184184

185185
extern int StartBlob(Archive *AH, Oid oid);
186186
extern int EndBlob(Archive *AH, Oid oid);
@@ -208,7 +208,7 @@ extern RestoreOptions *NewRestoreOptions(void);
208208
extern void SortTocFromFile(Archive *AHX, RestoreOptions *ropt);
209209

210210
/* Convenience functions used only when writing DATA */
211-
extern int archputs(const char *s, Archive *AH);
211+
extern void archputs(const char *s, Archive *AH);
212212
extern int
213213
archprintf(Archive *AH, const char *fmt,...)
214214
/* This extension allows gcc to check the format string */

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