PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_backup_directory.c File Reference
#include "postgres_fe.h"
#include "compress_io.h"
#include "parallel.h"
#include "pg_backup_utils.h"
#include <dirent.h>
#include <sys/stat.h>
Include dependency graph for pg_backup_directory.c:

Go to the source code of this file.

Data Structures

struct  lclContext
 
struct  lclTocEntry
 

Functions

static void _ArchiveEntry (ArchiveHandle *AH, TocEntry *te)
 
static void _StartData (ArchiveHandle *AH, TocEntry *te)
 
static void _EndData (ArchiveHandle *AH, TocEntry *te)
 
static void _WriteData (ArchiveHandle *AH, const void *data, size_t dLen)
 
static int _WriteByte (ArchiveHandle *AH, const int i)
 
static int _ReadByte (ArchiveHandle *)
 
static void _WriteBuf (ArchiveHandle *AH, const void *buf, size_t len)
 
static void _ReadBuf (ArchiveHandle *AH, void *buf, size_t len)
 
static void _CloseArchive (ArchiveHandle *AH)
 
static void _ReopenArchive (ArchiveHandle *AH)
 
static void _PrintTocData (ArchiveHandle *AH, TocEntry *te)
 
static void _WriteExtraToc (ArchiveHandle *AH, TocEntry *te)
 
static void _ReadExtraToc (ArchiveHandle *AH, TocEntry *te)
 
static void _PrintExtraToc (ArchiveHandle *AH, TocEntry *te)
 
static void _StartBlobs (ArchiveHandle *AH, TocEntry *te)
 
static void _StartBlob (ArchiveHandle *AH, TocEntry *te, Oid oid)
 
static void _EndBlob (ArchiveHandle *AH, TocEntry *te, Oid oid)
 
static void _EndBlobs (ArchiveHandle *AH, TocEntry *te)
 
static void _LoadBlobs (ArchiveHandle *AH)
 
static void _Clone (ArchiveHandle *AH)
 
static void _DeClone (ArchiveHandle *AH)
 
static int _WorkerJobRestoreDirectory (ArchiveHandle *AH, TocEntry *te)
 
static int _WorkerJobDumpDirectory (ArchiveHandle *AH, TocEntry *te)
 
static void setFilePath (ArchiveHandle *AH, char *buf, const char *relativeFilename)
 
void InitArchiveFmt_Directory (ArchiveHandle *AH)
 
static void _PrintFileData (ArchiveHandle *AH, char *filename)
 

Variables

static const char * modulename = gettext_noop("directory archiver")
 

Function Documentation

static void _ArchiveEntry ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 236 of file pg_backup_directory.c.

References _tocEntry::dataDumper, _tocEntry::desc, _tocEntry::dumpId, lclTocEntry::filename, fn(), _tocEntry::formatData, MAXPGPATH, NULL, pg_malloc0(), pg_strdup(), and snprintf().

Referenced by InitArchiveFmt_Directory().

