PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_backup_tar.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_backup_tar.c
4  *
5  * This file is copied from the 'files' format file, but dumps data into
6  * one temp file then sends it to the output TAR archive.
7  *
8  * The tar format also includes a 'restore.sql' script which is there for
9  * the benefit of humans. This script is never used by pg_restore.
10  *
11  * NOTE: If you untar the created 'tar' file, the resulting files are
12  * compatible with the 'directory' format. Please keep the two formats in
13  * sync.
14  *
15  * See the headers to pg_backup_directory & pg_restore for more details.
16  *
17  * Copyright (c) 2000, Philip Warner
18  * Rights are granted to use this software in any way so long
19  * as this notice is not removed.
20  *
21  * The author is not responsible for loss or damages that may
22  * result from it's use.
23  *
24  *
25  * IDENTIFICATION
26  * src/bin/pg_dump/pg_backup_tar.c
27  *
28  *-------------------------------------------------------------------------
29  */
30 #include "postgres_fe.h"
31 
32 #include "pg_backup_archiver.h"
33 #include "pg_backup_tar.h"
34 #include "pg_backup_utils.h"
35 #include "pgtar.h"
36 #include "common/file_utils.h"
37 #include "fe_utils/string_utils.h"
38 
39 #include <sys/stat.h>
40 #include <ctype.h>
41 #include <limits.h>
42 #include <unistd.h>
43 
44 static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te);
45 static void _StartData(ArchiveHandle *AH, TocEntry *te);
46 static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);
47 static void _EndData(ArchiveHandle *AH, TocEntry *te);
48 static int _WriteByte(ArchiveHandle *AH, const int i);
49 static int _ReadByte(ArchiveHandle *);
50 static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
51 static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len);
52 static void _CloseArchive(ArchiveHandle *AH);
53 static void _PrintTocData(ArchiveHandle *AH, TocEntry *te);
54 static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te);
55 static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te);
56 static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te);
57 
58 static void _StartBlobs(ArchiveHandle *AH, TocEntry *te);
59 static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);
60 static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);
61 static void _EndBlobs(ArchiveHandle *AH, TocEntry *te);
62 
63 #define K_STD_BUF_SIZE 1024
64 
65 
66 typedef struct
67 {
68 #ifdef HAVE_LIBZ
69  gzFile zFH;
70 #else
71  FILE *zFH;
72 #endif
73  FILE *nFH;
74  FILE *tarFH;
75  FILE *tmpFH;
76  char *targetFile;
77  char mode;
81 } TAR_MEMBER;
82 
83 typedef struct
84 {
85  int hasSeek;
86  pgoff_t filePos;
88  FILE *tarFH;
94 } lclContext;
95 
96 typedef struct
97 {
99  char *filename;
100 } lclTocEntry;
101 
102 /* translator: this is a module name */
103 static const char *modulename = gettext_noop("tar archiver");
104 
105 static void _LoadBlobs(ArchiveHandle *AH);
106 
107 static TAR_MEMBER *tarOpen(ArchiveHandle *AH, const char *filename, char mode);
108 static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH);
109 
110 #ifdef __NOT_USED__
111 static char *tarGets(char *buf, size_t len, TAR_MEMBER *th);
112 #endif
113 static int tarPrintf(ArchiveHandle *AH, TAR_MEMBER *th, const char *fmt,...) pg_attribute_printf(3, 4);
114 
115 static void _tarAddFile(ArchiveHandle *AH, TAR_MEMBER *th);
116 static TAR_MEMBER *_tarPositionTo(ArchiveHandle *AH, const char *filename);
117 static size_t tarRead(void *buf, size_t len, TAR_MEMBER *th);
118 static size_t tarWrite(const void *buf, size_t len, TAR_MEMBER *th);
119 static void _tarWriteHeader(TAR_MEMBER *th);
120 static int _tarGetHeader(ArchiveHandle *AH, TAR_MEMBER *th);
121 static size_t _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh);
122 
123 static size_t _scriptOut(ArchiveHandle *AH, const void *buf, size_t len);
124 
125 /*
126  * Initializer
127  */
128 void
130 {
131  lclContext *ctx;
132 
133  /* Assuming static functions, this can be copied for each format. */
134  AH->ArchiveEntryPtr = _ArchiveEntry;
135  AH->StartDataPtr = _StartData;
136  AH->WriteDataPtr = _WriteData;
137  AH->EndDataPtr = _EndData;
138  AH->WriteBytePtr = _WriteByte;
139  AH->ReadBytePtr = _ReadByte;
140  AH->WriteBufPtr = _WriteBuf;
141  AH->ReadBufPtr = _ReadBuf;
142  AH->ClosePtr = _CloseArchive;
143  AH->ReopenPtr = NULL;
144  AH->PrintTocDataPtr = _PrintTocData;
145  AH->ReadExtraTocPtr = _ReadExtraToc;
146  AH->WriteExtraTocPtr = _WriteExtraToc;
147  AH->PrintExtraTocPtr = _PrintExtraToc;
148 
149  AH->StartBlobsPtr = _StartBlobs;
150  AH->StartBlobPtr = _StartBlob;
151  AH->EndBlobPtr = _EndBlob;
152  AH->EndBlobsPtr = _EndBlobs;
153  AH->ClonePtr = NULL;
154  AH->DeClonePtr = NULL;
155 
156  AH->WorkerJobDumpPtr = NULL;
157  AH->WorkerJobRestorePtr = NULL;
158 
159  /*
160  * Set up some special context used in compressing data.
161  */
162  ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
163  AH->formatData = (void *) ctx;
164  ctx->filePos = 0;
165  ctx->isSpecialScript = 0;
166 
167  /* Initialize LO buffering */
168  AH->lo_buf_size = LOBBUFSIZE;
169  AH->lo_buf = (void *) pg_malloc(LOBBUFSIZE);
170 
171  /*
172  * Now open the tar file, and load the TOC if we're in read mode.
173  */
174  if (AH->mode == archModeWrite)
175  {
176  if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
177  {
178  ctx->tarFH = fopen(AH->fSpec, PG_BINARY_W);
179  if (ctx->tarFH == NULL)
181  "could not open TOC file \"%s\" for output: %s\n",
182  AH->fSpec, strerror(errno));
183  }
184  else
185  {
186  ctx->tarFH = stdout;
187  if (ctx->tarFH == NULL)
189  "could not open TOC file for output: %s\n",
190  strerror(errno));
191  }
192 
193  ctx->tarFHpos = 0;
194 
195  /*
196  * Make unbuffered since we will dup() it, and the buffers screw each
197  * other
198  */
199  /* setvbuf(ctx->tarFH, NULL, _IONBF, 0); */
200 
201  ctx->hasSeek = checkSeek(ctx->tarFH);
202 
203  /*
204  * We don't support compression because reading the files back is not
205  * possible since gzdopen uses buffered IO which totally screws file
206  * positioning.
207  */
208  if (AH->compression != 0)
210  "compression is not supported by tar archive format\n");
211  }
212  else
213  { /* Read Mode */
214  if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
215  {
216  ctx->tarFH = fopen(AH->fSpec, PG_BINARY_R);
217  if (ctx->tarFH == NULL)
218  exit_horribly(modulename, "could not open TOC file \"%s\" for input: %s\n",
219  AH->fSpec, strerror(errno));
220  }
221  else
222  {
223  ctx->tarFH = stdin;
224  if (ctx->tarFH == NULL)
225  exit_horribly(modulename, "could not open TOC file for input: %s\n",
226  strerror(errno));
227  }
228 
229  /*
230  * Make unbuffered since we will dup() it, and the buffers screw each
231  * other
232  */
233  /* setvbuf(ctx->tarFH, NULL, _IONBF, 0); */
234 
235  ctx->tarFHpos = 0;
236 
237  ctx->hasSeek = checkSeek(ctx->tarFH);
238 
239  /*
240  * Forcibly unmark the header as read since we use the lookahead
241  * buffer
242  */
243  AH->readHeader = 0;
244 
245  ctx->FH = (void *) tarOpen(AH, "toc.dat", 'r');
246  ReadHead(AH);
247  ReadToc(AH);
248  tarClose(AH, ctx->FH); /* Nothing else in the file... */
249  }
250 }
251 
252 /*
253  * - Start a new TOC entry
254  * Setup the output file name.
255  */
256 static void
258 {
259  lclTocEntry *ctx;
260  char fn[K_STD_BUF_SIZE];
261 
262  ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
263  if (te->dataDumper != NULL)
264  {
265 #ifdef HAVE_LIBZ
266  if (AH->compression == 0)
267  sprintf(fn, "%d.dat", te->dumpId);
268  else
269  sprintf(fn, "%d.dat.gz", te->dumpId);
270 #else
271  sprintf(fn, "%d.dat", te->dumpId);
272 #endif
273  ctx->filename = pg_strdup(fn);
274  }
275  else
276  {
277  ctx->filename = NULL;
278  ctx->TH = NULL;
279  }
280  te->formatData = (void *) ctx;
281 }
282 
283 static void
285 {
286  lclTocEntry *ctx = (lclTocEntry *) te->formatData;
287 
288  if (ctx->filename)
289  WriteStr(AH, ctx->filename);
290  else
291  WriteStr(AH, "");
292 }
293 
294 static void
296 {
297  lclTocEntry *ctx = (lclTocEntry *) te->formatData;
298 
299  if (ctx == NULL)
300  {
301  ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
302  te->formatData = (void *) ctx;
303  }
304 
305  ctx->filename = ReadStr(AH);
306  if (strlen(ctx->filename) == 0)
307  {
308  free(ctx->filename);
309  ctx->filename = NULL;
310  }
311  ctx->TH = NULL;
312 }
313 
314 static void
316 {
317  lclTocEntry *ctx = (lclTocEntry *) te->formatData;
318 
319  if (AH->public.verbose && ctx->filename != NULL)
320  ahprintf(AH, "-- File: %s\n", ctx->filename);
321 }
322 
323 static void
325 {
326  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
327 
328  tctx->TH = tarOpen(AH, tctx->filename, 'w');
329 }
330 
331 static TAR_MEMBER *
332 tarOpen(ArchiveHandle *AH, const char *filename, char mode)
333 {
334  lclContext *ctx = (lclContext *) AH->formatData;
335  TAR_MEMBER *tm;
336 
337 #ifdef HAVE_LIBZ
338  char fmode[10];
339 #endif
340 
341  if (mode == 'r')
342  {
343  tm = _tarPositionTo(AH, filename);
344  if (!tm) /* Not found */
345  {
346  if (filename)
347  {
348  /*
349  * Couldn't find the requested file. Future: do SEEK(0) and
350  * retry.
351  */
352  exit_horribly(modulename, "could not find file \"%s\" in archive\n", filename);
353  }
354  else
355  {
356  /* Any file OK, none left, so return NULL */
357  return NULL;
358  }
359  }
360 
361 #ifdef HAVE_LIBZ
362 
363  if (AH->compression == 0)
364  tm->nFH = ctx->tarFH;
365  else
366  exit_horribly(modulename, "compression is not supported by tar archive format\n");
367  /* tm->zFH = gzdopen(dup(fileno(ctx->tarFH)), "rb"); */
368 #else
369  tm->nFH = ctx->tarFH;
370 #endif
371  }
372  else
373  {
374  int old_umask;
375 
376  tm = pg_malloc0(sizeof(TAR_MEMBER));
377 
378  /*
379  * POSIX does not require, but permits, tmpfile() to restrict file
380  * permissions. Given an OS crash after we write data, the filesystem
381  * might retain the data but forget tmpfile()'s unlink(). If so, the
382  * file mode protects confidentiality of the data written.
383  */
384  old_umask = umask(S_IRWXG | S_IRWXO);
385 
386 #ifndef WIN32
387  tm->tmpFH = tmpfile();
388 #else
389 
390  /*
391  * On WIN32, tmpfile() generates a filename in the root directory,
392  * which requires administrative permissions on certain systems. Loop
393  * until we find a unique file name we can create.
394  */
395  while (1)
396  {
397  char *name;
398  int fd;
399 
400  name = _tempnam(NULL, "pg_temp_");
401  if (name == NULL)
402  break;
403  fd = open(name, O_RDWR | O_CREAT | O_EXCL | O_BINARY |
404  O_TEMPORARY, S_IRUSR | S_IWUSR);
405  free(name);
406 
407  if (fd != -1) /* created a file */
408  {
409  tm->tmpFH = fdopen(fd, "w+b");
410  break;
411  }
412  else if (errno != EEXIST) /* failure other than file exists */
413  break;
414  }
415 #endif
416 
417  if (tm->tmpFH == NULL)
418  exit_horribly(modulename, "could not generate temporary file name: %s\n", strerror(errno));
419 
420  umask(old_umask);
421 
422 #ifdef HAVE_LIBZ
423 
424  if (AH->compression != 0)
425  {
426  sprintf(fmode, "wb%d", AH->compression);
427  tm->zFH = gzdopen(dup(fileno(tm->tmpFH)), fmode);
428  if (tm->zFH == NULL)
429  exit_horribly(modulename, "could not open temporary file\n");
430  }
431  else
432  tm->nFH = tm->tmpFH;
433 #else
434 
435  tm->nFH = tm->tmpFH;
436 #endif
437 
438  tm->AH = AH;
439  tm->targetFile = pg_strdup(filename);
440  }
441 
442  tm->mode = mode;
443  tm->tarFH = ctx->tarFH;
444 
445  return tm;
446 }
447 
448 static void
450 {
451  /*
452  * Close the GZ file since we dup'd. This will flush the buffers.
453  */
454  if (AH->compression != 0)
455  if (GZCLOSE(th->zFH) != 0)
456  exit_horribly(modulename, "could not close tar member\n");
457 
458  if (th->mode == 'w')
459  _tarAddFile(AH, th); /* This will close the temp file */
460 
461  /*
462  * else Nothing to do for normal read since we don't dup() normal file
463  * handle, and we don't use temp files.
464  */
465 
466  if (th->targetFile)
467  free(th->targetFile);
468 
469  th->nFH = NULL;
470  th->zFH = NULL;
471 }
472 
473 #ifdef __NOT_USED__
474 static char *
475 tarGets(char *buf, size_t len, TAR_MEMBER *th)
476 {
477  char *s;
478  size_t cnt = 0;
479  char c = ' ';
480  int eof = 0;
481 
482  /* Can't read past logical EOF */
483  if (len > (th->fileLen - th->pos))
484  len = th->fileLen - th->pos;
485 
486  while (cnt < len && c != '\n')
487  {
488  if (_tarReadRaw(th->AH, &c, 1, th, NULL) <= 0)
489  {
490  eof = 1;
491  break;
492  }
493  buf[cnt++] = c;
494  }
495 
496  if (eof && cnt == 0)
497  s = NULL;
498  else
499  {
500  buf[cnt++] = '\0';
501  s = buf;
502  }
503 
504  if (s)
505  {
506  len = strlen(s);
507  th->pos += len;
508  }
509 
510  return s;
511 }
512 #endif
513 
514 /*
515  * Just read bytes from the archive. This is the low level read routine
516  * that is used for ALL reads on a tar file.
517  */
518 static size_t
519 _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh)
520 {
521  lclContext *ctx = (lclContext *) AH->formatData;
522  size_t avail;
523  size_t used = 0;
524  size_t res = 0;
525 
526  avail = AH->lookaheadLen - AH->lookaheadPos;
527  if (avail > 0)
528  {
529  /* We have some lookahead bytes to use */
530  if (avail >= len) /* Just use the lookahead buffer */
531  used = len;
532  else
533  used = avail;
534 
535  /* Copy, and adjust buffer pos */
536  memcpy(buf, AH->lookahead + AH->lookaheadPos, used);
537  AH->lookaheadPos += used;
538 
539  /* Adjust required length */
540  len -= used;
541  }
542 
543  /* Read the file if len > 0 */
544  if (len > 0)
545  {
546  if (fh)
547  {
548  res = fread(&((char *) buf)[used], 1, len, fh);
549  if (res != len && !feof(fh))
550  READ_ERROR_EXIT(fh);
551  }
552  else if (th)
553  {
554  if (th->zFH)
555  {
556  res = GZREAD(&((char *) buf)[used], 1, len, th->zFH);
557  if (res != len && !GZEOF(th->zFH))
558  {
559 #ifdef HAVE_LIBZ
560  int errnum;
561  const char *errmsg = gzerror(th->zFH, &errnum);
562 
564  "could not read from input file: %s\n",
565  errnum == Z_ERRNO ? strerror(errno) : errmsg);
566 #else
568  "could not read from input file: %s\n",
569  strerror(errno));
570 #endif
571  }
572  }
573  else
574  {
575  res = fread(&((char *) buf)[used], 1, len, th->nFH);
576  if (res != len && !feof(th->nFH))
577  READ_ERROR_EXIT(th->nFH);
578  }
579  }
580  else
581  exit_horribly(modulename, "internal error -- neither th nor fh specified in tarReadRaw()\n");
582  }
583 
584  ctx->tarFHpos += res + used;
585 
586  return (res + used);
587 }
588 
589 static size_t
590 tarRead(void *buf, size_t len, TAR_MEMBER *th)
591 {
592  size_t res;
593 
594  if (th->pos + len > th->fileLen)
595  len = th->fileLen - th->pos;
596 
597  if (len <= 0)
598  return 0;
599 
600  res = _tarReadRaw(th->AH, buf, len, th, NULL);
601 
602  th->pos += res;
603 
604  return res;
605 }
606 
607 static size_t
608 tarWrite(const void *buf, size_t len, TAR_MEMBER *th)
609 {
610  size_t res;
611 
612  if (th->zFH != NULL)
613  res = GZWRITE(buf, 1, len, th->zFH);
614  else
615  res = fwrite(buf, 1, len, th->nFH);
616 
617  th->pos += res;
618  return res;
619 }
620 
621 static void
622 _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
623 {
624  lclTocEntry *tctx = (lclTocEntry *) AH->currToc->formatData;
625 
626  if (tarWrite(data, dLen, tctx->TH) != dLen)
628 
629  return;
630 }
631 
632 static void
634 {
635  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
636 
637  /* Close the file */
638  tarClose(AH, tctx->TH);
639  tctx->TH = NULL;
640 }
641 
642 /*
643  * Print data for a given file
644  */
645 static void
647 {
648  lclContext *ctx = (lclContext *) AH->formatData;
649  char buf[4096];
650  size_t cnt;
651  TAR_MEMBER *th;
652 
653  if (!filename)
654  return;
655 
656  th = tarOpen(AH, filename, 'r');
657  ctx->FH = th;
658 
659  while ((cnt = tarRead(buf, 4095, th)) > 0)
660  {
661  buf[cnt] = '\0';
662  ahwrite(buf, 1, cnt, AH);
663  }
664 
665  tarClose(AH, th);
666 }
667 
668 
669 /*
670  * Print data for a given TOC entry
671 */
672 static void
674 {
675  lclContext *ctx = (lclContext *) AH->formatData;
676  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
677  int pos1;
678 
679  if (!tctx->filename)
680  return;
681 
682  /*
683  * If we're writing the special restore.sql script, emit a suitable
684  * command to include each table's data from the corresponding file.
685  *
686  * In the COPY case this is a bit klugy because the regular COPY command
687  * was already printed before we get control.
688  */
689  if (ctx->isSpecialScript)
690  {
691  if (te->copyStmt)
692  {
693  /* Abort the COPY FROM stdin */
694  ahprintf(AH, "\\.\n");
695 
696  /*
697  * The COPY statement should look like "COPY ... FROM stdin;\n",
698  * see dumpTableData().
699  */
700  pos1 = (int) strlen(te->copyStmt) - 13;
701  if (pos1 < 6 || strncmp(te->copyStmt, "COPY ", 5) != 0 ||
702  strcmp(te->copyStmt + pos1, " FROM stdin;\n") != 0)
704  "unexpected COPY statement syntax: \"%s\"\n",
705  te->copyStmt);
706 
707  /* Emit all but the FROM part ... */
708  ahwrite(te->copyStmt, 1, pos1, AH);
709  /* ... and insert modified FROM */
710  ahprintf(AH, " FROM '$$PATH$$/%s';\n\n", tctx->filename);
711  }
712  else
713  {
714  /* --inserts mode, no worries, just include the data file */
715  ahprintf(AH, "\\i $$PATH$$/%s\n\n", tctx->filename);
716  }
717 
718  return;
719  }
720 
721  if (strcmp(te->desc, "BLOBS") == 0)
722  _LoadBlobs(AH);
723  else
724  _PrintFileData(AH, tctx->filename);
725 }
726 
727 static void
729 {
730  Oid oid;
731  lclContext *ctx = (lclContext *) AH->formatData;
732  TAR_MEMBER *th;
733  size_t cnt;
734  bool foundBlob = false;
735  char buf[4096];
736 
737  StartRestoreBlobs(AH);
738 
739  th = tarOpen(AH, NULL, 'r'); /* Open next file */
740  while (th != NULL)
741  {
742  ctx->FH = th;
743 
744  if (strncmp(th->targetFile, "blob_", 5) == 0)
745  {
746  oid = atooid(&th->targetFile[5]);
747  if (oid != 0)
748  {
749  ahlog(AH, 1, "restoring large object with OID %u\n", oid);
750 
751  StartRestoreBlob(AH, oid, AH->public.ropt->dropSchema);
752 
753  while ((cnt = tarRead(buf, 4095, th)) > 0)
754  {
755  buf[cnt] = '\0';
756  ahwrite(buf, 1, cnt, AH);
757  }
758  EndRestoreBlob(AH, oid);
759  foundBlob = true;
760  }
761  tarClose(AH, th);
762  }
763  else
764  {
765  tarClose(AH, th);
766 
767  /*
768  * Once we have found the first blob, stop at the first non-blob
769  * entry (which will be 'blobs.toc'). This coding would eat all
770  * the rest of the archive if there are no blobs ... but this
771  * function shouldn't be called at all in that case.
772  */
773  if (foundBlob)
774  break;
775  }
776 
777  th = tarOpen(AH, NULL, 'r');
778  }
779  EndRestoreBlobs(AH);
780 }
781 
782 
783 static int
784 _WriteByte(ArchiveHandle *AH, const int i)
785 {
786  lclContext *ctx = (lclContext *) AH->formatData;
787  char b = i; /* Avoid endian problems */
788 
789  if (tarWrite(&b, 1, ctx->FH) != 1)
791 
792  ctx->filePos += 1;
793  return 1;
794 }
795 
796 static int
798 {
799  lclContext *ctx = (lclContext *) AH->formatData;
800  size_t res;
801  unsigned char c;
802 
803  res = tarRead(&c, 1, ctx->FH);
804  if (res != 1)
805  /* We already would have exited for errors on reads, must be EOF */
807  "could not read from input file: end of file\n");
808  ctx->filePos += 1;
809  return c;
810 }
811 
812 static void
813 _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
814 {
815  lclContext *ctx = (lclContext *) AH->formatData;
816 
817  if (tarWrite(buf, len, ctx->FH) != len)
819 
820  ctx->filePos += len;
821 }
822 
823 static void
824 _ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
825 {
826  lclContext *ctx = (lclContext *) AH->formatData;
827 
828  if (tarRead(buf, len, ctx->FH) != len)
829  /* We already would have exited for errors on reads, must be EOF */
831  "could not read from input file: end of file\n");
832 
833  ctx->filePos += len;
834  return;
835 }
836 
837 static void
839 {
840  lclContext *ctx = (lclContext *) AH->formatData;
841  TAR_MEMBER *th;
842  RestoreOptions *ropt;
843  RestoreOptions *savRopt;
844  DumpOptions *savDopt;
845  int savVerbose,
846  i;
847 
848  if (AH->mode == archModeWrite)
849  {
850  /*
851  * Write the Header & TOC to the archive FIRST
852  */
853  th = tarOpen(AH, "toc.dat", 'w');
854  ctx->FH = th;
855  WriteHead(AH);
856  WriteToc(AH);
857  tarClose(AH, th); /* Not needed any more */
858 
859  /*
860  * Now send the data (tables & blobs)
861  */
862  WriteDataChunks(AH, NULL);
863 
864  /*
865  * Now this format wants to append a script which does a full restore
866  * if the files have been extracted.
867  */
868  th = tarOpen(AH, "restore.sql", 'w');
869 
870  tarPrintf(AH, th, "--\n"
871  "-- NOTE:\n"
872  "--\n"
873  "-- File paths need to be edited. Search for $$PATH$$ and\n"
874  "-- replace it with the path to the directory containing\n"
875  "-- the extracted data files.\n"
876  "--\n");
877 
878  AH->CustomOutPtr = _scriptOut;
879 
880  ctx->isSpecialScript = 1;
881  ctx->scriptTH = th;
882 
883  ropt = NewRestoreOptions();
884  memcpy(ropt, AH->public.ropt, sizeof(RestoreOptions));
885  ropt->filename = NULL;
886  ropt->dropSchema = 1;
887  ropt->compression = 0;
888  ropt->superuser = NULL;
889  ropt->suppressDumpWarnings = true;
890 
891  savDopt = AH->public.dopt;
892  savRopt = AH->public.ropt;
893 
894  SetArchiveOptions((Archive *) AH, NULL, ropt);
895 
896  savVerbose = AH->public.verbose;
897  AH->public.verbose = 0;
898 
899  RestoreArchive((Archive *) AH);
900 
901  SetArchiveOptions((Archive *) AH, savDopt, savRopt);
902 
903  AH->public.verbose = savVerbose;
904 
905  tarClose(AH, th);
906 
907  ctx->isSpecialScript = 0;
908 
909  /*
910  * EOF marker for tar files is two blocks of NULLs.
911  */
912  for (i = 0; i < 512 * 2; i++)
913  {
914  if (fputc(0, ctx->tarFH) == EOF)
916  }
917 
918  /* Sync the output file if one is defined */
919  if (AH->dosync && AH->fSpec)
920  (void) fsync_fname(AH->fSpec, false, progname);
921  }
922 
923  AH->FH = NULL;
924 }
925 
926 static size_t
927 _scriptOut(ArchiveHandle *AH, const void *buf, size_t len)
928 {
929  lclContext *ctx = (lclContext *) AH->formatData;
930 
931  return tarWrite(buf, len, ctx->scriptTH);
932 }
933 
934 /*
935  * BLOB support
936  */
937 
938 /*
939  * Called by the archiver when starting to save all BLOB DATA (not schema).
940  * This routine should save whatever format-specific information is needed
941  * to read the BLOBs back into memory.
942  *
943  * It is called just prior to the dumper's DataDumper routine.
944  *
945  * Optional, but strongly recommended.
946  *
947  */
948 static void
950 {
951  lclContext *ctx = (lclContext *) AH->formatData;
952  char fname[K_STD_BUF_SIZE];
953 
954  sprintf(fname, "blobs.toc");
955  ctx->blobToc = tarOpen(AH, fname, 'w');
956 }
957 
958 /*
959  * Called by the archiver when the dumper calls StartBlob.
960  *
961  * Mandatory.
962  *
963  * Must save the passed OID for retrieval at restore-time.
964  */
965 static void
967 {
968  lclContext *ctx = (lclContext *) AH->formatData;
969  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
970  char fname[255];
971  char *sfx;
972 
973  if (oid == 0)
974  exit_horribly(modulename, "invalid OID for large object (%u)\n", oid);
975 
976  if (AH->compression != 0)
977  sfx = ".gz";
978  else
979  sfx = "";
980 
981  sprintf(fname, "blob_%u.dat%s", oid, sfx);
982 
983  tarPrintf(AH, ctx->blobToc, "%u %s\n", oid, fname);
984 
985  tctx->TH = tarOpen(AH, fname, 'w');
986 }
987 
988 /*
989  * Called by the archiver when the dumper calls EndBlob.
990  *
991  * Optional.
992  *
993  */
994 static void
996 {
997  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
998 
999  tarClose(AH, tctx->TH);
1000 }
1001 
1002 /*
1003  * Called by the archiver when finishing saving all BLOB DATA.
1004  *
1005  * Optional.
1006  *
1007  */
1008 static void
1010 {
1011  lclContext *ctx = (lclContext *) AH->formatData;
1012 
1013  /* Write out a fake zero OID to mark end-of-blobs. */
1014  /* WriteInt(AH, 0); */
1015 
1016  tarClose(AH, ctx->blobToc);
1017 }
1018 
1019 
1020 
1021 /*------------
1022  * TAR Support
1023  *------------
1024  */
1025 
1026 static int
1027 tarPrintf(ArchiveHandle *AH, TAR_MEMBER *th, const char *fmt,...)
1028 {
1029  char *p;
1030  size_t len = 128; /* initial assumption about buffer size */
1031  size_t cnt;
1032 
1033  for (;;)
1034  {
1035  va_list args;
1036 
1037  /* Allocate work buffer. */
1038  p = (char *) pg_malloc(len);
1039 
1040  /* Try to format the data. */
1041  va_start(args, fmt);
1042  cnt = pvsnprintf(p, len, fmt, args);
1043  va_end(args);
1044 
1045  if (cnt < len)
1046  break; /* success */
1047 
1048  /* Release buffer and loop around to try again with larger len. */
1049  free(p);
1050  len = cnt;
1051  }
1052 
1053  cnt = tarWrite(p, cnt, th);
1054  free(p);
1055  return (int) cnt;
1056 }
1057 
1058 bool
1060 {
1061  int sum;
1062  int chk = tarChecksum(header);
1063 
1064  sum = read_tar_number(&header[148], 8);
1065 
1066  if (sum != chk)
1067  return false;
1068 
1069  /* POSIX tar format */
1070  if (memcmp(&header[257], "ustar\0", 6) == 0 &&
1071  memcmp(&header[263], "00", 2) == 0)
1072  return true;
1073  /* GNU tar format */
1074  if (memcmp(&header[257], "ustar \0", 8) == 0)
1075  return true;
1076  /* not-quite-POSIX format written by pre-9.3 pg_dump */
1077  if (memcmp(&header[257], "ustar00\0", 8) == 0)
1078  return true;
1079 
1080  return false;
1081 }
1082 
1083 /* Given the member, write the TAR header & copy the file */
1084 static void
1086 {
1087  lclContext *ctx = (lclContext *) AH->formatData;
1088  FILE *tmp = th->tmpFH; /* Grab it for convenience */
1089  char buf[32768];
1090  size_t cnt;
1091  pgoff_t len = 0;
1092  size_t res;
1093  size_t i,
1094  pad;
1095 
1096  /*
1097  * Find file len & go back to start.
1098  */
1099  fseeko(tmp, 0, SEEK_END);
1100  th->fileLen = ftello(tmp);
1101  if (th->fileLen < 0)
1102  exit_horribly(modulename, "could not determine seek position in archive file: %s\n",
1103  strerror(errno));
1104  fseeko(tmp, 0, SEEK_SET);
1105 
1106  _tarWriteHeader(th);
1107 
1108  while ((cnt = fread(buf, 1, sizeof(buf), tmp)) > 0)
1109  {
1110  if ((res = fwrite(buf, 1, cnt, th->tarFH)) != cnt)
1112  len += res;
1113  }
1114  if (!feof(tmp))
1115  READ_ERROR_EXIT(tmp);
1116 
1117  if (fclose(tmp) != 0) /* This *should* delete it... */
1118  exit_horribly(modulename, "could not close temporary file: %s\n",
1119  strerror(errno));
1120 
1121  if (len != th->fileLen)
1122  {
1123  char buf1[32],
1124  buf2[32];
1125 
1126  snprintf(buf1, sizeof(buf1), INT64_FORMAT, (int64) len);
1127  snprintf(buf2, sizeof(buf2), INT64_FORMAT, (int64) th->fileLen);
1128  exit_horribly(modulename, "actual file length (%s) does not match expected (%s)\n",
1129  buf1, buf2);
1130  }
1131 
1132  pad = ((len + 511) & ~511) - len;
1133  for (i = 0; i < pad; i++)
1134  {
1135  if (fputc('\0', th->tarFH) == EOF)
1137  }
1138 
1139  ctx->tarFHpos += len + pad;
1140 }
1141 
1142 /* Locate the file in the archive, read header and position to data */
1143 static TAR_MEMBER *
1145 {
1146  lclContext *ctx = (lclContext *) AH->formatData;
1147  TAR_MEMBER *th = pg_malloc0(sizeof(TAR_MEMBER));
1148  char c;
1149  char header[512];
1150  size_t i,
1151  len,
1152  blks;
1153  int id;
1154 
1155  th->AH = AH;
1156 
1157  /* Go to end of current file, if any */
1158  if (ctx->tarFHpos != 0)
1159  {
1160  char buf1[100],
1161  buf2[100];
1162 
1163  snprintf(buf1, sizeof(buf1), INT64_FORMAT, (int64) ctx->tarFHpos);
1164  snprintf(buf2, sizeof(buf2), INT64_FORMAT, (int64) ctx->tarNextMember);
1165  ahlog(AH, 4, "moving from position %s to next member at file position %s\n",
1166  buf1, buf2);
1167 
1168  while (ctx->tarFHpos < ctx->tarNextMember)
1169  _tarReadRaw(AH, &c, 1, NULL, ctx->tarFH);
1170  }
1171 
1172  {
1173  char buf[100];
1174 
1175  snprintf(buf, sizeof(buf), INT64_FORMAT, (int64) ctx->tarFHpos);
1176  ahlog(AH, 4, "now at file position %s\n", buf);
1177  }
1178 
1179  /* We are at the start of the file, or at the next member */
1180 
1181  /* Get the header */
1182  if (!_tarGetHeader(AH, th))
1183  {
1184  if (filename)
1185  exit_horribly(modulename, "could not find header for file \"%s\" in tar archive\n", filename);
1186  else
1187  {
1188  /*
1189  * We're just scanning the archive for the next file, so return
1190  * null
1191  */
1192  free(th);
1193  return NULL;
1194  }
1195  }
1196 
1197  while (filename != NULL && strcmp(th->targetFile, filename) != 0)
1198  {
1199  ahlog(AH, 4, "skipping tar member %s\n", th->targetFile);
1200 
1201  id = atoi(th->targetFile);
1202  if ((TocIDRequired(AH, id) & REQ_DATA) != 0)
1203  exit_horribly(modulename, "restoring data out of order is not supported in this archive format: "
1204  "\"%s\" is required, but comes before \"%s\" in the archive file.\n",
1205  th->targetFile, filename);
1206 
1207  /* Header doesn't match, so read to next header */
1208  len = ((th->fileLen + 511) & ~511); /* Padded length */
1209  blks = len >> 9; /* # of 512 byte blocks */
1210 
1211  for (i = 0; i < blks; i++)
1212  _tarReadRaw(AH, &header[0], 512, NULL, ctx->tarFH);
1213 
1214  if (!_tarGetHeader(AH, th))
1215  exit_horribly(modulename, "could not find header for file \"%s\" in tar archive\n", filename);
1216  }
1217 
1218  ctx->tarNextMember = ctx->tarFHpos + ((th->fileLen + 511) & ~511);
1219  th->pos = 0;
1220 
1221  return th;
1222 }
1223 
1224 /* Read & verify a header */
1225 static int
1227 {
1228  lclContext *ctx = (lclContext *) AH->formatData;
1229  char h[512];
1230  char tag[100 + 1];
1231  int sum,
1232  chk;
1233  pgoff_t len;
1234  pgoff_t hPos;
1235  bool gotBlock = false;
1236 
1237  while (!gotBlock)
1238  {
1239  /* Save the pos for reporting purposes */
1240  hPos = ctx->tarFHpos;
1241 
1242  /* Read a 512 byte block, return EOF, exit if short */
1243  len = _tarReadRaw(AH, h, 512, NULL, ctx->tarFH);
1244  if (len == 0) /* EOF */
1245  return 0;
1246 
1247  if (len != 512)
1249  ngettext("incomplete tar header found (%lu byte)\n",
1250  "incomplete tar header found (%lu bytes)\n",
1251  len),
1252  (unsigned long) len);
1253 
1254  /* Calc checksum */
1255  chk = tarChecksum(h);
1256  sum = read_tar_number(&h[148], 8);
1257 
1258  /*
1259  * If the checksum failed, see if it is a null block. If so, silently
1260  * continue to the next block.
1261  */
1262  if (chk == sum)
1263  gotBlock = true;
1264  else
1265  {
1266  int i;
1267 
1268  for (i = 0; i < 512; i++)
1269  {
1270  if (h[i] != 0)
1271  {
1272  gotBlock = true;
1273  break;
1274  }
1275  }
1276  }
1277  }
1278 
1279  /* Name field is 100 bytes, might not be null-terminated */
1280  strlcpy(tag, &h[0], 100 + 1);
1281 
1282  len = read_tar_number(&h[124], 12);
1283 
1284  {
1285  char posbuf[32];
1286  char lenbuf[32];
1287 
1288  snprintf(posbuf, sizeof(posbuf), UINT64_FORMAT, (uint64) hPos);
1289  snprintf(lenbuf, sizeof(lenbuf), UINT64_FORMAT, (uint64) len);
1290  ahlog(AH, 3, "TOC Entry %s at %s (length %s, checksum %d)\n",
1291  tag, posbuf, lenbuf, sum);
1292  }
1293 
1294  if (chk != sum)
1295  {
1296  char posbuf[32];
1297 
1298  snprintf(posbuf, sizeof(posbuf), UINT64_FORMAT,
1299  (uint64) ftello(ctx->tarFH));
1301  "corrupt tar header found in %s "
1302  "(expected %d, computed %d) file position %s\n",
1303  tag, sum, chk, posbuf);
1304  }
1305 
1306  th->targetFile = pg_strdup(tag);
1307  th->fileLen = len;
1308 
1309  return 1;
1310 }
1311 
1312 
1313 static void
1315 {
1316  char h[512];
1317 
1318  tarCreateHeader(h, th->targetFile, NULL, th->fileLen,
1319  0600, 04000, 02000, time(NULL));
1320 
1321  /* Now write the completed header. */
1322  if (fwrite(h, 1, 512, th->tarFH) != 512)
1324 }
static int tarPrintf(ArchiveHandle *AH, TAR_MEMBER *th, const char *fmt,...) pg_attribute_printf(3
static void _LoadBlobs(ArchiveHandle *AH)
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
FILE * zFH
Definition: pg_backup_tar.c:71
void ReadToc(ArchiveHandle *AH)
#define TH
static void _StartData(ArchiveHandle *AH, TocEntry *te)
void ReadHead(ArchiveHandle *AH)
static void _PrintFileData(ArchiveHandle *AH, char *filename)
TAR_MEMBER * blobToc
Definition: pg_backup_tar.c:87
TAR_MEMBER * scriptTH
Definition: pg_backup_tar.c:93
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
RestoreOptions * ropt
Definition: pg_backup.h:182
uint64 read_tar_number(const char *s, int len)
Definition: tar.c:56
static TAR_MEMBER * tarOpen(ArchiveHandle *AH, const char *filename, char mode)
pgoff_t tarFHpos
Definition: pg_backup_tar.c:89
void WriteDataChunks(ArchiveHandle *AH, ParallelState *pstate)
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:567
#define gettext_noop(x)
Definition: c.h:139
#define PG_BINARY_W
Definition: c.h:1042
void WriteToc(ArchiveHandle *AH)
ArchiveHandle * AH
Definition: pg_backup_tar.c:80
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
DataDumperPtr dataDumper
unsigned int Oid
Definition: postgres_ext.h:31
#define fseeko(stream, offset, origin)
Definition: win32.h:237
const char * progname
Definition: pg_standby.c:37
#define PG_BINARY_R
Definition: c.h:1041
void InitArchiveFmt_Tar(ArchiveHandle *AH)
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void StartRestoreBlobs(ArchiveHandle *AH)
struct _tocEntry * currToc
RestoreOptions * NewRestoreOptions(void)
#define pg_attribute_printf(f, a)
Definition: c.h:639
pgoff_t tarNextMember
Definition: pg_backup_tar.c:90
static struct pg_tm tm
Definition: localtime.c:111
size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args)
Definition: psprintf.c:104
int isSpecialScript
Definition: pg_backup_tar.c:92
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
DumpOptions * dopt
Definition: pg_backup.h:181
FILE * tarFH
Definition: pg_backup_tar.c:88
void StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop)
char * c
static void _StartBlobs(ArchiveHandle *AH, TocEntry *te)
static char * buf
Definition: pg_test_fsync.c:66
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
static TAR_MEMBER * _tarPositionTo(ArchiveHandle *AH, const char *filename)
#define GZEOF(fh)
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
#define atooid(x)
Definition: postgres_ext.h:42
bool isValidTarHeader(char *header)
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _EndBlobs(ArchiveHandle *AH, TocEntry *te)
#define GZWRITE(p, s, n, fh)
void EndRestoreBlobs(ArchiveHandle *AH)
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)
Definition: tar.c:112
#define pgoff_t
Definition: win32.h:231
#define ngettext(s, p, n)
Definition: c.h:127
FILE * nFH
Definition: pg_backup_tar.c:73
static const char * modulename
int verbose
Definition: pg_backup.h:184
static size_t _scriptOut(ArchiveHandle *AH, const void *buf, size_t len)
#define S_IRWXO
Definition: win32.h:455
int tarChecksum(char *header)
Definition: tar.c:88
pgoff_t pos
Definition: pg_backup_tar.c:78
static void * fn(void *arg)
#define free(a)
Definition: header.h:65
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define NULL
Definition: c.h:229
CustomOutPtrType CustomOutPtr
static void _tarWriteHeader(TAR_MEMBER *th)
char * ReadStr(ArchiveHandle *AH)
teReqs TocIDRequired(ArchiveHandle *AH, DumpId id)
static void _EndData(ArchiveHandle *AH, TocEntry *te)
FILE * tmpFH
Definition: pg_backup_tar.c:75
pgoff_t fileLen
Definition: pg_backup_tar.c:79
#define ftello(stream)
Definition: win32.h:240
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te)
static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
#define GZCLOSE(fh)
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:207
#define INT64_FORMAT
Definition: c.h:315
const char * name
Definition: encode.c:521
static int static void _tarAddFile(ArchiveHandle *AH, TAR_MEMBER *th)
void WriteHead(ArchiveHandle *AH)
#define GZREAD(p, s, n, fh)
#define WRITE_ERROR_EXIT
void exit_horribly(const char *modulename, const char *fmt,...)
#define S_IRWXG
Definition: win32.h:451
static void _CloseArchive(ArchiveHandle *AH)
#define K_STD_BUF_SIZE
Definition: pg_backup_tar.c:63
static char * filename
Definition: pg_dumpall.c:90
static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
void ahlog(ArchiveHandle *AH, int level, const char *fmt,...)
int errmsg(const char *fmt,...)
Definition: elog.c:797
void EndRestoreBlob(ArchiveHandle *AH, Oid oid)
void RestoreArchive(Archive *AH)
static size_t tarRead(void *buf, size_t len, TAR_MEMBER *th)
int i
const char * strerror(int errnum)
Definition: strerror.c:19
size_t WriteStr(ArchiveHandle *AH, const char *c)
TAR_MEMBER * TH
Definition: pg_backup_tar.c:98
#define READ_ERROR_EXIT(fd)
static int _tarGetHeader(ArchiveHandle *AH, TAR_MEMBER *th)
void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
bool checkSeek(FILE *fp)
static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te)
pgoff_t filePos
static int _ReadByte(ArchiveHandle *)
static size_t tarWrite(const void *buf, size_t len, TAR_MEMBER *th)
FILE * tarFH
Definition: pg_backup_tar.c:74
#define UINT64_FORMAT
Definition: c.h:316
#define LOBBUFSIZE
static int _WriteByte(ArchiveHandle *AH, const int i)
char * targetFile
Definition: pg_backup_tar.c:76
static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
static size_t _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh)