PostgreSQL Source Code  git master
pg_backup_archiver.h File Reference
#include <time.h>
#include "libpq-fe.h"
#include "pg_backup.h"
#include "pqexpbuffer.h"
Include dependency graph for pg_backup_archiver.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  sqlparseInfo
 
struct  _archiveHandle
 
struct  _tocEntry
 
struct  _archiveOpts
 

Macros

#define LOBBUFSIZE   16384
 
#define BLK_DATA   1
 
#define BLK_BLOBS   3
 
#define MAKE_ARCHIVE_VERSION(major, minor, rev)   (((major) * 256 + (minor)) * 256 + (rev))
 
#define ARCHIVE_MAJOR(version)   (((version) >> 16) & 255)
 
#define ARCHIVE_MINOR(version)   (((version) >> 8) & 255)
 
#define ARCHIVE_REV(version)   (((version) ) & 255)
 
#define K_VERS_1_0   MAKE_ARCHIVE_VERSION(1, 0, 0)
 
#define K_VERS_1_2   MAKE_ARCHIVE_VERSION(1, 2, 0) /* Allow No ZLIB */
 
#define K_VERS_1_3   MAKE_ARCHIVE_VERSION(1, 3, 0) /* BLOBS */
 
#define K_VERS_1_4   MAKE_ARCHIVE_VERSION(1, 4, 0) /* Date & name in header */
 
#define K_VERS_1_5   MAKE_ARCHIVE_VERSION(1, 5, 0) /* Handle dependencies */
 
#define K_VERS_1_6   MAKE_ARCHIVE_VERSION(1, 6, 0) /* Schema field in TOCs */
 
#define K_VERS_1_7
 
#define K_VERS_1_8
 
#define K_VERS_1_9
 
#define K_VERS_1_10   MAKE_ARCHIVE_VERSION(1, 10, 0) /* add tablespace */
 
#define K_VERS_1_11
 
#define K_VERS_1_12
 
#define K_VERS_1_13
 
#define K_VERS_1_14   MAKE_ARCHIVE_VERSION(1, 14, 0) /* add tableam */
 
#define K_VERS_1_15
 
#define K_VERS_1_16
 
#define K_VERS_MAJOR   1
 
#define K_VERS_MINOR   16
 
#define K_VERS_REV   0
 
#define K_VERS_SELF   MAKE_ARCHIVE_VERSION(K_VERS_MAJOR, K_VERS_MINOR, K_VERS_REV)
 
#define K_VERS_MAX   MAKE_ARCHIVE_VERSION(K_VERS_MAJOR, K_VERS_MINOR, 255)
 
#define K_OFFSET_POS_NOT_SET   1
 
#define K_OFFSET_POS_SET   2
 
#define K_OFFSET_NO_DATA   3
 
#define WORKER_OK   0
 
#define WORKER_CREATE_DONE   10
 
#define WORKER_INHIBIT_DATA   11
 
#define WORKER_IGNORED_ERRORS   12
 
#define READ_ERROR_EXIT(fd)
 
#define WRITE_ERROR_EXIT
 
#define RESTORE_PASS_LAST   RESTORE_PASS_POST_ACL
 
#define REQ_SCHEMA   0x01 /* want schema */
 
#define REQ_DATA   0x02 /* want data */
 
#define REQ_SPECIAL   0x04 /* for special TOC entries */
 
#define ARCHIVE_OPTS(...)   &(ArchiveOpts){__VA_ARGS__}
 
#define appendStringLiteralAHX(buf, str, AH)    appendStringLiteral(buf, str, (AH)->public.encoding, (AH)->public.std_strings)
 
#define appendByteaLiteralAHX(buf, str, len, AH)    appendByteaLiteral(buf, str, len, (AH)->public.std_strings)
 

Typedefs

typedef struct _archiveHandle ArchiveHandle
 
typedef struct _tocEntry TocEntry
 
typedef enum T_Action T_Action
 
typedef void(* ClosePtrType) (ArchiveHandle *AH)
 
typedef void(* ReopenPtrType) (ArchiveHandle *AH)
 
typedef void(* ArchiveEntryPtrType) (ArchiveHandle *AH, TocEntry *te)
 
typedef void(* StartDataPtrType) (ArchiveHandle *AH, TocEntry *te)
 
typedef void(* WriteDataPtrType) (ArchiveHandle *AH, const void *data, size_t dLen)
 
typedef void(* EndDataPtrType) (ArchiveHandle *AH, TocEntry *te)
 
typedef void(* StartLOsPtrType) (ArchiveHandle *AH, TocEntry *te)
 
typedef void(* StartLOPtrType) (ArchiveHandle *AH, TocEntry *te, Oid oid)
 
typedef void(* EndLOPtrType) (ArchiveHandle *AH, TocEntry *te, Oid oid)
 
typedef void(* EndLOsPtrType) (ArchiveHandle *AH, TocEntry *te)
 
typedef int(* WriteBytePtrType) (ArchiveHandle *AH, const int i)
 
typedef int(* ReadBytePtrType) (ArchiveHandle *AH)
 
typedef void(* WriteBufPtrType) (ArchiveHandle *AH, const void *c, size_t len)
 
typedef void(* ReadBufPtrType) (ArchiveHandle *AH, void *buf, size_t len)
 
typedef void(* WriteExtraTocPtrType) (ArchiveHandle *AH, TocEntry *te)
 
typedef void(* ReadExtraTocPtrType) (ArchiveHandle *AH, TocEntry *te)
 
typedef void(* PrintExtraTocPtrType) (ArchiveHandle *AH, TocEntry *te)
 
typedef void(* PrintTocDataPtrType) (ArchiveHandle *AH, TocEntry *te)
 
typedef void(* PrepParallelRestorePtrType) (ArchiveHandle *AH)
 
typedef void(* ClonePtrType) (ArchiveHandle *AH)
 
typedef void(* DeClonePtrType) (ArchiveHandle *AH)
 
typedef int(* WorkerJobDumpPtrType) (ArchiveHandle *AH, TocEntry *te)
 
typedef int(* WorkerJobRestorePtrType) (ArchiveHandle *AH, TocEntry *te)
 
typedef size_t(* CustomOutPtrType) (ArchiveHandle *AH, const void *buf, size_t len)
 
typedef void struct _archiveOpts ArchiveOpts
 

Enumerations

enum  T_Action { ACT_DUMP , ACT_RESTORE }
 
enum  sqlparseState { SQL_SCAN = 0 , SQL_IN_SINGLE_QUOTE , SQL_IN_DOUBLE_QUOTE }
 
enum  ArchiverStage { STAGE_NONE = 0 , STAGE_INITIALIZING , STAGE_PROCESSING , STAGE_FINALIZING }
 
enum  ArchiverOutput { OUTPUT_SQLCMDS = 0 , OUTPUT_COPYDATA , OUTPUT_OTHERDATA }
 
enum  RestorePass { RESTORE_PASS_MAIN = 0 , RESTORE_PASS_ACL , RESTORE_PASS_POST_ACL }
 

Functions

int parallel_restore (ArchiveHandle *AH, TocEntry *te)
 
void on_exit_close_archive (Archive *AHX)
 
