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:215

◆ _Clone()

static void _Clone ( ArchiveHandle AH)
static

Definition at line 794 of file pg_backup_directory.c.

References _archiveHandle::formatData, and pg_malloc().

Referenced by InitArchiveFmt_Directory().

795 {
796  lclContext *ctx = (lclContext *) AH->formatData;
797 
798  AH->formatData = (lclContext *) pg_malloc(sizeof(lclContext));
799  memcpy(AH->formatData, ctx, sizeof(lclContext));
800  ctx = (lclContext *) AH->formatData;
801 
802  /*
803  * Note: we do not make a local lo_buf because we expect at most one BLOBS
804  * entry per archive, so no parallelism is possible. Likewise,
805  * TOC-entry-local state isn't an issue because any one TOC entry is
806  * touched by just one worker child.
807  */
808 
809  /*
810  * We also don't copy the ParallelState pointer (pstate), only the leader
811  * process ever writes to it.
812  */
813 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47

◆ _CloseArchive()

static void _CloseArchive ( ArchiveHandle AH)
static

Definition at line 568 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, MAXPGPATH, _archiveHandle::mode, ParallelBackupEnd(), ParallelBackupStart(), PG_BINARY_W, lclContext::pstate, setFilePath(), WriteDataChunks(), WriteHead(), and WriteToc().

Referenced by InitArchiveFmt_Directory().

569 {
570  lclContext *ctx = (lclContext *) AH->formatData;
571 
572  if (AH->mode == archModeWrite)
573  {
574  cfp *tocFH;
575  char fname[MAXPGPATH];
576 
577  setFilePath(AH, fname, "toc.dat");
578 
579  /* this will actually fork the processes for a parallel backup */
580  ctx->pstate = ParallelBackupStart(AH);
581 
582  /* The TOC is always created uncompressed */
583  tocFH = cfopen_write(fname, PG_BINARY_W, 0);
584  if (tocFH == NULL)
585  fatal("could not open output file \"%s\": %m", fname);
586  ctx->dataFH = tocFH;
587 
588  /*
589  * Write 'tar' in the format field of the toc.dat file. The directory
590  * is compatible with 'tar', so there's no point having a different
591  * format code for it.
592  */
593  AH->format = archTar;
594  WriteHead(AH);
595  AH->format = archDirectory;
596  WriteToc(AH);
597  if (cfclose(tocFH) != 0)
598  fatal("could not close TOC file: %m");
599  WriteDataChunks(AH, ctx->pstate);
600 
601  ParallelBackupEnd(AH, ctx->pstate);
602 
603  /*
604  * In directory mode, there is no need to sync all the entries
605  * individually. Just recurse once through all the files generated.
606  */
607  if (AH->dosync)
608  fsync_dir_recurse(ctx->directory);
609  }
610  AH->FH = NULL;
611 }
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:1216
void WriteToc(ArchiveHandle *AH)
int cfclose(cfp *fp)
Definition: compress_io.c:649
#define MAXPGPATH
ParallelState * ParallelBackupStart(ArchiveHandle *AH)
Definition: parallel.c:902
void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate)
Definition: parallel.c:1064
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 816 of file pg_backup_directory.c.

References _archiveHandle::formatData, and free.

Referenced by InitArchiveFmt_Directory().

817 {
818  lclContext *ctx = (lclContext *) AH->formatData;
819 
820  free(ctx);
821 }
#define free(a)
Definition: header.h:65

◆ _EndBlob()

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

Definition at line 676 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

677 {
678  lclContext *ctx = (lclContext *) AH->formatData;
679  char buf[50];
680  int len;
681 
682  /* Close the BLOB data file itself */
683  cfclose(ctx->dataFH);
684  ctx->dataFH = NULL;
685 
686  /* register the blob in blobs.toc */
687  len = snprintf(buf, sizeof(buf), "%u blob_%u.dat\n", oid, oid);
688  if (cfwrite(buf, len, ctx->blobsTocFH) != len)
689  fatal("could not write to blobs TOC file");
690 }
int cfclose(cfp *fp)
Definition: compress_io.c:649
static char * buf
Definition: pg_test_fsync.c:68
int cfwrite(const void *ptr, int size, cfp *fp)
Definition: compress_io.c:599
#define fatal(...)
#define snprintf
Definition: port.h:215

◆ _EndBlobs()

static void _EndBlobs ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 698 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

699 {
700  lclContext *ctx = (lclContext *) AH->formatData;
701 
702  cfclose(ctx->blobsTocFH);
703  ctx->blobsTocFH = NULL;
704 }
int cfclose(cfp *fp)
Definition: compress_io.c:649

◆ _EndData()

static void _EndData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 367 of file pg_backup_directory.c.

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

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:649

◆ _LoadBlobs()

static void _LoadBlobs ( ArchiveHandle AH)
static

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

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

◆ _PrepParallelRestore()

static void _PrepParallelRestore ( ArchiveHandle AH)
static

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

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

◆ _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:193

◆ _PrintFileData()

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(), fatal, free, PG_BINARY_R, pg_malloc(), 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  fatal("could not open input file \"%s\": %m", filename);
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  fatal("could not close data file \"%s\": %m", filename);
407 }
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:1215
struct cfp cfp
Definition: compress_io.h:57
static char * buf
Definition: pg_test_fsync.c:68
#define free(a)
Definition: header.h:65
#define fatal(...)
#define ZLIB_OUT_SIZE
Definition: walmethods.c:31
static char * filename
Definition: pg_dumpall.c:91
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 413 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

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

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

Referenced by InitArchiveFmt_Directory().

