PostgreSQL Source Code  git master
pg_backup_directory.c File Reference
#include "postgres_fe.h"
#include <dirent.h>
#include <sys/stat.h>
#include "common/file_utils.h"
#include "compress_io.h"
#include "parallel.h"
#include "pg_backup_utils.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 _PrepParallelRestore (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)
 

Function Documentation

◆ _ArchiveEntry()

static void _ArchiveEntry ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 233 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

234 {
235  lclTocEntry *tctx;
236  char fn[MAXPGPATH];
237 
238  tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
239  if (strcmp(te->desc, "BLOBS") == 0)
240  tctx->filename = pg_strdup("blobs.toc");
241  else if (te->dataDumper)
242  {
243  snprintf(fn, MAXPGPATH, "%d.dat", te->dumpId);
244  tctx->filename = pg_strdup(fn);
245  }
246  else
247  tctx->filename = NULL;
248 
249  te->formatData = (void *) tctx;
250 }
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 snprintf
Definition: port.h:193

◆ _Clone()

static void _Clone ( ArchiveHandle AH)
static

Definition at line 776 of file pg_backup_directory.c.

References _archiveHandle::formatData, and pg_malloc().

Referenced by InitArchiveFmt_Directory().

777 {
778  lclContext *ctx = (lclContext *) AH->formatData;
779 
780  AH->formatData = (lclContext *) pg_malloc(sizeof(lclContext));
781  memcpy(AH->formatData, ctx, sizeof(lclContext));
782  ctx = (lclContext *) AH->formatData;
783 
784  /*
785  * Note: we do not make a local lo_buf because we expect at most one BLOBS
786  * entry per archive, so no parallelism is possible. Likewise,
787  * TOC-entry-local state isn't an issue because any one TOC entry is
788  * touched by just one worker child.
789  */
790 
791  /*
792  * We also don't copy the ParallelState pointer (pstate), only the master
793  * process ever writes to it.
794  */
795 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47

◆ _CloseArchive()

static void _CloseArchive ( ArchiveHandle AH)
static

Definition at line 550 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

551 {
552  lclContext *ctx = (lclContext *) AH->formatData;
553 
554  if (AH->mode == archModeWrite)
555  {
556  cfp *tocFH;
557  char fname[MAXPGPATH];
558 
559  setFilePath(AH, fname, "toc.dat");
560 
561  /* this will actually fork the processes for a parallel backup */
562  ctx->pstate = ParallelBackupStart(AH);
563 
564  /* The TOC is always created uncompressed */
565  tocFH = cfopen_write(fname, PG_BINARY_W, 0);
566  if (tocFH == NULL)
567  fatal("could not open output file \"%s\": %m", fname);
568  ctx->dataFH = tocFH;
569 
570  /*
571  * Write 'tar' in the format field of the toc.dat file. The directory
572  * is compatible with 'tar', so there's no point having a different
573  * format code for it.
574  */
575  AH->format = archTar;
576  WriteHead(AH);
577  AH->format = archDirectory;
578  WriteToc(AH);
579  if (cfclose(tocFH) != 0)
580  fatal("could not close TOC file: %m");
581  WriteDataChunks(AH, ctx->pstate);
582 
583  ParallelBackupEnd(AH, ctx->pstate);
584 
585  /*
586  * In directory mode, there is no need to sync all the entries
587  * individually. Just recurse once through all the files generated.
588  */
589  if (AH->dosync)
591  }
592  AH->FH = NULL;
593 }
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:1237
void WriteToc(ArchiveHandle *AH)
int cfclose(cfp *fp)
Definition: compress_io.c:649
void fsync_dir_recurse(const char *dir)
Definition: file_utils.c:120
#define MAXPGPATH
ParallelState * ParallelBackupStart(ArchiveHandle *AH)
Definition: parallel.c:916
void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate)
Definition: parallel.c:1078
ArchiveFormat format
#define fatal(...)
void WriteHead(ArchiveHandle *AH)
cfp * cfopen_write(const char *path, const char *mode, int compression)
Definition: compress_io.c:489

◆ _DeClone()

static void _DeClone ( ArchiveHandle AH)
static

Definition at line 798 of file pg_backup_directory.c.

References _archiveHandle::formatData, and free.

Referenced by InitArchiveFmt_Directory().

799 {
800  lclContext *ctx = (lclContext *) AH->formatData;
801 
802  free(ctx);
803 }
#define free(a)
Definition: header.h:65