void warn_or_exit_horribly (ArchiveHandle *AH, const char *fmt,...) pg_attribute_printf(2
 
TocEntryArchiveEntry (Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
 
void WriteHead (ArchiveHandle *AH)
 
void ReadHead (ArchiveHandle *AH)
 
void WriteToc (ArchiveHandle *AH)
 
void ReadToc (ArchiveHandle *AH)
 
void WriteDataChunks (ArchiveHandle *AH, struct ParallelState *pstate)
 
void WriteDataChunksForTocEntry (ArchiveHandle *AH, TocEntry *te)
 
ArchiveHandleCloneArchive (ArchiveHandle *AH)
 
void DeCloneArchive (ArchiveHandle *AH)
 
int TocIDRequired (ArchiveHandle *AH, DumpId id)
 
TocEntrygetTocEntryByDumpId (ArchiveHandle *AH, DumpId id)
 
bool checkSeek (FILE *fp)
 
size_t WriteInt (ArchiveHandle *AH, int i)
 
int ReadInt (ArchiveHandle *AH)
 
char * ReadStr (ArchiveHandle *AH)
 
size_t WriteStr (ArchiveHandle *AH, const char *c)
 
int ReadOffset (ArchiveHandle *, pgoff_t *)
 
size_t WriteOffset (ArchiveHandle *, pgoff_t, int)
 
void StartRestoreLOs (ArchiveHandle *AH)
 
void StartRestoreLO (ArchiveHandle *AH, Oid oid, bool drop)
 
void EndRestoreLO (ArchiveHandle *AH, Oid oid)
 
void EndRestoreLOs (ArchiveHandle *AH)
 
void InitArchiveFmt_Custom (ArchiveHandle *AH)
 
void InitArchiveFmt_Null (ArchiveHandle *AH)
 
void InitArchiveFmt_Directory (ArchiveHandle *AH)
 
void InitArchiveFmt_Tar (ArchiveHandle *AH)
 
bool isValidTarHeader (char *header)
 
void ReconnectToServer (ArchiveHandle *AH, const char *dbname)
 
void IssueCommandPerBlob (ArchiveHandle *AH, TocEntry *te, const char *cmdBegin, const char *cmdEnd)
 
void IssueACLPerBlob (ArchiveHandle *AH, TocEntry *te)
 
void DropLOIfExists (ArchiveHandle *AH, Oid oid)
 
void ahwrite (const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
 
int ahprintf (ArchiveHandle *AH, const char *fmt,...) pg_attribute_printf(2
 

Macro Definition Documentation

◆ appendByteaLiteralAHX

#define appendByteaLiteralAHX (   buf,
  str,
  len,
  AH 
)     appendByteaLiteral(buf, str, len, (AH)->public.std_strings)

Definition at line 420 of file pg_backup_archiver.h.

◆ appendStringLiteralAHX

#define appendStringLiteralAHX (   buf,
  str,
  AH 
)     appendStringLiteral(buf, str, (AH)->public.encoding, (AH)->public.std_strings)

Definition at line 417 of file pg_backup_archiver.h.

◆ ARCHIVE_MAJOR

#define ARCHIVE_MAJOR (   version)    (((version) >> 16) & 255)

Definition at line 42 of file pg_backup_archiver.h.

◆ ARCHIVE_MINOR

#define ARCHIVE_MINOR (   version)    (((version) >> 8) & 255)

Definition at line 43 of file pg_backup_archiver.h.

◆ ARCHIVE_OPTS

#define ARCHIVE_OPTS (   ...)    &(ArchiveOpts){__VA_ARGS__}

Definition at line 399 of file pg_backup_archiver.h.

◆ ARCHIVE_REV

#define ARCHIVE_REV (   version)    (((version) ) & 255)

Definition at line 44 of file pg_backup_archiver.h.

◆ BLK_BLOBS

#define BLK_BLOBS   3

Definition at line 37 of file pg_backup_archiver.h.

◆ BLK_DATA

#define BLK_DATA   1

Definition at line 36 of file pg_backup_archiver.h.

◆ K_OFFSET_NO_DATA

#define K_OFFSET_NO_DATA   3

Definition at line 77 of file pg_backup_archiver.h.

◆ K_OFFSET_POS_NOT_SET

#define K_OFFSET_POS_NOT_SET   1

Definition at line 75 of file pg_backup_archiver.h.

◆ K_OFFSET_POS_SET

#define K_OFFSET_POS_SET   2

Definition at line 76 of file pg_backup_archiver.h.

◆ K_VERS_1_0

#define K_VERS_1_0   MAKE_ARCHIVE_VERSION(1, 0, 0)

Definition at line 47 of file pg_backup_archiver.h.

◆ K_VERS_1_10

#define K_VERS_1_10   MAKE_ARCHIVE_VERSION(1, 10, 0) /* add tablespace */

Definition at line 56 of file pg_backup_archiver.h.

◆ K_VERS_1_11

#define K_VERS_1_11
Value:
MAKE_ARCHIVE_VERSION(1, 11, 0) /* add toc section
* indicator */
#define MAKE_ARCHIVE_VERSION(major, minor, rev)

Definition at line 57 of file pg_backup_archiver.h.

◆ K_VERS_1_12

#define K_VERS_1_12
Value:
MAKE_ARCHIVE_VERSION(1, 12, 0) /* add separate BLOB
* entries */

Definition at line 58 of file pg_backup_archiver.h.

◆ K_VERS_1_13

#define K_VERS_1_13
Value:
MAKE_ARCHIVE_VERSION(1, 13, 0) /* change search_path
* behavior */

Definition at line 59 of file pg_backup_archiver.h.

◆ K_VERS_1_14

#define K_VERS_1_14   MAKE_ARCHIVE_VERSION(1, 14, 0) /* add tableam */

Definition at line 60 of file pg_backup_archiver.h.

◆ K_VERS_1_15

#define K_VERS_1_15
Value:
MAKE_ARCHIVE_VERSION(1, 15, 0) /* add
* compression_algorithm
* in header */

Definition at line 61 of file pg_backup_archiver.h.

◆ K_VERS_1_16

#define K_VERS_1_16
Value:
MAKE_ARCHIVE_VERSION(1, 16, 0) /* BLOB METADATA entries
* and multiple BLOBS,
* relkind */

Definition at line 62 of file pg_backup_archiver.h.

◆ K_VERS_1_2

#define K_VERS_1_2   MAKE_ARCHIVE_VERSION(1, 2, 0) /* Allow No ZLIB */

Definition at line 48 of file pg_backup_archiver.h.

◆ K_VERS_1_3

#define K_VERS_1_3   MAKE_ARCHIVE_VERSION(1, 3, 0) /* BLOBS */

Definition at line 49 of file pg_backup_archiver.h.

◆ K_VERS_1_4

#define K_VERS_1_4   MAKE_ARCHIVE_VERSION(1, 4, 0) /* Date & name in header */

Definition at line 50 of file pg_backup_archiver.h.

◆ K_VERS_1_5

#define K_VERS_1_5   MAKE_ARCHIVE_VERSION(1, 5, 0) /* Handle dependencies */

Definition at line 51 of file pg_backup_archiver.h.

◆ K_VERS_1_6

#define K_VERS_1_6   MAKE_ARCHIVE_VERSION(1, 6, 0) /* Schema field in TOCs */

Definition at line 52 of file pg_backup_archiver.h.

◆ K_VERS_1_7

#define K_VERS_1_7
Value:
MAKE_ARCHIVE_VERSION(1, 7, 0) /* File Offset size in
* header */

Definition at line 53 of file pg_backup_archiver.h.

◆ K_VERS_1_8

#define K_VERS_1_8
Value:
MAKE_ARCHIVE_VERSION(1, 8, 0) /* change interpretation
* of ID numbers and
* dependencies */

Definition at line 54 of file pg_backup_archiver.h.

◆ K_VERS_1_9

#define K_VERS_1_9
Value:
MAKE_ARCHIVE_VERSION(1, 9, 0) /* add default_with_oids
* tracking */

Definition at line 55 of file pg_backup_archiver.h.

◆ K_VERS_MAJOR

#define K_VERS_MAJOR   1

Definition at line 65 of file pg_backup_archiver.h.

◆ K_VERS_MAX

#define K_VERS_MAX   MAKE_ARCHIVE_VERSION(K_VERS_MAJOR, K_VERS_MINOR, 255)

Definition at line 71 of file pg_backup_archiver.h.

◆ K_VERS_MINOR

#define K_VERS_MINOR   16

Definition at line 66 of file pg_backup_archiver.h.

◆ K_VERS_REV

#define K_VERS_REV   0

Definition at line 67 of file pg_backup_archiver.h.

◆ K_VERS_SELF

Definition at line 68 of file pg_backup_archiver.h.

◆ LOBBUFSIZE

#define LOBBUFSIZE   16384

Definition at line 33 of file pg_backup_archiver.h.

◆ MAKE_ARCHIVE_VERSION

#define MAKE_ARCHIVE_VERSION (   major,
  minor,
  rev 
)    (((major) * 256 + (minor)) * 256 + (rev))

Definition at line 40 of file pg_backup_archiver.h.

◆ READ_ERROR_EXIT

#define READ_ERROR_EXIT (   fd)
Value:
do { \
if (feof(fd)) \
pg_fatal("could not read from input file: end of file"); \
pg_fatal("could not read from input file: %m"); \
} while (0)
#define pg_fatal(...)
static int fd(const char *x, int i)
Definition: preproc-init.c:105

Definition at line 92 of file pg_backup_archiver.h.

◆ REQ_DATA

#define REQ_DATA   0x02 /* want data */

Definition at line 200 of file pg_backup_archiver.h.

◆ REQ_SCHEMA

#define REQ_SCHEMA   0x01 /* want schema */

Definition at line 199 of file pg_backup_archiver.h.

◆ REQ_SPECIAL

#define REQ_SPECIAL   0x04 /* for special TOC entries */

Definition at line 201 of file pg_backup_archiver.h.

◆ RESTORE_PASS_LAST

#define RESTORE_PASS_LAST   RESTORE_PASS_POST_ACL

Definition at line 196 of file pg_backup_archiver.h.

◆ WORKER_CREATE_DONE

#define WORKER_CREATE_DONE   10

Definition at line 84 of file pg_backup_archiver.h.

◆ WORKER_IGNORED_ERRORS

#define WORKER_IGNORED_ERRORS   12

Definition at line 86 of file pg_backup_archiver.h.

◆ WORKER_INHIBIT_DATA

#define WORKER_INHIBIT_DATA   11

Definition at line 85 of file pg_backup_archiver.h.

◆ WORKER_OK

#define WORKER_OK   0

Definition at line 83 of file pg_backup_archiver.h.

◆ WRITE_ERROR_EXIT

#define WRITE_ERROR_EXIT
Value:
do { \
pg_fatal("could not write to output file: %m"); \
} while (0)

Definition at line 100 of file pg_backup_archiver.h.

Typedef Documentation

◆ ArchiveEntryPtrType

typedef void(* ArchiveEntryPtrType) (ArchiveHandle *AH, TocEntry *te)

Definition at line 113 of file pg_backup_archiver.h.

◆ ArchiveHandle

typedef struct _archiveHandle ArchiveHandle

Definition at line 1 of file pg_backup_archiver.h.

◆ ArchiveOpts

typedef void struct _archiveOpts ArchiveOpts

◆ ClonePtrType

typedef void(* ClonePtrType) (ArchiveHandle *AH)

Definition at line 134 of file pg_backup_archiver.h.

◆ ClosePtrType

typedef void(* ClosePtrType) (ArchiveHandle *AH)

Definition at line 111 of file pg_backup_archiver.h.

◆ CustomOutPtrType

typedef size_t(* CustomOutPtrType) (ArchiveHandle *AH, const void *buf, size_t len)

Definition at line 140 of file pg_backup_archiver.h.

◆ DeClonePtrType

typedef void(* DeClonePtrType) (ArchiveHandle *AH)

Definition at line 135 of file pg_backup_archiver.h.

◆ EndDataPtrType

typedef void(* EndDataPtrType) (ArchiveHandle *AH, TocEntry *te)

Definition at line 117 of file pg_backup_archiver.h.

◆ EndLOPtrType

typedef void(* EndLOPtrType) (ArchiveHandle *AH, TocEntry *te, Oid oid)

Definition at line 121 of file pg_backup_archiver.h.

◆ EndLOsPtrType

typedef void(* EndLOsPtrType) (ArchiveHandle *AH, TocEntry *te)

Definition at line 122 of file pg_backup_archiver.h.

◆ PrepParallelRestorePtrType

typedef void(* PrepParallelRestorePtrType) (ArchiveHandle *AH)

Definition at line 133 of file pg_backup_archiver.h.

◆ PrintExtraTocPtrType

typedef void(* PrintExtraTocPtrType) (ArchiveHandle *AH, TocEntry *te)

Definition at line 130 of file pg_backup_archiver.h.

◆ PrintTocDataPtrType

typedef void(* PrintTocDataPtrType) (ArchiveHandle *AH, TocEntry *te)

Definition at line 131 of file pg_backup_archiver.h.

◆ ReadBufPtrType

typedef void(* ReadBufPtrType) (ArchiveHandle *AH, void *buf, size_t len)

Definition at line 127 of file pg_backup_archiver.h.

◆ ReadBytePtrType

typedef int(* ReadBytePtrType) (ArchiveHandle *AH)

Definition at line 125 of file pg_backup_archiver.h.

◆ ReadExtraTocPtrType

typedef void(* ReadExtraTocPtrType) (ArchiveHandle *AH, TocEntry *te)

Definition at line 129 of file pg_backup_archiver.h.

◆ ReopenPtrType

typedef void(* ReopenPtrType) (ArchiveHandle *AH)

Definition at line 112 of file pg_backup_archiver.h.

◆ StartDataPtrType

typedef void(* StartDataPtrType) (ArchiveHandle *AH, TocEntry *te)

Definition at line 115 of file pg_backup_archiver.h.

◆ StartLOPtrType

typedef void(* StartLOPtrType) (ArchiveHandle *AH, TocEntry *te, Oid oid)

Definition at line 120 of file pg_backup_archiver.h.

◆ StartLOsPtrType

typedef void(* StartLOsPtrType) (ArchiveHandle *AH, TocEntry *te)

Definition at line 119 of file pg_backup_archiver.h.

◆ T_Action

typedef enum T_Action T_Action

◆ TocEntry

typedef struct _tocEntry TocEntry

Definition at line 1 of file pg_backup_archiver.h.

◆ WorkerJobDumpPtrType

typedef int(* WorkerJobDumpPtrType) (ArchiveHandle *AH, TocEntry *te)

Definition at line 137 of file pg_backup_archiver.h.

◆ WorkerJobRestorePtrType

typedef int(* WorkerJobRestorePtrType) (ArchiveHandle *AH, TocEntry *te)

Definition at line 138 of file pg_backup_archiver.h.

◆ WriteBufPtrType

typedef void(* WriteBufPtrType) (ArchiveHandle *AH, const void *c, size_t len)

Definition at line 126 of file pg_backup_archiver.h.

◆ WriteBytePtrType

typedef int(* WriteBytePtrType) (ArchiveHandle *AH, const int i)

Definition at line 124 of file pg_backup_archiver.h.

◆ WriteDataPtrType

typedef void(* WriteDataPtrType) (ArchiveHandle *AH, const void *data, size_t dLen)

Definition at line 116 of file pg_backup_archiver.h.

◆ WriteExtraTocPtrType

typedef void(* WriteExtraTocPtrType) (ArchiveHandle *AH, TocEntry *te)

Definition at line 128 of file pg_backup_archiver.h.

Enumeration Type Documentation

◆ ArchiverOutput

Enumerator
OUTPUT_SQLCMDS 
OUTPUT_COPYDATA 
OUTPUT_OTHERDATA 

Definition at line 164 of file pg_backup_archiver.h.

168 {
169  STAGE_NONE = 0,
@ STAGE_NONE

◆ ArchiverStage

Enumerator
STAGE_NONE 
STAGE_INITIALIZING 
STAGE_PROCESSING 
STAGE_FINALIZING 

Definition at line 156 of file pg_backup_archiver.h.

161 {
162  sqlparseState state; /* see above */
sqlparseState
Definition: regguts.h:323

◆ RestorePass

Enumerator
RESTORE_PASS_MAIN 
RESTORE_PASS_ACL 
RESTORE_PASS_POST_ACL 

Definition at line 190 of file pg_backup_archiver.h.

202 {

◆ sqlparseState

Enumerator
SQL_SCAN 
SQL_IN_SINGLE_QUOTE 
SQL_IN_DOUBLE_QUOTE 

Definition at line 142 of file pg_backup_archiver.h.

154 {

◆ T_Action

enum T_Action
Enumerator
ACT_DUMP 
ACT_RESTORE 

Definition at line 105 of file pg_backup_archiver.h.

106  : end of file"); \
107  else \
108  pg_fatal("could not read from input file: %m"); \
109  } while (0)
FILE * input
#define read(a, b, c)
Definition: win32.h:13

Function Documentation

◆ ahprintf()

int ahprintf ( ArchiveHandle AH,
const char *  fmt,
  ... 
)

◆ ahwrite()

void ahwrite ( const void *  ptr,
size_t  size,
size_t  nmemb,
ArchiveHandle AH 
)

Definition at line 1805 of file pg_backup_archiver.c.

1806 {
1807  int bytes_written = 0;
1808 
1809  if (AH->writingLO)
1810  {
1811  size_t remaining = size * nmemb;
1812 
1813  while (AH->lo_buf_used + remaining > AH->lo_buf_size)
1814  {
1815  size_t avail = AH->lo_buf_size - AH->lo_buf_used;
1816 
1817  memcpy((char *) AH->lo_buf + AH->lo_buf_used, ptr, avail);
1818  ptr = (const void *) ((const char *) ptr + avail);
1819  remaining -= avail;
1820  AH->lo_buf_used += avail;
1821  dump_lo_buf(AH);
1822  }
1823 
1824  memcpy((char *) AH->lo_buf + AH->lo_buf_used, ptr, remaining);
1825  AH->lo_buf_used += remaining;
1826 
1827  bytes_written = size * nmemb;
1828  }
1829  else if (AH->CustomOutPtr)
1830  bytes_written = AH->CustomOutPtr(AH, ptr, size * nmemb);
1831 
1832  /*
1833  * If we're doing a restore, and it's direct to DB, and we're connected
1834  * then send it to the DB.
1835  */
1836  else if (RestoringToDB(AH))
1837  bytes_written = ExecuteSqlCommandBuf(&AH->public, (const char *) ptr, size * nmemb);
1838  else
1839  {
1840  CompressFileHandle *CFH = (CompressFileHandle *) AH->OF;
1841 
1842  if (CFH->write_func(ptr, size * nmemb, CFH))
1843  bytes_written = size * nmemb;
1844  }
1845 
1846  if (bytes_written != size * nmemb)
1848 }
int remaining
Definition: informix.c:673
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
static int RestoringToDB(ArchiveHandle *AH)
static void dump_lo_buf(ArchiveHandle *AH)
#define WRITE_ERROR_EXIT
int ExecuteSqlCommandBuf(Archive *AHX, const char *buf, size_t bufLen)
Definition: pg_backup_db.c:445
static pg_noinline void Size size
Definition: slab.c:607
bool(* write_func)(const void *ptr, size_t size, struct CompressFileHandle *CFH)
Definition: compress_io.h:139
CustomOutPtrType CustomOutPtr

References _archiveHandle::CustomOutPtr, dump_lo_buf(), ExecuteSqlCommandBuf(), if(), _archiveHandle::lo_buf, _archiveHandle::lo_buf_size, _archiveHandle::lo_buf_used, _archiveHandle::OF, _archiveHandle::public, remaining, RestoringToDB(), size, WRITE_ERROR_EXIT, CompressFileHandle::write_func, and _archiveHandle::writingLO.

Referenced by _LoadLOs(), _PrintFileData(), _PrintTocData(), _WriteData(), ahprintf(), and ReadDataFromArchiveNone().

◆ ArchiveEntry()

TocEntry* ArchiveEntry ( Archive AHX,
CatalogId  catalogId,
DumpId  dumpId,
ArchiveOpts opts 
)

Definition at line 1203 of file pg_backup_archiver.c.

1205 {
1206  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1207  TocEntry *newToc;
1208 
1209  newToc = (TocEntry *) pg_malloc0(sizeof(TocEntry));
1210 
1211  AH->tocCount++;
1212  if (dumpId > AH->maxDumpId)
1213  AH->maxDumpId = dumpId;
1214 
1215  newToc->prev = AH->toc->prev;
1216  newToc->next = AH->toc;
1217  AH->toc->prev->next = newToc;
1218  AH->toc->prev = newToc;
1219 
1220  newToc->catalogId = catalogId;
1221  newToc->dumpId = dumpId;
1222  newToc->section = opts->section;
1223 
1224  newToc->tag = pg_strdup(opts->tag);
1225  newToc->namespace = opts->namespace ? pg_strdup(opts->namespace) : NULL;
1226  newToc->tablespace = opts->tablespace ? pg_strdup(opts->tablespace) : NULL;
1227  newToc->tableam = opts->tableam ? pg_strdup(opts->tableam) : NULL;
1228  newToc->relkind = opts->relkind;
1229  newToc->owner = opts->owner ? pg_strdup(opts->owner) : NULL;
1230  newToc->desc = pg_strdup(opts->description);
1231  newToc->defn = opts->createStmt ? pg_strdup(opts->createStmt) : NULL;
1232  newToc->dropStmt = opts->dropStmt ? pg_strdup(opts->dropStmt) : NULL;
1233  newToc->copyStmt = opts->copyStmt ? pg_strdup(opts->copyStmt) : NULL;
1234 
1235  if (opts->nDeps > 0)
1236  {
1237  newToc->dependencies = (DumpId *) pg_malloc(opts->nDeps * sizeof(DumpId));
1238  memcpy(newToc->dependencies, opts->deps, opts->nDeps * sizeof(DumpId));
1239  newToc->nDeps = opts->nDeps;
1240  }
1241  else
1242  {
1243  newToc->dependencies = NULL;
1244  newToc->nDeps = 0;
1245  }
1246 
1247  newToc->dataDumper = opts->dumpFn;
1248  newToc->dataDumperArg = opts->dumpArg;
1249  newToc->hadDumper = opts->dumpFn ? true : false;
1250 
1251  newToc->formatData = NULL;
1252  newToc->dataLength = 0;
1253 
1254  if (AH->ArchiveEntryPtr != NULL)
1255  AH->ArchiveEntryPtr(AH, newToc);
1256 
1257  return newToc;
1258 }
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
return true
Definition: isn.c:126
static AmcheckOptions opts
Definition: pg_amcheck.c:111
int DumpId
Definition: pg_backup.h:270
struct _tocEntry * toc
ArchiveEntryPtrType ArchiveEntryPtr
struct _tocEntry * prev
teSection section
DataDumperPtr dataDumper
pgoff_t dataLength
CatalogId catalogId
struct _tocEntry * next
const void * dataDumperArg
DumpId * dependencies

References _archiveHandle::ArchiveEntryPtr, _tocEntry::catalogId, _tocEntry::copyStmt, _tocEntry::dataDumper, _tocEntry::dataDumperArg, _tocEntry::dataLength, _tocEntry::defn, _tocEntry::dependencies, _tocEntry::desc, _tocEntry::dropStmt, _tocEntry::dumpId, _tocEntry::formatData, _tocEntry::hadDumper, _archiveHandle::maxDumpId, _tocEntry::nDeps, _tocEntry::next, opts, _tocEntry::owner, pg_malloc(), pg_malloc0(), pg_strdup(), _tocEntry::prev, _tocEntry::relkind, _tocEntry::section, _tocEntry::tableam, _tocEntry::tablespace, _tocEntry::tag, _archiveHandle::toc, _archiveHandle::tocCount, and true.

Referenced by dumpAccessMethod(), dumpACL(), dumpAgg(), dumpAttrDef(), dumpBaseType(), dumpCast(), dumpCollation(), dumpCommentExtended(), dumpCompositeType(), dumpCompositeTypeColComments(), dumpConstraint(), dumpConversion(), dumpDatabase(), dumpDefaultACL(), dumpDomain(), dumpDumpableObject(), dumpEncoding(), dumpEnumType(), dumpEventTrigger(), dumpExtension(), dumpForeignDataWrapper(), dumpForeignServer(), dumpFunc(), dumpIndex(), dumpIndexAttach(), dumpLO(), dumpNamespace(), dumpOpclass(), dumpOpfamily(), dumpOpr(), dumpPolicy(), dumpProcLang(), dumpPublication(), dumpPublicationNamespace(), dumpPublicationTable(), dumpRangeType(), dumpRule(), dumpSearchPath(), dumpSecLabel(), dumpSequence(), dumpSequenceData(), dumpShellType(), dumpStatisticsExt(), dumpStdStrings(), dumpSubscription(), dumpSubscriptionTable(), dumpTableAttach(), dumpTableComment(), dumpTableData(), dumpTableSchema(), dumpTableSecLabel(), dumpTransform(), dumpTrigger(), dumpTSConfig(), dumpTSDictionary(), dumpTSParser(), dumpTSTemplate(), dumpUndefinedType(), dumpUserMappings(), and refreshMatViewData().

◆ checkSeek()

bool checkSeek ( FILE *  fp)

Definition at line 4092 of file pg_backup_archiver.c.

4093 {
4094  pgoff_t tpos;
4095 
4096  /* Check that ftello works on this file */
4097  tpos = ftello(fp);
4098  if (tpos < 0)
4099  return false;
4100 
4101  /*
4102  * Check that fseeko(SEEK_SET) works, too. NB: we used to try to test
4103  * this with fseeko(fp, 0, SEEK_CUR). But some platforms treat that as a
4104  * successful no-op even on files that are otherwise unseekable.
4105  */
4106  if (fseeko(fp, tpos, SEEK_SET) != 0)
4107  return false;
4108 
4109  return true;
4110 }
#define ftello(stream)
Definition: win32_port.h:219
#define fseeko(stream, offset, origin)
Definition: win32_port.h:216
#define pgoff_t
Definition: win32_port.h:207

References fseeko, ftello, and pgoff_t.

Referenced by InitArchiveFmt_Custom(), and InitArchiveFmt_Tar().

◆ CloneArchive()

ArchiveHandle* CloneArchive ( ArchiveHandle AH)

Definition at line 4959 of file pg_backup_archiver.c.

4960 {
4961  ArchiveHandle *clone;
4962 
4963  /* Make a "flat" copy */
4964  clone = (ArchiveHandle *) pg_malloc(sizeof(ArchiveHandle));
4965  memcpy(clone, AH, sizeof(ArchiveHandle));
4966 
4967  /* Likewise flat-copy the RestoreOptions, so we can alter them locally */
4968  clone->public.ropt = (RestoreOptions *) pg_malloc(sizeof(RestoreOptions));
4969  memcpy(clone->public.ropt, AH->public.ropt, sizeof(RestoreOptions));
4970 
4971  /* Handle format-independent fields */
4972  memset(&(clone->sqlparse), 0, sizeof(clone->sqlparse));
4973 
4974  /* The clone will have its own connection, so disregard connection state */
4975  clone->connection = NULL;
4976  clone->connCancel = NULL;
4977  clone->currUser = NULL;
4978  clone->currSchema = NULL;
4979  clone->currTableAm = NULL;
4980  clone->currTablespace = NULL;
4981 
4982  /* savedPassword must be local in case we change it while connecting */
4983  if (clone->savedPassword)
4984  clone->savedPassword = pg_strdup(clone->savedPassword);
4985 
4986  /* clone has its own error count, too */
4987  clone->public.n_errors = 0;
4988 
4989  /* clones should not share lo_buf */
4990  clone->lo_buf = NULL;
4991 
4992  /*
4993  * Clone connections disregard --transaction-size; they must commit after
4994  * each command so that the results are immediately visible to other
4995  * workers.
4996  */
4997  clone->public.ropt->txn_size = 0;
4998 
4999  /*
5000  * Connect our new clone object to the database, using the same connection
5001  * parameters used for the original connection.
5002  */
5003  ConnectDatabase((Archive *) clone, &clone->public.ropt->cparams, true);
5004 
5005  /* re-establish fixed state */
5006  if (AH->mode == archModeRead)
5007  _doSetFixedOutputState(clone);
5008  /* in write case, setupDumpWorker will fix up connection state */
5009 
5010  /* Let the format-specific code have a chance too */
5011  clone->ClonePtr(clone);
5012 
5013  Assert(clone->connection != NULL);
5014  return clone;
5015 }
#define Assert(condition)
Definition: c.h:858
void ConnectDatabase(Archive *AHX, const ConnParams *cparams, bool isReconnect)
Definition: pg_backup_db.c:110
@ archModeRead
Definition: pg_backup.h:52
static void _doSetFixedOutputState(ArchiveHandle *AH)
int n_errors
Definition: pg_backup.h:238
RestoreOptions * ropt
Definition: pg_backup.h:215
PGcancel *volatile connCancel
ClonePtrType ClonePtr
sqlparseInfo sqlparse
ConnParams cparams
Definition: pg_backup.h:144

References _doSetFixedOutputState(), archModeRead, Assert, _archiveHandle::ClonePtr, _archiveHandle::connCancel, ConnectDatabase(), _archiveHandle::connection, _restoreOptions::cparams, _archiveHandle::currSchema, _archiveHandle::currTableAm, _archiveHandle::currTablespace, _archiveHandle::currUser, _archiveHandle::lo_buf, _archiveHandle::mode, Archive::n_errors, pg_malloc(), pg_strdup(), _archiveHandle::public, Archive::ropt, _archiveHandle::savedPassword, _archiveHandle::sqlparse, and _restoreOptions::txn_size.

Referenced by RunWorker().

◆ DeCloneArchive()

void DeCloneArchive ( ArchiveHandle AH)

Definition at line 5023 of file pg_backup_archiver.c.

5024 {
5025  /* Should not have an open database connection */
5026  Assert(AH->connection == NULL);
5027 
5028  /* Clear format-specific state */
5029  AH->DeClonePtr(AH);
5030 
5031  /* Clear state allocated by CloneArchive */
5032  if (AH->sqlparse.curCmd)
5034 
5035  /* Clear any connection-local state */
5036  free(AH->currUser);
5037  free(AH->currSchema);
5038  free(AH->currTablespace);
5039  free(AH->currTableAm);
5040  free(AH->savedPassword);
5041 
5042  free(AH);
5043 }
#define free(a)
Definition: header.h:65
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114
DeClonePtrType DeClonePtr
PQExpBuffer curCmd

References Assert, _archiveHandle::connection, sqlparseInfo::curCmd, _archiveHandle::currSchema, _archiveHandle::currTableAm, _archiveHandle::currTablespace, _archiveHandle::currUser, _archiveHandle::DeClonePtr, destroyPQExpBuffer(), free, _archiveHandle::savedPassword, and _archiveHandle::sqlparse.

Referenced by RunWorker().

◆ DropLOIfExists()

void DropLOIfExists ( ArchiveHandle AH,
Oid  oid 
)

Definition at line 673 of file pg_backup_db.c.

674 {
675  ahprintf(AH,
676  "SELECT pg_catalog.lo_unlink(oid) "
677  "FROM pg_catalog.pg_largeobject_metadata "
678  "WHERE oid = '%u';\n",
679  oid);
680 }
int ahprintf(ArchiveHandle *AH, const char *fmt,...)

References ahprintf().

Referenced by _StartLO(), RestoreArchive(), and StartRestoreLO().

◆ EndRestoreLO()

void EndRestoreLO ( ArchiveHandle AH,
Oid  oid 
)

Definition at line 1500 of file pg_backup_archiver.c.

1501 {
1502  if (AH->lo_buf_used > 0)
1503  {
1504  /* Write remaining bytes from the LO buffer */
1505  dump_lo_buf(AH);
1506  }
1507 
1508  AH->writingLO = false;
1509 
1510  if (AH->connection)
1511  {
1512  lo_close(AH->connection, AH->loFd);
1513  AH->loFd = -1;
1514  }
1515  else
1516  {
1517  ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n");
1518  }
1519 }
int lo_close(PGconn *conn, int fd)
Definition: fe-lobj.c:96

References ahprintf(), _archiveHandle::connection, dump_lo_buf(), _archiveHandle::lo_buf_used, lo_close(), _archiveHandle::loFd, and _archiveHandle::writingLO.

Referenced by _LoadLOs().

◆ EndRestoreLOs()

void EndRestoreLOs ( ArchiveHandle AH)

Definition at line 1427 of file pg_backup_archiver.c.

1428 {
1429  RestoreOptions *ropt = AH->public.ropt;
1430 
1431  if (!(ropt->single_txn || ropt->txn_size > 0))
1432  {
1433  if (AH->connection)
1434  CommitTransaction(&AH->public);
1435  else
1436  ahprintf(AH, "COMMIT;\n\n");
1437  }
1438 
1439  pg_log_info(ngettext("restored %d large object",
1440  "restored %d large objects",
1441  AH->loCount),
1442  AH->loCount);
1443 }
#define ngettext(s, p, n)
Definition: c.h:1181
#define pg_log_info(...)
Definition: logging.h:124
static void CommitTransaction(void)
Definition: xact.c:2178

References ahprintf(), CommitTransaction(), _archiveHandle::connection, _archiveHandle::loCount, ngettext, pg_log_info, _archiveHandle::public, Archive::ropt, _restoreOptions::single_txn, and _restoreOptions::txn_size.

Referenced by _LoadLOs().

◆ getTocEntryByDumpId()

TocEntry* getTocEntryByDumpId ( ArchiveHandle AH,
DumpId  id 
)

Definition at line 1985 of file pg_backup_archiver.c.

1986 {
1987  /* build index arrays if we didn't already */
1988  if (AH->tocsByDumpId == NULL)
1989  buildTocEntryArrays(AH);
1990 
1991  if (id > 0 && id <= AH->maxDumpId)
1992  return AH->tocsByDumpId[id];
1993 
1994  return NULL;
1995 }
static void buildTocEntryArrays(ArchiveHandle *AH)
struct _tocEntry ** tocsByDumpId

References buildTocEntryArrays(), and _archiveHandle::tocsByDumpId.

Referenced by _PrintTocData(), _tocEntryRequired(), IssueACLPerBlob(), parseWorkerCommand(), SortTocFromFile(), and TocIDRequired().

◆ InitArchiveFmt_Custom()

void InitArchiveFmt_Custom ( ArchiveHandle AH)

Definition at line 106 of file pg_backup_custom.c.

107 {
108  lclContext *ctx;
109 
110  /* Assuming static functions, this can be copied for each format. */
112  AH->StartDataPtr = _StartData;
113  AH->WriteDataPtr = _WriteData;
114  AH->EndDataPtr = _EndData;
115  AH->WriteBytePtr = _WriteByte;
116  AH->ReadBytePtr = _ReadByte;
117  AH->WriteBufPtr = _WriteBuf;
118  AH->ReadBufPtr = _ReadBuf;
119  AH->ClosePtr = _CloseArchive;
125 
126  AH->StartLOsPtr = _StartLOs;
127  AH->StartLOPtr = _StartLO;
128  AH->EndLOPtr = _EndLO;
129  AH->EndLOsPtr = _EndLOs;
130 
132  AH->ClonePtr = _Clone;
133  AH->DeClonePtr = _DeClone;
134 
135  /* no parallel dump in the custom archive, only parallel restore */
136  AH->WorkerJobDumpPtr = NULL;
138 
139  /* Set up a private area. */
140  ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
141  AH->formatData = (void *) ctx;
142 
143  /*
144  * Now open the file
145  */
146  if (AH->mode == archModeWrite)
147  {
148  if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
149  {
150  AH->FH = fopen(AH->fSpec, PG_BINARY_W);
151  if (!AH->FH)
152  pg_fatal("could not open output file \"%s\": %m", AH->fSpec);
153  }
154  else
155  {
156  AH->FH = stdout;
157  if (!AH->FH)
158  pg_fatal("could not open output file: %m");
159  }
160 
161  ctx->hasSeek = checkSeek(AH->FH);
162  }
163  else
164  {
165  if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
166  {
167  AH->FH = fopen(AH->fSpec, PG_BINARY_R);
168  if (!AH->FH)
169  pg_fatal("could not open input file \"%s\": %m", AH->fSpec);
170  }
171  else
172  {
173  AH->FH = stdin;
174  if (!AH->FH)
175  pg_fatal("could not open input file: %m");
176  }
177 
178  ctx->hasSeek = checkSeek(AH->FH);
179 
180  ReadHead(AH);
181  ReadToc(AH);
182 
183  /*
184  * Remember location of first data block (i.e., the point after TOC)
185  * in case we have to search for desired data blocks.
186  */
187  ctx->lastFilePos = _getFilePos(AH, ctx);
188  }
189 }
#define PG_BINARY_R
Definition: c.h:1275
#define PG_BINARY_W
Definition: c.h:1276
@ archModeWrite
Definition: pg_backup.h:51
bool checkSeek(FILE *fp)
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 pgoff_t _getFilePos(ArchiveHandle *AH, lclContext *ctx)
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 int _WorkerJobRestoreCustom(ArchiveHandle *AH, TocEntry *te)
static void _Clone(ArchiveHandle *AH)
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
EndLOsPtrType EndLOsPtr
ReadExtraTocPtrType ReadExtraTocPtr
WorkerJobDumpPtrType WorkerJobDumpPtr
StartLOsPtrType StartLOsPtr
WriteDataPtrType WriteDataPtr
StartLOPtrType StartLOPtr
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
pgoff_t lastFilePos

References _ArchiveEntry(), _Clone(), _CloseArchive(), _DeClone(), _EndData(), _EndLO(), _EndLOs(), _getFilePos(), _PrepParallelRestore(), _PrintExtraToc(), _PrintTocData(), _ReadBuf(), _ReadByte(), _ReadExtraToc(), _ReopenArchive(), _StartData(), _StartLO(), _StartLOs(), _WorkerJobRestoreCustom(), _WriteBuf(), _WriteByte(), _WriteData(), _WriteExtraToc(), _archiveHandle::ArchiveEntryPtr, archModeWrite, checkSeek(), _archiveHandle::ClonePtr, _archiveHandle::ClosePtr, _archiveHandle::DeClonePtr, _archiveHandle::EndDataPtr, _archiveHandle::EndLOPtr, _archiveHandle::EndLOsPtr, _archiveHandle::FH, _archiveHandle::formatData, _archiveHandle::fSpec, lclContext::hasSeek, lclContext::lastFilePos, _archiveHandle::mode, PG_BINARY_R, PG_BINARY_W, pg_fatal, pg_malloc0(), _archiveHandle::PrepParallelRestorePtr, _archiveHandle::PrintExtraTocPtr, _archiveHandle::PrintTocDataPtr, _archiveHandle::ReadBufPtr, _archiveHandle::ReadBytePtr, _archiveHandle::ReadExtraTocPtr, ReadHead(), ReadToc(), _archiveHandle::ReopenPtr, _archiveHandle::StartDataPtr, _archiveHandle::StartLOPtr, _archiveHandle::StartLOsPtr, generate_unaccent_rules::stdout, _archiveHandle::WorkerJobDumpPtr, _archiveHandle::WorkerJobRestorePtr, _archiveHandle::WriteBufPtr, _archiveHandle::WriteBytePtr, _archiveHandle::WriteDataPtr, and _archiveHandle::WriteExtraTocPtr.

Referenced by _allocAH().

◆ InitArchiveFmt_Directory()

void InitArchiveFmt_Directory ( ArchiveHandle AH)

Definition at line 109 of file pg_backup_directory.c.

110 {
111  lclContext *ctx;
112 
113  /* Assuming static functions, this can be copied for each format. */
115  AH->StartDataPtr = _StartData;
116  AH->WriteDataPtr = _WriteData;
117  AH->EndDataPtr = _EndData;
118  AH->WriteBytePtr = _WriteByte;
119  AH->ReadBytePtr = _ReadByte;
120  AH->WriteBufPtr = _WriteBuf;
121  AH->ReadBufPtr = _ReadBuf;
122  AH->ClosePtr = _CloseArchive;
128 
129  AH->StartLOsPtr = _StartLOs;
130  AH->StartLOPtr = _StartLO;
131  AH->EndLOPtr = _EndLO;
132  AH->EndLOsPtr = _EndLOs;
133 
135  AH->ClonePtr = _Clone;
136  AH->DeClonePtr = _DeClone;
137 
140 
141  /* Set up our private context */
142  ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
143  AH->formatData = (void *) ctx;
144 
145  ctx->dataFH = NULL;
146  ctx->LOsTocFH = NULL;
147 
148  /*
149  * Now open the TOC file
150  */
151 
152  if (!AH->fSpec || strcmp(AH->fSpec, "") == 0)
153  pg_fatal("no output directory specified");
154 
155  ctx->directory = AH->fSpec;
156 
157  if (AH->mode == archModeWrite)
158  {
159  struct stat st;
160  bool is_empty = false;
161 
162  /* we accept an empty existing directory */
163  if (stat(ctx->directory, &st) == 0 && S_ISDIR(st.st_mode))
164  {
165  DIR *dir = opendir(ctx->directory);
166 
167  if (dir)
168  {
169  struct dirent *d;
170 
171  is_empty = true;
172  while (errno = 0, (d = readdir(dir)))
173  {
174  if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0)
175  {
176  is_empty = false;
177  break;
178  }
179  }
180 
181  if (errno)
182  pg_fatal("could not read directory \"%s\": %m",
183  ctx->directory);
184 
185  if (closedir(dir))
186  pg_fatal("could not close directory \"%s\": %m",
187  ctx->directory);
188  }
189  }
190 
191  if (!is_empty && mkdir(ctx->directory, 0700) < 0)
192  pg_fatal("could not create directory \"%s\": %m",
193  ctx->directory);
194  }
195  else
196  { /* Read Mode */
197  char fname[MAXPGPATH];
198  CompressFileHandle *tocFH;
199 
200  setFilePath(AH, fname, "toc.dat");
201 
203  if (tocFH == NULL)
204  pg_fatal("could not open input file \"%s\": %m", fname);
205 
206  ctx->dataFH = tocFH;
207 
208  /*
209  * The TOC of a directory format dump shares the format code of the
210  * tar format.
211  */
212  AH->format = archTar;
213  ReadHead(AH);
214  AH->format = archDirectory;
215  ReadToc(AH);
216 
217  /* Nothing else in the file, so close it again... */
218  if (!EndCompressFileHandle(tocFH))
219  pg_fatal("could not close TOC file: %m");
220  ctx->dataFH = NULL;
221  }
222 }
bool EndCompressFileHandle(CompressFileHandle *CFH)
Definition: compress_io.c:289
CompressFileHandle * InitDiscoverCompressFileHandle(const char *path, const char *mode)
Definition: compress_io.c:241
int closedir(DIR *)
Definition: dirent.c:127
struct dirent * readdir(DIR *)
Definition: dirent.c:78
DIR * opendir(const char *)
Definition: dirent.c:33
@ archTar
Definition: pg_backup.h:43
@ archDirectory
Definition: pg_backup.h:45
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 void setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
static int _WorkerJobDumpDirectory(ArchiveHandle *AH, TocEntry *te)
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
#define MAXPGPATH
Definition: dirent.c:26
ArchiveFormat format
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15
CompressFileHandle * LOsTocFH
CompressFileHandle * dataFH
#define stat
Definition: win32_port.h:284
#define S_ISDIR(m)
Definition: win32_port.h:325
#define mkdir(a, b)
Definition: win32_port.h:80

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, closedir(), _archiveHandle::ClosePtr, dirent::d_name, lclContext::dataFH, _archiveHandle::DeClonePtr, lclContext::directory, EndCompressFileHandle(), _archiveHandle::EndDataPtr, _archiveHandle::EndLOPtr, _archiveHandle::EndLOsPtr, _archiveHandle::format, _archiveHandle::formatData, _archiveHandle::fSpec, InitDiscoverCompressFileHandle(), lclContext::LOsTocFH, MAXPGPATH, mkdir, _archiveHandle::mode, opendir(), PG_BINARY_R, pg_fatal, 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::StartDataPtr, _archiveHandle::StartLOPtr, _archiveHandle::StartLOsPtr, stat, _archiveHandle::WorkerJobDumpPtr, _archiveHandle::WorkerJobRestorePtr, _archiveHandle::WriteBufPtr, _archiveHandle::WriteBytePtr, _archiveHandle::WriteDataPtr, and _archiveHandle::WriteExtraTocPtr.

Referenced by _allocAH().

◆ InitArchiveFmt_Null()

void InitArchiveFmt_Null ( ArchiveHandle AH)

Definition at line 48 of file pg_backup_null.c.

49 {
50  /* Assuming static functions, this can be copied for each format. */
52  AH->EndDataPtr = _EndData;
54  AH->WriteBufPtr = _WriteBuf;
55  AH->ClosePtr = _CloseArchive;
56  AH->ReopenPtr = NULL;
58 
59  AH->StartLOsPtr = _StartLOs;
60  AH->StartLOPtr = _StartLO;
61  AH->EndLOPtr = _EndLO;
62  AH->EndLOsPtr = _EndLOs;
63  AH->ClonePtr = NULL;
64  AH->DeClonePtr = NULL;
65 
66  /*
67  * Now prevent reading...
68  */
69  if (AH->mode == archModeRead)
70  pg_fatal("this format cannot be read");
71 }
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te)
static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
static void _CloseArchive(ArchiveHandle *AH)
static void _StartLOs(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 _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid)
static void _EndData(ArchiveHandle *AH, TocEntry *te)
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)

References _CloseArchive(), _EndData(), _EndLO(), _EndLOs(), _PrintTocData(), _StartLO(), _StartLOs(), _WriteBuf(), _WriteByte(), _WriteData(), archModeRead, _archiveHandle::ClonePtr, _archiveHandle::ClosePtr, _archiveHandle::DeClonePtr, _archiveHandle::EndDataPtr, _archiveHandle::EndLOPtr, _archiveHandle::EndLOsPtr, _archiveHandle::mode, pg_fatal, _archiveHandle::PrintTocDataPtr, _archiveHandle::ReopenPtr, _archiveHandle::StartLOPtr, _archiveHandle::StartLOsPtr, _archiveHandle::WriteBufPtr, _archiveHandle::WriteBytePtr, and _archiveHandle::WriteDataPtr.

Referenced by _allocAH().

◆ InitArchiveFmt_Tar()

void InitArchiveFmt_Tar ( ArchiveHandle AH)

Definition at line 121 of file pg_backup_tar.c.

122 {
123  lclContext *ctx;
124 
125  /* Assuming static functions, this can be copied for each format. */
127  AH->StartDataPtr = _StartData;
128  AH->WriteDataPtr = _WriteData;
129  AH->EndDataPtr = _EndData;
130  AH->WriteBytePtr = _WriteByte;
131  AH->ReadBytePtr = _ReadByte;
132  AH->WriteBufPtr = _WriteBuf;
133  AH->ReadBufPtr = _ReadBuf;
134  AH->ClosePtr = _CloseArchive;
135  AH->ReopenPtr = NULL;
140 
141  AH->StartLOsPtr = _StartLOs;
142  AH->StartLOPtr = _StartLO;
143  AH->EndLOPtr = _EndLO;
144  AH->EndLOsPtr = _EndLOs;
145  AH->ClonePtr = NULL;
146  AH->DeClonePtr = NULL;
147 
148  AH->WorkerJobDumpPtr = NULL;
149  AH->WorkerJobRestorePtr = NULL;
150 
151  /*
152  * Set up some special context used in compressing data.
153  */
155  AH->formatData = (void *) ctx;
156  ctx->filePos = 0;
157  ctx->isSpecialScript = 0;
158 
159  /*
160  * Now open the tar file, and load the TOC if we're in read mode.
161  */
162  if (AH->mode == archModeWrite)
163  {
164  if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
165  {
166  ctx->tarFH = fopen(AH->fSpec, PG_BINARY_W);
167  if (ctx->tarFH == NULL)
168  pg_fatal("could not open TOC file \"%s\" for output: %m",
169  AH->fSpec);
170  }
171  else
172  {
173  ctx->tarFH = stdout;
174  if (ctx->tarFH == NULL)
175  pg_fatal("could not open TOC file for output: %m");
176  }
177 
178  ctx->tarFHpos = 0;
179 
180  /*
181  * Make unbuffered since we will dup() it, and the buffers screw each
182  * other
183  */
184  /* setvbuf(ctx->tarFH, NULL, _IONBF, 0); */
185 
186  ctx->hasSeek = checkSeek(ctx->tarFH);
187 
188  /*
189  * We don't support compression because reading the files back is not
190  * possible since gzdopen uses buffered IO which totally screws file
191  * positioning.
192  */
194  pg_fatal("compression is not supported by tar archive format");
195  }
196  else
197  { /* Read Mode */
198  if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
199  {
200  ctx->tarFH = fopen(AH->fSpec, PG_BINARY_R);
201  if (ctx->tarFH == NULL)
202  pg_fatal("could not open TOC file \"%s\" for input: %m",
203  AH->fSpec);
204  }
205  else
206  {
207  ctx->tarFH = stdin;
208  if (ctx->tarFH == NULL)
209  pg_fatal("could not open TOC file for input: %m");
210  }
211 
212  /*
213  * Make unbuffered since we will dup() it, and the buffers screw each
214  * other
215  */
216  /* setvbuf(ctx->tarFH, NULL, _IONBF, 0); */
217 
218  ctx->tarFHpos = 0;
219 
220  ctx->hasSeek = checkSeek(ctx->tarFH);
221 
222  ctx->FH = (void *) tarOpen(AH, "toc.dat", 'r');
223  ReadHead(AH);
224  ReadToc(AH);
225  tarClose(AH, ctx->FH); /* Nothing else in the file... */
226  }
227 }
@ PG_COMPRESSION_NONE
Definition: compression.h:23
#define pg_malloc0_object(type)
Definition: fe_memutils.h:39
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 _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 _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 TAR_MEMBER * tarOpen(ArchiveHandle *AH, const char *filename, char mode)
static void _EndData(ArchiveHandle *AH, TocEntry *te)
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *th)
pg_compress_specification compression_spec
FILE * tarFH
Definition: pg_backup_tar.c:83
pgoff_t filePos
Definition: pg_backup_tar.c:81
pgoff_t tarFHpos
Definition: pg_backup_tar.c:84
TAR_MEMBER * FH
Definition: pg_backup_tar.c:86
int isSpecialScript
Definition: pg_backup_tar.c:87
pg_compress_algorithm algorithm
Definition: compression.h:34

References _ArchiveEntry(), _CloseArchive(), _EndData(), _EndLO(), _EndLOs(), _PrintExtraToc(), _PrintTocData(), _ReadBuf(), _ReadByte(), _ReadExtraToc(), _StartData(), _StartLO(), _StartLOs(), _WriteBuf(), _WriteByte(), _WriteData(), _WriteExtraToc(), archModeWrite, checkSeek(), lclContext::FH, lclContext::filePos, lclContext::hasSeek, lclContext::isSpecialScript, PG_BINARY_R, PG_BINARY_W, PG_COMPRESSION_NONE, pg_fatal, pg_malloc0_object, ReadHead(), ReadToc(), generate_unaccent_rules::stdout, tarClose(), lclContext::tarFH, lclContext::tarFHpos, and tarOpen().

Referenced by _allocAH().

◆ IssueACLPerBlob()

void IssueACLPerBlob ( ArchiveHandle AH,
TocEntry te 
)

Definition at line 599 of file pg_backup_db.c.

600 {
601  TocEntry *blobte = getTocEntryByDumpId(AH, te->dependencies[0]);
602  char *buf;
603  char *st;
604  char *st2;
605  char *en;
606  bool inquotes;
607 
608  if (!blobte)
609  pg_fatal("could not find entry for ID %d", te->dependencies[0]);
610  Assert(strcmp(blobte->desc, "BLOB METADATA") == 0);
611 
612  /* Make a writable copy of the ACL commands string */
613  buf = pg_strdup(te->defn);
614 
615  /*
616  * We have to parse out the commands sufficiently to locate the blob OIDs
617  * and find the command-ending semicolons. The commands should not
618  * contain anything hard to parse except for double-quoted role names,
619  * which are easy to ignore. Once we've split apart the first and second
620  * halves of a command, apply IssueCommandPerBlob. (This means the
621  * updates on the blobs are interleaved if there's multiple commands, but
622  * that should cause no trouble.)
623  */
624  inquotes = false;
625  st = en = buf;
626  st2 = NULL;
627  while (*en)
628  {
629  /* Ignore double-quoted material */
630  if (*en == '"')
631  inquotes = !inquotes;
632  if (inquotes)
633  {
634  en++;
635  continue;
636  }
637  /* If we found "LARGE OBJECT", that's the end of the first half */
638  if (strncmp(en, "LARGE OBJECT ", 13) == 0)
639  {
640  /* Terminate the first-half string */
641  en += 13;
642  Assert(isdigit((unsigned char) *en));
643  *en++ = '\0';
644  /* Skip the rest of the blob OID */
645  while (isdigit((unsigned char) *en))
646  en++;
647  /* Second half starts here */
648  Assert(st2 == NULL);
649  st2 = en;
650  }
651  /* If we found semicolon, that's the end of the second half */
652  else if (*en == ';')
653  {
654  /* Terminate the second-half string */
655  *en++ = '\0';
656  Assert(st2 != NULL);
657  /* Issue this command for each blob */
658  IssueCommandPerBlob(AH, blobte, st, st2);
659  /* For neatness, skip whitespace before the next command */
660  while (isspace((unsigned char) *en))
661  en++;
662  /* Reset for new command */
663  st = en;
664  st2 = NULL;
665  }
666  else
667  en++;
668  }
669  pg_free(buf);
670 }
void pg_free(void *ptr)
Definition: fe_memutils.c:105
TocEntry * getTocEntryByDumpId(ArchiveHandle *AH, DumpId id)
void IssueCommandPerBlob(ArchiveHandle *AH, TocEntry *te, const char *cmdBegin, const char *cmdEnd)
Definition: pg_backup_db.c:552
static char * buf
Definition: pg_test_fsync.c:73

References Assert, buf, _tocEntry::defn, _tocEntry::dependencies, _tocEntry::desc, getTocEntryByDumpId(), IssueCommandPerBlob(), pg_fatal, pg_free(), and pg_strdup().

Referenced by _printTocEntry().

◆ IssueCommandPerBlob()

void IssueCommandPerBlob ( ArchiveHandle AH,
TocEntry te,
const char *  cmdBegin,
const char *  cmdEnd 
)

Definition at line 552 of file pg_backup_db.c.

554 {
555  /* Make a writable copy of the command string */
556  char *buf = pg_strdup(te->defn);
557  RestoreOptions *ropt = AH->public.ropt;
558  char *st;
559  char *en;
560 
561  st = buf;
562  while ((en = strchr(st, '\n')) != NULL)
563  {
564  *en++ = '\0';
565  ahprintf(AH, "%s%s%s;\n", cmdBegin, st, cmdEnd);
566 
567  /* In --transaction-size mode, count each command as an action */
568  if (ropt && ropt->txn_size > 0)
569  {
570  if (++AH->txnCount >= ropt->txn_size)
571  {
572  if (AH->connection)
573  {
575  StartTransaction(&AH->public);
576  }
577  else
578  ahprintf(AH, "COMMIT;\nBEGIN;\n\n");
579  AH->txnCount = 0;
580  }
581  }
582 
583  st = en;
584  }
585  ahprintf(AH, "\n");
586  pg_free(buf);
587 }
void StartTransaction(Archive *AHX)
Definition: pg_backup_db.c:529
void CommitTransaction(Archive *AHX)
Definition: pg_backup_db.c:537

References ahprintf(), buf, CommitTransaction(), _archiveHandle::connection, _tocEntry::defn, pg_free(), pg_strdup(), _archiveHandle::public, Archive::ropt, StartTransaction(), _restoreOptions::txn_size, and _archiveHandle::txnCount.

Referenced by _printTocEntry(), IssueACLPerBlob(), and RestoreArchive().

◆ isValidTarHeader()

bool isValidTarHeader ( char *  header)

Definition at line 988 of file pg_backup_tar.c.

989 {
990  int sum;
991  int chk = tarChecksum(header);
992 
993  sum = read_tar_number(&header[TAR_OFFSET_CHECKSUM], 8);
994 
995  if (sum != chk)
996  return false;
997 
998  /* POSIX tar format */
999  if (memcmp(&header[TAR_OFFSET_MAGIC], "ustar\0", 6) == 0 &&
1000  memcmp(&header[TAR_OFFSET_VERSION], "00", 2) == 0)
1001  return true;
1002  /* GNU tar format */
1003  if (memcmp(&header[TAR_OFFSET_MAGIC], "ustar \0", 8) == 0)
1004  return true;
1005  /* not-quite-POSIX format written by pre-9.3 pg_dump */
1006  if (memcmp(&header[TAR_OFFSET_MAGIC], "ustar00\0", 8) == 0)
1007  return true;
1008 
1009  return false;
1010 }
uint64 read_tar_number(const char *s, int len)
Definition: tar.c:58
int tarChecksum(char *header)
Definition: tar.c:90
@ TAR_OFFSET_VERSION
Definition: pgtar.h:49
@ TAR_OFFSET_CHECKSUM
Definition: pgtar.h:45
@ TAR_OFFSET_MAGIC
Definition: pgtar.h:48

References read_tar_number(), TAR_OFFSET_CHECKSUM, TAR_OFFSET_MAGIC, TAR_OFFSET_VERSION, and tarChecksum().

Referenced by _discoverArchiveFormat().

◆ on_exit_close_archive()

void on_exit_close_archive ( Archive AHX)

Definition at line 328 of file parallel.c.

329 {
330  shutdown_info.AHX = AHX;
332 }
static void archive_close_connection(int code, void *arg)
Definition: parallel.c:339
static ShutdownInformation shutdown_info
Definition: parallel.c:152
void on_exit_nicely(on_exit_nicely_callback function, void *arg)

References ShutdownInformation::AHX, archive_close_connection(), on_exit_nicely(), and shutdown_info.

Referenced by main().

◆ parallel_restore()

int parallel_restore ( ArchiveHandle AH,
TocEntry te 
)

Definition at line 4592 of file pg_backup_archiver.c.

4593 {
4594  int status;
4595 
4596  Assert(AH->connection != NULL);
4597 
4598  /* Count only errors associated with this TOC entry */
4599  AH->public.n_errors = 0;
4600 
4601  /* Restore the TOC item */
4602  status = restore_toc_entry(AH, te, true);
4603 
4604  return status;
4605 }
static int restore_toc_entry(ArchiveHandle *AH, TocEntry *te, bool is_parallel)

References Assert, _archiveHandle::connection, Archive::n_errors, _archiveHandle::public, and restore_toc_entry().

Referenced by _WorkerJobRestoreCustom(), and _WorkerJobRestoreDirectory().

◆ ReadHead()

void ReadHead ( ArchiveHandle AH)

Definition at line 3958 of file pg_backup_archiver.c.

3959 {
3960  char *errmsg;
3961  char vmaj,
3962  vmin,
3963  vrev;
3964  int fmt;
3965 
3966  /*
3967  * If we haven't already read the header, do so.
3968  *
3969  * NB: this code must agree with _discoverArchiveFormat(). Maybe find a
3970  * way to unify the cases?
3971  */
3972  if (!AH->readHeader)
3973  {
3974  char tmpMag[7];
3975 
3976  AH->ReadBufPtr(AH, tmpMag, 5);
3977 
3978  if (strncmp(tmpMag, "PGDMP", 5) != 0)
3979  pg_fatal("did not find magic string in file header");
3980  }
3981 
3982  vmaj = AH->ReadBytePtr(AH);
3983  vmin = AH->ReadBytePtr(AH);
3984 
3985  if (vmaj > 1 || (vmaj == 1 && vmin > 0)) /* Version > 1.0 */
3986  vrev = AH->ReadBytePtr(AH);
3987  else
3988  vrev = 0;
3989 
3990  AH->version = MAKE_ARCHIVE_VERSION(vmaj, vmin, vrev);
3991 
3992  if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX)
3993  pg_fatal("unsupported version (%d.%d) in file header",
3994  vmaj, vmin);
3995 
3996  AH->intSize = AH->ReadBytePtr(AH);
3997  if (AH->intSize > 32)
3998  pg_fatal("sanity check on integer size (%lu) failed",
3999  (unsigned long) AH->intSize);
4000 
4001  if (AH->intSize > sizeof(int))
4002  pg_log_warning("archive was made on a machine with larger integers, some operations might fail");
4003 
4004  if (AH->version >= K_VERS_1_7)
4005  AH->offSize = AH->ReadBytePtr(AH);
4006  else
4007  AH->offSize = AH->intSize;
4008 
4009  fmt = AH->ReadBytePtr(AH);
4010 
4011  if (AH->format != fmt)
4012  pg_fatal("expected format (%d) differs from format found in file (%d)",
4013  AH->format, fmt);
4014 
4015  if (AH->version >= K_VERS_1_15)
4016  AH->compression_spec.algorithm = AH->ReadBytePtr(AH);
4017  else if (AH->version >= K_VERS_1_2)
4018  {
4019  /* Guess the compression method based on the level */
4020  if (AH->version < K_VERS_1_4)
4021  AH->compression_spec.level = AH->ReadBytePtr(AH);
4022  else
4023  AH->compression_spec.level = ReadInt(AH);
4024 
4025  if (AH->compression_spec.level != 0)
4027  }
4028  else
4030 
4032  if (errmsg)
4033  {
4034  pg_log_warning("archive is compressed, but this installation does not support compression (%s) -- no data will be available",
4035  errmsg);
4036  pg_free(errmsg);
4037  }
4038 
4039  if (AH->version >= K_VERS_1_4)
4040  {
4041  struct tm crtm;
4042 
4043  crtm.tm_sec = ReadInt(AH);
4044  crtm.tm_min = ReadInt(AH);
4045  crtm.tm_hour = ReadInt(AH);
4046  crtm.tm_mday = ReadInt(AH);
4047  crtm.tm_mon = ReadInt(AH);
4048  crtm.tm_year = ReadInt(AH);
4049  crtm.tm_isdst = ReadInt(AH);
4050 
4051  /*
4052  * Newer versions of glibc have mktime() report failure if tm_isdst is
4053  * inconsistent with the prevailing timezone, e.g. tm_isdst = 1 when
4054  * TZ=UTC. This is problematic when restoring an archive under a
4055  * different timezone setting. If we get a failure, try again with
4056  * tm_isdst set to -1 ("don't know").
4057  *
4058  * XXX with or without this hack, we reconstruct createDate
4059  * incorrectly when the prevailing timezone is different from
4060  * pg_dump's. Next time we bump the archive version, we should flush
4061  * this representation and store a plain seconds-since-the-Epoch
4062  * timestamp instead.
4063  */
4064  AH->createDate = mktime(&crtm);
4065  if (AH->createDate == (time_t) -1)
4066  {
4067  crtm.tm_isdst = -1;
4068  AH->createDate = mktime(&crtm);
4069  if (AH->createDate == (time_t) -1)
4070  pg_log_warning("invalid creation date in header");
4071  }
4072  }
4073 
4074  if (AH->version >= K_VERS_1_4)
4075  {
4076  AH->archdbname = ReadStr(AH);
4077  }
4078 
4079  if (AH->version >= K_VERS_1_10)
4080  {
4081  AH->archiveRemoteVersion = ReadStr(AH);
4082  AH->archiveDumpVersion = ReadStr(AH);
4083  }
4084 }
char * supports_compression(const pg_compress_specification compression_spec)
Definition: compress_io.c:88
@ PG_COMPRESSION_GZIP
Definition: compression.h:24
int errmsg(const char *fmt,...)
Definition: elog.c:1072
static void const char * fmt
static struct pg_tm tm
Definition: localtime.c:104
char * ReadStr(ArchiveHandle *AH)
int ReadInt(ArchiveHandle *AH)
#define K_VERS_1_15
#define K_VERS_1_10
#define K_VERS_1_2
#define K_VERS_1_4
#define K_VERS_1_0
#define K_VERS_MAX
#define K_VERS_1_7
#define pg_log_warning(...)
Definition: pgfnames.c:24
int tm_sec
Definition: pgtime.h:36

