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 "common/file_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 237 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().

238 {
239  lclTocEntry *tctx;
240  char fn[MAXPGPATH];
241 
242  tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
243  if (te->dataDumper)
244  {
245  snprintf(fn, MAXPGPATH, "%d.dat", te->dumpId);
246  tctx->filename = pg_strdup(fn);
247  }
248  else if (strcmp(te->desc, "BLOBS") == 0)
249  tctx->filename = pg_strdup("blobs.toc");
250  else
251  tctx->filename = NULL;
252 
253  te->formatData = (void *) tctx;
254 }
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:229
static void _Clone ( ArchiveHandle AH)
static

Definition at line 729 of file pg_backup_directory.c.

References _archiveHandle::formatData, and pg_malloc().

Referenced by InitArchiveFmt_Directory().

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

Definition at line 561 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

562 {
563  lclContext *ctx = (lclContext *) AH->formatData;
564 
565  if (AH->mode == archModeWrite)
566  {
567  cfp *tocFH;
568  char fname[MAXPGPATH];
569 
570  setFilePath(AH, fname, "toc.dat");
571 
572  /* this will actually fork the processes for a parallel backup */
573  ctx->pstate = ParallelBackupStart(AH);
574 
575  /* The TOC is always created uncompressed */
576  tocFH = cfopen_write(fname, PG_BINARY_W, 0);
577  if (tocFH == NULL)
578  exit_horribly(modulename, "could not open output file \"%s\": %s\n",
579  fname, strerror(errno));
580  ctx->dataFH = tocFH;
581 
582  /*
583  * Write 'tar' in the format field of the toc.dat file. The directory
584  * is compatible with 'tar', so there's no point having a different
585  * format code for it.
586  */
587  AH->format = archTar;
588  WriteHead(AH);
589  AH->format = archDirectory;
590  WriteToc(AH);
591  if (cfclose(tocFH) != 0)
592  exit_horribly(modulename, "could not close TOC file: %s\n",
593  strerror(errno));
594  WriteDataChunks(AH, ctx->pstate);
595 
596  ParallelBackupEnd(AH, ctx->pstate);
597 
598  /*
599  * In directory mode, there is no need to sync all the entries
600  * individually. Just recurse once through all the files generated.
601  */
602  if (AH->dosync)
604  }
605  AH->FH = NULL;
606 }
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
const char * progname
Definition: pg_standby.c:37
void fsync_dir_recurse(const char *dir, const char *progname)
Definition: file_utils.c:124
#define MAXPGPATH
ParallelState * ParallelBackupStart(ArchiveHandle *AH)
Definition: parallel.c:909
void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate)
Definition: parallel.c:1079
ArchiveFormat format
#define NULL
Definition: c.h:229
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 751 of file pg_backup_directory.c.

References _archiveHandle::formatData, and free.

Referenced by InitArchiveFmt_Directory().

752 {
753  lclContext *ctx = (lclContext *) AH->formatData;
754 
755  free(ctx);
756 }
#define free(a)
Definition: header.h:65
static void _EndBlob ( ArchiveHandle AH,
TocEntry te,
Oid  oid 
)
static

Definition at line 673 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().

674 {
675  lclContext *ctx = (lclContext *) AH->formatData;
676  char buf[50];
677  int len;
678 
679  /* Close the BLOB data file itself */
680  cfclose(ctx->dataFH);
681  ctx->dataFH = NULL;
682 
683  /* register the blob in blobs.toc */
684  len = snprintf(buf, sizeof(buf), "%u blob_%u.dat\n", oid, oid);
685  if (cfwrite(buf, len, ctx->blobsTocFH) != len)
686  exit_horribly(modulename, "could not write to blobs TOC file\n");
687 }
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:66
#define NULL
Definition: c.h:229
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 695 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

696 {
697  lclContext *ctx = (lclContext *) AH->formatData;
698 
699  cfclose(ctx->blobsTocFH);
700  ctx->blobsTocFH = NULL;
701 }
int cfclose(cfp *fp)
Definition: compress_io.c:661
#define NULL
Definition: c.h:229
static void _EndData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 367 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

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

