Previously some I/O errors were ignored.
static void DeflateCompressorZlib(ArchiveHandle *AH, CompressorState *cs,
bool flush);
static void ReadDataFromArchiveZlib(ArchiveHandle *AH, ReadFunc readF);
-static size_t WriteDataToArchiveZlib(ArchiveHandle *AH, CompressorState *cs,
+static void WriteDataToArchiveZlib(ArchiveHandle *AH, CompressorState *cs,
const char *data, size_t dLen);
static void EndCompressorZlib(ArchiveHandle *AH, CompressorState *cs);
#endif
/* Routines that support uncompressed data I/O */
static void ReadDataFromArchiveNone(ArchiveHandle *AH, ReadFunc readF);
-static size_t WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
+static void WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
const char *data, size_t dLen);
/*
/*
* Compress and write data to the output stream (via writeF).
*/
-size_t
+void
WriteDataToArchive(ArchiveHandle *AH, CompressorState *cs,
const void *data, size_t dLen)
{
{
case COMPR_ALG_LIBZ:
#ifdef HAVE_LIBZ
- return WriteDataToArchiveZlib(AH, cs, data, dLen);
+ WriteDataToArchiveZlib(AH, cs, data, dLen);
#else
exit_horribly(modulename, "not built with zlib support\n");
#endif
+ break;
case COMPR_ALG_NONE:
- return WriteDataToArchiveNone(AH, cs, data, dLen);
+ WriteDataToArchiveNone(AH, cs, data, dLen);
+ break;
}
- return 0; /* keep compiler quiet */
+ return;
}
/*
*/
size_t len = cs->zlibOutSize - zp->avail_out;
- if (cs->writeF(AH, out, len) != len)
- exit_horribly(modulename,
- "could not write to output file: %s\n",
- strerror(errno));
+ cs->writeF(AH, out, len);
}
zp->next_out = (void *) out;
zp->avail_out = cs->zlibOutSize;
}
}
-static size_t
+static void
WriteDataToArchiveZlib(ArchiveHandle *AH, CompressorState *cs,
const char *data, size_t dLen)
{
cs->zp->avail_in = dLen;
DeflateCompressorZlib(AH, cs, false);
- /*
- * we have either succeeded in writing dLen bytes or we have called
- * exit_horribly()
- */
- return dLen;
+ return;
}
static void
free(buf);
}
-static size_t
+static void
WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
const char *data, size_t dLen)
{
- /*
- * Any write function should do its own error checking but to make sure we
- * do a check here as well...
- */
- if (cs->writeF(AH, data, dLen) != dLen)
- exit_horribly(modulename,
- "could not write to output file: %s\n",
- strerror(errno));
- return dLen;
+ cs->writeF(AH, data, dLen);
+ return;
}
int
cfread(void *ptr, int size, cfp *fp)
{
+ int ret;
+
+ if (size == 0)
+ return 0;
+
#ifdef HAVE_LIBZ
if (fp->compressedfp)
- return gzread(fp->compressedfp, ptr, size);
+ {
+ ret = gzread(fp->compressedfp, ptr, size);
+ if (ret != size && !gzeof(fp->compressedfp))
+ exit_horribly(modulename,
+ "could not read from input file: %s\n", strerror(errno));
+ }
else
#endif
- return fread(ptr, 1, size, fp->uncompressedfp);
+ {
+ ret = fread(ptr, 1, size, fp->uncompressedfp);
+ if (ret != size && !feof(fp->uncompressedfp))
+ READ_ERROR_EXIT(fp->uncompressedfp);
+ }
+ return ret;
}
int
int
cfgetc(cfp *fp)
{
+ int ret;
+
#ifdef HAVE_LIBZ
if (fp->compressedfp)
- return gzgetc(fp->compressedfp);
+ {
+ ret = gzgetc(fp->compressedfp);
+ if (ret == EOF)
+ {
+ if (!gzeof(fp->compressedfp))
+ exit_horribly(modulename,
+ "could not read from input file: %s\n", strerror(errno));
+ else
+ exit_horribly(modulename,
+ "could not read from input file: end of file\n");
+ }
+ }
else
#endif
- return fgetc(fp->uncompressedfp);
+ {
+ ret = fgetc(fp->uncompressedfp);
+ if (ret == EOF)
+ READ_ERROR_EXIT(fp->uncompressedfp);
+ }
+
+ return ret;
}
char *
} CompressionAlgorithm;
/* Prototype for callback function to WriteDataToArchive() */
-typedef size_t (*WriteFunc) (ArchiveHandle *AH, const char *buf, size_t len);
+typedef void (*WriteFunc) (ArchiveHandle *AH, const char *buf, size_t len);
/*
* Prototype for callback function to ReadDataFromArchive()
extern CompressorState *AllocateCompressor(int compression, WriteFunc writeF);
extern void ReadDataFromArchive(ArchiveHandle *AH, int compression,
ReadFunc readF);
-extern size_t WriteDataToArchive(ArchiveHandle *AH, CompressorState *cs,
+extern void WriteDataToArchive(ArchiveHandle *AH, CompressorState *cs,
const void *data, size_t dLen);
extern void EndCompressor(ArchiveHandle *AH, CompressorState *cs);
DataDumperPtr dumpFn, void *dumpArg);
/* Called to write *data* to the archive */
-extern size_t WriteData(Archive *AH, const void *data, size_t dLen);
+extern void WriteData(Archive *AH, const void *data, size_t dLen);
extern int StartBlob(Archive *AH, Oid oid);
extern int EndBlob(Archive *AH, Oid oid);
extern void SortTocFromFile(Archive *AHX, RestoreOptions *ropt);
/* Convenience functions used only when writing DATA */
-extern int archputs(const char *s, Archive *AH);
+extern void archputs(const char *s, Archive *AH);
extern int
archprintf(Archive *AH, const char *fmt,...)
/* This extension allows gcc to check the format string */
*/
/* Public */
-size_t
+void
WriteData(Archive *AHX, const void *data, size_t dLen)
{
ArchiveHandle *AH = (ArchiveHandle *) AHX;
if (!AH->currToc)
exit_horribly(modulename, "internal error -- WriteData cannot be called outside the context of a DataDumper routine\n");
- return (*AH->WriteDataPtr) (AH, data, dLen);
+ (*AH->WriteDataPtr) (AH, data, dLen);
+
+ return;
}
/*
**********************/
/* Public */
-int
+void
archputs(const char *s, Archive *AH)
{
- return WriteData(AH, s, strlen(s));
+ WriteData(AH, s, strlen(s));
+ return;
}
/* Public */
* format to create a custom output routine to 'fake' a restore if it
* wants to generate a script (see TAR output).
*/
-int
+void
ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
{
- size_t res;
-
+ int bytes_written = 0;
+
if (AH->writingBlob)
{
size_t remaining = size * nmemb;
memcpy((char *) AH->lo_buf + AH->lo_buf_used, ptr, remaining);
AH->lo_buf_used += remaining;
- return size * nmemb;
+ bytes_written = size * nmemb;
}
else if (AH->gzOut)
- {
- res = GZWRITE(ptr, size, nmemb, AH->OF);
- if (res != (nmemb * size))
- exit_horribly(modulename, "could not write to output file: %s\n", strerror(errno));
- return res;
- }
+ bytes_written = GZWRITE(ptr, size, nmemb, AH->OF);
else if (AH->CustomOutPtr)
- {
- res = AH->CustomOutPtr (AH, ptr, size * nmemb);
-
- if (res != (nmemb * size))
- exit_horribly(modulename, "could not write to custom output routine\n");
- return res;
- }
+ bytes_written = AH->CustomOutPtr (AH, ptr, size * nmemb);
else
{
/*
* connected then send it to the DB.
*/
if (RestoringToDB(AH))
- return ExecuteSqlCommandBuf(AH, (const char *) ptr, size * nmemb);
+ bytes_written = ExecuteSqlCommandBuf(AH, (const char *) ptr, size * nmemb);
else
- {
- res = fwrite(ptr, size, nmemb, AH->OF);
- if (res != nmemb)
- exit_horribly(modulename, "could not write to output file: %s\n",
- strerror(errno));
- return res;
- }
+ bytes_written = fwrite(ptr, size, nmemb, AH->OF) * size;
}
+
+ if (bytes_written != size * nmemb)
+ WRITE_ERROR_EXIT;
+
+ return;
}
/* on some error, we may decide to go on... */
if (c)
{
- res = WriteInt(AH, strlen(c));
- res += (*AH->WriteBufPtr) (AH, c, strlen(c));
+ int len = strlen(c);
+
+ res = WriteInt(AH, len);
+ (*AH->WriteBufPtr) (AH, c, len);
+ res += len;
}
else
res = WriteInt(AH, -1);
else
{
buf = (char *) pg_malloc(l + 1);
- if ((*AH->ReadBufPtr) (AH, (void *) buf, l) != l)
- exit_horribly(modulename, "unexpected end of file\n");
+ (*AH->ReadBufPtr) (AH, (void *) buf, l);
buf[l] = '\0';
}
strerror(errno));
}
- cnt = fread(sig, 1, 5, fh);
-
- if (cnt != 5)
+ if ((cnt = fread(sig, 1, 5, fh)) != 5)
{
if (ferror(fh))
exit_horribly(modulename, "could not read input file: %s\n", strerror(errno));
* NB: this code must agree with ReadHead().
*/
if ((byteread = fgetc(fh)) == EOF)
- exit_horribly(modulename, "could not read input file: %s\n", strerror(errno));
+ READ_ERROR_EXIT(fh);
AH->vmaj = byteread;
if ((byteread = fgetc(fh)) == EOF)
- exit_horribly(modulename, "could not read input file: %s\n", strerror(errno));
+ READ_ERROR_EXIT(fh);
AH->vmin = byteread;
if (AH->vmaj > 1 || ((AH->vmaj == 1) && (AH->vmin > 0))) /* Version > 1.0 */
{
if ((byteread = fgetc(fh)) == EOF)
- exit_horribly(modulename, "could not read input file: %s\n", strerror(errno));
+ READ_ERROR_EXIT(fh);
AH->vrev = byteread;
AH->lookahead[AH->lookaheadLen++] = AH->vrev;
AH->version = ((AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev) * 256 + 0;
if ((AH->intSize = fgetc(fh)) == EOF)
- exit_horribly(modulename, "could not read input file: %s\n", strerror(errno));
+ READ_ERROR_EXIT(fh);
AH->lookahead[AH->lookaheadLen++] = AH->intSize;
if (AH->version >= K_VERS_1_7)
{
if ((AH->offSize = fgetc(fh)) == EOF)
- exit_horribly(modulename, "could not read input file: %s\n", strerror(errno));
+ READ_ERROR_EXIT(fh);
AH->lookahead[AH->lookaheadLen++] = AH->offSize;
}
else
AH->offSize = AH->intSize;
if ((byteread = fgetc(fh)) == EOF)
- exit_horribly(modulename, "could not read input file: %s\n", strerror(errno));
+ READ_ERROR_EXIT(fh);
AH->format = byteread;
AH->lookahead[AH->lookaheadLen++] = AH->format;
* read first 512 byte header...
*/
cnt = fread(&AH->lookahead[AH->lookaheadLen], 1, 512 - AH->lookaheadLen, fh);
+ /* read failure is checked below */
AH->lookaheadLen += cnt;
if (AH->lookaheadLen >= strlen(TEXT_DUMPALL_HEADER) &&
exit_horribly(modulename, "input file appears to be a text format dump. Please use psql.\n");
}
- if (AH->lookaheadLen != 512)
+ if (AH->lookaheadLen != 512 && feof(fh))
exit_horribly(modulename, "input file does not appear to be a valid archive (too short?)\n");
+ else
+ READ_ERROR_EXIT(fh);
if (!isValidTarHeader(AH->lookahead))
exit_horribly(modulename, "input file does not appear to be a valid archive\n");
*/
if (!AH->readHeader)
{
- if ((*AH->ReadBufPtr) (AH, tmpMag, 5) != 5)
- exit_horribly(modulename, "unexpected end of file\n");
+ (*AH->ReadBufPtr) (AH, tmpMag, 5);
if (strncmp(tmpMag, "PGDMP", 5) != 0)
exit_horribly(modulename, "did not find magic string in file header\n");
#define GZCLOSE(fh) gzclose(fh)
#define GZWRITE(p, s, n, fh) gzwrite(fh, p, (n) * (s))
#define GZREAD(p, s, n, fh) gzread(fh, p, (n) * (s))
+#define GZEOF(fh) gzeof(fh)
#else
#define GZCLOSE(fh) fclose(fh)
#define GZWRITE(p, s, n, fh) (fwrite(p, s, n, fh) * (s))
#define GZREAD(p, s, n, fh) fread(p, s, n, fh)
+#define GZEOF(fh) feof(fh)
/* this is just the redefinition of a libz constant */
#define Z_DEFAULT_COMPRESSION (-1)
struct ParallelArgs;
struct ParallelState;
+#define READ_ERROR_EXIT(fd) \
+ do { \
+ if (feof(fd)) \
+ exit_horribly(modulename, \
+ "could not read from input file: end of file\n"); \
+ else \
+ exit_horribly(modulename, \
+ "could not read from input file: %s\n", strerror(errno)); \
+ } while (0)
+
+#define WRITE_ERROR_EXIT \
+ do { \
+ exit_horribly(modulename, "could not write to output file: %s\n", \
+ strerror(errno)); \
+ } while (0)
+
typedef enum T_Action
{
ACT_DUMP,
typedef void (*ArchiveEntryPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
typedef void (*StartDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
-typedef size_t (*WriteDataPtr) (struct _archiveHandle * AH, const void *data, size_t dLen);
+typedef void (*WriteDataPtr) (struct _archiveHandle * AH, const void *data, size_t dLen);
typedef void (*EndDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
typedef void (*StartBlobsPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
typedef int (*WriteBytePtr) (struct _archiveHandle * AH, const int i);
typedef int (*ReadBytePtr) (struct _archiveHandle * AH);
-typedef size_t (*WriteBufPtr) (struct _archiveHandle * AH, const void *c, size_t len);
-typedef size_t (*ReadBufPtr) (struct _archiveHandle * AH, void *buf, size_t len);
+typedef void (*WriteBufPtr) (struct _archiveHandle * AH, const void *c, size_t len);
+typedef void (*ReadBufPtr) (struct _archiveHandle * AH, void *buf, size_t len);
typedef void (*SaveArchivePtr) (struct _archiveHandle * AH);
typedef void (*WriteExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
typedef void (*ReadExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
extern int ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *newUser);
extern void DropBlobIfExists(ArchiveHandle *AH, Oid oid);
-int ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH);
+void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH);
int ahprintf(ArchiveHandle *AH, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
void ahlog(ArchiveHandle *AH, int level, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te);
static void _StartData(ArchiveHandle *AH, TocEntry *te);
-static size_t _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);
+static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);
static void _EndData(ArchiveHandle *AH, TocEntry *te);
static int _WriteByte(ArchiveHandle *AH, const int i);
static int _ReadByte(ArchiveHandle *);
-static size_t _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
-static size_t _ReadBuf(ArchiveHandle *AH, void *buf, size_t len);
+static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
+static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len);
static void _CloseArchive(ArchiveHandle *AH);
static void _ReopenArchive(ArchiveHandle *AH);
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
static void _readBlockHeader(ArchiveHandle *AH, int *type, int *id);
static pgoff_t _getFilePos(ArchiveHandle *AH, lclContext *ctx);
-static size_t _CustomWriteFunc(ArchiveHandle *AH, const char *buf, size_t len);
+static void _CustomWriteFunc(ArchiveHandle *AH, const char *buf, size_t len);
static size_t _CustomReadFunc(ArchiveHandle *AH, char **buf, size_t *buflen);
/* translator: this is a module name */
*
* Mandatory.
*/
-static size_t
+static void
_WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
{
lclContext *ctx = (lclContext *) AH->formatData;
CompressorState *cs = ctx->cs;
- if (dLen == 0)
- return 0;
+ if (dLen > 0)
+ /* WriteDataToArchive() internally throws write errors */
+ WriteDataToArchive(AH, cs, data, dLen);
- return WriteDataToArchive(AH, cs, data, dLen);
+ return;
}
/*
buf = (char *) pg_malloc(blkLen);
buflen = blkLen;
}
- cnt = fread(buf, 1, blkLen, AH->FH);
- if (cnt != blkLen)
+ if ((cnt = fread(buf, 1, blkLen, AH->FH)) != blkLen)
{
if (feof(AH->FH))
exit_horribly(modulename,
_WriteByte(ArchiveHandle *AH, const int i)
{
lclContext *ctx = (lclContext *) AH->formatData;
- int res;
+ int res;
- res = fputc(i, AH->FH);
- if (res != EOF)
- ctx->filePos += 1;
- else
- exit_horribly(modulename, "could not write byte: %s\n", strerror(errno));
- return res;
+ if ((res = fputc(i, AH->FH)) == EOF)
+ WRITE_ERROR_EXIT;
+ ctx->filePos += 1;
+
+ return 1;
}
/*
res = getc(AH->FH);
if (res == EOF)
- exit_horribly(modulename, "unexpected end of file\n");
+ READ_ERROR_EXIT(AH->FH);
ctx->filePos += 1;
return res;
}
*
* Called by the archiver to write a block of bytes to the archive.
*/
-static size_t
+static void
_WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
{
lclContext *ctx = (lclContext *) AH->formatData;
- size_t res;
- res = fwrite(buf, 1, len, AH->FH);
+ if (fwrite(buf, 1, len, AH->FH) != len)
+ WRITE_ERROR_EXIT;
+ ctx->filePos += len;
- if (res != len)
- exit_horribly(modulename,
- "could not write to output file: %s\n", strerror(errno));
-
- ctx->filePos += res;
- return res;
+ return;
}
/*
*
* Called by the archiver to read a block of bytes from the archive
*/
-static size_t
+static void
_ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
{
lclContext *ctx = (lclContext *) AH->formatData;
- size_t res;
- res = fread(buf, 1, len, AH->FH);
- ctx->filePos += res;
+ if (fread(buf, 1, len, AH->FH) != len)
+ READ_ERROR_EXIT(AH->FH);
+ ctx->filePos += len;
- return res;
+ return;
}
/*
* Callback function for WriteDataToArchive. Writes one block of (compressed)
* data to the archive.
*/
-static size_t
+static void
_CustomWriteFunc(ArchiveHandle *AH, const char *buf, size_t len)
{
/* never write 0-byte blocks (this should not happen) */
- if (len == 0)
- return 0;
-
- WriteInt(AH, len);
- return _WriteBuf(AH, buf, len);
+ if (len > 0)
+ {
+ WriteInt(AH, len);
+ _WriteBuf(AH, buf, len);
+ }
+ return;
}
/*
_CustomReadFunc(ArchiveHandle *AH, char **buf, size_t *buflen)
{
size_t blkLen;
- size_t cnt;
/* Read length */
blkLen = ReadInt(AH);
*buflen = blkLen;
}
- cnt = _ReadBuf(AH, *buf, blkLen);
- if (cnt != blkLen)
- {
- if (feof(AH->FH))
- exit_horribly(modulename,
- "could not read from input file: end of file\n");
- else
- exit_horribly(modulename,
- "could not read from input file: %s\n", strerror(errno));
- }
- return cnt;
+ /* exits app on read errors */
+ _ReadBuf(AH, *buf, blkLen);
+
+ return blkLen;
}
}
}
- return 1;
+ return bufLen;
}
/*
static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te);
static void _StartData(ArchiveHandle *AH, TocEntry *te);
static void _EndData(ArchiveHandle *AH, TocEntry *te);
-static size_t _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);
+static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);
static int _WriteByte(ArchiveHandle *AH, const int i);
static int _ReadByte(ArchiveHandle *);
-static size_t _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
-static size_t _ReadBuf(ArchiveHandle *AH, void *buf, size_t len);
+static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
+static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len);
static void _CloseArchive(ArchiveHandle *AH);
static void _ReopenArchive(ArchiveHandle *AH);
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
*
* We write the data to the open data file.
*/
-static size_t
+static void
_WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
{
lclContext *ctx = (lclContext *) AH->formatData;
- if (dLen == 0)
- return 0;
-
/* Are we aborting? */
checkAborting(AH);
+
+ if (dLen > 0 && cfwrite(data, dLen, ctx->dataFH) != dLen)
+ WRITE_ERROR_EXIT;
- return cfwrite(data, dLen, ctx->dataFH);
+ return;
}
/*
ahwrite(buf, 1, cnt, AH);
free(buf);
- if (cfclose(cfp) !=0)
+ if (cfclose(cfp) != 0)
exit_horribly(modulename, "could not close data file: %s\n",
strerror(errno));
}
lclContext *ctx = (lclContext *) AH->formatData;
if (cfwrite(&c, 1, ctx->dataFH) != 1)
- exit_horribly(modulename, "could not write byte\n");
+ WRITE_ERROR_EXIT;
return 1;
}
_ReadByte(ArchiveHandle *AH)
{
lclContext *ctx = (lclContext *) AH->formatData;
- int res;
- res = cfgetc(ctx->dataFH);
- if (res == EOF)
- exit_horribly(modulename, "unexpected end of file\n");
-
- return res;
+ return cfgetc(ctx->dataFH);
}
/*
* Write a buffer of data to the archive.
* Called by the archiver to write a block of bytes to the TOC or a data file.
*/
-static size_t
+static void
_WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
{
lclContext *ctx = (lclContext *) AH->formatData;
- size_t res;
/* Are we aborting? */
checkAborting(AH);
- res = cfwrite(buf, len, ctx->dataFH);
- if (res != len)
- exit_horribly(modulename, "could not write to output file: %s\n",
- strerror(errno));
+ if (cfwrite(buf, len, ctx->dataFH) != len)
+ WRITE_ERROR_EXIT;
- return res;
+ return;
}
/*
*
* Called by the archiver to read a block of bytes from the archive
*/
-static size_t
+static void
_ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
{
lclContext *ctx = (lclContext *) AH->formatData;
- size_t res;
- res = cfread(buf, len, ctx->dataFH);
+ /*
+ * If there was an I/O error, we already exited in cfread(),
+ * so here we exit on short reads.
+ */
+ if (cfread(buf, len, ctx->dataFH) != len)
+ exit_horribly(modulename,
+ "could not read from input file: end of file\n");
- return res;
+ return;
}
/*
#include "libpq/libpq-fs.h"
-
-static size_t _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);
-static size_t _WriteBlobData(ArchiveHandle *AH, const void *data, size_t dLen);
+static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);
+static void _WriteBlobData(ArchiveHandle *AH, const void *data, size_t dLen);
static void _EndData(ArchiveHandle *AH, TocEntry *te);
static int _WriteByte(ArchiveHandle *AH, const int i);
-static size_t _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
+static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
static void _CloseArchive(ArchiveHandle *AH);
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
static void _StartBlobs(ArchiveHandle *AH, TocEntry *te);
/*
* Called by dumper via archiver from within a data dump routine
*/
-static size_t
+static void
_WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
{
- /* Just send it to output */
+ /* Just send it to output, ahwrite() already errors on failure */
ahwrite(data, 1, dLen, AH);
- return dLen;
+ return;
}
/*
* Called by dumper via archiver from within a data dump routine
* We substitute this for _WriteData while emitting a BLOB
*/
-static size_t
+static void
_WriteBlobData(ArchiveHandle *AH, const void *data, size_t dLen)
{
if (dLen > 0)
destroyPQExpBuffer(buf);
}
- return dLen;
+ return;
}
static void
return 0;
}
-static size_t
+static void
_WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
{
/* Don't do anything */
- return len;
+ return;
}
static void
static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te);
static void _StartData(ArchiveHandle *AH, TocEntry *te);
-static size_t _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);
+static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);
static void _EndData(ArchiveHandle *AH, TocEntry *te);
static int _WriteByte(ArchiveHandle *AH, const int i);
static int _ReadByte(ArchiveHandle *);
-static size_t _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
-static size_t _ReadBuf(ArchiveHandle *AH, void *buf, size_t len);
+static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
+static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len);
static void _CloseArchive(ArchiveHandle *AH);
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te);
if (len > 0)
{
if (fh)
+ {
res = fread(&((char *) buf)[used], 1, len, fh);
+ if (res != len && !feof(fh))
+ READ_ERROR_EXIT(fh);
+ }
else if (th)
{
if (th->zFH)
+ {
res = GZREAD(&((char *) buf)[used], 1, len, th->zFH);
+ if (res != len && !GZEOF(fh))
+ exit_horribly(modulename,
+ "could not read from input file: %s\n", strerror(errno));
+ }
else
+ {
res = fread(&((char *) buf)[used], 1, len, th->nFH);
+ if (res != len && !feof(fh))
+ READ_ERROR_EXIT(fh);
+ }
}
else
exit_horribly(modulename, "internal error -- neither th nor fh specified in tarReadRaw()\n");
else
res = fwrite(buf, 1, len, th->nFH);
- if (res != len)
- exit_horribly(modulename,
- "could not write to output file: %s\n", strerror(errno));
-
th->pos += res;
return res;
}
-static size_t
+static void
_WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
{
lclTocEntry *tctx = (lclTocEntry *) AH->currToc->formatData;
- dLen = tarWrite(data, dLen, tctx->TH);
+ if (tarWrite(data, dLen, tctx->TH) != dLen)
+ WRITE_ERROR_EXIT;
- return dLen;
+ return;
}
static void
_WriteByte(ArchiveHandle *AH, const int i)
{
lclContext *ctx = (lclContext *) AH->formatData;
- int res;
char b = i; /* Avoid endian problems */
- res = tarWrite(&b, 1, ctx->FH);
- if (res != EOF)
- ctx->filePos += res;
- return res;
+ if (tarWrite(&b, 1, ctx->FH) != 1)
+ WRITE_ERROR_EXIT;
+
+ ctx->filePos += 1;
+ return 1;
}
static int
res = tarRead(&c, 1, ctx->FH);
if (res != 1)
- exit_horribly(modulename, "unexpected end of file\n");
+ /* We already would have exited for errors on reads, must be EOF */
+ exit_horribly(modulename,
+ "could not read from input file: end of file\n");
ctx->filePos += 1;
return c;
}
-static size_t
+static void
_WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
{
lclContext *ctx = (lclContext *) AH->formatData;
- size_t res;
- res = tarWrite(buf, len, ctx->FH);
- ctx->filePos += res;
- return res;
+ if (tarWrite(buf, len, ctx->FH) != len)
+ WRITE_ERROR_EXIT;
+
+ ctx->filePos += len;
}
-static size_t
+static void
_ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
{
lclContext *ctx = (lclContext *) AH->formatData;
- size_t res;
- res = tarRead(buf, len, ctx->FH);
- ctx->filePos += res;
- return res;
+ if (tarRead(buf, len, ctx->FH) != len)
+ /* We already would have exited for errors on reads, must be EOF */
+ exit_horribly(modulename,
+ "could not read from input file: end of file\n");
+
+ ctx->filePos += len;
+ return;
}
static void
for (i = 0; i < 512 * 2; i++)
{
if (fputc(0, ctx->tarFH) == EOF)
- exit_horribly(modulename,
- "could not write null block at end of tar archive\n");
+ WRITE_ERROR_EXIT;
}
}
while ((cnt = fread(buf, 1, sizeof(buf), tmp)) > 0)
{
- res = fwrite(buf, 1, cnt, th->tarFH);
- if (res != cnt)
- exit_horribly(modulename,
- "could not write to output file: %s\n",
- strerror(errno));
+ if ((res = fwrite(buf, 1, cnt, th->tarFH)) != cnt)
+ WRITE_ERROR_EXIT;
len += res;
}
+ if (!feof(tmp))
+ READ_ERROR_EXIT(tmp);
if (fclose(tmp) != 0) /* This *should* delete it... */
exit_horribly(modulename, "could not close temporary file: %s\n",
for (i = 0; i < pad; i++)
{
if (fputc('\0', th->tarFH) == EOF)
- exit_horribly(modulename, "could not output padding at end of tar member\n");
+ WRITE_ERROR_EXIT;
}
ctx->tarFHpos += len + pad;
/* Now write the completed header. */
if (fwrite(h, 1, 512, th->tarFH) != 512)
- exit_horribly(modulename, "could not write to output file: %s\n", strerror(errno));
+ WRITE_ERROR_EXIT;
}