References pg_compress_specification::algorithm, _archiveHandle::archdbname, _archiveHandle::archiveDumpVersion, _archiveHandle::archiveRemoteVersion, _archiveHandle::compression_spec, _archiveHandle::createDate, errmsg(), fmt, _archiveHandle::format, _archiveHandle::intSize, K_VERS_1_0, K_VERS_1_10, K_VERS_1_15, K_VERS_1_2, K_VERS_1_4, K_VERS_1_7, K_VERS_MAX, pg_compress_specification::level, MAKE_ARCHIVE_VERSION, _archiveHandle::offSize, PG_COMPRESSION_GZIP, pg_fatal, pg_free(), pg_log_warning, _archiveHandle::ReadBufPtr, _archiveHandle::ReadBytePtr, _archiveHandle::readHeader, ReadInt(), ReadStr(), supports_compression(), tm, pg_tm::tm_sec, and _archiveHandle::version.

Referenced by InitArchiveFmt_Custom(), InitArchiveFmt_Directory(), and InitArchiveFmt_Tar().

◆ ReadInt()

int ReadInt ( ArchiveHandle AH)

Definition at line 2121 of file pg_backup_archiver.c.

2122 {
2123  int res = 0;
2124  int bv,
2125  b;
2126  int sign = 0; /* Default positive */
2127  int bitShift = 0;
2128 
2129  if (AH->version > K_VERS_1_0)
2130  /* Read a sign byte */
2131  sign = AH->ReadBytePtr(AH);
2132 
2133  for (b = 0; b < AH->intSize; b++)
2134  {
2135  bv = AH->ReadBytePtr(AH) & 0xFF;
2136  if (bv != 0)
2137  res = res + (bv << bitShift);
2138  bitShift += 8;
2139  }
2140 
2141  if (sign)
2142  res = -res;
2143 
2144  return res;
2145 }
char sign
Definition: informix.c:674
int b
Definition: isn.c:70

