aboutsummaryrefslogtreecommitdiff
path: root/src/bin/pg_dump/pg_backup_tar.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2012-09-28 15:35:51 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2012-09-28 15:35:51 -0400
commitdfa6eda5e44f1525aa6b142b8e252f5a79110ee6 (patch)
treea2409a647f0b49008e3d772ce4f000c076875cb2 /src/bin/pg_dump/pg_backup_tar.c
parentbc99397563ff88c31078d4bf66b8538c78a08bda (diff)
downloadpostgresql-dfa6eda5e44f1525aa6b142b8e252f5a79110ee6.tar.gz
postgresql-dfa6eda5e44f1525aa6b142b8e252f5a79110ee6.zip
Fix tar files emitted by pg_basebackup to be POSIX conformant.
Back-patch portions of commit 05b555d12bc2ad0d581f48a12b45174db41dc10d. There doesn't seem to be any reason not to fix pg_basebackup fully, but we can't change pg_dump's "magic" string without breaking older versions of pg_restore. Instead, just patch pg_restore to accept either version of the magic string, in hopes of avoiding compatibility problems when 9.3 comes out. I also fixed pg_dump to write the correct 2-block EOF marker, since that won't create a compatibility problem with pg_restore and it could help with some versions of tar. Brian Weaver and Tom Lane
Diffstat (limited to 'src/bin/pg_dump/pg_backup_tar.c')
-rw-r--r--src/bin/pg_dump/pg_backup_tar.c27
1 files changed, 19 insertions, 8 deletions
diff --git a/src/bin/pg_dump/pg_backup_tar.c b/src/bin/pg_dump/pg_backup_tar.c
index 7459d50f733..a8f56d10319 100644
--- a/src/bin/pg_dump/pg_backup_tar.c
+++ b/src/bin/pg_dump/pg_backup_tar.c
@@ -876,8 +876,10 @@ _CloseArchive(ArchiveHandle *AH)
tarClose(AH, th);
- /* Add a block of NULLs since it's de-rigeur. */
- for (i = 0; i < 512; i++)
+ /*
+ * EOF marker for tar files is two blocks of NULLs.
+ */
+ for (i = 0; i < 512 * 2; i++)
{
if (fputc(0, ctx->tarFH) == EOF)
die_horribly(AH, modulename,
@@ -1028,11 +1030,16 @@ _tarChecksum(char *header)
int i,
sum;
- sum = 0;
+ /*
+ * Per POSIX, the checksum is the simple sum of all bytes in the header,
+ * treating the bytes as unsigned, and treating the checksum field (at
+ * offset 148) as though it contained 8 spaces.
+ */
+ sum = 8 * ' '; /* presumed value for checksum field */
for (i = 0; i < 512; i++)
if (i < 148 || i >= 156)
sum += 0xFF & header[i];
- return sum + 256; /* Assume 8 blanks in checksum field */
+ return sum;
}
bool
@@ -1046,11 +1053,15 @@ isValidTarHeader(char *header)
if (sum != chk)
return false;
- /* POSIX format */
- if (strncmp(&header[257], "ustar00", 7) == 0)
+ /* POSIX tar format */
+ if (memcmp(&header[257], "ustar\0", 6) == 0 &&
+ memcmp(&header[263], "00", 2) == 0)
+ return true;
+ /* GNU tar format */
+ if (memcmp(&header[257], "ustar \0", 8) == 0)
return true;
- /* older format */
- if (strncmp(&header[257], "ustar ", 7) == 0)
+ /* not-quite-POSIX format written by pre-9.3 pg_dump */
+ if (memcmp(&header[257], "ustar00\0", 8) == 0)
return true;
return false;