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 "dumputils.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 *AH)
 
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 _StartLOs (ArchiveHandle *AH, TocEntry *te)
 
static void _StartLO (ArchiveHandle *AH, TocEntry *te, Oid oid)
 
static void _EndLO (ArchiveHandle *AH, TocEntry *te, Oid oid)
 
static void _EndLOs (ArchiveHandle *AH, TocEntry *te)
 
static void _LoadLOs (ArchiveHandle *AH, TocEntry *te)
 
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 198 of file pg_backup_directory.c.

199{
200 lclTocEntry *tctx;
201 char fn[MAXPGPATH];
202
203 tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
204 if (strcmp(te->desc, "BLOBS") == 0)
205 {
206 snprintf(fn, MAXPGPATH, "blobs_%d.toc", te->dumpId);
207 tctx->filename = pg_strdup(fn);
208 }
209 else if (te->dataDumper)
210 {
211 snprintf(fn, MAXPGPATH, "%d.dat", te->dumpId);
212 tctx->filename = pg_strdup(fn);
213 }
214 else
215 tctx->filename = NULL;
216
217 te->formatData = tctx;
218}
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
#define MAXPGPATH
#define snprintf
Definition: port.h:239
DataDumperPtr dataDumper
static void * fn(void *arg)
Definition: thread-alloc.c:119

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

Referenced by InitArchiveFmt_Directory().

◆ _Clone()

static void _Clone ( ArchiveHandle AH)
static

Definition at line 793 of file pg_backup_directory.c.

794{
795 lclContext *ctx = (lclContext *) AH->formatData;
796
797 AH->formatData = (lclContext *) pg_malloc(sizeof(lclContext));
798 memcpy(AH->formatData, ctx, sizeof(lclContext));
799 ctx = (lclContext *) AH->formatData;
800
801 /*
802 * TOC-entry-local state isn't an issue because any one TOC entry is
803 * touched by just one worker child.
804 */
805
806 /*
807 * We also don't copy the ParallelState pointer (pstate), only the leader
808 * process ever writes to it.
809 */
810}
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47

References _archiveHandle::formatData, and pg_malloc().

Referenced by InitArchiveFmt_Directory().

◆ _CloseArchive()

static void _CloseArchive ( ArchiveHandle AH)
static

Definition at line 549 of file pg_backup_directory.c.

550{
551 lclContext *ctx = (lclContext *) AH->formatData;
552
553 if (AH->mode == archModeWrite)
554 {
555 CompressFileHandle *tocFH;
556 pg_compress_specification compression_spec = {0};
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 compression_spec.algorithm = PG_COMPRESSION_NONE;
566 tocFH = InitCompressFileHandle(compression_spec);
567 if (!tocFH->open_write_func(fname, PG_BINARY_W, tocFH))
568 pg_fatal("could not open output file \"%s\": %m", fname);
569 ctx->dataFH = tocFH;
570
571 /*
572 * Write 'tar' in the format field of the toc.dat file. The directory
573 * is compatible with 'tar', so there's no point having a different
574 * format code for it.
575 */
576 AH->format = archTar;
577 WriteHead(AH);
578 AH->format = archDirectory;
579 WriteToc(AH);
580 if (!EndCompressFileHandle(tocFH))
581 pg_fatal("could not close TOC file: %m");
582 WriteDataChunks(AH, ctx->pstate);
583
584 ParallelBackupEnd(AH, ctx->pstate);
585
586 /*
587 * In directory mode, there is no need to sync all the entries
588 * individually. Just recurse once through all the files generated.
589 */
590 if (AH->dosync)
591 sync_dir_recurse(ctx->directory, AH->sync_method);
592 }
593 AH->FH = NULL;
594}
void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate)
Definition: parallel.c:1071
ParallelState * ParallelBackupStart(ArchiveHandle *AH)
Definition: parallel.c:909
#define PG_BINARY_W
Definition: c.h:1247
bool EndCompressFileHandle(CompressFileHandle *CFH)
Definition: compress_io.c:288
CompressFileHandle * InitCompressFileHandle(const pg_compress_specification compression_spec)
Definition: compress_io.c:194
@ PG_COMPRESSION_NONE
Definition: compression.h:23
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
@ archModeWrite
Definition: pg_backup.h:51
@ archTar
Definition: pg_backup.h:43
@ archDirectory
Definition: pg_backup.h:45
void WriteHead(ArchiveHandle *AH)
void WriteDataChunks(ArchiveHandle *AH, ParallelState *pstate)
void WriteToc(ArchiveHandle *AH)
static void setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
#define pg_fatal(...)
bool(* open_write_func)(const char *path, const char *mode, CompressFileHandle *CFH)
Definition: compress_io.h:122
ArchiveFormat format
DataDirSyncMethod sync_method
ParallelState * pstate
CompressFileHandle * dataFH
pg_compress_algorithm algorithm
Definition: compression.h:34

References pg_compress_specification::algorithm, archDirectory, archModeWrite, archTar, lclContext::dataFH, lclContext::directory, _archiveHandle::dosync, EndCompressFileHandle(), _archiveHandle::FH, _archiveHandle::format, _archiveHandle::formatData, if(), InitCompressFileHandle(), MAXPGPATH, _archiveHandle::mode, CompressFileHandle::open_write_func, ParallelBackupEnd(), ParallelBackupStart(), PG_BINARY_W, PG_COMPRESSION_NONE, pg_fatal, lclContext::pstate, setFilePath(), _archiveHandle::sync_method, WriteDataChunks(), WriteHead(), and WriteToc().

Referenced by InitArchiveFmt_Directory().

◆ _DeClone()

static void _DeClone ( ArchiveHandle AH)
static

Definition at line 813 of file pg_backup_directory.c.

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

References _archiveHandle::formatData, and free.

Referenced by InitArchiveFmt_Directory().

◆ _EndData()

static void _EndData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 337 of file pg_backup_directory.c.

338{
339 lclContext *ctx = (lclContext *) AH->formatData;
340
341 /* Close the file */
343 pg_fatal("could not close data file: %m");
344
345 ctx->dataFH = NULL;
346}

References lclContext::dataFH, EndCompressFileHandle(), _archiveHandle::formatData, if(), and pg_fatal.

Referenced by InitArchiveFmt_Directory().

◆ _EndLO()

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

Definition at line 661 of file pg_backup_directory.c.

662{
663 lclContext *ctx = (lclContext *) AH->formatData;
664 CompressFileHandle *CFH = ctx->LOsTocFH;
665 char buf[50];
666 int len;
667
668 /* Close the BLOB data file itself */
670 pg_fatal("could not close LO data file: %m");
671 ctx->dataFH = NULL;
672
673 /* register the LO in blobs_NNN.toc */
674 len = snprintf(buf, sizeof(buf), "%u blob_%u.dat\n", oid, oid);
675 if (!CFH->write_func(buf, len, CFH))
676 {
677 /* if write didn't set errno, assume problem is no disk space */
678 if (errno == 0)
679 errno = ENOSPC;
680 pg_fatal("could not write to LOs TOC file: %s",
681 CFH->get_error_func(CFH));
682 }
683}
const void size_t len
static char * buf
Definition: pg_test_fsync.c:72
CompressFileHandle * LOsTocFH

References buf, lclContext::dataFH, EndCompressFileHandle(), _archiveHandle::formatData, if(), len, lclContext::LOsTocFH, pg_fatal, and snprintf.

Referenced by InitArchiveFmt_Directory().

◆ _EndLOs()

static void _EndLOs ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 691 of file pg_backup_directory.c.

692{
693 lclContext *ctx = (lclContext *) AH->formatData;
694
696 pg_fatal("could not close LOs TOC file: %m");
697 ctx->LOsTocFH = NULL;
698}

References EndCompressFileHandle(), _archiveHandle::formatData, if(), lclContext::LOsTocFH, and pg_fatal.

Referenced by InitArchiveFmt_Directory().

◆ _LoadLOs()

static void _LoadLOs ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 402 of file pg_backup_directory.c.

403{
404 Oid oid;
405 lclContext *ctx = (lclContext *) AH->formatData;
406 lclTocEntry *tctx = (lclTocEntry *) te->formatData;
408 char tocfname[MAXPGPATH];
409 char line[MAXPGPATH];
410
411 StartRestoreLOs(AH);
412
413 /*
414 * Note: before archive v16, there was always only one BLOBS TOC entry,
415 * now there can be multiple. We don't need to worry what version we are
416 * reading though, because tctx->filename should be correct either way.
417 */
418 setFilePath(AH, tocfname, tctx->filename);
419
421
422 if (ctx->LOsTocFH == NULL)
423 pg_fatal("could not open large object TOC file \"%s\" for input: %m",
424 tocfname);
425
426 /* Read the LOs TOC file line-by-line, and process each LO */
427 while ((CFH->gets_func(line, MAXPGPATH, CFH)) != NULL)
428 {
429 char lofname[MAXPGPATH + 1];
430 char path[MAXPGPATH];
431
432 /* Can't overflow because line and lofname are the same length */
433 if (sscanf(line, "%u %" CppAsString2(MAXPGPATH) "s\n", &oid, lofname) != 2)
434 pg_fatal("invalid line in large object TOC file \"%s\": \"%s\"",
435 tocfname, line);
436
437 StartRestoreLO(AH, oid, AH->public.ropt->dropSchema);
438 snprintf(path, MAXPGPATH, "%s/%s", ctx->directory, lofname);
439 _PrintFileData(AH, path);
440 EndRestoreLO(AH, oid);
441 }
442 if (!CFH->eof_func(CFH))
443 pg_fatal("error reading large object TOC file \"%s\"",
444 tocfname);
445
447 pg_fatal("could not close large object TOC file \"%s\": %m",
448 tocfname);
449
450 ctx->LOsTocFH = NULL;
451
452 EndRestoreLOs(AH);
453}
#define PG_BINARY_R
Definition: c.h:1246
#define CppAsString2(x)
Definition: c.h:363
CompressFileHandle * InitDiscoverCompressFileHandle(const char *path, const char *mode)
Definition: compress_io.c:240
void StartRestoreLOs(ArchiveHandle *AH)
void EndRestoreLO(ArchiveHandle *AH, Oid oid)
void EndRestoreLOs(ArchiveHandle *AH)
void StartRestoreLO(ArchiveHandle *AH, Oid oid, bool drop)
static void _PrintFileData(ArchiveHandle *AH, char *filename)
unsigned int Oid
Definition: postgres_ext.h:30
RestoreOptions * ropt
Definition: pg_backup.h:225
char *(* gets_func)(char *s, int size, CompressFileHandle *CFH)
Definition: compress_io.h:152
bool(* eof_func)(CompressFileHandle *CFH)
Definition: compress_io.h:168

References _PrintFileData(), CppAsString2, lclContext::directory, _restoreOptions::dropSchema, EndCompressFileHandle(), EndRestoreLO(), EndRestoreLOs(), CompressFileHandle::eof_func, _archiveHandle::formatData, _tocEntry::formatData, CompressFileHandle::gets_func, InitDiscoverCompressFileHandle(), lclContext::LOsTocFH, MAXPGPATH, PG_BINARY_R, pg_fatal, _archiveHandle::public, Archive::ropt, setFilePath(), snprintf, StartRestoreLO(), and StartRestoreLOs().

Referenced by _PrintTocData().

◆ _PrepParallelRestore()

static void _PrepParallelRestore ( ArchiveHandle AH)
static

Definition at line 733 of file pg_backup_directory.c.

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

References pg_compress_specification::algorithm, _archiveHandle::compression_spec, _tocEntry::dataLength, _tocEntry::desc, lclTocEntry::filename, _tocEntry::formatData, if(), MAXPGPATH, _tocEntry::next, PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, PG_COMPRESSION_ZSTD, REQ_DATA, REQ_STATS, _tocEntry::reqs, setFilePath(), stat, strlcat(), and _archiveHandle::toc.

Referenced by InitArchiveFmt_Directory().

◆ _PrintExtraToc()

static void _PrintExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 272 of file pg_backup_directory.c.

273{
274 lclTocEntry *tctx = (lclTocEntry *) te->formatData;
275
276 if (AH->public.verbose && tctx->filename)
277 ahprintf(AH, "-- File: %s\n", tctx->filename);
278}
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
int verbose
Definition: pg_backup.h:227

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

Referenced by InitArchiveFmt_Directory().

◆ _PrintFileData()

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

Definition at line 352 of file pg_backup_directory.c.

353{
354 size_t cnt = 0;
355 char *buf;
356 size_t buflen;
358
359 if (!filename)
360 return;
361
363 if (!CFH)
364 pg_fatal("could not open input file \"%s\": %m", filename);
365
366 buflen = DEFAULT_IO_BUFFER_SIZE;
367 buf = pg_malloc(buflen);
368
369 while (CFH->read_func(buf, buflen, &cnt, CFH) && cnt > 0)
370 {
371 ahwrite(buf, 1, cnt, AH);
372 }
373
374 free(buf);
375 if (!EndCompressFileHandle(CFH))
376 pg_fatal("could not close data file \"%s\": %m", filename);
377}
#define DEFAULT_IO_BUFFER_SIZE
Definition: compress_io.h:27
void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
static char * filename
Definition: pg_dumpall.c:123
bool(* read_func)(void *ptr, size_t size, size_t *rsize, CompressFileHandle *CFH)
Definition: compress_io.h:131

References ahwrite(), buf, DEFAULT_IO_BUFFER_SIZE, EndCompressFileHandle(), filename, free, InitDiscoverCompressFileHandle(), PG_BINARY_R, pg_fatal, pg_malloc(), and CompressFileHandle::read_func.

Referenced by _LoadLOs(), and _PrintTocData().

◆ _PrintTocData()

static void _PrintTocData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 383 of file pg_backup_directory.c.

384{
385 lclTocEntry *tctx = (lclTocEntry *) te->formatData;
386
387 if (!tctx->filename)
388 return;
389
390 if (strcmp(te->desc, "BLOBS") == 0)
391 _LoadLOs(AH, te);
392 else
393 {
394 char fname[MAXPGPATH];
395
396 setFilePath(AH, fname, tctx->filename);
397 _PrintFileData(AH, fname);
398 }
399}
static void _LoadLOs(ArchiveHandle *AH, TocEntry *te)

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

Referenced by InitArchiveFmt_Directory().

◆ _ReadBuf()

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

Definition at line 523 of file pg_backup_directory.c.

524{
525 lclContext *ctx = (lclContext *) AH->formatData;
526 CompressFileHandle *CFH = ctx->dataFH;
527
528 /*
529 * If there was an I/O error, we already exited in readF(), so here we
530 * exit on short reads.
531 */
532 if (!CFH->read_func(buf, len, NULL, CFH))
533 pg_fatal("could not read from input file: end of file");
534}

References buf, lclContext::dataFH, _archiveHandle::formatData, if(), len, and pg_fatal.

Referenced by InitArchiveFmt_Directory().

◆ _ReadByte()

static int _ReadByte ( ArchiveHandle AH)
static

Definition at line 488 of file pg_backup_directory.c.

489{
490 lclContext *ctx = (lclContext *) AH->formatData;
491 CompressFileHandle *CFH = ctx->dataFH;
492
493 return CFH->getc_func(CFH);
494}
int(* getc_func)(CompressFileHandle *CFH)
Definition: compress_io.h:161

References lclContext::dataFH, _archiveHandle::formatData, and CompressFileHandle::getc_func.

Referenced by InitArchiveFmt_Directory().

◆ _ReadExtraToc()

static void _ReadExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 249 of file pg_backup_directory.c.

250{
251 lclTocEntry *tctx = (lclTocEntry *) te->formatData;
252
253 if (tctx == NULL)
254 {
255 tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
256 te->formatData = tctx;
257 }
258
259 tctx->filename = ReadStr(AH);
260 if (strlen(tctx->filename) == 0)
261 {
262 free(tctx->filename);
263 tctx->filename = NULL;
264 }
265}
char * ReadStr(ArchiveHandle *AH)

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

Referenced by InitArchiveFmt_Directory().

◆ _ReopenArchive()

static void _ReopenArchive ( ArchiveHandle AH)
static

Definition at line 600 of file pg_backup_directory.c.

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

Referenced by InitArchiveFmt_Directory().

◆ _StartData()

static void _StartData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 290 of file pg_backup_directory.c.

291{
292 lclTocEntry *tctx = (lclTocEntry *) te->formatData;
293 lclContext *ctx = (lclContext *) AH->formatData;
294 char fname[MAXPGPATH];
295
296 setFilePath(AH, fname, tctx->filename);
297
298 ctx->dataFH = InitCompressFileHandle(AH->compression_spec);
299
300 if (!ctx->dataFH->open_write_func(fname, PG_BINARY_W, ctx->dataFH))
301 pg_fatal("could not open output file \"%s\": %m", fname);
302}

References _archiveHandle::compression_spec, lclTocEntry::filename, _archiveHandle::formatData, _tocEntry::formatData, InitCompressFileHandle(), MAXPGPATH, PG_BINARY_W, pg_fatal, and setFilePath().

Referenced by InitArchiveFmt_Directory().

◆ _StartLO()

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

Definition at line 643 of file pg_backup_directory.c.

644{
645 lclContext *ctx = (lclContext *) AH->formatData;
646 char fname[MAXPGPATH];
647
648 snprintf(fname, MAXPGPATH, "%s/blob_%u.dat", ctx->directory, oid);
649
651 if (!ctx->dataFH->open_write_func(fname, PG_BINARY_W, ctx->dataFH))
652 pg_fatal("could not open output file \"%s\": %m", fname);
653}

References _archiveHandle::compression_spec, lclContext::dataFH, lclContext::directory, _archiveHandle::formatData, InitCompressFileHandle(), MAXPGPATH, CompressFileHandle::open_write_func, PG_BINARY_W, pg_fatal, and snprintf.

Referenced by InitArchiveFmt_Directory().

◆ _StartLOs()

static void _StartLOs ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 621 of file pg_backup_directory.c.

622{
623 lclContext *ctx = (lclContext *) AH->formatData;
624 lclTocEntry *tctx = (lclTocEntry *) te->formatData;
625 pg_compress_specification compression_spec = {0};
626 char fname[MAXPGPATH];
627
628 setFilePath(AH, fname, tctx->filename);
629
630 /* The LO TOC file is never compressed */
631 compression_spec.algorithm = PG_COMPRESSION_NONE;
632 ctx->LOsTocFH = InitCompressFileHandle(compression_spec);
633 if (!ctx->LOsTocFH->open_write_func(fname, "ab", ctx->LOsTocFH))
634 pg_fatal("could not open output file \"%s\": %m", fname);
635}

References pg_compress_specification::algorithm, _archiveHandle::formatData, _tocEntry::formatData, InitCompressFileHandle(), lclContext::LOsTocFH, MAXPGPATH, CompressFileHandle::open_write_func, PG_COMPRESSION_NONE, pg_fatal, and setFilePath().

Referenced by InitArchiveFmt_Directory().

◆ _WorkerJobDumpDirectory()

static int _WorkerJobDumpDirectory ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 825 of file pg_backup_directory.c.

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

References WriteDataChunksForTocEntry().

Referenced by InitArchiveFmt_Directory().

◆ _WorkerJobRestoreDirectory()

static int _WorkerJobRestoreDirectory ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 842 of file pg_backup_directory.c.

843{
844 return parallel_restore(AH, te);
845}
int parallel_restore(ArchiveHandle *AH, TocEntry *te)

References parallel_restore().

Referenced by InitArchiveFmt_Directory().

◆ _WriteBuf()

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

Definition at line 501 of file pg_backup_directory.c.

502{
503 lclContext *ctx = (lclContext *) AH->formatData;
504 CompressFileHandle *CFH = ctx->dataFH;
505
506 errno = 0;
507 if (!CFH->write_func(buf, len, CFH))
508 {
509 /* if write didn't set errno, assume problem is no disk space */
510 if (errno == 0)
511 errno = ENOSPC;
512 pg_fatal("could not write to output file: %s",
513 CFH->get_error_func(CFH));
514 }
515}

References buf, lclContext::dataFH, _archiveHandle::formatData, if(), len, and pg_fatal.

Referenced by InitArchiveFmt_Directory().

◆ _WriteByte()

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

Definition at line 462 of file pg_backup_directory.c.

463{
464 unsigned char c = (unsigned char) i;
465 lclContext *ctx = (lclContext *) AH->formatData;
466 CompressFileHandle *CFH = ctx->dataFH;
467
468 errno = 0;
469 if (!CFH->write_func(&c, 1, CFH))
470 {
471 /* if write didn't set errno, assume problem is no disk space */
472 if (errno == 0)
473 errno = ENOSPC;
474 pg_fatal("could not write to output file: %s",
475 CFH->get_error_func(CFH));
476 }
477
478 return 1;
479}
int i
Definition: isn.c:77
char * c

References lclContext::dataFH, _archiveHandle::formatData, i, if(), and pg_fatal.

Referenced by InitArchiveFmt_Directory().

◆ _WriteData()

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

Definition at line 314 of file pg_backup_directory.c.

315{
316 lclContext *ctx = (lclContext *) AH->formatData;
317 CompressFileHandle *CFH = ctx->dataFH;
318
319 errno = 0;
320 if (dLen > 0 && !CFH->write_func(data, dLen, CFH))
321 {
322 /* if write didn't set errno, assume problem is no disk space */
323 if (errno == 0)
324 errno = ENOSPC;
325 pg_fatal("could not write to output file: %s",
326 CFH->get_error_func(CFH));
327 }
328}
const void * data

References data, lclContext::dataFH, _archiveHandle::formatData, if(), and pg_fatal.

Referenced by InitArchiveFmt_Directory().

◆ _WriteExtraToc()

static void _WriteExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 228 of file pg_backup_directory.c.

229{
230 lclTocEntry *tctx = (lclTocEntry *) te->formatData;
231
232 /*
233 * A dumpable object has set tctx->filename, any other object has not.
234 * (see _ArchiveEntry).
235 */
236 if (tctx->filename)
237 WriteStr(AH, tctx->filename);
238 else
239 WriteStr(AH, "");
240}
size_t WriteStr(ArchiveHandle *AH, const char *c)

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

Referenced by InitArchiveFmt_Directory().

◆ InitArchiveFmt_Directory()

void InitArchiveFmt_Directory ( ArchiveHandle AH)

Definition at line 110 of file pg_backup_directory.c.

111{
112 lclContext *ctx;
113
114 /* Assuming static functions, this can be copied for each format. */
118 AH->EndDataPtr = _EndData;
122 AH->ReadBufPtr = _ReadBuf;
129
131 AH->StartLOPtr = _StartLO;
132 AH->EndLOPtr = _EndLO;
133 AH->EndLOsPtr = _EndLOs;
134
136 AH->ClonePtr = _Clone;
137 AH->DeClonePtr = _DeClone;
138
141
142 /* Set up our private context */
143 ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
144 AH->formatData = ctx;
145
146 ctx->dataFH = NULL;
147 ctx->LOsTocFH = NULL;
148
149 /*
150 * Now open the TOC file
151 */
152
153 if (!AH->fSpec || strcmp(AH->fSpec, "") == 0)
154 pg_fatal("no output directory specified");
155
156 ctx->directory = AH->fSpec;
157
158 if (AH->mode == archModeWrite)
159 {
160 /* we accept an empty existing directory */
162 }
163 else
164 { /* Read Mode */
165 char fname[MAXPGPATH];
166 CompressFileHandle *tocFH;
167
168 setFilePath(AH, fname, "toc.dat");
169
171 if (tocFH == NULL)
172 pg_fatal("could not open input file \"%s\": %m", fname);
173
174 ctx->dataFH = tocFH;
175
176 /*
177 * The TOC of a directory format dump shares the format code of the
178 * tar format.
179 */
180 AH->format = archTar;
181 ReadHead(AH);
182 AH->format = archDirectory;
183 ReadToc(AH);
184
185 /* Nothing else in the file, so close it again... */
186 if (!EndCompressFileHandle(tocFH))
187 pg_fatal("could not close TOC file: %m");
188 ctx->dataFH = NULL;
189 }
190}
void create_or_open_dir(const char *dirname)
Definition: dumputils.c:897
void ReadHead(ArchiveHandle *AH)
void ReadToc(ArchiveHandle *AH)
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te)
static void _StartData(ArchiveHandle *AH, TocEntry *te)
static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
static void _CloseArchive(ArchiveHandle *AH)
static void _DeClone(ArchiveHandle *AH)
static void _ReopenArchive(ArchiveHandle *AH)
static void _StartLOs(ArchiveHandle *AH, TocEntry *te)
static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid)
static void _EndLOs(ArchiveHandle *AH, TocEntry *te)
static int _WriteByte(ArchiveHandle *AH, const int i)
static void _PrepParallelRestore(ArchiveHandle *AH)
static void _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid)
static int _ReadByte(ArchiveHandle *AH)
static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
static void _EndData(ArchiveHandle *AH, TocEntry *te)
static void _Clone(ArchiveHandle *AH)
static int _WorkerJobRestoreDirectory(ArchiveHandle *AH, TocEntry *te)
static int _WorkerJobDumpDirectory(ArchiveHandle *AH, TocEntry *te)
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
DeClonePtrType DeClonePtr
EndLOsPtrType EndLOsPtr
ReadExtraTocPtrType ReadExtraTocPtr
WorkerJobDumpPtrType WorkerJobDumpPtr
StartLOsPtrType StartLOsPtr
ArchiveEntryPtrType ArchiveEntryPtr
WriteDataPtrType WriteDataPtr
StartLOPtrType StartLOPtr
ClonePtrType ClonePtr
WriteBufPtrType WriteBufPtr
PrepParallelRestorePtrType PrepParallelRestorePtr
EndLOPtrType EndLOPtr
WriteExtraTocPtrType WriteExtraTocPtr
ReadBytePtrType ReadBytePtr
WorkerJobRestorePtrType WorkerJobRestorePtr
PrintTocDataPtrType PrintTocDataPtr
WriteBytePtrType WriteBytePtr
ReadBufPtrType ReadBufPtr
PrintExtraTocPtrType PrintExtraTocPtr
StartDataPtrType StartDataPtr
ReopenPtrType ReopenPtr
EndDataPtrType EndDataPtr
ClosePtrType ClosePtr

References _ArchiveEntry(), _Clone(), _CloseArchive(), _DeClone(), _EndData(), _EndLO(), _EndLOs(), _PrepParallelRestore(), _PrintExtraToc(), _PrintTocData(), _ReadBuf(), _ReadByte(), _ReadExtraToc(), _ReopenArchive(), _StartData(), _StartLO(), _StartLOs(), _WorkerJobDumpDirectory(), _WorkerJobRestoreDirectory(), _WriteBuf(), _WriteByte(), _WriteData(), _WriteExtraToc(), archDirectory, _archiveHandle::ArchiveEntryPtr, archModeWrite, archTar, _archiveHandle::ClonePtr, _archiveHandle::ClosePtr, create_or_open_dir(), lclContext::dataFH, _archiveHandle::DeClonePtr, lclContext::directory, EndCompressFileHandle(), _archiveHandle::EndDataPtr, _archiveHandle::EndLOPtr, _archiveHandle::EndLOsPtr, _archiveHandle::format, _archiveHandle::formatData, _archiveHandle::fSpec, InitDiscoverCompressFileHandle(), lclContext::LOsTocFH, MAXPGPATH, _archiveHandle::mode, PG_BINARY_R, pg_fatal, pg_malloc0(), _archiveHandle::PrepParallelRestorePtr, _archiveHandle::PrintExtraTocPtr, _archiveHandle::PrintTocDataPtr, _archiveHandle::ReadBufPtr, _archiveHandle::ReadBytePtr, _archiveHandle::ReadExtraTocPtr, ReadHead(), ReadToc(), _archiveHandle::ReopenPtr, setFilePath(), _archiveHandle::StartDataPtr, _archiveHandle::StartLOPtr, _archiveHandle::StartLOsPtr, _archiveHandle::WorkerJobDumpPtr, _archiveHandle::WorkerJobRestorePtr, _archiveHandle::WriteBufPtr, _archiveHandle::WriteBytePtr, _archiveHandle::WriteDataPtr, and _archiveHandle::WriteExtraTocPtr.

Referenced by _allocAH().

◆ setFilePath()

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

Definition at line 707 of file pg_backup_directory.c.

708{
709 lclContext *ctx = (lclContext *) AH->formatData;
710 char *dname;
711
712 dname = ctx->directory;
713
714 if (strlen(dname) + 1 + strlen(relativeFilename) + 1 > MAXPGPATH)
715 pg_fatal("file name too long: \"%s\"", dname);
716
717 strcpy(buf, dname);
718 strcat(buf, "/");
719 strcat(buf, relativeFilename);
720}

References buf, lclContext::directory, _archiveHandle::formatData, if(), MAXPGPATH, and pg_fatal.

Referenced by _CloseArchive(), _LoadLOs(), _PrepParallelRestore(), _PrintTocData(), _StartData(), _StartLOs(), and InitArchiveFmt_Directory().