References b, _archiveHandle::intSize, K_VERS_1_0, _archiveHandle::ReadBytePtr, res, sign, and _archiveHandle::version.

Referenced by _CustomReadFunc(), _LoadLOs(), _readBlockHeader(), _ReadExtraToc(), _skipData(), _skipLOs(), ReadHead(), ReadOffset(), ReadStr(), and ReadToc().

◆ ReadOffset()

int ReadOffset ( ArchiveHandle AH,
pgoff_t o 
)

Definition at line 2026 of file pg_backup_archiver.c.

2027 {
2028  int i;
2029  int off;
2030  int offsetFlg;
2031 
2032  /* Initialize to zero */
2033  *o = 0;
2034 
2035  /* Check for old version */
2036  if (AH->version < K_VERS_1_7)
2037  {
2038  /* Prior versions wrote offsets using WriteInt */
2039  i = ReadInt(AH);
2040  /* -1 means not set */
2041  if (i < 0)
2042  return K_OFFSET_POS_NOT_SET;
2043  else if (i == 0)
2044  return K_OFFSET_NO_DATA;
2045 
2046  /* Cast to pgoff_t because it was written as an int. */
2047  *o = (pgoff_t) i;
2048  return K_OFFSET_POS_SET;
2049  }
2050 
2051  /*
2052  * Read the flag indicating the state of the data pointer. Check if valid
2053  * and die if not.
2054  *
2055  * This used to be handled by a negative or zero pointer, now we use an
2056  * extra byte specifically for the state.
2057  */
2058  offsetFlg = AH->ReadBytePtr(AH) & 0xFF;
2059 
2060  switch (offsetFlg)
2061  {
2062  case K_OFFSET_POS_NOT_SET:
2063  case K_OFFSET_NO_DATA:
2064  case K_OFFSET_POS_SET:
2065 
2066  break;
2067 
2068  default:
2069  pg_fatal("unexpected data offset flag %d", offsetFlg);
2070  }
2071 
2072  /*
2073  * Read the bytes
2074  */
2075  for (off = 0; off < AH->offSize; off++)
2076  {
2077  if (off < sizeof(pgoff_t))
2078  *o |= ((pgoff_t) (AH->ReadBytePtr(AH))) << (off * 8);
2079  else
2080  {
2081  if (AH->ReadBytePtr(AH) != 0)
2082  pg_fatal("file offset in dump file is too large");
2083  }
2084  }
2085 
2086  return offsetFlg;
2087 }
int i
Definition: isn.c:73
#define K_OFFSET_NO_DATA
#define K_OFFSET_POS_NOT_SET
#define K_OFFSET_POS_SET