237 {
238  lclTocEntry *tctx;
239  char fn[MAXPGPATH];
240 
241  tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
242  if (te->dataDumper)
243  {
244  snprintf(fn, MAXPGPATH, "%d.dat", te->dumpId);
245  tctx->filename = pg_strdup(fn);
246  }
247  else if (strcmp(te->desc, "BLOBS") == 0)
248  tctx->filename = pg_strdup("blobs.toc");
249  else
250  tctx->filename = NULL;
251 
252  te->formatData = (void *) tctx;
253 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
DataDumperPtr dataDumper
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
#define MAXPGPATH
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static void * fn(void *arg)
#define NULL
Definition: c.h:226
static void _Clone ( ArchiveHandle AH)
static

Definition at line 721 of file pg_backup_directory.c.

References _archiveHandle::formatData, and pg_malloc().

Referenced by InitArchiveFmt_Directory().

722 {
723  lclContext *ctx = (lclContext *) AH->formatData;
724 
725  AH->formatData = (lclContext *) pg_malloc(sizeof(lclContext));
726  memcpy(AH->formatData, ctx, sizeof(lclContext));
727  ctx = (lclContext *) AH->formatData;
728 
729  /*
730  * Note: we do not make a local lo_buf because we expect at most one BLOBS
731  * entry per archive, so no parallelism is possible. Likewise,
732  * TOC-entry-local state isn't an issue because any one TOC entry is
733  * touched by just one worker child.
734  */
735 
736  /*
737  * We also don't copy the ParallelState pointer (pstate), only the master
738  * process ever writes to it.
739  */
740 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static void _CloseArchive ( ArchiveHandle AH)
static

Definition at line 560 of file pg_backup_directory.c.

References archDirectory, archModeWrite, archTar, cfclose(), cfopen_write(), lclContext::dataFH, exit_horribly(), _archiveHandle::FH, _archiveHandle::format, _archiveHandle::formatData, MAXPGPATH, _archiveHandle::mode, modulename, NULL, ParallelBackupEnd(), ParallelBackupStart(), PG_BINARY_W, lclContext::pstate, setFilePath(), strerror(), WriteDataChunks(), WriteHead(), and WriteToc().

Referenced by InitArchiveFmt_Directory().

561 {
562  lclContext *ctx = (lclContext *) AH->formatData;
563 
564  if (AH->mode == archModeWrite)
565  {
566  cfp *tocFH;
567  char fname[MAXPGPATH];
568 
569  setFilePath(AH, fname, "toc.dat");
570 
571  /* this will actually fork the processes for a parallel backup */
572  ctx->pstate = ParallelBackupStart(AH);
573 
574  /* The TOC is always created uncompressed */
575  tocFH = cfopen_write(fname, PG_BINARY_W, 0);
576  if (tocFH == NULL)
577  exit_horribly(modulename, "could not open output file \"%s\": %s\n",
578  fname, strerror(errno));
579  ctx->dataFH = tocFH;
580 
581  /*
582  * Write 'tar' in the format field of the toc.dat file. The directory
583  * is compatible with 'tar', so there's no point having a different
584  * format code for it.
585  */
586  AH->format = archTar;
587  WriteHead(AH);
588  AH->format = archDirectory;
589  WriteToc(AH);
590  if (cfclose(tocFH) != 0)
591  exit_horribly(modulename, "could not close TOC file: %s\n",
592  strerror(errno));
593  WriteDataChunks(AH, ctx->pstate);
594 
595  ParallelBackupEnd(AH, ctx->pstate);
596  }
597  AH->FH = NULL;
598 }
ParallelState * pstate
void WriteDataChunks(ArchiveHandle *AH, ParallelState *pstate)
static void setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
#define PG_BINARY_W
Definition: c.h:1041
void WriteToc(ArchiveHandle *AH)
static const char * modulename
int cfclose(cfp *fp)
Definition: compress_io.c:661
#define MAXPGPATH
ParallelState * ParallelBackupStart(ArchiveHandle *AH)
Definition: parallel.c:911
void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate)
Definition: parallel.c:1081
ArchiveFormat format
#define NULL
Definition: c.h:226
void WriteHead(ArchiveHandle *AH)
void exit_horribly(const char *modulename, const char *fmt,...)
cfp * cfopen_write(const char *path, const char *mode, int compression)
Definition: compress_io.c:504
const char * strerror(int errnum)
Definition: strerror.c:19
static void _DeClone ( ArchiveHandle AH)
static

Definition at line 743 of file pg_backup_directory.c.

References _archiveHandle::formatData, and free.

Referenced by InitArchiveFmt_Directory().

744 {
745  lclContext *ctx = (lclContext *) AH->formatData;
746 
747  free(ctx);
748 }
#define free(a)
Definition: header.h:60
static void _EndBlob ( ArchiveHandle AH,
TocEntry te,
Oid  oid 
)
static

Definition at line 665 of file pg_backup_directory.c.

References lclContext::blobsTocFH, buf, cfclose(), cfwrite(), lclContext::dataFH, exit_horribly(), _archiveHandle::formatData, modulename, NULL, and snprintf().

Referenced by InitArchiveFmt_Directory().

666 {
667  lclContext *ctx = (lclContext *) AH->formatData;
668  char buf[50];
669  int len;
670 
671  /* Close the BLOB data file itself */
672  cfclose(ctx->dataFH);
673  ctx->dataFH = NULL;
674 
675  /* register the blob in blobs.toc */
676  len = snprintf(buf, sizeof(buf), "%u blob_%u.dat\n", oid, oid);
677  if (cfwrite(buf, len, ctx->blobsTocFH) != len)
678  exit_horribly(modulename, "could not write to blobs TOC file\n");
679 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static const char * modulename
int cfclose(cfp *fp)
Definition: compress_io.c:661
static char * buf
Definition: pg_test_fsync.c:65
#define NULL
Definition: c.h:226
int cfwrite(const void *ptr, int size, cfp *fp)
Definition: compress_io.c:609
void exit_horribly(const char *modulename, const char *fmt,...)
static void _EndBlobs ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 687 of file pg_backup_directory.c.

References lclContext::blobsTocFH, cfclose(), _archiveHandle::formatData, and NULL.

Referenced by InitArchiveFmt_Directory().

688 {
689  lclContext *ctx = (lclContext *) AH->formatData;
690 
691  cfclose(ctx->blobsTocFH);
692  ctx->blobsTocFH = NULL;
693 }
int cfclose(cfp *fp)
Definition: compress_io.c:661
#define NULL
Definition: c.h:226
static void _EndData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 366 of file pg_backup_directory.c.

References cfclose(), lclContext::dataFH, _archiveHandle::formatData, and NULL.

Referenced by InitArchiveFmt_Directory().

367 {
368  lclContext *ctx = (lclContext *) AH->formatData;
369 
370  /* Close the file */
371  cfclose(ctx->dataFH);
372 
373  ctx->dataFH = NULL;
374 }
int cfclose(cfp *fp)
Definition: compress_io.c:661
#define NULL
Definition: c.h:226
static void _LoadBlobs ( ArchiveHandle AH)
static

Definition at line 433 of file pg_backup_directory.c.

References _PrintFileData(), lclContext::blobsTocFH, cfclose(), cfeof(), cfgets(), cfopen_read(), lclContext::directory, _restoreOptions::dropSchema, EndRestoreBlob(), EndRestoreBlobs(), exit_horribly(), _archiveHandle::formatData, MAXPGPATH, modulename, NULL, PG_BINARY_R, _archiveHandle::public, Archive::ropt, setFilePath(), snprintf(), StartRestoreBlob(), StartRestoreBlobs(), and strerror().

Referenced by _PrintTocData().

434 {
435  Oid oid;
436  lclContext *ctx = (lclContext *) AH->formatData;
437  char fname[MAXPGPATH];
438  char line[MAXPGPATH];
439 
440  StartRestoreBlobs(AH);
441 
442  setFilePath(AH, fname, "blobs.toc");
443 
444  ctx->blobsTocFH = cfopen_read(fname, PG_BINARY_R);
445 
446  if (ctx->blobsTocFH == NULL)
447  exit_horribly(modulename, "could not open large object TOC file \"%s\" for input: %s\n",
448  fname, strerror(errno));
449 
450  /* Read the blobs TOC file line-by-line, and process each blob */
451  while ((cfgets(ctx->blobsTocFH, line, MAXPGPATH)) != NULL)
452  {
453  char fname[MAXPGPATH];
454  char path[MAXPGPATH];
455 
456  /* Can't overflow because line and fname are the same length. */
457  if (sscanf(line, "%u %s\n", &oid, fname) != 2)
458  exit_horribly(modulename, "invalid line in large object TOC file \"%s\": \"%s\"\n",
459  fname, line);
460 
461  StartRestoreBlob(AH, oid, AH->public.ropt->dropSchema);
462  snprintf(path, MAXPGPATH, "%s/%s", ctx->directory, fname);
463  _PrintFileData(AH, path);
464  EndRestoreBlob(AH, oid);
465  }
466  if (!cfeof(ctx->blobsTocFH))
467  exit_horribly(modulename, "error reading large object TOC file \"%s\"\n",
468  fname);
469 
470  if (cfclose(ctx->blobsTocFH) != 0)
471  exit_horribly(modulename, "could not close large object TOC file \"%s\": %s\n",
472  fname, strerror(errno));
473 
474  ctx->blobsTocFH = NULL;
475 
476  EndRestoreBlobs(AH);
477 }
RestoreOptions * ropt
Definition: pg_backup.h:179
int cfeof(cfp *fp)
Definition: compress_io.c:688
static void setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static const char * modulename
unsigned int Oid
Definition: postgres_ext.h:31
int cfclose(cfp *fp)
Definition: compress_io.c:661
#define PG_BINARY_R
Definition: c.h:1040
void StartRestoreBlobs(ArchiveHandle *AH)
#define MAXPGPATH
void StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop)
char * cfgets(cfp *fp, char *buf, int len)
Definition: compress_io.c:650
void EndRestoreBlobs(ArchiveHandle *AH)
static void _PrintFileData(ArchiveHandle *AH, char *filename)
#define NULL
Definition: c.h:226
void exit_horribly(const char *modulename, const char *fmt,...)
void EndRestoreBlob(ArchiveHandle *AH, Oid oid)
cfp * cfopen_read(const char *path, const char *mode)
Definition: compress_io.c:467
const char * strerror(int errnum)
Definition: strerror.c:19
static void _PrintExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 307 of file pg_backup_directory.c.

References ahprintf(), lclTocEntry::filename, _tocEntry::formatData, _archiveHandle::public, and Archive::verbose.

Referenced by InitArchiveFmt_Directory().

308 {
309  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
310 
311  if (AH->public.verbose && tctx->filename)
312  ahprintf(AH, "-- File: %s\n", tctx->filename);
313 }
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
int verbose
Definition: pg_backup.h:181
static void _PrintFileData ( ArchiveHandle AH,
char *  filename 
)
static

Definition at line 380 of file pg_backup_directory.c.

References ahwrite(), buf, cfclose(), cfopen_read(), cfread(), exit_horribly(), free, modulename, PG_BINARY_R, pg_malloc(), strerror(), and ZLIB_OUT_SIZE.

Referenced by _LoadBlobs(), and _PrintTocData().

381 {
382  size_t cnt;
383  char *buf;
384  size_t buflen;
385  cfp *cfp;
386 
387  if (!filename)
388  return;
389 
391 
392  if (!cfp)
393  exit_horribly(modulename, "could not open input file \"%s\": %s\n",
394  filename, strerror(errno));
395 
396  buf = pg_malloc(ZLIB_OUT_SIZE);
397  buflen = ZLIB_OUT_SIZE;
398 
399  while ((cnt = cfread(buf, buflen, cfp)))
400  {
401  ahwrite(buf, 1, cnt, AH);
402  }
403 
404  free(buf);
405  if (cfclose(cfp) !=0)
406  exit_horribly(modulename, "could not close data file: %s\n",
407  strerror(errno));
408 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int cfread(void *ptr, int size, cfp *fp)
Definition: compress_io.c:583
static const char * modulename
int cfclose(cfp *fp)
Definition: compress_io.c:661
#define PG_BINARY_R
Definition: c.h:1040
struct cfp cfp
Definition: compress_io.h:57
static char * buf
Definition: pg_test_fsync.c:65
#define free(a)
Definition: header.h:60
#define ZLIB_OUT_SIZE
Definition: walmethods.c:31
void exit_horribly(const char *modulename, const char *fmt,...)
static char * filename
Definition: pg_dumpall.c:84
cfp * cfopen_read(const char *path, const char *mode)
Definition: compress_io.c:467
const char * strerror(int errnum)
Definition: strerror.c:19
void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
static void _PrintTocData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 414 of file pg_backup_directory.c.

References _LoadBlobs(), _PrintFileData(), _tocEntry::desc, lclTocEntry::filename, _tocEntry::formatData, MAXPGPATH, and setFilePath().

Referenced by InitArchiveFmt_Directory().

415 {
416  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
417 
418  if (!tctx->filename)
419  return;
420 
421  if (strcmp(te->desc, "BLOBS") == 0)
422  _LoadBlobs(AH);
423  else
424  {
425  char fname[MAXPGPATH];
426 
427  setFilePath(AH, fname, tctx->filename);
428  _PrintFileData(AH, fname);
429  }
430 }
static void setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
#define MAXPGPATH
static void _PrintFileData(ArchiveHandle *AH, char *filename)
static void _LoadBlobs(ArchiveHandle *AH)
static void _ReadBuf ( ArchiveHandle AH,
void *  buf,
size_t  len 
)
static

Definition at line 532 of file pg_backup_directory.c.

References cfread(), lclContext::dataFH, exit_horribly(), _archiveHandle::formatData, and modulename.

Referenced by InitArchiveFmt_Directory().

533 {
534  lclContext *ctx = (lclContext *) AH->formatData;
535 
536  /*
537  * If there was an I/O error, we already exited in cfread(), so here we
538  * exit on short reads.
539  */
540  if (cfread(buf, len, ctx->dataFH) != len)
542  "could not read from input file: end of file\n");
543 
544  return;
545 }
int cfread(void *ptr, int size, cfp *fp)
Definition: compress_io.c:583
static const char * modulename
static char * buf
Definition: pg_test_fsync.c:65
void exit_horribly(const char *modulename, const char *fmt,...)
static int _ReadByte ( ArchiveHandle AH)
static

Definition at line 504 of file pg_backup_directory.c.

References cfgetc(), lclContext::dataFH, and _archiveHandle::formatData.

Referenced by InitArchiveFmt_Directory().

505 {
506  lclContext *ctx = (lclContext *) AH->formatData;
507 
508  return cfgetc(ctx->dataFH);
509 }
int cfgetc(cfp *fp)
Definition: compress_io.c:620
static void _ReadExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 284 of file pg_backup_directory.c.

References lclTocEntry::filename, _tocEntry::formatData, free, NULL, pg_malloc0(), and ReadStr().

Referenced by InitArchiveFmt_Directory().

285 {
286  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
287 
288  if (tctx == NULL)
289  {
290  tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
291  te->formatData = (void *) tctx;
292  }
293 
294  tctx->filename = ReadStr(AH);
295  if (strlen(tctx->filename) == 0)
296  {
297  free(tctx->filename);
298  tctx->filename = NULL;
299  }
300 }
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
#define free(a)
Definition: header.h:60
#define NULL
Definition: c.h:226
char * ReadStr(ArchiveHandle *AH)
static void _ReopenArchive ( ArchiveHandle AH)
static

Definition at line 604 of file pg_backup_directory.c.

Referenced by InitArchiveFmt_Directory().

605 {
606  /*
607  * Our TOC is in memory, our data files are opened by each child anyway as
608  * they are separate. We support reopening the archive by just doing
609  * nothing.
610  */
611 }
static void _StartBlob ( ArchiveHandle AH,
TocEntry te,
Oid  oid 
)
static

Definition at line 645 of file pg_backup_directory.c.

References cfopen_write(), _archiveHandle::compression, lclContext::dataFH, lclContext::directory, exit_horribly(), _archiveHandle::formatData, MAXPGPATH, modulename, NULL, PG_BINARY_W, snprintf(), and strerror().

Referenced by InitArchiveFmt_Directory().

646 {
647  lclContext *ctx = (lclContext *) AH->formatData;
648  char fname[MAXPGPATH];
649 
650  snprintf(fname, MAXPGPATH, "%s/blob_%u.dat", ctx->directory, oid);
651 
652  ctx->dataFH = cfopen_write(fname, PG_BINARY_W, AH->compression);
653 
654  if (ctx->dataFH == NULL)
655  exit_horribly(modulename, "could not open output file \"%s\": %s\n",
656  fname, strerror(errno));
657 }
#define PG_BINARY_W
Definition: c.h:1041
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static const char * modulename
#define MAXPGPATH
#define NULL
Definition: c.h:226
void exit_horribly(const char *modulename, const char *fmt,...)
cfp * cfopen_write(const char *path, const char *mode, int compression)
Definition: compress_io.c:504
const char * strerror(int errnum)
Definition: strerror.c:19
static void _StartBlobs ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 625 of file pg_backup_directory.c.

References lclContext::blobsTocFH, cfopen_write(), exit_horribly(), _archiveHandle::formatData, MAXPGPATH, modulename, NULL, setFilePath(), and strerror().

Referenced by InitArchiveFmt_Directory().

626 {
627  lclContext *ctx = (lclContext *) AH->formatData;
628  char fname[MAXPGPATH];
629 
630  setFilePath(AH, fname, "blobs.toc");
631 
632  /* The blob TOC file is never compressed */
633  ctx->blobsTocFH = cfopen_write(fname, "ab", 0);
634  if (ctx->blobsTocFH == NULL)
635  exit_horribly(modulename, "could not open output file \"%s\": %s\n",
636  fname, strerror(errno));
637 }
static void setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
static const char * modulename
#define MAXPGPATH
#define NULL
Definition: c.h:226
void exit_horribly(const char *modulename, const char *fmt,...)
cfp * cfopen_write(const char *path, const char *mode, int compression)
Definition: compress_io.c:504
const char * strerror(int errnum)
Definition: strerror.c:19
static void _StartData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 325 of file pg_backup_directory.c.

References cfopen_write(), _archiveHandle::compression, exit_horribly(), lclTocEntry::filename, _archiveHandle::formatData, _tocEntry::formatData, MAXPGPATH, modulename, NULL, PG_BINARY_W, setFilePath(), and strerror().

Referenced by InitArchiveFmt_Directory().

326 {
327  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
328  lclContext *ctx = (lclContext *) AH->formatData;
329  char fname[MAXPGPATH];
330 
331  setFilePath(AH, fname, tctx->filename);
332 
333  ctx->dataFH = cfopen_write(fname, PG_BINARY_W, AH->compression);
334  if (ctx->dataFH == NULL)
335  exit_horribly(modulename, "could not open output file \"%s\": %s\n",
336  fname, strerror(errno));
337 }
static void setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
#define PG_BINARY_W
Definition: c.h:1041
static const char * modulename
#define MAXPGPATH
#define NULL
Definition: c.h:226
void exit_horribly(const char *modulename, const char *fmt,...)
cfp * cfopen_write(const char *path, const char *mode, int compression)
Definition: compress_io.c:504
const char * strerror(int errnum)
Definition: strerror.c:19
static int _WorkerJobDumpDirectory ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 755 of file pg_backup_directory.c.

References WriteDataChunksForTocEntry().

Referenced by InitArchiveFmt_Directory().

756 {
757  /*
758  * This function returns void. We either fail and die horribly or
759  * succeed... A failure will be detected by the parent when the child dies
760  * unexpectedly.
761  */
763 
764  return 0;
765 }
void WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te)
static int _WorkerJobRestoreDirectory ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 772 of file pg_backup_directory.c.