◆ _EndBlob()

static void _EndBlob ( ArchiveHandle AH,
TocEntry te,
Oid  oid 
)
static

Definition at line 658 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

659 {
660  lclContext *ctx = (lclContext *) AH->formatData;
661  char buf[50];
662  int len;
663 
664  /* Close the BLOB data file itself */
665  cfclose(ctx->dataFH);
666  ctx->dataFH = NULL;
667 
668  /* register the blob in blobs.toc */
669  len = snprintf(buf, sizeof(buf), "%u blob_%u.dat\n", oid, oid);
670  if (cfwrite(buf, len, ctx->blobsTocFH) != len)
671  fatal("could not write to blobs TOC file");
672 }
int cfclose(cfp *fp)
Definition: compress_io.c:649
static char * buf
Definition: pg_test_fsync.c:67
int cfwrite(const void *ptr, int size, cfp *fp)
Definition: compress_io.c:599
#define fatal(...)
#define snprintf
Definition: port.h:193

◆ _EndBlobs()

static void _EndBlobs ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 680 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

681 {
682  lclContext *ctx = (lclContext *) AH->formatData;
683 
684  cfclose(ctx->blobsTocFH);
685  ctx->blobsTocFH = NULL;
686 }
int cfclose(cfp *fp)
Definition: compress_io.c:649

◆ _EndData()

static void _EndData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 361 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

362 {
363  lclContext *ctx = (lclContext *) AH->formatData;
364 
365  /* Close the file */
366  cfclose(ctx->dataFH);
367 
368  ctx->dataFH = NULL;
369 }
int cfclose(cfp *fp)
Definition: compress_io.c:649

◆ _LoadBlobs()

static void _LoadBlobs ( ArchiveHandle AH)
static

Definition at line 426 of file pg_backup_directory.c.

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

Referenced by _PrintTocData().

427 {
428  Oid oid;
429  lclContext *ctx = (lclContext *) AH->formatData;
430  char fname[MAXPGPATH];
431  char line[MAXPGPATH];
432 
433  StartRestoreBlobs(AH);
434 
435  setFilePath(AH, fname, "blobs.toc");
436 
437  ctx->blobsTocFH = cfopen_read(fname, PG_BINARY_R);
438 
439  if (ctx->blobsTocFH == NULL)
440  fatal("could not open large object TOC file \"%s\" for input: %m",
441  fname);
442 
443  /* Read the blobs TOC file line-by-line, and process each blob */
444  while ((cfgets(ctx->blobsTocFH, line, MAXPGPATH)) != NULL)
445  {
446  char fname[MAXPGPATH];
447  char path[MAXPGPATH];
448 
449  /* Can't overflow because line and fname are the same length. */
450  if (sscanf(line, "%u %s\n", &oid, fname) != 2)
451  fatal("invalid line in large object TOC file \"%s\": \"%s\"",
452  fname, line);
453 
454  StartRestoreBlob(AH, oid, AH->public.ropt->dropSchema);
455  snprintf(path, MAXPGPATH, "%s/%s", ctx->directory, fname);
456  _PrintFileData(AH, path);
457  EndRestoreBlob(AH, oid);
458  }
459  if (!cfeof(ctx->blobsTocFH))
460  fatal("error reading large object TOC file \"%s\"",
461  fname);
462 
463  if (cfclose(ctx->blobsTocFH) != 0)
464  fatal("could not close large object TOC file \"%s\": %m",
465  fname);
466 
467  ctx->blobsTocFH = NULL;
468 
469  EndRestoreBlobs(AH);
470 }
RestoreOptions * ropt
Definition: pg_backup.h:183
int cfeof(cfp *fp)
Definition: compress_io.c:676
static void setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
unsigned int Oid
Definition: postgres_ext.h:31
int cfclose(cfp *fp)
Definition: compress_io.c:649
#define PG_BINARY_R
Definition: c.h:1236
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:638
void EndRestoreBlobs(ArchiveHandle *AH)
static void _PrintFileData(ArchiveHandle *AH, char *filename)
#define fatal(...)
void EndRestoreBlob(ArchiveHandle *AH, Oid oid)
cfp * cfopen_read(const char *path, const char *mode)
Definition: compress_io.c:452
#define snprintf
Definition: port.h:193

◆ _PrepParallelRestore()

static void _PrepParallelRestore ( ArchiveHandle AH)
static

