1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-24 00:23:06 +03:00

Unify some tar functionality across different parts

Move some of the tar functionality that existed mostly duplicated
in both pg_dump and the walsender basebackup functionality into
port/tar.c instead, so it can be used from both. It will also be
used by pg_basebackup in the future, which would've caused a third
copy of it around.

Zoltan Boszormenyi and Magnus Hagander
This commit is contained in:
Magnus Hagander
2013-01-01 18:15:57 +01:00
parent a266f7dd93
commit f5d4bdd3a5
5 changed files with 154 additions and 228 deletions

View File

@@ -115,7 +115,6 @@ static char *tarGets(char *buf, size_t len, TAR_MEMBER *th);
static int tarPrintf(ArchiveHandle *AH, TAR_MEMBER *th, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
static void _tarAddFile(ArchiveHandle *AH, TAR_MEMBER *th);
static int _tarChecksum(char *th);
static TAR_MEMBER *_tarPositionTo(ArchiveHandle *AH, const char *filename);
static size_t tarRead(void *buf, size_t len, TAR_MEMBER *th);
static size_t tarWrite(const void *buf, size_t len, TAR_MEMBER *th);
@@ -1016,29 +1015,11 @@ tarPrintf(ArchiveHandle *AH, TAR_MEMBER *th, const char *fmt,...)
return cnt;
}
static int
_tarChecksum(char *header)
{
int i,
sum;
/*
* 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;
}
bool
isValidTarHeader(char *header)
{
int sum;
int chk = _tarChecksum(header);
int chk = tarChecksum(header);
sscanf(&header[148], "%8o", &sum);
@@ -1251,7 +1232,7 @@ _tarGetHeader(ArchiveHandle *AH, TAR_MEMBER *th)
(unsigned long) len);
/* Calc checksum */
chk = _tarChecksum(h);
chk = tarChecksum(h);
sscanf(&h[148], "%8o", &sum);
/*
@@ -1305,92 +1286,12 @@ _tarGetHeader(ArchiveHandle *AH, TAR_MEMBER *th)
}
/*
* Utility routine to print possibly larger than 32 bit integers in a
* portable fashion. Filled with zeros.
*/
static void
print_val(char *s, uint64 val, unsigned int base, size_t len)
{
int i;
for (i = len; i > 0; i--)
{
int digit = val % base;
s[i - 1] = '0' + digit;
val = val / base;
}
}
static void
_tarWriteHeader(TAR_MEMBER *th)
{
char h[512];
/*
* Note: most of the fields in a tar header are not supposed to be
* null-terminated. We use sprintf, which will write a null after the
* required bytes; that null goes into the first byte of the next field.
* This is okay as long as we fill the fields in order.
*/
memset(h, 0, sizeof(h));
/* Name 100 */
sprintf(&h[0], "%.99s", th->targetFile);
/* Mode 8 */
sprintf(&h[100], "0000600 ");
/* User ID 8 */
sprintf(&h[108], "0004000 ");
/* Group 8 */
sprintf(&h[116], "0002000 ");
/* File size 12 - 11 digits, 1 space; use print_val for 64 bit support */
print_val(&h[124], th->fileLen, 8, 11);
sprintf(&h[135], " ");
/* Mod Time 12 */
sprintf(&h[136], "%011o ", (int) time(NULL));
/* Checksum 8 cannot be calculated until we've filled all other fields */
/* Type - regular file */
sprintf(&h[156], "0");
/* Link Name 100 (leave as nulls) */
/* Magic 6 */
sprintf(&h[257], "ustar");
/* Version 2 */
sprintf(&h[263], "00");
/* User 32 */
/* XXX: Do we need to care about setting correct username? */
sprintf(&h[265], "%.31s", "postgres");
/* Group 32 */
/* XXX: Do we need to care about setting correct group name? */
sprintf(&h[297], "%.31s", "postgres");
/* Major Dev 8 */
sprintf(&h[329], "%07o ", 0);
/* Minor Dev 8 */
sprintf(&h[337], "%07o ", 0);
/* Prefix 155 - not used, leave as nulls */
/*
* We mustn't overwrite the next field while inserting the checksum.
* Fortunately, the checksum can't exceed 6 octal digits, so we just write
* 6 digits, a space, and a null, which is legal per POSIX.
*/
sprintf(&h[148], "%06o ", _tarChecksum(h));
tarCreateHeader(h, th->targetFile, NULL, th->fileLen, 0600, 04000, 02000, time(NULL));
/* Now write the completed header. */
if (fwrite(h, 1, 512, th->tarFH) != 512)