544 {
545  lclContext *ctx = (lclContext *) AH->formatData;
546 
547  /*
548  * If there was an I/O error, we already exited in cfread(), so here we
549  * exit on short reads.
550  */
551  if (cfread(buf, len, ctx->dataFH) != len)
552  fatal("could not read from input file: end of file");
553 }
int cfread(void *ptr, int size, cfp *fp)
Definition: compress_io.c:568
static char * buf
Definition: pg_test_fsync.c:68
#define fatal(...)

◆ _ReadByte()

static int _ReadByte ( ArchiveHandle AH)
static

Definition at line 510 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

511 {
512  lclContext *ctx = (lclContext *) AH->formatData;
513 
514  return cfgetc(ctx->dataFH);
515 }
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 617 of file pg_backup_directory.c.

Referenced by InitArchiveFmt_Directory().

618 {
619  /*
620  * Our TOC is in memory, our data files are opened by each child anyway as
621  * they are separate. We support reopening the archive by just doing
622  * nothing.
623  */
624 }

◆ _StartBlob()

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

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

658 {
659  lclContext *ctx = (lclContext *) AH->formatData;
660  char fname[MAXPGPATH];
661 
662  snprintf(fname, MAXPGPATH, "%s/blob_%u.dat", ctx->directory, oid);
663 
664  ctx->dataFH = cfopen_write(fname, PG_BINARY_W, AH->compression);
665 
666  if (ctx->dataFH == NULL)
667  fatal("could not open output file \"%s\": %m", fname);
668 }
#define PG_BINARY_W
Definition: c.h:1216
#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:215

◆ _StartBlobs()

static void _StartBlobs ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 638 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

639 {
640  lclContext *ctx = (lclContext *) AH->formatData;
641  char fname[MAXPGPATH];
642 
643  setFilePath(AH, fname, "blobs.toc");
644 
645  /* The blob TOC file is never compressed */
646  ctx->blobsTocFH = cfopen_write(fname, "ab", 0);
647  if (ctx->blobsTocFH == NULL)
648  fatal("could not open output file \"%s\": %m", fname);
649 }
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:1216
#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 828 of file pg_backup_directory.c.

References WriteDataChunksForTocEntry().

Referenced by InitArchiveFmt_Directory().

829 {
830  /*
831  * This function returns void. We either fail and die horribly or
832  * succeed... A failure will be detected by the parent when the child dies
833  * unexpectedly.
834  */
836 
837  return 0;
838 }
void WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te)

◆ _WorkerJobRestoreDirectory()

static int _WorkerJobRestoreDirectory ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 845 of file pg_backup_directory.c.

References parallel_restore().

Referenced by InitArchiveFmt_Directory().

846 {
847  return parallel_restore(AH, te);
848 }
int parallel_restore(ArchiveHandle *AH, TocEntry *te)

◆ _WriteBuf()

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

Definition at line 522 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

523 {
524  lclContext *ctx = (lclContext *) AH->formatData;
525 
526  errno = 0;
527  if (cfwrite(buf, len, ctx->dataFH) != len)
528  {
529  /* if write didn't set errno, assume problem is no disk space */
530  if (errno == 0)
531  errno = ENOSPC;
532  fatal("could not write to output file: %s",
533  get_cfp_error(ctx->dataFH));
534  }
535 }
static char * buf
Definition: pg_test_fsync.c:68
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 485 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

486 {
487  unsigned char c = (unsigned char) i;
488  lclContext *ctx = (lclContext *) AH->formatData;
489 
490  errno = 0;
491  if (cfwrite(&c, 1, ctx->dataFH) != 1)
492  {
493  /* if write didn't set errno, assume problem is no disk space */
494  if (errno == 0)
495  errno = ENOSPC;
496  fatal("could not write to output file: %s",
497  get_cfp_error(ctx->dataFH));
498  }
499 
500  return 1;
501 }
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  errno = 0;
350  if (dLen > 0 && cfwrite(data, dLen, ctx->dataFH) != dLen)
351  {
352  /* if write didn't set errno, assume problem is no disk space */
353  if (errno == 0)
354  errno = ENOSPC;
355  fatal("could not write to output file: %s",
356  get_cfp_error(ctx->dataFH));
357  }
358 }
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(), stat::st_mode, _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:123
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:1215
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 *)
PrepParallelRestorePtrType PrepParallelRestorePtr
EndBlobsPtrType EndBlobsPtr
static void _StartBlobs(ArchiveHandle *AH, TocEntry *te)
ArchiveEntryPtrType ArchiveEntryPtr
struct dirent * readdir(DIR *)
Definition: dirent.c:78
#define fatal(...)
#define S_ISDIR(m)
Definition: win32_port.h:316
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:15
PrintExtraTocPtrType PrintExtraTocPtr
#define mkdir(a, b)
Definition: win32_port.h:63
static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te)
WriteExtraTocPtrType WriteExtraTocPtr
static void _EndBlobs(ArchiveHandle *AH, TocEntry *te)
#define LOBBUFSIZE
ReadExtraTocPtrType ReadExtraTocPtr
#define stat
Definition: win32_port.h:275
WorkerJobDumpPtrType WorkerJobDumpPtr

◆ setFilePath()

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

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

714 {
715  lclContext *ctx = (lclContext *) AH->formatData;
716  char *dname;
717 
718  dname = ctx->directory;
719 
720  if (strlen(dname) + 1 + strlen(relativeFilename) + 1 > MAXPGPATH)
721  fatal("file name too long: \"%s\"", dname);
722 
723  strcpy(buf, dname);
724  strcat(buf, "/");
725  strcat(buf, relativeFilename);
726 }
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:68
#define fatal(...)