References i, K_OFFSET_NO_DATA, K_OFFSET_POS_NOT_SET, K_OFFSET_POS_SET, K_VERS_1_7, _archiveHandle::offSize, pg_fatal, pgoff_t, _archiveHandle::ReadBytePtr, ReadInt(), and _archiveHandle::version.

Referenced by _ReadExtraToc().

◆ ReadStr()

char* ReadStr ( ArchiveHandle AH)

Definition at line 2167 of file pg_backup_archiver.c.

2168 {
2169  char *buf;
2170  int l;
2171 
2172  l = ReadInt(AH);
2173  if (l < 0)
2174  buf = NULL;
2175  else
2176  {
2177  buf = (char *) pg_malloc(l + 1);
2178  AH->ReadBufPtr(AH, (void *) buf, l);
2179 
2180  buf[l] = '\0';
2181  }
2182 
2183  return buf;
2184 }

References buf, pg_malloc(), _archiveHandle::ReadBufPtr, and ReadInt().

Referenced by _ReadExtraToc(), ReadHead(), and ReadToc().

◆ ReadToc()

void ReadToc ( ArchiveHandle AH)

Definition at line 2627 of file pg_backup_archiver.c.

2628 {
2629  int i;
2630  char *tmp;
2631  DumpId *deps;
2632  int depIdx;
2633  int depSize;
2634  TocEntry *te;
2635  bool is_supported;
2636 
2637  AH->tocCount = ReadInt(AH);
2638  AH->maxDumpId = 0;
2639 
2640  for (i = 0; i < AH->tocCount; i++)
2641  {
2642  te = (TocEntry *) pg_malloc0(sizeof(TocEntry));
2643  te->dumpId = ReadInt(AH);
2644 
2645  if (te->dumpId > AH->maxDumpId)
2646  AH->maxDumpId = te->dumpId;
2647 
2648  /* Sanity check */
2649  if (te->dumpId <= 0)
2650  pg_fatal("entry ID %d out of range -- perhaps a corrupt TOC",
2651  te->dumpId);
2652 
2653  te->hadDumper = ReadInt(AH);
2654 
2655  if (AH->version >= K_VERS_1_8)
2656  {
2657  tmp = ReadStr(AH);
2658  sscanf(tmp, "%u", &te->catalogId.tableoid);
2659  free(tmp);
2660  }
2661  else
2663  tmp = ReadStr(AH);
2664  sscanf(tmp, "%u", &te->catalogId.oid);
2665  free(tmp);
2666 
2667  te->tag = ReadStr(AH);
2668  te->desc = ReadStr(AH);
2669 
2670  if (AH->version >= K_VERS_1_11)
2671  {
2672  te->section = ReadInt(AH);
2673  }
2674  else
2675  {
2676  /*
2677  * Rules for pre-8.4 archives wherein pg_dump hasn't classified
2678  * the entries into sections. This list need not cover entry
2679  * types added later than 8.4.
2680  */
2681  if (strcmp(te->desc, "COMMENT") == 0 ||
2682  strcmp(te->desc, "ACL") == 0 ||
2683  strcmp(te->desc, "ACL LANGUAGE") == 0)
2684  te->section = SECTION_NONE;
2685  else if (strcmp(te->desc, "TABLE DATA") == 0 ||
2686  strcmp(te->desc, "BLOBS") == 0 ||
2687  strcmp(te->desc, "BLOB COMMENTS") == 0)
2688  te->section = SECTION_DATA;
2689  else if (strcmp(te->desc, "CONSTRAINT") == 0 ||
2690  strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
2691  strcmp(te->desc, "FK CONSTRAINT") == 0 ||
2692  strcmp(te->desc, "INDEX") == 0 ||
2693  strcmp(te->desc, "RULE") == 0 ||
2694  strcmp(te->desc, "TRIGGER") == 0)
2695  te->section = SECTION_POST_DATA;
2696  else
2697  te->section = SECTION_PRE_DATA;
2698  }
2699 
2700  te->defn = ReadStr(AH);
2701  te->dropStmt = ReadStr(AH);
2702 
2703  if (AH->version >= K_VERS_1_3)
2704  te->copyStmt = ReadStr(AH);
2705 
2706  if (AH->version >= K_VERS_1_6)
2707  te->namespace = ReadStr(AH);
2708 
2709  if (AH->version >= K_VERS_1_10)
2710  te->tablespace = ReadStr(AH);
2711 
2712  if (AH->version >= K_VERS_1_14)
2713  te->tableam = ReadStr(AH);
2714 
2715  if (AH->version >= K_VERS_1_16)
2716  te->relkind = ReadInt(AH);
2717 
2718  te->owner = ReadStr(AH);
2719  is_supported = true;
2720  if (AH->version < K_VERS_1_9)
2721  is_supported = false;
2722  else
2723  {
2724  tmp = ReadStr(AH);
2725 
2726  if (strcmp(tmp, "true") == 0)
2727  is_supported = false;
2728 
2729  free(tmp);
2730  }
2731 
2732  if (!is_supported)
2733  pg_log_warning("restoring tables WITH OIDS is not supported anymore");
2734 
2735  /* Read TOC entry dependencies */
2736  if (AH->version >= K_VERS_1_5)
2737  {
2738  depSize = 100;
2739  deps = (DumpId *) pg_malloc(sizeof(DumpId) * depSize);
2740  depIdx = 0;
2741  for (;;)
2742  {
2743  tmp = ReadStr(AH);
2744  if (!tmp)
2745  break; /* end of list */
2746  if (depIdx >= depSize)
2747  {
2748  depSize *= 2;
2749  deps = (DumpId *) pg_realloc(deps, sizeof(DumpId) * depSize);
2750  }
2751  sscanf(tmp, "%d", &deps[depIdx]);
2752  free(tmp);
2753  depIdx++;
2754  }
2755 
2756  if (depIdx > 0) /* We have a non-null entry */
2757  {
2758  deps = (DumpId *) pg_realloc(deps, sizeof(DumpId) * depIdx);
2759  te->dependencies = deps;
2760  te->nDeps = depIdx;
2761  }
2762  else
2763  {
2764  free(deps);
2765  te->dependencies = NULL;
2766  te->nDeps = 0;
2767  }
2768  }
2769  else
2770  {
2771  te->dependencies = NULL;
2772  te->nDeps = 0;
2773  }
2774  te->dataLength = 0;
2775 
2776  if (AH->ReadExtraTocPtr)
2777  AH->ReadExtraTocPtr(AH, te);
2778 
2779  pg_log_debug("read TOC entry %d (ID %d) for %s %s",
2780  i, te->dumpId, te->desc, te->tag);
2781 
2782  /* link completed entry into TOC circular list */
2783  te->prev = AH->toc->prev;
2784  AH->toc->prev->next = te;
2785  AH->toc->prev = te;
2786  te->next = AH->toc;
2787 
2788  /* special processing immediately upon read for some items */
2789  if (strcmp(te->desc, "ENCODING") == 0)
2790  processEncodingEntry(AH, te);
2791  else if (strcmp(te->desc, "STDSTRINGS") == 0)
2792  processStdStringsEntry(AH, te);
2793  else if (strcmp(te->desc, "SEARCHPATH") == 0)
2794  processSearchPathEntry(AH, te);
2795  }
2796 }
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
#define pg_log_debug(...)
Definition: logging.h:133
@ SECTION_NONE
Definition: pg_backup.h:57
@ SECTION_POST_DATA
Definition: pg_backup.h:60
@ SECTION_PRE_DATA
Definition: pg_backup.h:58
@ SECTION_DATA
Definition: pg_backup.h:59
static void processEncodingEntry(ArchiveHandle *AH, TocEntry *te)
static void processSearchPathEntry(ArchiveHandle *AH, TocEntry *te)
static void processStdStringsEntry(ArchiveHandle *AH, TocEntry *te)
#define K_VERS_1_14
#define K_VERS_1_5
#define K_VERS_1_6
#define K_VERS_1_8
#define K_VERS_1_11
#define K_VERS_1_9
#define K_VERS_1_16
#define K_VERS_1_3
#define InvalidOid
Definition: postgres_ext.h:36
Oid tableoid
Definition: pg_backup.h:266

