aboutsummaryrefslogtreecommitdiff
path: root/src/bin/pg_dump/pg_backup_archiver.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/pg_dump/pg_backup_archiver.c')
-rw-r--r--src/bin/pg_dump/pg_backup_archiver.c137
1 files changed, 39 insertions, 98 deletions
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 5b9fbe89d01..068e86f436d 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -2086,6 +2086,7 @@ _discoverArchiveFormat(ArchiveHandle *AH)
if (AH->lookahead)
free(AH->lookahead);
+ AH->readHeader = 0;
AH->lookaheadSize = 512;
AH->lookahead = pg_malloc0(512);
AH->lookaheadLen = 0;
@@ -2157,62 +2158,9 @@ _discoverArchiveFormat(ArchiveHandle *AH)
if (strncmp(sig, "PGDMP", 5) == 0)
{
- int byteread;
- char vmaj,
- vmin,
- vrev;
-
- /*
- * Finish reading (most of) a custom-format header.
- *
- * NB: this code must agree with ReadHead().
- */
- if ((byteread = fgetc(fh)) == EOF)
- READ_ERROR_EXIT(fh);
-
- vmaj = byteread;
-
- if ((byteread = fgetc(fh)) == EOF)
- READ_ERROR_EXIT(fh);
-
- vmin = byteread;
-
- /* Save these too... */
- AH->lookahead[AH->lookaheadLen++] = vmaj;
- AH->lookahead[AH->lookaheadLen++] = vmin;
-
- /* Check header version; varies from V1.0 */
- if (vmaj > 1 || (vmaj == 1 && vmin > 0)) /* Version > 1.0 */
- {
- if ((byteread = fgetc(fh)) == EOF)
- READ_ERROR_EXIT(fh);
-
- vrev = byteread;
- AH->lookahead[AH->lookaheadLen++] = vrev;
- }
- else
- vrev = 0;
-
- AH->version = MAKE_ARCHIVE_VERSION(vmaj, vmin, vrev);
-
- if ((AH->intSize = fgetc(fh)) == EOF)
- READ_ERROR_EXIT(fh);
- AH->lookahead[AH->lookaheadLen++] = AH->intSize;
-
- if (AH->version >= K_VERS_1_7)
- {
- if ((AH->offSize = fgetc(fh)) == EOF)
- READ_ERROR_EXIT(fh);
- AH->lookahead[AH->lookaheadLen++] = AH->offSize;
- }
- else
- AH->offSize = AH->intSize;
-
- if ((byteread = fgetc(fh)) == EOF)
- READ_ERROR_EXIT(fh);
-
- AH->format = byteread;
- AH->lookahead[AH->lookaheadLen++] = AH->format;
+ /* It's custom format, stop here */
+ AH->format = archCustom;
+ AH->readHeader = 1;
}
else
{
@@ -2249,22 +2197,15 @@ _discoverArchiveFormat(ArchiveHandle *AH)
AH->format = archTar;
}
- /* If we can't seek, then mark the header as read */
- if (fseeko(fh, 0, SEEK_SET) != 0)
- {
- /*
- * NOTE: Formats that use the lookahead buffer can unset this in their
- * Init routine.
- */
- AH->readHeader = 1;
- }
- else
- AH->lookaheadLen = 0; /* Don't bother since we've reset the file */
-
- /* Close the file */
+ /* Close the file if we opened it */
if (wantClose)
+ {
if (fclose(fh) != 0)
fatal("could not close input file: %m");
+ /* Forget lookahead, since we'll re-read header after re-opening */
+ AH->readHeader = 0;
+ AH->lookaheadLen = 0;
+ }
return AH->format;
}
@@ -3773,7 +3714,9 @@ WriteHead(ArchiveHandle *AH)
void
ReadHead(ArchiveHandle *AH)
{
- char tmpMag[7];
+ char vmaj,
+ vmin,
+ vrev;
int fmt;
struct tm crtm;
@@ -3785,48 +3728,46 @@ ReadHead(ArchiveHandle *AH)
*/
if (!AH->readHeader)
{
- char vmaj,
- vmin,
- vrev;
+ char tmpMag[7];
AH->ReadBufPtr(AH, tmpMag, 5);
if (strncmp(tmpMag, "PGDMP", 5) != 0)
fatal("did not find magic string in file header");
+ }
- vmaj = AH->ReadBytePtr(AH);
- vmin = AH->ReadBytePtr(AH);
+ vmaj = AH->ReadBytePtr(AH);
+ vmin = AH->ReadBytePtr(AH);
- if (vmaj > 1 || (vmaj == 1 && vmin > 0)) /* Version > 1.0 */
- vrev = AH->ReadBytePtr(AH);
- else
- vrev = 0;
+ if (vmaj > 1 || (vmaj == 1 && vmin > 0)) /* Version > 1.0 */
+ vrev = AH->ReadBytePtr(AH);
+ else
+ vrev = 0;
- AH->version = MAKE_ARCHIVE_VERSION(vmaj, vmin, vrev);
+ AH->version = MAKE_ARCHIVE_VERSION(vmaj, vmin, vrev);
- if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX)
- fatal("unsupported version (%d.%d) in file header",
- vmaj, vmin);
+ if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX)
+ fatal("unsupported version (%d.%d) in file header",
+ vmaj, vmin);
- AH->intSize = AH->ReadBytePtr(AH);
- if (AH->intSize > 32)
- fatal("sanity check on integer size (%lu) failed",
- (unsigned long) AH->intSize);
+ AH->intSize = AH->ReadBytePtr(AH);
+ if (AH->intSize > 32)
+ fatal("sanity check on integer size (%lu) failed",
+ (unsigned long) AH->intSize);
- if (AH->intSize > sizeof(int))
- pg_log_warning("archive was made on a machine with larger integers, some operations might fail");
+ if (AH->intSize > sizeof(int))
+ pg_log_warning("archive was made on a machine with larger integers, some operations might fail");
- if (AH->version >= K_VERS_1_7)
- AH->offSize = AH->ReadBytePtr(AH);
- else
- AH->offSize = AH->intSize;
+ if (AH->version >= K_VERS_1_7)
+ AH->offSize = AH->ReadBytePtr(AH);
+ else
+ AH->offSize = AH->intSize;
- fmt = AH->ReadBytePtr(AH);
+ fmt = AH->ReadBytePtr(AH);
- if (AH->format != fmt)
- fatal("expected format (%d) differs from format found in file (%d)",
- AH->format, fmt);
- }
+ if (AH->format != fmt)
+ fatal("expected format (%d) differs from format found in file (%d)",
+ AH->format, fmt);
if (AH->version >= K_VERS_1_2)
{