References parallel_restore().

Referenced by InitArchiveFmt_Directory().

773 {
774  return parallel_restore(AH, te);
775 }
int parallel_restore(ArchiveHandle *AH, TocEntry *te)
static void _WriteBuf ( ArchiveHandle AH,
const void *  buf,
size_t  len 
)
static

Definition at line 516 of file pg_backup_directory.c.

References cfwrite(), lclContext::dataFH, _archiveHandle::formatData, and WRITE_ERROR_EXIT.

Referenced by InitArchiveFmt_Directory().

517 {
518  lclContext *ctx = (lclContext *) AH->formatData;
519 
520  if (cfwrite(buf, len, ctx->dataFH) != len)
522 
523  return;
524 }
static char * buf
Definition: pg_test_fsync.c:65
int cfwrite(const void *ptr, int size, cfp *fp)
Definition: compress_io.c:609
#define WRITE_ERROR_EXIT
static int _WriteByte ( ArchiveHandle AH,
const int  i 
)
static

Definition at line 486 of file pg_backup_directory.c.

References cfwrite(), lclContext::dataFH, _archiveHandle::formatData, and WRITE_ERROR_EXIT.

Referenced by InitArchiveFmt_Directory().

487 {
488  unsigned char c = (unsigned char) i;
489  lclContext *ctx = (lclContext *) AH->formatData;
490 
491  if (cfwrite(&c, 1, ctx->dataFH) != 1)
493 
494  return 1;
495 }
char * c
int cfwrite(const void *ptr, int size, cfp *fp)
Definition: compress_io.c:609
#define WRITE_ERROR_EXIT
int i
static void _WriteData ( ArchiveHandle AH,
const void *  data,
size_t  dLen 
)
static

