63 #define K_STD_BUF_SIZE 1024
135 AH->ReopenPtr = NULL;
146 AH->DeClonePtr = NULL;
148 AH->WorkerJobDumpPtr = NULL;
149 AH->WorkerJobRestorePtr = NULL;
155 AH->formatData = (
void *) ctx;
168 if (AH->fSpec && strcmp(AH->fSpec,
"") != 0)
171 if (ctx->
tarFH == NULL)
172 pg_fatal(
"could not open TOC file \"%s\" for output: %m",
178 if (ctx->
tarFH == NULL)
179 pg_fatal(
"could not open TOC file for output: %m");
197 if (AH->compression != 0)
198 pg_fatal(
"compression is not supported by tar archive format");
202 if (AH->fSpec && strcmp(AH->fSpec,
"") != 0)
205 if (ctx->
tarFH == NULL)
206 pg_fatal(
"could not open TOC file \"%s\" for input: %m",
212 if (ctx->
tarFH == NULL)
213 pg_fatal(
"could not open TOC file for input: %m");
226 ctx->
FH = (
void *)
tarOpen(AH,
"toc.dat",
'r');
334 pg_fatal(
"compression is not supported by tar archive format");
351 tm->tmpFH = tmpfile();
364 name = _tempnam(NULL,
"pg_temp_");
367 fd = open(
name, O_RDWR | O_CREAT | O_EXCL | O_BINARY |
373 tm->tmpFH = fdopen(
fd,
"w+b");
376 else if (errno != EEXIST)
381 if (
tm->tmpFH == NULL)
382 pg_fatal(
"could not generate temporary file name: %m");
389 pg_fatal(
"compression is not supported by tar archive format");
405 pg_fatal(
"compression is not supported by tar archive format");
434 while (cnt <
len &&
c !=
'\n')
498 res = fread(&((
char *)
buf)[used], 1,
len, fh);
499 if (
res !=
len && !feof(fh))
621 pos1 = (int) strlen(te->
copyStmt) - 13;
622 if (pos1 < 6 || strncmp(te->
copyStmt,
"COPY ", 5) != 0 ||
623 strcmp(te->
copyStmt + pos1,
" FROM stdin;\n") != 0)
624 pg_fatal(
"unexpected COPY statement syntax: \"%s\"",
630 ahprintf(AH,
" FROM '$$PATH$$/%s';\n\n", tctx->filename);
635 ahprintf(AH,
"\\i $$PATH$$/%s\n\n", tctx->filename);
641 if (strcmp(te->
desc,
"BLOBS") == 0)
654 bool foundBlob =
false;
669 pg_log_info(
"restoring large object with OID %u", oid);
726 pg_fatal(
"could not read from input file: end of file");
749 pg_fatal(
"could not read from input file: end of file");
770 th =
tarOpen(AH,
"toc.dat",
'w');
785 th =
tarOpen(AH,
"restore.sql",
'w');
790 "-- File paths need to be edited. Search for $$PATH$$ and\n"
791 "-- replace it with the path to the directory containing\n"
792 "-- the extracted data files.\n"
802 ropt->filename = NULL;
803 ropt->dropSchema = 1;
804 ropt->compression = 0;
805 ropt->superuser = NULL;
806 ropt->suppressDumpWarnings =
true;
813 savVerbose = AH->public.
verbose;
820 AH->public.
verbose = savVerbose;
831 if (fputc(0, ctx->
tarFH) == EOF)
836 if (AH->dosync && AH->fSpec)
890 pg_fatal(
"invalid OID for large object (%u)", oid);
893 pg_fatal(
"compression is not supported by tar archive format");
895 sprintf(fname,
"blob_%u.dat", oid);
899 tctx->TH =
tarOpen(AH, fname,
'w');
943 int save_errno = errno;
986 if (memcmp(&
header[257],
"ustar\0", 6) == 0 &&
987 memcmp(&
header[263],
"00", 2) == 0)
990 if (memcmp(&
header[257],
"ustar \0", 8) == 0)
993 if (memcmp(&
header[257],
"ustar00\0", 8) == 0)
1004 FILE *tmp = th->
tmpFH;
1016 pg_fatal(
"error during file seek: %m");
1019 pg_fatal(
"could not determine seek position in archive file: %m");
1020 if (
fseeko(tmp, 0, SEEK_SET) != 0)
1021 pg_fatal(
"error during file seek: %m");
1025 while ((cnt = fread(
buf, 1,
sizeof(
buf), tmp)) > 0)
1027 if ((
res = fwrite(
buf, 1, cnt, th->
tarFH)) != cnt)
1034 if (fclose(tmp) != 0)
1035 pg_fatal(
"could not close temporary file: %m");
1038 pg_fatal(
"actual file length (%lld) does not match expected (%lld)",
1042 for (
i = 0;
i < pad;
i++)
1044 if (fputc(
'\0', th->
tarFH) == EOF)
1069 pg_log_debug(
"moving from position %lld to next member at file position %lld",
1084 pg_fatal(
"could not find header for file \"%s\" in tar archive",
filename);
1102 pg_fatal(
"restoring data out of order is not supported in this archive format: "
1103 "\"%s\" is required, but comes before \"%s\" in the archive file.",
1111 for (
i = 0;
i < blks;
i++)
1115 pg_fatal(
"could not find header for file \"%s\" in tar archive",
filename);
1136 bool gotBlock =
false;
1150 "incomplete tar header found (%lu bytes)",
1152 (
unsigned long)
len);
1184 pg_log_debug(
"TOC Entry %s at %llu (length %llu, checksum %d)",
1185 tag, (
unsigned long long) hPos, (
unsigned long long)
len, sum);
1188 pg_fatal(
"corrupt tar header found in %s (expected %d, computed %d) file position %llu",
1189 tag, sum, chk, (
unsigned long long)
ftello(ctx->
tarFH));
1204 0600, 04000, 02000, time(NULL));
#define ngettext(s, p, n)
#define pg_attribute_printf(f, a)
static void PGresult * res
void fsync_fname(const char *fname, bool isdir)
void * pg_malloc0(size_t size)
char * pg_strdup(const char *in)
void * pg_malloc(size_t size)
Assert(fmt[strlen(fmt) - 1] !='\n')
static void const char * fmt
#define pg_log_debug(...)
void RestoreArchive(Archive *AH)
RestoreOptions * NewRestoreOptions(void)
void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
int TocIDRequired(ArchiveHandle *AH, DumpId id)
void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
void WriteHead(ArchiveHandle *AH)
void StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop)
void WriteDataChunks(ArchiveHandle *AH, ParallelState *pstate)
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
char * ReadStr(ArchiveHandle *AH)
void StartRestoreBlobs(ArchiveHandle *AH)
void ReadHead(ArchiveHandle *AH)
void ReadToc(ArchiveHandle *AH)
void WriteToc(ArchiveHandle *AH)
void EndRestoreBlob(ArchiveHandle *AH, Oid oid)
void EndRestoreBlobs(ArchiveHandle *AH)
size_t WriteStr(ArchiveHandle *AH, const char *c)
#define READ_ERROR_EXIT(fd)
static void _LoadBlobs(ArchiveHandle *AH)
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te)
static void _StartBlobs(ArchiveHandle *AH, TocEntry *te)
static void _StartData(ArchiveHandle *AH, TocEntry *te)
static size_t tarRead(void *buf, size_t len, TAR_MEMBER *th)
static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
static void _CloseArchive(ArchiveHandle *AH)
static int tarPrintf(TAR_MEMBER *th, const char *fmt,...) pg_attribute_printf(2
static void _tarWriteHeader(TAR_MEMBER *th)
void InitArchiveFmt_Tar(ArchiveHandle *AH)
static size_t _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh)
static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te)
static int _WriteByte(ArchiveHandle *AH, const int i)
static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
bool isValidTarHeader(char *header)
static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
static TAR_MEMBER * tarOpen(ArchiveHandle *AH, const char *filename, char mode)
static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
static void _EndData(ArchiveHandle *AH, TocEntry *te)
static void _EndBlobs(ArchiveHandle *AH, TocEntry *te)
static size_t tarWrite(const void *buf, size_t len, TAR_MEMBER *th)
static int _tarGetHeader(ArchiveHandle *AH, TAR_MEMBER *th)
static size_t _scriptOut(ArchiveHandle *AH, const void *buf, size_t len)
static int _ReadByte(ArchiveHandle *)
static TAR_MEMBER * _tarPositionTo(ArchiveHandle *AH, const char *filename)
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
static void _PrintFileData(ArchiveHandle *AH, char *filename)
static int static void _tarAddFile(ArchiveHandle *AH, TAR_MEMBER *th)
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
static PgChecksumMode mode
static void header(const char *fmt,...) pg_attribute_printf(1
uint64 read_tar_number(const char *s, int len)
static size_t tarPaddingBytesRequired(size_t len)
int tarChecksum(char *header)
enum tarError tarCreateHeader(char *h, const char *filename, const char *linktarget, pgoff_t size, mode_t mode, uid_t uid, gid_t gid, time_t mtime)
size_t strlcpy(char *dst, const char *src, size_t siz)
static int fd(const char *x, int i)
size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args)
CustomOutPtrType CustomOutPtr
struct _tocEntry * currToc
static void * fn(void *arg)
#define fseeko(stream, offset, origin)