Definition at line 721 of file pg_backup_directory.c.

References _tocEntry::dataLength, _tocEntry::desc, lclTocEntry::filename, _tocEntry::formatData, MAXPGPATH, _tocEntry::next, REQ_DATA, _tocEntry::reqs, setFilePath(), stat, strlcat(), and _archiveHandle::toc.

Referenced by InitArchiveFmt_Directory().

722 {
723  TocEntry *te;
724 
725  for (te = AH->toc->next; te != AH->toc; te = te->next)
726  {
727  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
728  char fname[MAXPGPATH];
729  struct stat st;
730 
731  /*
732  * A dumpable object has set tctx->filename, any other object has not.
733  * (see _ArchiveEntry).
734  */
735  if (tctx->filename == NULL)
736  continue;
737 
738  /* We may ignore items not due to be restored */
739  if ((te->reqs & REQ_DATA) == 0)
740  continue;
741 
742  /*
743  * Stat the file and, if successful, put its size in dataLength. When
744  * using compression, the physical file size might not be a very good
745  * guide to the amount of work involved in restoring the file, but we
746  * only need an approximate indicator of that.
747  */
748  setFilePath(AH, fname, tctx->filename);
749 
750  if (stat(fname, &st) == 0)
751  te->dataLength = st.st_size;
752  else
753  {
754  /* It might be compressed */
755  strlcat(fname, ".gz", sizeof(fname));
756  if (stat(fname, &st) == 0)
757  te->dataLength = st.st_size;
758  }
759 
760  /*
761  * If this is the BLOBS entry, what we stat'd was blobs.toc, which
762  * most likely is a lot smaller than the actual blob data. We don't
763  * have a cheap way to estimate how much smaller, but fortunately it
764  * doesn't matter too much as long as we get the blobs processed
765  * reasonably early. Arbitrarily scale up by a factor of 1K.
766  */
767  if (strcmp(te->desc, "BLOBS") == 0)
768  te->dataLength *= 1024;
769  }
770 }
struct _tocEntry * next
static void setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
#define MAXPGPATH
struct _tocEntry * toc
pgoff_t dataLength
#define stat(a, b)
Definition: win32_port.h:255
size_t strlcat(char *dst, const char *src, size_t siz)
Definition: strlcat.c:33

◆ _PrintExtraToc()

static void _PrintExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 304 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

305 {
306  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
307 
308  if (AH->public.verbose && tctx->filename)
309  ahprintf(AH, "-- File: %s\n", tctx->filename);
310 }
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
int verbose
Definition: pg_backup.h:185

◆ _PrintFileData()

static void _PrintFileData ( ArchiveHandle AH,
char *  filename 
)
static

Definition at line 375 of file pg_backup_directory.c.

References ahwrite(), buf, cfclose(), cfopen_read(), cfread(), fatal, free, PG_BINARY_R, pg_malloc(), and ZLIB_OUT_SIZE.

Referenced by _LoadBlobs(), and _PrintTocData().

376 {
377  size_t cnt;
378  char *buf;
379  size_t buflen;
380  cfp *cfp;
381 
382  if (!filename)
383  return;
384 
386 
387  if (!cfp)
388  fatal("could not open input file \"%s\": %m", filename);
389 
390  buf = pg_malloc(ZLIB_OUT_SIZE);
391  buflen = ZLIB_OUT_SIZE;
392 
393  while ((cnt = cfread(buf, buflen, cfp)))
394  {
395  ahwrite(buf, 1, cnt, AH);
396  }
397 
398  free(buf);
399  if (cfclose(cfp) != 0)
400  fatal("could not close data file \"%s\": %m", filename);
401 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int cfread(void *ptr, int size, cfp *fp)
Definition: compress_io.c:568
int cfclose(cfp *fp)
Definition: compress_io.c:649
#define PG_BINARY_R
Definition: c.h:1236
struct cfp cfp
Definition: compress_io.h:57
static char * buf
Definition: pg_test_fsync.c:67
#define free(a)
Definition: header.h:65
#define fatal(...)
#define ZLIB_OUT_SIZE
Definition: walmethods.c:31
static char * filename
Definition: pg_dumpall.c:90
cfp * cfopen_read(const char *path, const char *mode)
Definition: compress_io.c:452
void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)

◆ _PrintTocData()

static void _PrintTocData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 407 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