References _tocEntry::catalogId, _tocEntry::copyStmt, _tocEntry::dataLength, _tocEntry::defn, _tocEntry::dependencies, _tocEntry::desc, _tocEntry::dropStmt, _tocEntry::dumpId, free, _tocEntry::hadDumper, i, InvalidOid, K_VERS_1_10, K_VERS_1_11, K_VERS_1_14, K_VERS_1_16, K_VERS_1_3, K_VERS_1_5, K_VERS_1_6, K_VERS_1_8, K_VERS_1_9, _archiveHandle::maxDumpId, _tocEntry::nDeps, _tocEntry::next, CatalogId::oid, _tocEntry::owner, pg_fatal, pg_log_debug, pg_log_warning, pg_malloc(), pg_malloc0(), pg_realloc(), _tocEntry::prev, processEncodingEntry(), processSearchPathEntry(), processStdStringsEntry(), _archiveHandle::ReadExtraTocPtr, ReadInt(), ReadStr(), _tocEntry::relkind, _tocEntry::section, SECTION_DATA, SECTION_NONE, SECTION_POST_DATA, SECTION_PRE_DATA, _tocEntry::tableam, CatalogId::tableoid, _tocEntry::tablespace, _tocEntry::tag, _archiveHandle::toc, _archiveHandle::tocCount, and _archiveHandle::version.