Definition at line 349 of file pg_backup_directory.c.

References cfwrite(), lclContext::dataFH, _archiveHandle::formatData, and WRITE_ERROR_EXIT.

Referenced by InitArchiveFmt_Directory().

350 {
351  lclContext *ctx = (lclContext *) AH->formatData;
352 
353  if (dLen > 0 && cfwrite(data, dLen, ctx->dataFH) != dLen)
355 
356  return;
357 }
int cfwrite(const void *ptr, int size, cfp *fp)
Definition: compress_io.c:609
#define WRITE_ERROR_EXIT
static void _WriteExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 263 of file pg_backup_directory.c.

References lclTocEntry::filename, _tocEntry::formatData, and WriteStr().

Referenced by InitArchiveFmt_Directory().

264 {
265  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
266 
267  /*
268  * A dumpable object has set tctx->filename, any other object has not.
269  * (see _ArchiveEntry).
270  */
271  if (tctx->filename)
272  WriteStr(AH, tctx->filename);
273  else
274  WriteStr(AH, "");
275 }
size_t WriteStr(ArchiveHandle *AH, const char *c)
void InitArchiveFmt_Directory ( ArchiveHandle AH)

Definition at line 109 of file pg_backup_directory.c.

References _ArchiveEntry(), _Clone(), _CloseArchive(), _DeClone(), _EndBlob(), _EndBlobs(), _EndData(), _PrintExtraToc(), _PrintTocData(), _ReadBuf(), _ReadByte(), _ReadExtraToc(), _ReopenArchive(), _StartBlob(), _StartBlobs(), _StartData(), _WorkerJobDumpDirectory(), _WorkerJobRestoreDirectory(), _WriteBuf(), _WriteByte(), _WriteData(), _WriteExtraToc(), archDirectory, _archiveHandle::ArchiveEntryPtr, archModeWrite, archTar, lclContext::blobsTocFH, cfclose(), cfopen_read(), _archiveHandle::ClonePtr, closedir(), _archiveHandle::ClosePtr, dirent::d_name, lclContext::dataFH, _archiveHandle::DeClonePtr, lclContext::directory, _archiveHandle::EndBlobPtr, _archiveHandle::EndBlobsPtr, _archiveHandle::EndDataPtr, exit_horribly(), _archiveHandle::format, _archiveHandle::formatData, _archiveHandle::fSpec, _archiveHandle::lo_buf, _archiveHandle::lo_buf_size, LOBBUFSIZE, MAXPGPATH, mkdir, _archiveHandle::mode, modulename, NULL, opendir(), PG_BINARY_R, pg_malloc(), pg_malloc0(), _archiveHandle::PrintExtraTocPtr, _archiveHandle::PrintTocDataPtr, _archiveHandle::ReadBufPtr, _archiveHandle::ReadBytePtr, readdir(), _archiveHandle::ReadExtraTocPtr, ReadHead(), ReadToc(), _archiveHandle::ReopenPtr, setFilePath(), _archiveHandle::StartBlobPtr, _archiveHandle::StartBlobsPtr, _archiveHandle::StartDataPtr, strerror(), _archiveHandle::WorkerJobDumpPtr, _archiveHandle::WorkerJobRestorePtr, _archiveHandle::WriteBufPtr, _archiveHandle::WriteBytePtr, _archiveHandle::WriteDataPtr, and _archiveHandle::WriteExtraTocPtr.