408 {
409  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
410 
411  if (!tctx->filename)
412  return;
413 
414  if (strcmp(te->desc, "BLOBS") == 0)
415  _LoadBlobs(AH);
416  else
417  {
418  char fname[MAXPGPATH];
419 
420  setFilePath(AH, fname, tctx->filename);
421  _PrintFileData(AH, fname);
422  }
423 }
static void setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
#define MAXPGPATH
static void _PrintFileData(ArchiveHandle *AH, char *filename)
static void _LoadBlobs(ArchiveHandle *AH)

◆ _ReadBuf()

static void _ReadBuf ( ArchiveHandle AH,
void *  buf,
size_t  len 
)
static

Definition at line 525 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

526 {
527  lclContext *ctx = (lclContext *) AH->formatData;
528 
529  /*
530  * If there was an I/O error, we already exited in cfread(), so here we
531  * exit on short reads.
532  */
533  if (cfread(buf, len, ctx->dataFH) != len)
534  fatal("could not read from input file: end of file");
535 }
int cfread(void *ptr, int size, cfp *fp)
Definition: compress_io.c:568
static char * buf
Definition: pg_test_fsync.c:67
#define fatal(...)

◆ _ReadByte()

static int _ReadByte ( ArchiveHandle AH)
static

Definition at line 498 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

499 {
500  lclContext *ctx = (lclContext *) AH->formatData;
501 
502  return cfgetc(ctx->dataFH);
503 }
int cfgetc(cfp *fp)
Definition: compress_io.c:610

◆ _ReadExtraToc()

static void _ReadExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 281 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

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

◆ _ReopenArchive()

static void _ReopenArchive ( ArchiveHandle AH)
static

Definition at line 599 of file pg_backup_directory.c.

Referenced by InitArchiveFmt_Directory().

600 {
601  /*
602  * Our TOC is in memory, our data files are opened by each child anyway as
603  * they are separate. We support reopening the archive by just doing
604  * nothing.
605  */
606 }

◆ _StartBlob()

static void _StartBlob ( ArchiveHandle AH,
TocEntry te,
Oid  oid 
)
static

Definition at line 639 of file pg_backup_directory.c.

References cfopen_write(), _archiveHandle::compression, lclContext::dataFH, lclContext::directory, fatal, _archiveHandle::formatData, MAXPGPATH, PG_BINARY_W, and snprintf.

Referenced by InitArchiveFmt_Directory().

640 {
641  lclContext *ctx = (lclContext *) AH->formatData;
642  char fname[MAXPGPATH];
643 
644  snprintf(fname, MAXPGPATH, "%s/blob_%u.dat", ctx->directory, oid);
645 
646  ctx->dataFH = cfopen_write(fname, PG_BINARY_W, AH->compression);
647 
648  if (ctx->dataFH == NULL)
649  fatal("could not open output file \"%s\": %m", fname);
650 }
#define PG_BINARY_W
Definition: c.h:1237
#define MAXPGPATH
#define fatal(...)
cfp * cfopen_write(const char *path, const char *mode, int compression)
Definition: compress_io.c:489
#define snprintf
Definition: port.h:193

◆ _StartBlobs()

static void _StartBlobs ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 620 of file pg_backup_directory.c.

References lclContext::blobsTocFH, cfopen_write(), fatal, _archiveHandle::formatData, MAXPGPATH, and setFilePath().

Referenced by InitArchiveFmt_Directory().

621 {
622  lclContext *ctx = (lclContext *) AH->formatData;
623  char fname[MAXPGPATH];
624 
625  setFilePath(AH, fname, "blobs.toc");
626 
627  /* The blob TOC file is never compressed */
628  ctx->blobsTocFH = cfopen_write(fname, "ab", 0);
629  if (ctx->blobsTocFH == NULL)
630  fatal("could not open output file \"%s\": %m", fname);
631 }
static void setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
#define MAXPGPATH
#define fatal(...)
cfp * cfopen_write(const char *path, const char *mode, int compression)
Definition: compress_io.c:489

◆ _StartData()

static void _StartData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 322 of file pg_backup_directory.c.

References cfopen_write(), _archiveHandle::compression, fatal, lclTocEntry::filename, _archiveHandle::formatData, _tocEntry::formatData, MAXPGPATH, PG_BINARY_W, and setFilePath().

Referenced by InitArchiveFmt_Directory().