Referenced by InitArchiveFmt_Custom(), InitArchiveFmt_Directory(), and InitArchiveFmt_Tar().

◆ ReconnectToServer()

void ReconnectToServer ( ArchiveHandle AH,
const char *  dbname 
)

Definition at line 74 of file pg_backup_db.c.

75 {
76  PGconn *oldConn = AH->connection;
77  RestoreOptions *ropt = AH->public.ropt;
78 
79  /*
80  * Save the dbname, if given, in override_dbname so that it will also
81  * affect any later reconnection attempt.
82  */
83  if (dbname)
85 
86  /*
87  * Note: we want to establish the new connection, and in particular update
88  * ArchiveHandle's connCancel, before closing old connection. Otherwise
89  * an ill-timed SIGINT could try to access a dead connection.
90  */
91  AH->connection = NULL; /* dodge error check in ConnectDatabase */
92 
93  ConnectDatabase((Archive *) AH, &ropt->cparams, true);
94 
95  PQfinish(oldConn);
96 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4868
void ConnectDatabase(Archive *AHX, const ConnParams *cparams, bool isReconnect)
Definition: pg_backup_db.c:110
char * dbname
Definition: streamutil.c:52
char * override_dbname
Definition: pg_backup.h:91

References ConnectDatabase(), _archiveHandle::connection, _restoreOptions::cparams, dbname, _connParams::override_dbname, pg_strdup(), PQfinish(), _archiveHandle::public, and Archive::ropt.

Referenced by _reconnectToDB().

◆ StartRestoreLO()

void StartRestoreLO ( ArchiveHandle AH,
Oid  oid,
bool  drop 
)

Definition at line 1450 of file pg_backup_archiver.c.

1451 {
1452  bool old_lo_style = (AH->version < K_VERS_1_12);
1453  Oid loOid;
1454 
1455  AH->loCount++;
1456 
1457  /* Initialize the LO Buffer */
1458  if (AH->lo_buf == NULL)
1459  {
1460  /* First time through (in this process) so allocate the buffer */
1461  AH->lo_buf_size = LOBBUFSIZE;
1462  AH->lo_buf = (void *) pg_malloc(LOBBUFSIZE);
1463  }
1464  AH->lo_buf_used = 0;
1465 
1466  pg_log_info("restoring large object with OID %u", oid);
1467 
1468  /* With an old archive we must do drop and create logic here */
1469  if (old_lo_style && drop)
1470  DropLOIfExists(AH, oid);
1471 
1472  if (AH->connection)
1473  {
1474  if (old_lo_style)
1475  {
1476  loOid = lo_create(AH->connection, oid);
1477  if (loOid == 0 || loOid != oid)
1478  pg_fatal("could not create large object %u: %s",
1479  oid, PQerrorMessage(AH->connection));
1480  }
1481  AH->loFd = lo_open(AH->connection, oid, INV_WRITE);
1482  if (AH->loFd == -1)
1483  pg_fatal("could not open large object %u: %s",
1484  oid, PQerrorMessage(AH->connection));
1485  }
1486  else
1487  {
1488  if (old_lo_style)
1489  ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n",
1490  oid, INV_WRITE);
1491  else
1492  ahprintf(AH, "SELECT pg_catalog.lo_open('%u', %d);\n",
1493  oid, INV_WRITE);
1494  }
1495 
1496  AH->writingLO = true;
1497 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7147
int lo_open(PGconn *conn, Oid lobjId, int mode)
Definition: fe-lobj.c:57
Oid lo_create(PGconn *conn, Oid lobjId)
Definition: fe-lobj.c:474
#define INV_WRITE
Definition: libpq-fs.h:21
#define LOBBUFSIZE
void DropLOIfExists(ArchiveHandle *AH, Oid oid)
Definition: pg_backup_db.c:673
#define K_VERS_1_12
unsigned int Oid
Definition: postgres_ext.h:31

References ahprintf(), _archiveHandle::connection, DropLOIfExists(), INV_WRITE, K_VERS_1_12, _archiveHandle::lo_buf, _archiveHandle::lo_buf_size, _archiveHandle::lo_buf_used, lo_create(), lo_open(), LOBBUFSIZE, _archiveHandle::loCount, _archiveHandle::loFd, pg_fatal, pg_log_info, pg_malloc(), PQerrorMessage(), _archiveHandle::version, and _archiveHandle::writingLO.

Referenced by _LoadLOs().

◆ StartRestoreLOs()

void StartRestoreLOs ( ArchiveHandle AH)

Definition at line 1403 of file pg_backup_archiver.c.

1404 {
1405  RestoreOptions *ropt = AH->public.ropt;
1406 
1407  /*
1408  * LOs must be restored within a transaction block, since we need the LO
1409  * handle to stay open while we write it. Establish a transaction unless
1410  * there's one being used globally.
1411  */
1412  if (!(ropt->single_txn || ropt->txn_size > 0))
1413  {
1414  if (AH->connection)
1415  StartTransaction(&AH->public);
1416  else
1417  ahprintf(AH, "BEGIN;\n\n");
1418  }
1419 
1420  AH->loCount = 0;
1421 }
static void StartTransaction(void)
Definition: xact.c:2014

References ahprintf(), _archiveHandle::connection, _archiveHandle::loCount, _archiveHandle::public, Archive::ropt, _restoreOptions::single_txn, StartTransaction(), and _restoreOptions::txn_size.

Referenced by _LoadLOs().

◆ TocIDRequired()

int TocIDRequired ( ArchiveHandle AH,
DumpId  id 
)

Definition at line 1998 of file pg_backup_archiver.c.

1999 {
2000  TocEntry *te = getTocEntryByDumpId(AH, id);
2001 
2002  if (!te)
2003  return 0;
2004 
2005  return te->reqs;
2006 }

References getTocEntryByDumpId(), and _tocEntry::reqs.

Referenced by _tarPositionTo(), and findDumpableDependencies().

◆ warn_or_exit_horribly()

void warn_or_exit_horribly ( ArchiveHandle AH,
const char *  fmt,
  ... 
)

◆ WriteDataChunks()

void WriteDataChunks ( ArchiveHandle AH,
struct ParallelState pstate 
)

Definition at line 2453 of file pg_backup_archiver.c.

2454 {
2455  TocEntry *te;
2456 
2457  if (pstate && pstate->numWorkers > 1)
2458  {
2459  /*
2460  * In parallel mode, this code runs in the leader process. We
2461  * construct an array of candidate TEs, then sort it into decreasing
2462  * size order, then dispatch each TE to a data-transfer worker. By
2463  * dumping larger tables first, we avoid getting into a situation
2464  * where we're down to one job and it's big, losing parallelism.
2465  */
2466  TocEntry **tes;
2467  int ntes;
2468 
2469  tes = (TocEntry **) pg_malloc(AH->tocCount * sizeof(TocEntry *));
2470  ntes = 0;
2471  for (te = AH->toc->next; te != AH->toc; te = te->next)
2472  {
2473  /* Consider only TEs with dataDumper functions ... */
2474  if (!te->dataDumper)
2475  continue;
2476  /* ... and ignore ones not enabled for dump */
2477  if ((te->reqs & REQ_DATA) == 0)
2478  continue;
2479 
2480  tes[ntes++] = te;
2481  }
2482 
2483  if (ntes > 1)
2484  qsort(tes, ntes, sizeof(TocEntry *), TocEntrySizeCompareQsort);
2485 
2486  for (int i = 0; i < ntes; i++)
2487  DispatchJobForTocEntry(AH, pstate, tes[i], ACT_DUMP,
2488  mark_dump_job_done, NULL);
2489 
2490  pg_free(tes);
2491 
2492  /* Now wait for workers to finish. */
2493  WaitForWorkers(AH, pstate, WFW_ALL_IDLE);
2494  }
2495  else
2496  {
2497  /* Non-parallel mode: just dump all candidate TEs sequentially. */
2498  for (te = AH->toc->next; te != AH->toc; te = te->next)
2499  {
2500  /* Must have same filter conditions as above */
2501  if (!te->dataDumper)
2502  continue;
2503  if ((te->reqs & REQ_DATA) == 0)
2504  continue;
2505 
2507  }
2508  }
2509 }
void WaitForWorkers(ArchiveHandle *AH, ParallelState *pstate, WFW_WaitOption mode)
Definition: parallel.c:1451
void DispatchJobForTocEntry(ArchiveHandle *AH, ParallelState *pstate, TocEntry *te, T_Action act, ParallelCompletionPtr callback, void *callback_data)
Definition: parallel.c:1205
@ WFW_ALL_IDLE
Definition: parallel.h:35
void WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te)
static int TocEntrySizeCompareQsort(const void *p1, const void *p2)
static void mark_dump_job_done(ArchiveHandle *AH, TocEntry *te, int status, void *callback_data)
@ ACT_DUMP
#define REQ_DATA
#define qsort(a, b, c, d)
Definition: port.h:449
int numWorkers
Definition: parallel.h:57

