649
649
lclContext *ctx = (lclContext *) AH->formatData;
650
650
lclTocEntry *tctx = (lclTocEntry *) te->formatData;
656
653
if (!tctx->filename)
657
* If we're writing the special restore.sql script, emit a suitable
658
* command to include each table's data from the corresponding file.
660
* In the COPY case this is a bit klugy because the regular COPY command
661
* was already printed before we get control.
659
663
if (ctx->isSpecialScript)
664
/* Abort the default COPY */
665
ahprintf(AH, "\\.\n");
667
/* Get a copy of the COPY statement and clean it up */
668
tmpCopy = strdup(te->copyStmt);
669
for (i = 0; i < strlen(tmpCopy); i++)
670
tmpCopy[i] = pg_tolower((unsigned char) tmpCopy[i]);
673
* This is very nasty; we don't know if the archive used WITH OIDS, so
674
* we search the string for it in a paranoid sort of way.
676
if (strncmp(tmpCopy, "copy ", 5) != 0)
677
die_horribly(AH, modulename,
678
"invalid COPY statement -- could not find \"copy\" in string \"%s\"\n", tmpCopy);
681
for (pos1 = 5; pos1 < strlen(tmpCopy); pos1++)
682
if (tmpCopy[pos1] != ' ')
685
if (tmpCopy[pos1] == '"')
688
pos1 += strlen(te->tag);
690
for (pos2 = pos1; pos2 < strlen(tmpCopy); pos2++)
691
if (strncmp(&tmpCopy[pos2], "from stdin", 10) == 0)
694
if (pos2 >= strlen(tmpCopy))
695
die_horribly(AH, modulename,
696
"invalid COPY statement -- could not find \"from stdin\" in string \"%s\" starting at position %lu\n",
697
tmpCopy, (unsigned long) pos1);
699
ahwrite(tmpCopy, 1, pos2, AH); /* 'copy "table" [with oids]' */
700
ahprintf(AH, " from '$$PATH$$/%s' %s", tctx->filename, &tmpCopy[pos2 + 10]);
667
/* Abort the COPY FROM stdin */
668
ahprintf(AH, "\\.\n");
671
* The COPY statement should look like "COPY ... FROM stdin;\n",
672
* see dumpTableData().
674
pos1 = (int) strlen(te->copyStmt) - 13;
675
if (pos1 < 6 || strncmp(te->copyStmt, "COPY ", 5) != 0 ||
676
strcmp(te->copyStmt + pos1, " FROM stdin;\n") != 0)
677
die_horribly(AH, modulename,
678
"unexpected COPY statement syntax: \"%s\"\n",
681
/* Emit all but the FROM part ... */
682
ahwrite(te->copyStmt, 1, pos1, AH);
683
/* ... and insert modified FROM */
684
ahprintf(AH, " FROM '$$PATH$$/%s';\n\n", tctx->filename);
688
/* --inserts mode, no worries, just include the data file */
689
ahprintf(AH, "\\i $$PATH$$/%s\n\n", tctx->filename);
843
833
* if the files have been extracted.
845
835
th = tarOpen(AH, "restore.sql", 'w');
846
tarPrintf(AH, th, "create temporary table pgdump_restore_path(p text);\n");
847
837
tarPrintf(AH, th, "--\n"
850
840
"-- File paths need to be edited. Search for $$PATH$$ and\n"
851
841
"-- replace it with the path to the directory containing\n"
852
842
"-- the extracted data files.\n"
854
"-- Edit the following to match the path where the\n"
855
"-- tar archive has been extracted.\n"
857
tarPrintf(AH, th, "insert into pgdump_restore_path values('/tmp');\n\n");
859
845
AH->CustomOutPtr = _scriptOut;
877
863
tarClose(AH, th);
879
/* Add a block of NULLs since it's de-rigeur. */
880
for (i = 0; i < 512; i++)
865
ctx->isSpecialScript = 0;
868
* EOF marker for tar files is two blocks of NULLs.
870
for (i = 0; i < 512 * 2; i++)
882
872
if (fputc(0, ctx->tarFH) == EOF)
883
873
die_horribly(AH, modulename,
1046
1041
if (sum != chk)
1050
if (strncmp(&header[257], "ustar00", 7) == 0)
1053
if (strncmp(&header[257], "ustar ", 7) == 0)
1044
/* POSIX tar format */
1045
if (memcmp(&header[257], "ustar\0", 6) == 0 &&
1046
memcmp(&header[263], "00", 2) == 0)
1048
/* GNU tar format */
1049
if (memcmp(&header[257], "ustar \0", 8) == 0)
1051
/* not-quite-POSIX format written by pre-9.3 pg_dump */
1052
if (memcmp(&header[257], "ustar00\0", 8) == 0)