323 {
324  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
325  lclContext *ctx = (lclContext *) AH->formatData;
326  char fname[MAXPGPATH];
327 
328  setFilePath(AH, fname, tctx->filename);
329 
330  ctx->dataFH = cfopen_write(fname, PG_BINARY_W, AH->compression);
331  if (ctx->dataFH == NULL)
332  fatal("could not open output file \"%s\": %m", fname);
333 }
static void setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
#define PG_BINARY_W
Definition: c.h:1237
#define MAXPGPATH
#define fatal(...)
cfp * cfopen_write(const char *path, const char *mode, int compression)
Definition: compress_io.c:489

◆ _WorkerJobDumpDirectory()

static int _WorkerJobDumpDirectory ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 810 of file pg_backup_directory.c.

References WriteDataChunksForTocEntry().

Referenced by InitArchiveFmt_Directory().

811 {
812  /*
813  * This function returns void. We either fail and die horribly or
814  * succeed... A failure will be detected by the parent when the child dies
815  * unexpectedly.
816  */
818 
819  return 0;
820 }
void WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te)

◆ _WorkerJobRestoreDirectory()

static int _WorkerJobRestoreDirectory ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 827 of file pg_backup_directory.c.

References parallel_restore().

Referenced by InitArchiveFmt_Directory().

828 {
829  return parallel_restore(AH, te);
830 }
int parallel_restore(ArchiveHandle *AH, TocEntry *te)

◆ _WriteBuf()

static void _WriteBuf ( ArchiveHandle AH,
const void *  buf,
size_t  len 
)
static

Definition at line 510 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

511 {
512  lclContext *ctx = (lclContext *) AH->formatData;
513 
514  if (cfwrite(buf, len, ctx->dataFH) != len)
515  fatal("could not write to output file: %s",
516  get_cfp_error(ctx->dataFH));
517 }
static char * buf
Definition: pg_test_fsync.c:67
int cfwrite(const void *ptr, int size, cfp *fp)
Definition: compress_io.c:599
#define fatal(...)
const char * get_cfp_error(cfp *fp)
Definition: compress_io.c:687

◆ _WriteByte()

static int _WriteByte ( ArchiveHandle AH,
const int  i 
)
static

Definition at line 479 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

480 {
481  unsigned char c = (unsigned char) i;
482  lclContext *ctx = (lclContext *) AH->formatData;
483 
484  if (cfwrite(&c, 1, ctx->dataFH) != 1)
485  fatal("could not write to output file: %s",
486  get_cfp_error(ctx->dataFH));
487 
488  return 1;
489 }
char * c
int cfwrite(const void *ptr, int size, cfp *fp)
Definition: compress_io.c:599
#define fatal(...)
int i
const char * get_cfp_error(cfp *fp)
Definition: compress_io.c:687

◆ _WriteData()

static void _WriteData ( ArchiveHandle AH,
const void *  data,
size_t  dLen 
)
static

Definition at line 345 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

346 {
347  lclContext *ctx = (lclContext *) AH->formatData;
348 
349  if (dLen > 0 && cfwrite(data, dLen, ctx->dataFH) != dLen)
350  fatal("could not write to output file: %s",
351  get_cfp_error(ctx->dataFH));
352 }
int cfwrite(const void *ptr, int size, cfp *fp)
Definition: compress_io.c:599
#define fatal(...)
const char * get_cfp_error(cfp *fp)
Definition: compress_io.c:687

◆ _WriteExtraToc()

static void _WriteExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 260 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

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

◆ InitArchiveFmt_Directory()

void InitArchiveFmt_Directory ( ArchiveHandle AH)

Definition at line 108 of file pg_backup_directory.c.

References _ArchiveEntry(), _Clone(), _CloseArchive(), _DeClone(), _EndBlob(), _EndBlobs(), _EndData(), _PrepParallelRestore(), _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, fatal, _archiveHandle::format, _archiveHandle::formatData, _archiveHandle::fSpec, _archiveHandle::lo_buf, _archiveHandle::lo_buf_size, LOBBUFSIZE, MAXPGPATH, mkdir, _archiveHandle::mode, opendir(), PG_BINARY_R, pg_malloc(), pg_malloc0(), _archiveHandle::PrepParallelRestorePtr, _archiveHandle::PrintExtraTocPtr, _archiveHandle::PrintTocDataPtr, _archiveHandle::ReadBufPtr, _archiveHandle::ReadBytePtr, readdir(), _archiveHandle::ReadExtraTocPtr, ReadHead(), ReadToc(), _archiveHandle::ReopenPtr, S_ISDIR, setFilePath(), _archiveHandle::StartBlobPtr, _archiveHandle::StartBlobsPtr, _archiveHandle::StartDataPtr, stat, _archiveHandle::WorkerJobDumpPtr, _archiveHandle::WorkerJobRestorePtr, _archiveHandle::WriteBufPtr, _archiveHandle::WriteBytePtr, _archiveHandle::WriteDataPtr, and _archiveHandle::WriteExtraTocPtr.