References ACT_DUMP, _tocEntry::dataDumper, DispatchJobForTocEntry(), i, mark_dump_job_done(), _tocEntry::next, ParallelState::numWorkers, pg_free(), pg_malloc(), qsort, REQ_DATA, _tocEntry::reqs, _archiveHandle::toc, _archiveHandle::tocCount, TocEntrySizeCompareQsort(), WaitForWorkers(), WFW_ALL_IDLE, and WriteDataChunksForTocEntry().

Referenced by _CloseArchive().

◆ WriteDataChunksForTocEntry()

void WriteDataChunksForTocEntry ( ArchiveHandle AH,
TocEntry te 
)

Definition at line 2534 of file pg_backup_archiver.c.

2535 {
2536  StartDataPtrType startPtr;
2537  EndDataPtrType endPtr;
2538 
2539  AH->currToc = te;
2540 
2541  if (strcmp(te->desc, "BLOBS") == 0)
2542  {
2543  startPtr = AH->StartLOsPtr;
2544  endPtr = AH->EndLOsPtr;
2545  }
2546  else
2547  {
2548  startPtr = AH->StartDataPtr;
2549  endPtr = AH->EndDataPtr;
2550  }
2551 
2552  if (startPtr != NULL)
2553  (*startPtr) (AH, te);
2554 
2555  /*
2556  * The user-provided DataDumper routine needs to call AH->WriteData
2557  */
2558  te->dataDumper((Archive *) AH, te->dataDumperArg);
2559 
2560  if (endPtr != NULL)
2561  (*endPtr) (AH, te);
2562 
2563  AH->currToc = NULL;
2564 }
void(* EndDataPtrType)(ArchiveHandle *AH, TocEntry *te)
void(* StartDataPtrType)(ArchiveHandle *AH, TocEntry *te)
struct _tocEntry * currToc

References _archiveHandle::currToc, _tocEntry::dataDumper, _tocEntry::dataDumperArg, _tocEntry::desc, _archiveHandle::EndDataPtr, _archiveHandle::EndLOsPtr, _archiveHandle::StartDataPtr, and _archiveHandle::StartLOsPtr.

Referenced by _WorkerJobDumpDirectory(), and WriteDataChunks().

◆ WriteHead()

void WriteHead ( ArchiveHandle AH)

Definition at line 3932 of file pg_backup_archiver.c.

3933 {
3934  struct tm crtm;
3935 
3936  AH->WriteBufPtr(AH, "PGDMP", 5); /* Magic code */
3937  AH->WriteBytePtr(AH, ARCHIVE_MAJOR(AH->version));
3938  AH->WriteBytePtr(AH, ARCHIVE_MINOR(AH->version));
3939  AH->WriteBytePtr(AH, ARCHIVE_REV(AH->version));
3940  AH->WriteBytePtr(AH, AH->intSize);
3941  AH->WriteBytePtr(AH, AH->offSize);
3942  AH->WriteBytePtr(AH, AH->format);
3944  crtm = *localtime(&AH->createDate);
3945  WriteInt(AH, crtm.tm_sec);
3946  WriteInt(AH, crtm.tm_min);
3947  WriteInt(AH, crtm.tm_hour);
3948  WriteInt(AH, crtm.tm_mday);
3949  WriteInt(AH, crtm.tm_mon);
3950  WriteInt(AH, crtm.tm_year);
3951  WriteInt(AH, crtm.tm_isdst);
3952  WriteStr(AH, PQdb(AH->connection));
3953  WriteStr(AH, AH->public.remoteVersionStr);
3954  WriteStr(AH, PG_VERSION);
3955 }
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6993
size_t WriteInt(ArchiveHandle *AH, int i)
size_t WriteStr(ArchiveHandle *AH, const char *c)
#define ARCHIVE_MAJOR(version)
#define ARCHIVE_MINOR(version)
#define ARCHIVE_REV(version)
char * remoteVersionStr
Definition: pg_backup.h:218

References pg_compress_specification::algorithm, ARCHIVE_MAJOR, ARCHIVE_MINOR, ARCHIVE_REV, _archiveHandle::compression_spec, _archiveHandle::connection, _archiveHandle::createDate, _archiveHandle::format, _archiveHandle::intSize, _archiveHandle::offSize, PQdb(), _archiveHandle::public, Archive::remoteVersionStr, tm, _archiveHandle::version, _archiveHandle::WriteBufPtr, _archiveHandle::WriteBytePtr, WriteInt(), and WriteStr().

Referenced by _CloseArchive().

◆ WriteInt()

size_t WriteInt ( ArchiveHandle AH,
int  i 
)

Definition at line 2090 of file pg_backup_archiver.c.

2091 {
2092  int b;
2093 
2094  /*
2095  * This is a bit yucky, but I don't want to make the binary format very
2096  * dependent on representation, and not knowing much about it, I write out
2097  * a sign byte. If you change this, don't forget to change the file
2098  * version #, and modify ReadInt to read the new format AS WELL AS the old
2099  * formats.
2100  */
2101 
2102  /* SIGN byte */
2103  if (i < 0)
2104  {
2105  AH->WriteBytePtr(AH, 1);
2106  i = -i;
2107  }
2108  else
2109  AH->WriteBytePtr(AH, 0);
2110 
2111  for (b = 0; b < AH->intSize; b++)
2112  {
2113  AH->WriteBytePtr(AH, i & 0xFF);
2114  i >>= 8;
2115  }
2116 
2117  return AH->intSize + 1;
2118 }

References b, i, _archiveHandle::intSize, and _archiveHandle::WriteBytePtr.

Referenced by _CustomWriteFunc(), _EndData(), _EndLO(), _EndLOs(), _StartData(), _StartLO(), _StartLOs(), WriteHead(), WriteStr(), and WriteToc().

◆ WriteOffset()

size_t WriteOffset ( ArchiveHandle AH,
pgoff_t  o,
int  wasSet 
)

Definition at line 2009 of file pg_backup_archiver.c.

2010 {
2011  int off;
2012 
2013  /* Save the flag */
2014  AH->WriteBytePtr(AH, wasSet);
2015 
2016  /* Write out pgoff_t smallest byte first, prevents endian mismatch */
2017  for (off = 0; off < sizeof(pgoff_t); off++)
2018  {
2019  AH->WriteBytePtr(AH, o & 0xFF);
2020  o >>= 8;
2021  }
2022  return sizeof(pgoff_t) + 1;
2023 }

References pgoff_t, and _archiveHandle::WriteBytePtr.

Referenced by _WriteExtraToc().

◆ WriteStr()

size_t WriteStr ( ArchiveHandle AH,
const char *  c 
)

Definition at line 2148 of file pg_backup_archiver.c.

2149 {
2150  size_t res;
2151 
2152  if (c)
2153  {
2154  int len = strlen(c);
2155 
2156  res = WriteInt(AH, len);
2157  AH->WriteBufPtr(AH, c, len);
2158  res += len;
2159  }
2160  else
2161  res = WriteInt(AH, -1);
2162 
2163  return res;
2164 }
const void size_t len
char * c

References len, res, _archiveHandle::WriteBufPtr, and WriteInt().

Referenced by _WriteExtraToc(), WriteHead(), and WriteToc().

◆ WriteToc()

void WriteToc ( ArchiveHandle AH)

Definition at line 2567 of file pg_backup_archiver.c.

2568 {
2569  TocEntry *te;
2570  char workbuf[32];
2571  int tocCount;
2572  int i;
2573 
2574  /* count entries that will actually be dumped */
2575  tocCount = 0;
2576  for (te = AH->toc->next; te != AH->toc; te = te->next)
2577  {
2578  if ((te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_SPECIAL)) != 0)
2579  tocCount++;
2580  }
2581 
2582  /* printf("%d TOC Entries to save\n", tocCount); */
2583 
2584  WriteInt(AH, tocCount);
2585 
2586  for (te = AH->toc->next; te != AH->toc; te = te->next)
2587  {
2588  if ((te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_SPECIAL)) == 0)
2589  continue;
2590 
2591  WriteInt(AH, te->dumpId);
2592  WriteInt(AH, te->dataDumper ? 1 : 0);
2593 
2594  /* OID is recorded as a string for historical reasons */
2595  sprintf(workbuf, "%u", te->catalogId.tableoid);
2596  WriteStr(AH, workbuf);
2597  sprintf(workbuf, "%u", te->catalogId.oid);
2598  WriteStr(AH, workbuf);
2599 
2600  WriteStr(AH, te->tag);
2601  WriteStr(AH, te->desc);
2602  WriteInt(AH, te->section);
2603  WriteStr(AH, te->defn);
2604  WriteStr(AH, te->dropStmt);
2605  WriteStr(AH, te->copyStmt);
2606  WriteStr(AH, te->namespace);
2607  WriteStr(AH, te->tablespace);
2608  WriteStr(AH, te->tableam);
2609  WriteInt(AH, te->relkind);
2610  WriteStr(AH, te->owner);
2611  WriteStr(AH, "false");
2612 
2613  /* Dump list of dependencies */
2614  for (i = 0; i < te->nDeps; i++)
2615  {
2616  sprintf(workbuf, "%d", te->dependencies[i]);
2617  WriteStr(AH, workbuf);
2618  }
2619  WriteStr(AH, NULL); /* Terminate List */
2620 
2621  if (AH->WriteExtraTocPtr)
2622  AH->WriteExtraTocPtr(AH, te);
2623  }
2624 }
#define REQ_SCHEMA
#define REQ_SPECIAL
#define sprintf
Definition: port.h:240

References _tocEntry::catalogId, _tocEntry::copyStmt, _tocEntry::dataDumper, _tocEntry::defn, _tocEntry::dependencies, _tocEntry::desc, _tocEntry::dropStmt, _tocEntry::dumpId, i, _tocEntry::nDeps, _tocEntry::next, CatalogId::oid, _tocEntry::owner, _tocEntry::relkind, REQ_DATA, REQ_SCHEMA, REQ_SPECIAL, _tocEntry::reqs, _tocEntry::section, sprintf, _tocEntry::tableam, CatalogId::tableoid, _tocEntry::tablespace, _tocEntry::tag, _archiveHandle::toc, _archiveHandle::WriteExtraTocPtr, WriteInt(), and WriteStr().

Referenced by _CloseArchive().