Definition at line 434 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().

435 {
436  Oid oid;
437  lclContext *ctx = (lclContext *) AH->formatData;
438  char fname[MAXPGPATH];
439  char line[MAXPGPATH];
440 
441  StartRestoreBlobs(AH);
442 
443  setFilePath(AH, fname, "blobs.toc");
444 
445  ctx->blobsTocFH = cfopen_read(fname, PG_BINARY_R);
446 
447  if (ctx->blobsTocFH == NULL)
448  exit_horribly(modulename, "could not open large object TOC file \"%s\" for input: %s\n",
449  fname, strerror(errno));
450 
451  /* Read the blobs TOC file line-by-line, and process each blob */
452  while ((cfgets(ctx->blobsTocFH, line, MAXPGPATH)) != NULL)
453  {
454  char fname[MAXPGPATH];
455  char path[MAXPGPATH];
456 
457  /* Can't overflow because line and fname are the same length. */
458  if (sscanf(line, "%u %s\n", &oid, fname) != 2)
459  exit_horribly(modulename, "invalid line in large object TOC file \"%s\": \"%s\"\n",
460  fname, line);
461 
462  StartRestoreBlob(AH, oid, AH->public.ropt->dropSchema);
463  snprintf(path, MAXPGPATH, "%s/%s", ctx->directory, fname);
464  _PrintFileData(AH, path);
465  EndRestoreBlob(AH, oid);
466  }
467  if (!cfeof(ctx->blobsTocFH))
468  exit_horribly(modulename, "error reading large object TOC file \"%s\"\n",
469  fname);
470 
471  if (cfclose(ctx->blobsTocFH) != 0)
472  exit_horribly(modulename, "could not close large object TOC file \"%s\": %s\n",
473  fname, strerror(errno));
474 
475  ctx->blobsTocFH = NULL;
476 
477  EndRestoreBlobs(AH);
478 }
RestoreOptions * ropt
Definition: pg_backup.h:181
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:229
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 308 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

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

Definition at line 381 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().

382 {
383  size_t cnt;
384  char *buf;
385  size_t buflen;
386  cfp *cfp;
387 
388  if (!filename)
389  return;
390 
392 
393  if (!cfp)
394  exit_horribly(modulename, "could not open input file \"%s\": %s\n",
395  filename, strerror(errno));
396 
397  buf = pg_malloc(ZLIB_OUT_SIZE);
398  buflen = ZLIB_OUT_SIZE;
399 
400  while ((cnt = cfread(buf, buflen, cfp)))
401  {
402  ahwrite(buf, 1, cnt, AH);
403  }
404 
405  free(buf);
406  if (cfclose(cfp) !=0)
407  exit_horribly(modulename, "could not close data file: %s\n",
408  strerror(errno));
409 }
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:66
#define free(a)
Definition: header.h:65
#define ZLIB_OUT_SIZE
Definition: walmethods.c:31
void exit_horribly(const char *modulename, const char *fmt,...)
static char * filename
Definition: pg_dumpall.c:89
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 415 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

416 {
417  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
418 
419  if (!tctx->filename)
420  return;
421 
422  if (strcmp(te->desc, "BLOBS") == 0)
423  _LoadBlobs(AH);
424  else
425  {
426  char fname[MAXPGPATH];
427 
428  setFilePath(AH, fname, tctx->filename);
429  _PrintFileData(AH, fname);
430  }
431 }
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 533 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

534 {
535  lclContext *ctx = (lclContext *) AH->formatData;
536 
537  /*
538  * If there was an I/O error, we already exited in cfread(), so here we
539  * exit on short reads.
540  */
541  if (cfread(buf, len, ctx->dataFH) != len)
543  "could not read from input file: end of file\n");
544 
545  return;
546 }
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:66
void exit_horribly(const char *modulename, const char *fmt,...)
static int _ReadByte ( ArchiveHandle AH)
static

Definition at line 505 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

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

Definition at line 285 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

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

Definition at line 612 of file pg_backup_directory.c.

Referenced by InitArchiveFmt_Directory().

613 {
614  /*
615  * Our TOC is in memory, our data files are opened by each child anyway as
616  * they are separate. We support reopening the archive by just doing
617  * nothing.
618  */
619 }
static void _StartBlob ( ArchiveHandle AH,
TocEntry te,
Oid  oid 
)
static

Definition at line 653 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().

654 {
655  lclContext *ctx = (lclContext *) AH->formatData;
656  char fname[MAXPGPATH];
657 
658  snprintf(fname, MAXPGPATH, "%s/blob_%u.dat", ctx->directory, oid);
659 
660  ctx->dataFH = cfopen_write(fname, PG_BINARY_W, AH->compression);
661 
662  if (ctx->dataFH == NULL)
663  exit_horribly(modulename, "could not open output file \"%s\": %s\n",
664  fname, strerror(errno));
665 }
#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:229
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 633 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().

634 {
635  lclContext *ctx = (lclContext *) AH->formatData;
636  char fname[MAXPGPATH];
637 
638  setFilePath(AH, fname, "blobs.toc");
639 
640  /* The blob TOC file is never compressed */
641  ctx->blobsTocFH = cfopen_write(fname, "ab", 0);
642  if (ctx->blobsTocFH == NULL)
643  exit_horribly(modulename, "could not open output file \"%s\": %s\n",
644  fname, strerror(errno));
645 }
static void setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
static const char * modulename
#define MAXPGPATH
#define NULL
Definition: c.h:229
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 326 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().

327 {
328  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
329  lclContext *ctx = (lclContext *) AH->formatData;
330  char fname[MAXPGPATH];
331 
332  setFilePath(AH, fname, tctx->filename);
333 
334  ctx->dataFH = cfopen_write(fname, PG_BINARY_W, AH->compression);
335  if (ctx->dataFH == NULL)
336  exit_horribly(modulename, "could not open output file \"%s\": %s\n",
337  fname, strerror(errno));
338 }
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:229
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 763 of file pg_backup_directory.c.

References WriteDataChunksForTocEntry().

Referenced by InitArchiveFmt_Directory().

764 {
765  /*
766  * This function returns void. We either fail and die horribly or
767  * succeed... A failure will be detected by the parent when the child dies
768  * unexpectedly.
769  */
771 
772  return 0;
773 }
void WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te)
static int _WorkerJobRestoreDirectory ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 780 of file pg_backup_directory.c.

References parallel_restore().

Referenced by InitArchiveFmt_Directory().

781 {
782  return parallel_restore(AH, te);
783 }
int parallel_restore(ArchiveHandle *AH, TocEntry *te)
static void _WriteBuf ( ArchiveHandle AH,
const void *  buf,
size_t  len 
)
static

Definition at line 517 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

518 {
519  lclContext *ctx = (lclContext *) AH->formatData;
520 
521  if (cfwrite(buf, len, ctx->dataFH) != len)
523 
524  return;
525 }
static char * buf
Definition: pg_test_fsync.c:66
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 487 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

488 {
489  unsigned char c = (unsigned char) i;
490  lclContext *ctx = (lclContext *) AH->formatData;
491 
492  if (cfwrite(&c, 1, ctx->dataFH) != 1)
494 
495  return 1;
496 }
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 350 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

351 {
352  lclContext *ctx = (lclContext *) AH->formatData;
353 
354  if (dLen > 0 && cfwrite(data, dLen, ctx->dataFH) != dLen)
356 
357  return;
358 }
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 264 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

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

Definition at line 110 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().

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

Definition at line 710 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().

711 {
712  lclContext *ctx = (lclContext *) AH->formatData;
713  char *dname;
714 
715  dname = ctx->directory;
716 
717  if (strlen(dname) + 1 + strlen(relativeFilename) + 1 > MAXPGPATH)
718  exit_horribly(modulename, "file name too long: \"%s\"\n", dname);
719 
720  strcpy(buf, dname);
721  strcat(buf, "/");
722  strcat(buf, relativeFilename);
723 }
static const char * modulename
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:66
void exit_horribly(const char *modulename, const char *fmt,...)

Variable Documentation

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