Referenced by _allocAH().

109 {
110  lclContext *ctx;
111 
112  /* Assuming static functions, this can be copied for each format. */
114  AH->StartDataPtr = _StartData;
115  AH->WriteDataPtr = _WriteData;
116  AH->EndDataPtr = _EndData;
117  AH->WriteBytePtr = _WriteByte;
118  AH->ReadBytePtr = _ReadByte;
119  AH->WriteBufPtr = _WriteBuf;
120  AH->ReadBufPtr = _ReadBuf;
121  AH->ClosePtr = _CloseArchive;
127 
129  AH->StartBlobPtr = _StartBlob;
130  AH->EndBlobPtr = _EndBlob;
131  AH->EndBlobsPtr = _EndBlobs;
132 
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  fatal("no output directory specified");
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  fatal("could not read directory \"%s\": %m",
186  ctx->directory);
187 
188  if (closedir(dir))
189  fatal("could not close directory \"%s\": %m",
190  ctx->directory);
191  }
192  }
193 
194  if (!is_empty && mkdir(ctx->directory, 0700) < 0)
195  fatal("could not create directory \"%s\": %m",
196  ctx->directory);
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)
207  fatal("could not open input file \"%s\": %m", fname);
208 
209  ctx->dataFH = tocFH;
210 
211  /*
212  * The TOC of a directory format dump shares the format code of the
213  * tar format.
214  */
215  AH->format = archTar;
216  ReadHead(AH);
217  AH->format = archDirectory;
218  ReadToc(AH);
219 
220  /* Nothing else in the file, so close it again... */
221  if (cfclose(tocFH) != 0)
222  fatal("could not close TOC file: %m");
223  ctx->dataFH = NULL;
224  }
225 }
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)
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 void _PrepParallelRestore(ArchiveHandle *AH)
StartBlobsPtrType StartBlobsPtr
int cfclose(cfp *fp)
Definition: compress_io.c:649
Definition: dirent.h:9
#define PG_BINARY_R
Definition: c.h:1236
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
#define stat(a, b)
Definition: win32_port.h:255
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 *)
PrepParallelRestorePtrType PrepParallelRestorePtr
EndBlobsPtrType EndBlobsPtr
static void _StartBlobs(ArchiveHandle *AH, TocEntry *te)
ArchiveEntryPtrType ArchiveEntryPtr
struct dirent * readdir(DIR *)
Definition: dirent.c:77
#define fatal(...)
#define S_ISDIR(m)
Definition: win32_port.h:296
static void _CloseArchive(ArchiveHandle *AH)
ClonePtrType ClonePtr
cfp * cfopen_read(const char *path, const char *mode)
Definition: compress_io.c:452
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
#define mkdir(a, b)
Definition: win32_port.h:57
static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te)
WriteExtraTocPtrType WriteExtraTocPtr
static void _EndBlobs(ArchiveHandle *AH, TocEntry *te)
#define LOBBUFSIZE
ReadExtraTocPtrType ReadExtraTocPtr
WorkerJobDumpPtrType WorkerJobDumpPtr

◆ setFilePath()

static void setFilePath ( ArchiveHandle AH,
char *  buf,
const char *  relativeFilename 
)
static

Definition at line 695 of file pg_backup_directory.c.

References lclContext::directory, fatal, _archiveHandle::formatData, and MAXPGPATH.

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

696 {
697  lclContext *ctx = (lclContext *) AH->formatData;
698  char *dname;
699 
700  dname = ctx->directory;
701 
702  if (strlen(dname) + 1 + strlen(relativeFilename) + 1 > MAXPGPATH)
703  fatal("file name too long: \"%s\"", dname);
704 
705  strcpy(buf, dname);
706  strcat(buf, "/");
707  strcat(buf, relativeFilename);
708 }
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:67
#define fatal(...)