Referenced by _allocAH().

110 {
111  lclContext *ctx;
112 
113  /* Assuming static functions, this can be copied for each format. */
115  AH->StartDataPtr = _StartData;
116  AH->WriteDataPtr = _WriteData;
117  AH->EndDataPtr = _EndData;
118  AH->WriteBytePtr = _WriteByte;
119  AH->ReadBytePtr = _ReadByte;
120  AH->WriteBufPtr = _WriteBuf;
121  AH->ReadBufPtr = _ReadBuf;
122  AH->ClosePtr = _CloseArchive;
128 
130  AH->StartBlobPtr = _StartBlob;
131  AH->EndBlobPtr = _EndBlob;
132  AH->EndBlobsPtr = _EndBlobs;
133 
134  AH->ClonePtr = _Clone;
135  AH->DeClonePtr = _DeClone;
136 
139 
140  /* Set up our private context */
141  ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
142  AH->formatData = (void *) ctx;
143 
144  ctx->dataFH = NULL;
145  ctx->blobsTocFH = NULL;
146 
147  /* Initialize LO buffering */
148  AH->lo_buf_size = LOBBUFSIZE;
149  AH->lo_buf = (void *) pg_malloc(LOBBUFSIZE);
150 
151  /*
152  * Now open the TOC file
153  */
154 
155  if (!AH->fSpec || strcmp(AH->fSpec, "") == 0)
156  exit_horribly(modulename, "no output directory specified\n");
157 
158  ctx->directory = AH->fSpec;
159 
160  if (AH->mode == archModeWrite)
161  {
162  struct stat st;
163  bool is_empty = false;
164 
165  /* we accept an empty existing directory */
166  if (stat(ctx->directory, &st) == 0 && S_ISDIR(st.st_mode))
167  {
168  DIR *dir = opendir(ctx->directory);
169 
170  if (dir)
171  {
172  struct dirent *d;
173 
174  is_empty = true;
175  while (errno = 0, (d = readdir(dir)))
176  {
177  if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0)
178  {
179  is_empty = false;
180  break;
181  }
182  }
183 
184  if (errno)
185  exit_horribly(modulename, "could not read directory \"%s\": %s\n",
186  ctx->directory, strerror(errno));
187 
188  if (closedir(dir))
189  exit_horribly(modulename, "could not close directory \"%s\": %s\n",
190  ctx->directory, strerror(errno));
191  }
192  }
193 
194  if (!is_empty && mkdir(ctx->directory, 0700) < 0)
195  exit_horribly(modulename, "could not create directory \"%s\": %s\n",
196  ctx->directory, strerror(errno));
197  }
198  else
199  { /* Read Mode */
200  char fname[MAXPGPATH];
201  cfp *tocFH;
202 
203  setFilePath(AH, fname, "toc.dat");
204 
205  tocFH = cfopen_read(fname, PG_BINARY_R);
206  if (tocFH == NULL)
208  "could not open input file \"%s\": %s\n",
209  fname, strerror(errno));
210 
211  ctx->dataFH = tocFH;
212 
213  /*
214  * The TOC of a directory format dump shares the format code of the
215  * tar format.
216  */
217  AH->format = archTar;
218  ReadHead(AH);
219  AH->format = archDirectory;
220  ReadToc(AH);
221 
222  /* Nothing else in the file, so close it again... */
223  if (cfclose(tocFH) != 0)
224  exit_horribly(modulename, "could not close TOC file: %s\n",
225  strerror(errno));
226  ctx->dataFH = NULL;
227  }
228 }
static void _StartData(ArchiveHandle *AH, TocEntry *te)
void ReadToc(ArchiveHandle *AH)
StartBlobsPtr StartBlobsPtr
void ReadHead(ArchiveHandle *AH)
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
PrintTocDataPtr PrintTocDataPtr
static int _WorkerJobRestoreDirectory(ArchiveHandle *AH, TocEntry *te)
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
static int _WriteByte(ArchiveHandle *AH, const int i)
static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
#define mkdir(a, b)
Definition: win32.h:65
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te)
WorkerJobDumpPtr WorkerJobDumpPtr
static void setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
WriteBufPtr WriteBufPtr
WriteExtraTocPtr WriteExtraTocPtr
int closedir(DIR *)
Definition: dirent.c:113
static void _EndData(ArchiveHandle *AH, TocEntry *te)
static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
static const char * modulename
int cfclose(cfp *fp)
Definition: compress_io.c:661
Definition: dirent.h:9
#define PG_BINARY_R
Definition: c.h:1040
static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te)
Definition: dirent.c:25
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
#define MAXPGPATH
PrintExtraTocPtr PrintExtraTocPtr
DIR * opendir(const char *)
Definition: dirent.c:33
WriteBytePtr WriteBytePtr
static void _Clone(ArchiveHandle *AH)
WorkerJobRestorePtr WorkerJobRestorePtr
ArchiveEntryPtr ArchiveEntryPtr
ReadBytePtr ReadBytePtr
ArchiveFormat format
static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
static void _DeClone(ArchiveHandle *AH)
static int _ReadByte(ArchiveHandle *)
#define NULL
Definition: c.h:226
WriteDataPtr WriteDataPtr
static void _StartBlobs(ArchiveHandle *AH, TocEntry *te)
struct dirent * readdir(DIR *)
Definition: dirent.c:78
void exit_horribly(const char *modulename, const char *fmt,...)
static void _CloseArchive(ArchiveHandle *AH)
StartDataPtr StartDataPtr
EndBlobsPtr EndBlobsPtr
cfp * cfopen_read(const char *path, const char *mode)
Definition: compress_io.c:467
const char * strerror(int errnum)
Definition: strerror.c:19
static int _WorkerJobDumpDirectory(ArchiveHandle *AH, TocEntry *te)
static void _ReopenArchive(ArchiveHandle *AH)
char d_name[MAX_PATH]
Definition: dirent.h:14
static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te)
ReadExtraTocPtr ReadExtraTocPtr
static void _EndBlobs(ArchiveHandle *AH, TocEntry *te)
#define LOBBUFSIZE
StartBlobPtr StartBlobPtr
static void setFilePath ( ArchiveHandle AH,
char *  buf,
const char *  relativeFilename 
)
static

Definition at line 702 of file pg_backup_directory.c.

References lclContext::directory, exit_horribly(), _archiveHandle::formatData, MAXPGPATH, and modulename.

Referenced by _CloseArchive(), _LoadBlobs(), _PrintTocData(), _StartBlobs(), _StartData(), and InitArchiveFmt_Directory().

703 {
704  lclContext *ctx = (lclContext *) AH->formatData;
705  char *dname;
706 
707  dname = ctx->directory;
708 
709  if (strlen(dname) + 1 + strlen(relativeFilename) + 1 > MAXPGPATH)
710  exit_horribly(modulename, "file name too long: \"%s\"\n", dname);
711 
712  strcpy(buf, dname);
713  strcat(buf, "/");
714  strcat(buf, relativeFilename);
715 }
static const char * modulename
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:65
void exit_horribly(const char *modulename, const char *fmt,...)

Variable Documentation

const char* modulename = gettext_noop("directory archiver")
static