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  _z_stream
 
struct  sqlparseInfo
 
struct  _archiveHandle
 
struct  _tocEntry
 
struct  _archiveOpts
 

Macros

#define LOBBUFSIZE   16384
 
#define GZCLOSE(fh)   fclose(fh)
 
#define GZWRITE(p, s, n, fh)   (fwrite(p, s, n, fh) * (s))
 
#define GZREAD(p, s, n, fh)   fread(p, s, n, fh)
 
#define GZEOF(fh)   feof(fh)
 
#define Z_DEFAULT_COMPRESSION   (-1)
 
#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_MAJOR   1
 
#define K_VERS_MINOR   14
 
#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 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 _z_stream z_stream
 
typedef z_streamz_streamp
 
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(* StartBlobsPtrType) (ArchiveHandle *AH, TocEntry *te)
 
typedef void(* StartBlobPtrType) (ArchiveHandle *AH, TocEntry *te, Oid oid)
 
typedef void(* EndBlobPtrType) (ArchiveHandle *AH, TocEntry *te, Oid oid)
 
typedef void(* EndBlobsPtrType) (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 }
 
enum  teReqs { REQ_SCHEMA = 0x01, REQ_DATA = 0x02, REQ_SPECIAL = 0x04 }
 

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)
 
teReqs 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 *s)
 
int ReadOffset (ArchiveHandle *, pgoff_t *)
 
size_t WriteOffset (ArchiveHandle *, pgoff_t, int)
 
void StartRestoreBlobs (ArchiveHandle *AH)
 
void StartRestoreBlob (ArchiveHandle *AH, Oid oid, bool drop)
 
void EndRestoreBlob (ArchiveHandle *AH, Oid oid)
 
void EndRestoreBlobs (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 DropBlobIfExists (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 446 of file pg_backup_archiver.h.

Referenced by _WriteBlobData(), and dump_lo_buf().

◆ appendStringLiteralAHX

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

Definition at line 443 of file pg_backup_archiver.h.

Referenced by _doSetSessionAuth().

◆ ARCHIVE_MAJOR

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

Definition at line 70 of file pg_backup_archiver.h.

Referenced by PrintTOCSummary(), and WriteHead().

◆ ARCHIVE_MINOR

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

Definition at line 71 of file pg_backup_archiver.h.

Referenced by PrintTOCSummary(), and WriteHead().

◆ ARCHIVE_OPTS

◆ ARCHIVE_REV

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

Definition at line 72 of file pg_backup_archiver.h.

Referenced by PrintTOCSummary(), and WriteHead().

◆ BLK_BLOBS

#define BLK_BLOBS   3

Definition at line 65 of file pg_backup_archiver.h.

Referenced by _PrintTocData(), and _StartBlobs().

◆ BLK_DATA

#define BLK_DATA   1

Definition at line 64 of file pg_backup_archiver.h.

Referenced by _PrintTocData(), _readBlockHeader(), and _StartData().

◆ GZCLOSE

#define GZCLOSE (   fh)    fclose(fh)

Definition at line 46 of file pg_backup_archiver.h.

Referenced by CloseArchive(), RestoreOutput(), and tarClose().

◆ GZEOF

#define GZEOF (   fh)    feof(fh)

Definition at line 49 of file pg_backup_archiver.h.

Referenced by _tarReadRaw().

◆ GZREAD

#define GZREAD (   p,
  s,
  n,
  fh 
)    fread(p, s, n, fh)

Definition at line 48 of file pg_backup_archiver.h.

Referenced by _tarReadRaw().

◆ GZWRITE

#define GZWRITE (   p,
  s,
  n,
  fh 
)    (fwrite(p, s, n, fh) * (s))

Definition at line 47 of file pg_backup_archiver.h.

Referenced by ahwrite(), and tarWrite().

◆ K_OFFSET_NO_DATA

#define K_OFFSET_NO_DATA   3

Definition at line 110 of file pg_backup_archiver.h.

Referenced by _ArchiveEntry(), _PrintTocData(), and ReadOffset().

◆ K_OFFSET_POS_NOT_SET

#define K_OFFSET_POS_NOT_SET   1

Definition at line 108 of file pg_backup_archiver.h.

Referenced by _ArchiveEntry(), _PrintTocData(), and ReadOffset().

◆ K_OFFSET_POS_SET

#define K_OFFSET_POS_SET   2

◆ K_VERS_1_0

#define K_VERS_1_0   MAKE_ARCHIVE_VERSION(1, 0, 0)

Definition at line 75 of file pg_backup_archiver.h.

Referenced by ReadHead(), and ReadInt().

◆ K_VERS_1_10

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

Definition at line 88 of file pg_backup_archiver.h.

Referenced by ReadHead(), and ReadToc().

◆ 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 89 of file pg_backup_archiver.h.

Referenced by fix_dependencies(), and ReadToc().

◆ K_VERS_1_12

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

Definition at line 91 of file pg_backup_archiver.h.

Referenced by _StartBlob(), and StartRestoreBlob().

◆ K_VERS_1_13

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

Definition at line 93 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 95 of file pg_backup_archiver.h.

Referenced by ReadToc().

◆ K_VERS_1_2

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

Definition at line 76 of file pg_backup_archiver.h.

Referenced by ReadHead().

◆ K_VERS_1_3

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

Definition at line 77 of file pg_backup_archiver.h.

Referenced by _readBlockHeader(), ReadToc(), and RestoreArchive().

◆ K_VERS_1_4

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

Definition at line 78 of file pg_backup_archiver.h.

Referenced by ReadHead().

◆ K_VERS_1_5

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

Definition at line 79 of file pg_backup_archiver.h.

Referenced by ReadToc().

◆ K_VERS_1_6

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

Definition at line 80 of file pg_backup_archiver.h.

Referenced by ReadToc().

◆ K_VERS_1_7

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

Definition at line 81 of file pg_backup_archiver.h.

Referenced by _discoverArchiveFormat(), _ReadExtraToc(), ReadHead(), and ReadOffset().

◆ K_VERS_1_8

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

Definition at line 83 of file pg_backup_archiver.h.

Referenced by ReadToc(), and RestoreArchive().

◆ K_VERS_1_9

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

Definition at line 86 of file pg_backup_archiver.h.

Referenced by ReadToc().

◆ K_VERS_MAJOR

#define K_VERS_MAJOR   1

Definition at line 98 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 104 of file pg_backup_archiver.h.

Referenced by ReadHead().

◆ K_VERS_MINOR

#define K_VERS_MINOR   14

Definition at line 99 of file pg_backup_archiver.h.

◆ K_VERS_REV

#define K_VERS_REV   0

Definition at line 100 of file pg_backup_archiver.h.

◆ K_VERS_SELF

Definition at line 101 of file pg_backup_archiver.h.

Referenced by _allocAH().

◆ LOBBUFSIZE

#define LOBBUFSIZE   16384

◆ MAKE_ARCHIVE_VERSION

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

Definition at line 68 of file pg_backup_archiver.h.

Referenced by _discoverArchiveFormat(), and ReadHead().

◆ READ_ERROR_EXIT

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

Definition at line 125 of file pg_backup_archiver.h.

Referenced by _discoverArchiveFormat(), _ReadBuf(), _ReadByte(), _tarAddFile(), _tarReadRaw(), cfgetc(), and cfread().

◆ RESTORE_PASS_LAST

#define RESTORE_PASS_LAST   RESTORE_PASS_POST_ACL

Definition at line 229 of file pg_backup_archiver.h.

Referenced by restore_toc_entries_parallel().

◆ WORKER_CREATE_DONE

#define WORKER_CREATE_DONE   10

Definition at line 117 of file pg_backup_archiver.h.

Referenced by mark_restore_job_done(), and restore_toc_entry().

◆ WORKER_IGNORED_ERRORS

#define WORKER_IGNORED_ERRORS   12

◆ WORKER_INHIBIT_DATA

#define WORKER_INHIBIT_DATA   11

Definition at line 118 of file pg_backup_archiver.h.

Referenced by mark_restore_job_done(), and restore_toc_entry().

◆ WORKER_OK

#define WORKER_OK   0

Definition at line 116 of file pg_backup_archiver.h.

Referenced by restore_toc_entry().

◆ WRITE_ERROR_EXIT

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

Definition at line 133 of file pg_backup_archiver.h.

Referenced by _CloseArchive(), _tarAddFile(), _tarWriteHeader(), _WriteBuf(), _WriteByte(), _WriteData(), and ahwrite().

◆ Z_DEFAULT_COMPRESSION

#define Z_DEFAULT_COMPRESSION   (-1)

Definition at line 51 of file pg_backup_archiver.h.

Referenced by cfopen(), main(), ParseCompressionOption(), and ReadHead().

Typedef Documentation

◆ ArchiveEntryPtrType

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

Definition at line 146 of file pg_backup_archiver.h.

◆ ArchiveHandle

typedef struct _archiveHandle ArchiveHandle

Definition at line 121 of file pg_backup_archiver.h.

◆ ArchiveOpts

typedef void struct _archiveOpts ArchiveOpts

◆ ClonePtrType

typedef void(* ClonePtrType) (ArchiveHandle *AH)

Definition at line 167 of file pg_backup_archiver.h.

◆ ClosePtrType

typedef void(* ClosePtrType) (ArchiveHandle *AH)

Definition at line 144 of file pg_backup_archiver.h.

◆ CustomOutPtrType

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

Definition at line 173 of file pg_backup_archiver.h.

◆ DeClonePtrType

typedef void(* DeClonePtrType) (ArchiveHandle *AH)

Definition at line 168 of file pg_backup_archiver.h.

◆ EndBlobPtrType

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

Definition at line 154 of file pg_backup_archiver.h.

◆ EndBlobsPtrType

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

Definition at line 155 of file pg_backup_archiver.h.

◆ EndDataPtrType

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

Definition at line 150 of file pg_backup_archiver.h.

◆ PrepParallelRestorePtrType

typedef void(* PrepParallelRestorePtrType) (ArchiveHandle *AH)

Definition at line 166 of file pg_backup_archiver.h.

◆ PrintExtraTocPtrType

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

Definition at line 163 of file pg_backup_archiver.h.

◆ PrintTocDataPtrType

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

Definition at line 164 of file pg_backup_archiver.h.

◆ ReadBufPtrType

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

Definition at line 160 of file pg_backup_archiver.h.

◆ ReadBytePtrType

typedef int(* ReadBytePtrType) (ArchiveHandle *AH)

Definition at line 158 of file pg_backup_archiver.h.

◆ ReadExtraTocPtrType

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

Definition at line 162 of file pg_backup_archiver.h.

◆ ReopenPtrType

typedef void(* ReopenPtrType) (ArchiveHandle *AH)

Definition at line 145 of file pg_backup_archiver.h.

◆ StartBlobPtrType

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

Definition at line 153 of file pg_backup_archiver.h.

◆ StartBlobsPtrType

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

Definition at line 152 of file pg_backup_archiver.h.

◆ StartDataPtrType

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

Definition at line 148 of file pg_backup_archiver.h.

◆ T_Action

typedef enum T_Action T_Action

◆ TocEntry

typedef struct _tocEntry TocEntry

Definition at line 122 of file pg_backup_archiver.h.

◆ WorkerJobDumpPtrType

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

Definition at line 170 of file pg_backup_archiver.h.

◆ WorkerJobRestorePtrType

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

Definition at line 171 of file pg_backup_archiver.h.

◆ WriteBufPtrType

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

Definition at line 159 of file pg_backup_archiver.h.

◆ WriteBytePtrType

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

Definition at line 157 of file pg_backup_archiver.h.

◆ WriteDataPtrType

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

Definition at line 149 of file pg_backup_archiver.h.

◆ WriteExtraTocPtrType

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

Definition at line 161 of file pg_backup_archiver.h.

◆ z_stream

typedef struct _z_stream z_stream

◆ z_streamp

typedef z_stream* z_streamp

Definition at line 60 of file pg_backup_archiver.h.

Enumeration Type Documentation

◆ ArchiverOutput

Enumerator
OUTPUT_SQLCMDS 
OUTPUT_COPYDATA 
OUTPUT_OTHERDATA 

Definition at line 197 of file pg_backup_archiver.h.

198 {
199  OUTPUT_SQLCMDS = 0, /* emitting general SQL commands */
200  OUTPUT_COPYDATA, /* writing COPY data */
201  OUTPUT_OTHERDATA /* writing data as INSERT commands */
ArchiverOutput

◆ ArchiverStage

Enumerator
STAGE_NONE 
STAGE_INITIALIZING 
STAGE_PROCESSING 
STAGE_FINALIZING 

Definition at line 189 of file pg_backup_archiver.h.

◆ RestorePass

Enumerator
RESTORE_PASS_MAIN 
RESTORE_PASS_ACL 

Definition at line 223 of file pg_backup_archiver.h.

224 {
225  RESTORE_PASS_MAIN = 0, /* Main pass (most TOC item types) */
226  RESTORE_PASS_ACL, /* ACL item types */
227  RESTORE_PASS_POST_ACL /* Event trigger and matview refresh items */
228 
229 #define RESTORE_PASS_LAST RESTORE_PASS_POST_ACL
230 } RestorePass;

◆ sqlparseState

Enumerator
SQL_SCAN 
SQL_IN_SINGLE_QUOTE 
SQL_IN_DOUBLE_QUOTE 

Definition at line 175 of file pg_backup_archiver.h.

176 {
177  SQL_SCAN = 0, /* normal */
178  SQL_IN_SINGLE_QUOTE, /* '...' literal */
179  SQL_IN_DOUBLE_QUOTE /* "..." identifier */
180 } sqlparseState;
sqlparseState

◆ T_Action

enum T_Action
Enumerator
ACT_DUMP 
ACT_RESTORE 

Definition at line 138 of file pg_backup_archiver.h.

◆ teReqs

enum teReqs
Enumerator
REQ_SCHEMA 
REQ_DATA 
REQ_SPECIAL 

Definition at line 232 of file pg_backup_archiver.h.

233 {
234  REQ_SCHEMA = 0x01, /* want schema */
235  REQ_DATA = 0x02, /* want data */
236  REQ_SPECIAL = 0x04 /* for special TOC entries */
237 } teReqs;

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 1678 of file pg_backup_archiver.c.

References _archiveHandle::CustomOutPtr, dump_lo_buf(), ExecuteSqlCommandBuf(), _archiveHandle::gzOut, GZWRITE, _archiveHandle::lo_buf, _archiveHandle::lo_buf_size, _archiveHandle::lo_buf_used, _archiveHandle::OF, _archiveHandle::public, remaining, RestoringToDB(), WRITE_ERROR_EXIT, and _archiveHandle::writingBlob.

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

1679 {
1680  int bytes_written = 0;
1681 
1682  if (AH->writingBlob)
1683  {
1684  size_t remaining = size * nmemb;
1685 
1686  while (AH->lo_buf_used + remaining > AH->lo_buf_size)
1687  {
1688  size_t avail = AH->lo_buf_size - AH->lo_buf_used;
1689 
1690  memcpy((char *) AH->lo_buf + AH->lo_buf_used, ptr, avail);
1691  ptr = (const void *) ((const char *) ptr + avail);
1692  remaining -= avail;
1693  AH->lo_buf_used += avail;
1694  dump_lo_buf(AH);
1695  }
1696 
1697  memcpy((char *) AH->lo_buf + AH->lo_buf_used, ptr, remaining);
1698  AH->lo_buf_used += remaining;
1699 
1700  bytes_written = size * nmemb;
1701  }
1702  else if (AH->gzOut)
1703  bytes_written = GZWRITE(ptr, size, nmemb, AH->OF);
1704  else if (AH->CustomOutPtr)
1705  bytes_written = AH->CustomOutPtr(AH, ptr, size * nmemb);
1706 
1707  else
1708  {
1709  /*
1710  * If we're doing a restore, and it's direct to DB, and we're
1711  * connected then send it to the DB.
1712  */
1713  if (RestoringToDB(AH))
1714  bytes_written = ExecuteSqlCommandBuf(&AH->public, (const char *) ptr, size * nmemb);
1715  else
1716  bytes_written = fwrite(ptr, size, nmemb, AH->OF) * size;
1717  }
1718 
1719  if (bytes_written != size * nmemb)
1721 }
int remaining
Definition: informix.c:667
static int RestoringToDB(ArchiveHandle *AH)
int ExecuteSqlCommandBuf(Archive *AHX, const char *buf, size_t bufLen)
Definition: pg_backup_db.c:451
static void dump_lo_buf(ArchiveHandle *AH)
#define GZWRITE(p, s, n, fh)
CustomOutPtrType CustomOutPtr
#define WRITE_ERROR_EXIT

◆ ArchiveEntry()

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

Definition at line 1057 of file pg_backup_archiver.c.

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, _tocEntry::owner, pg_malloc(), pg_malloc0(), pg_strdup(), _tocEntry::prev, _tocEntry::section, _tocEntry::tableam, _tocEntry::tablespace, _tocEntry::tag, _archiveHandle::toc, _archiveHandle::tocCount, and true.

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

1059 {
1060  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1061  TocEntry *newToc;
1062 
1063  newToc = (TocEntry *) pg_malloc0(sizeof(TocEntry));
1064 
1065  AH->tocCount++;
1066  if (dumpId > AH->maxDumpId)
1067  AH->maxDumpId = dumpId;
1068 
1069  newToc->prev = AH->toc->prev;
1070  newToc->next = AH->toc;
1071  AH->toc->prev->next = newToc;
1072  AH->toc->prev = newToc;
1073 
1074  newToc->catalogId = catalogId;
1075  newToc->dumpId = dumpId;
1076  newToc->section = opts->section;
1077 
1078  newToc->tag = pg_strdup(opts->tag);
1079  newToc->namespace = opts->namespace ? pg_strdup(opts->namespace) : NULL;
1080  newToc->tablespace = opts->tablespace ? pg_strdup(opts->tablespace) : NULL;
1081  newToc->tableam = opts->tableam ? pg_strdup(opts->tableam) : NULL;
1082  newToc->owner = opts->owner ? pg_strdup(opts->owner) : NULL;
1083  newToc->desc = pg_strdup(opts->description);
1084  newToc->defn = opts->createStmt ? pg_strdup(opts->createStmt) : NULL;
1085  newToc->dropStmt = opts->dropStmt ? pg_strdup(opts->dropStmt) : NULL;
1086  newToc->copyStmt = opts->copyStmt ? pg_strdup(opts->copyStmt) : NULL;
1087 
1088  if (opts->nDeps > 0)
1089  {
1090  newToc->dependencies = (DumpId *) pg_malloc(opts->nDeps * sizeof(DumpId));
1091  memcpy(newToc->dependencies, opts->deps, opts->nDeps * sizeof(DumpId));
1092  newToc->nDeps = opts->nDeps;
1093  }
1094  else
1095  {
1096  newToc->dependencies = NULL;
1097  newToc->nDeps = 0;
1098  }
1099 
1100  newToc->dataDumper = opts->dumpFn;
1101  newToc->dataDumperArg = opts->dumpArg;
1102  newToc->hadDumper = opts->dumpFn ? true : false;
1103 
1104  newToc->formatData = NULL;
1105  newToc->dataLength = 0;
1106 
1107  if (AH->ArchiveEntryPtr != NULL)
1108  AH->ArchiveEntryPtr(AH, newToc);
1109 
1110  return newToc;
1111 }
struct _tocEntry * next
int DumpId
Definition: pg_backup.h:242
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
CatalogId catalogId
void * dataDumperArg
DataDumperPtr dataDumper
teSection section
#define true
Definition: c.h:328
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
struct _tocEntry * toc
DumpId * dependencies
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
pgoff_t dataLength
struct _tocEntry * prev
ArchiveEntryPtrType ArchiveEntryPtr

◆ checkSeek()

bool checkSeek ( FILE *  fp)

Definition at line 3861 of file pg_backup_archiver.c.

References fseeko, ftello, and pgoff_t.

Referenced by InitArchiveFmt_Custom(), and InitArchiveFmt_Tar().

3862 {
3863  pgoff_t tpos;
3864 
3865  /* Check that ftello works on this file */
3866  tpos = ftello(fp);
3867  if (tpos < 0)
3868  return false;
3869 
3870  /*
3871  * Check that fseeko(SEEK_SET) works, too. NB: we used to try to test
3872  * this with fseeko(fp, 0, SEEK_CUR). But some platforms treat that as a
3873  * successful no-op even on files that are otherwise unseekable.
3874  */
3875  if (fseeko(fp, tpos, SEEK_SET) != 0)
3876  return false;
3877 
3878  return true;
3879 }
#define pgoff_t
Definition: win32_port.h:194
#define ftello(stream)
Definition: win32_port.h:204
#define fseeko(stream, offset, origin)
Definition: win32_port.h:201

◆ CloneArchive()

ArchiveHandle* CloneArchive ( ArchiveHandle AH)

Definition at line 4785 of file pg_backup_archiver.c.

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

Referenced by RunWorker().

4786 {
4787  ArchiveHandle *clone;
4788 
4789  /* Make a "flat" copy */
4790  clone = (ArchiveHandle *) pg_malloc(sizeof(ArchiveHandle));
4791  memcpy(clone, AH, sizeof(ArchiveHandle));
4792 
4793  /* Handle format-independent fields */
4794  memset(&(clone->sqlparse), 0, sizeof(clone->sqlparse));
4795 
4796  /* The clone will have its own connection, so disregard connection state */
4797  clone->connection = NULL;
4798  clone->connCancel = NULL;
4799  clone->currUser = NULL;
4800  clone->currSchema = NULL;
4801  clone->currTablespace = NULL;
4802 
4803  /* savedPassword must be local in case we change it while connecting */
4804  if (clone->savedPassword)
4805  clone->savedPassword = pg_strdup(clone->savedPassword);
4806 
4807  /* clone has its own error count, too */
4808  clone->public.n_errors = 0;
4809 
4810  /*
4811  * Connect our new clone object to the database, using the same connection
4812  * parameters used for the original connection.
4813  */
4814  ConnectDatabase((Archive *) clone, &clone->public.ropt->cparams, true);
4815 
4816  /* re-establish fixed state */
4817  if (AH->mode == archModeRead)
4818  _doSetFixedOutputState(clone);
4819  /* in write case, setupDumpWorker will fix up connection state */
4820 
4821  /* Let the format-specific code have a chance too */
4822  clone->ClonePtr(clone);
4823 
4824  Assert(clone->connection != NULL);
4825  return clone;
4826 }
void ConnectDatabase(Archive *AHX, const ConnParams *cparams, bool isReconnect)
Definition: pg_backup_db.c:114
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
RestoreOptions * ropt
Definition: pg_backup.h:191
int n_errors
Definition: pg_backup.h:214
PGcancel *volatile connCancel
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static void _doSetFixedOutputState(ArchiveHandle *AH)
sqlparseInfo sqlparse
ConnParams cparams
Definition: pg_backup.h:124
#define Assert(condition)
Definition: c.h:745
ClonePtrType ClonePtr

◆ DeCloneArchive()

void DeCloneArchive ( ArchiveHandle AH)

Definition at line 4834 of file pg_backup_archiver.c.

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().

4835 {
4836  /* Should not have an open database connection */
4837  Assert(AH->connection == NULL);
4838 
4839  /* Clear format-specific state */
4840  AH->DeClonePtr(AH);
4841 
4842  /* Clear state allocated by CloneArchive */
4843  if (AH->sqlparse.curCmd)
4845 
4846  /* Clear any connection-local state */
4847  if (AH->currUser)
4848  free(AH->currUser);
4849  if (AH->currSchema)
4850  free(AH->currSchema);
4851  if (AH->currTablespace)
4852  free(AH->currTablespace);
4853  if (AH->currTableAm)
4854  free(AH->currTableAm);
4855  if (AH->savedPassword)
4856  free(AH->savedPassword);
4857 
4858  free(AH);
4859 }
PQExpBuffer curCmd
DeClonePtrType DeClonePtr
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
sqlparseInfo sqlparse
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:745

◆ DropBlobIfExists()

void DropBlobIfExists ( ArchiveHandle AH,
Oid  oid 
)

Definition at line 551 of file pg_backup_db.c.

References ahprintf(), _archiveHandle::connection, and PQserverVersion().

Referenced by _StartBlob(), RestoreArchive(), and StartRestoreBlob().

552 {
553  /*
554  * If we are not restoring to a direct database connection, we have to
555  * guess about how to detect whether the blob exists. Assume new-style.
556  */
557  if (AH->connection == NULL ||
558  PQserverVersion(AH->connection) >= 90000)
559  {
560  ahprintf(AH,
561  "SELECT pg_catalog.lo_unlink(oid) "
562  "FROM pg_catalog.pg_largeobject_metadata "
563  "WHERE oid = '%u';\n",
564  oid);
565  }
566  else
567  {
568  /* Restoring to pre-9.0 server, so do it the old way */
569  ahprintf(AH,
570  "SELECT CASE WHEN EXISTS("
571  "SELECT 1 FROM pg_catalog.pg_largeobject WHERE loid = '%u'"
572  ") THEN pg_catalog.lo_unlink('%u') END;\n",
573  oid, oid);
574  }
575 }
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6656
int ahprintf(ArchiveHandle *AH, const char *fmt,...)

◆ EndRestoreBlob()

void EndRestoreBlob ( ArchiveHandle AH,
Oid  oid 
)

Definition at line 1337 of file pg_backup_archiver.c.

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

Referenced by _LoadBlobs().

1338 {
1339  if (AH->lo_buf_used > 0)
1340  {
1341  /* Write remaining bytes from the LO buffer */
1342  dump_lo_buf(AH);
1343  }
1344 
1345  AH->writingBlob = 0;
1346 
1347  if (AH->connection)
1348  {
1349  lo_close(AH->connection, AH->loFd);
1350  AH->loFd = -1;
1351  }
1352  else
1353  {
1354  ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n");
1355  }
1356 }
int lo_close(PGconn *conn, int fd)
Definition: fe-lobj.c:99
static void dump_lo_buf(ArchiveHandle *AH)
int ahprintf(ArchiveHandle *AH, const char *fmt,...)

◆ EndRestoreBlobs()

void EndRestoreBlobs ( ArchiveHandle AH)

Definition at line 1270 of file pg_backup_archiver.c.

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

Referenced by _LoadBlobs().

1271 {
1272  RestoreOptions *ropt = AH->public.ropt;
1273 
1274  if (!ropt->single_txn)
1275  {
1276  if (AH->connection)
1277  CommitTransaction(&AH->public);
1278  else
1279  ahprintf(AH, "COMMIT;\n\n");
1280  }
1281 
1282  pg_log_info(ngettext("restored %d large object",
1283  "restored %d large objects",
1284  AH->blobCount),
1285  AH->blobCount);
1286 }
RestoreOptions * ropt
Definition: pg_backup.h:191
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
#define ngettext(s, p, n)
Definition: c.h:1123
static void CommitTransaction(void)
Definition: xact.c:2073
#define pg_log_info(...)
Definition: logging.h:88

◆ getTocEntryByDumpId()

TocEntry* getTocEntryByDumpId ( ArchiveHandle AH,
DumpId  id 
)

Definition at line 1858 of file pg_backup_archiver.c.

References buildTocEntryArrays(), and _archiveHandle::tocsByDumpId.

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

1859 {
1860  /* build index arrays if we didn't already */
1861  if (AH->tocsByDumpId == NULL)
1862  buildTocEntryArrays(AH);
1863 
1864  if (id > 0 && id <= AH->maxDumpId)
1865  return AH->tocsByDumpId[id];
1866 
1867  return NULL;
1868 }
static void buildTocEntryArrays(ArchiveHandle *AH)
struct _tocEntry ** tocsByDumpId

◆ InitArchiveFmt_Custom()

void InitArchiveFmt_Custom ( ArchiveHandle AH)

Definition at line 106 of file pg_backup_custom.c.

References _ArchiveEntry(), _Clone(), _CloseArchive(), _DeClone(), _EndBlob(), _EndBlobs(), _EndData(), _getFilePos(), _PrepParallelRestore(), _PrintExtraToc(), _PrintTocData(), _ReadBuf(), _ReadByte(), _ReadExtraToc(), _ReopenArchive(), _StartBlob(), _StartBlobs(), _StartData(), _WorkerJobRestoreCustom(), _WriteBuf(), _WriteByte(), _WriteData(), _WriteExtraToc(), _archiveHandle::ArchiveEntryPtr, archModeWrite, checkSeek(), _archiveHandle::ClonePtr, _archiveHandle::ClosePtr, _archiveHandle::DeClonePtr, _archiveHandle::EndBlobPtr, _archiveHandle::EndBlobsPtr, _archiveHandle::EndDataPtr, fatal, _archiveHandle::FH, _archiveHandle::formatData, _archiveHandle::fSpec, lclContext::hasSeek, lclContext::lastFilePos, _archiveHandle::lo_buf, _archiveHandle::lo_buf_size, LOBBUFSIZE, _archiveHandle::mode, PG_BINARY_R, PG_BINARY_W, pg_malloc(), pg_malloc0(), _archiveHandle::PrepParallelRestorePtr, _archiveHandle::PrintExtraTocPtr, _archiveHandle::PrintTocDataPtr, _archiveHandle::ReadBufPtr, _archiveHandle::ReadBytePtr, _archiveHandle::ReadExtraTocPtr, ReadHead(), ReadToc(), _archiveHandle::ReopenPtr, _archiveHandle::StartBlobPtr, _archiveHandle::StartBlobsPtr, _archiveHandle::StartDataPtr, generate_unaccent_rules::stdout, _archiveHandle::WorkerJobDumpPtr, _archiveHandle::WorkerJobRestorePtr, _archiveHandle::WriteBufPtr, _archiveHandle::WriteBytePtr, _archiveHandle::WriteDataPtr, and _archiveHandle::WriteExtraTocPtr.

Referenced by _allocAH().

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 
127  AH->StartBlobPtr = _StartBlob;
128  AH->EndBlobPtr = _EndBlob;
129  AH->EndBlobsPtr = _EndBlobs;
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  /* Initialize LO buffering */
144  AH->lo_buf_size = LOBBUFSIZE;
145  AH->lo_buf = (void *) pg_malloc(LOBBUFSIZE);
146 
147  /*
148  * Now open the file
149  */
150  if (AH->mode == archModeWrite)
151  {
152  if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
153  {
154  AH->FH = fopen(AH->fSpec, PG_BINARY_W);
155  if (!AH->FH)
156  fatal("could not open output file \"%s\": %m", AH->fSpec);
157  }
158  else
159  {
160  AH->FH = stdout;
161  if (!AH->FH)
162  fatal("could not open output file: %m");
163  }
164 
165  ctx->hasSeek = checkSeek(AH->FH);
166  }
167  else
168  {
169  if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
170  {
171  AH->FH = fopen(AH->fSpec, PG_BINARY_R);
172  if (!AH->FH)
173  fatal("could not open input file \"%s\": %m", AH->fSpec);
174  }
175  else
176  {
177  AH->FH = stdin;
178  if (!AH->FH)
179  fatal("could not open input file: %m");
180  }
181 
182  ctx->hasSeek = checkSeek(AH->FH);
183 
184  ReadHead(AH);
185  ReadToc(AH);
186 
187  /*
188  * Remember location of first data block (i.e., the point after TOC)
189  * in case we have to search for desired data blocks.
190  */
191  ctx->lastFilePos = _getFilePos(AH, ctx);
192  }
193 }
static void _ReopenArchive(ArchiveHandle *AH)
ReopenPtrType ReopenPtr
ReadBufPtrType ReadBufPtr
void ReadToc(ArchiveHandle *AH)
void ReadHead(ArchiveHandle *AH)
DeClonePtrType DeClonePtr
static void _Clone(ArchiveHandle *AH)
static void _StartData(ArchiveHandle *AH, TocEntry *te)
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
WriteBufPtrType WriteBufPtr
static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
StartBlobPtrType StartBlobPtr
static int _WorkerJobRestoreCustom(ArchiveHandle *AH, TocEntry *te)
EndDataPtrType EndDataPtr
#define PG_BINARY_W
Definition: c.h:1214
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te)
static void _CloseArchive(ArchiveHandle *AH)
StartBlobsPtrType StartBlobsPtr
#define PG_BINARY_R
Definition: c.h:1213
ReadBytePtrType ReadBytePtr
EndBlobPtrType EndBlobPtr
static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
static void _StartBlobs(ArchiveHandle *AH, TocEntry *te)
StartDataPtrType StartDataPtr
WriteBytePtrType WriteBytePtr
static void _EndData(ArchiveHandle *AH, TocEntry *te)
static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _PrepParallelRestore(ArchiveHandle *AH)
static int _ReadByte(ArchiveHandle *)
PrintTocDataPtrType PrintTocDataPtr
WriteDataPtrType WriteDataPtr
static pgoff_t _getFilePos(ArchiveHandle *AH, lclContext *ctx)
static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
static void _EndBlobs(ArchiveHandle *AH, TocEntry *te)
PrepParallelRestorePtrType PrepParallelRestorePtr
EndBlobsPtrType EndBlobsPtr
ArchiveEntryPtrType ArchiveEntryPtr
#define fatal(...)
ClonePtrType ClonePtr
WorkerJobRestorePtrType WorkerJobRestorePtr
pgoff_t lastFilePos
static void _DeClone(ArchiveHandle *AH)
bool checkSeek(FILE *fp)
ClosePtrType ClosePtr
PrintExtraTocPtrType PrintExtraTocPtr
static int _WriteByte(ArchiveHandle *AH, const int i)
WriteExtraTocPtrType WriteExtraTocPtr
#define LOBBUFSIZE
ReadExtraTocPtrType ReadExtraTocPtr
static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
WorkerJobDumpPtrType WorkerJobDumpPtr

◆ InitArchiveFmt_Directory()

void InitArchiveFmt_Directory ( ArchiveHandle AH)

Definition at line 108 of file pg_backup_directory.c.

References _ArchiveEntry(), _Clone(), _CloseArchive(), _DeClone(), _EndBlob(), _EndBlobs(), _EndData(), _PrepParallelRestore(), _PrintExtraToc(), _PrintTocData(), _ReadBuf(), _ReadByte(), _ReadExtraToc(), _ReopenArchive(), _StartBlob(), _StartBlobs(), _StartData(), _WorkerJobDumpDirectory(), _WorkerJobRestoreDirectory(), _WriteBuf(), _WriteByte(), _WriteData(), _WriteExtraToc(), archDirectory, _archiveHandle::ArchiveEntryPtr, archModeWrite, archTar, lclContext::blobsTocFH, cfclose(), cfopen_read(), _archiveHandle::ClonePtr, closedir(), _archiveHandle::ClosePtr, dirent::d_name, lclContext::dataFH, _archiveHandle::DeClonePtr, lclContext::directory, _archiveHandle::EndBlobPtr, _archiveHandle::EndBlobsPtr, _archiveHandle::EndDataPtr, fatal, _archiveHandle::format, _archiveHandle::formatData, _archiveHandle::fSpec, _archiveHandle::lo_buf, _archiveHandle::lo_buf_size, LOBBUFSIZE, MAXPGPATH, mkdir, _archiveHandle::mode, opendir(), PG_BINARY_R, pg_malloc(), pg_malloc0(), _archiveHandle::PrepParallelRestorePtr, _archiveHandle::PrintExtraTocPtr, _archiveHandle::PrintTocDataPtr, _archiveHandle::ReadBufPtr, _archiveHandle::ReadBytePtr, readdir(), _archiveHandle::ReadExtraTocPtr, ReadHead(), ReadToc(), _archiveHandle::ReopenPtr, S_ISDIR, setFilePath(), _archiveHandle::StartBlobPtr, _archiveHandle::StartBlobsPtr, _archiveHandle::StartDataPtr, stat, _archiveHandle::WorkerJobDumpPtr, _archiveHandle::WorkerJobRestorePtr, _archiveHandle::WriteBufPtr, _archiveHandle::WriteBytePtr, _archiveHandle::WriteDataPtr, and _archiveHandle::WriteExtraTocPtr.

Referenced by _allocAH().

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

◆ InitArchiveFmt_Null()

void InitArchiveFmt_Null ( ArchiveHandle AH)

Definition at line 48 of file pg_backup_null.c.

References _CloseArchive(), _EndBlob(), _EndBlobs(), _EndData(), _PrintTocData(), _StartBlob(), _StartBlobs(), _WriteBuf(), _WriteByte(), _WriteData(), archModeRead, _archiveHandle::ClonePtr, _archiveHandle::ClosePtr, _archiveHandle::DeClonePtr, _archiveHandle::EndBlobPtr, _archiveHandle::EndBlobsPtr, _archiveHandle::EndDataPtr, fatal, _archiveHandle::lo_buf, _archiveHandle::lo_buf_size, LOBBUFSIZE, _archiveHandle::mode, pg_malloc(), _archiveHandle::PrintTocDataPtr, _archiveHandle::ReopenPtr, _archiveHandle::StartBlobPtr, _archiveHandle::StartBlobsPtr, _archiveHandle::WriteBufPtr, _archiveHandle::WriteBytePtr, and _archiveHandle::WriteDataPtr.

Referenced by _allocAH().

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 
61  AH->EndBlobPtr = _EndBlob;
62  AH->EndBlobsPtr = _EndBlobs;
63  AH->ClonePtr = NULL;
64  AH->DeClonePtr = NULL;
65 
66  /* Initialize LO buffering */
67  AH->lo_buf_size = LOBBUFSIZE;
68  AH->lo_buf = (void *) pg_malloc(LOBBUFSIZE);
69 
70  /*
71  * Now prevent reading...
72  */
73  if (AH->mode == archModeRead)
74  fatal("this format cannot be read");
75 }
ReopenPtrType ReopenPtr
DeClonePtrType DeClonePtr
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
WriteBufPtrType WriteBufPtr
StartBlobPtrType StartBlobPtr
EndDataPtrType EndDataPtr
StartBlobsPtrType StartBlobsPtr
EndBlobPtrType EndBlobPtr
static void _CloseArchive(ArchiveHandle *AH)
static void _EndBlobs(ArchiveHandle *AH, TocEntry *te)
WriteBytePtrType WriteBytePtr
static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te)
static void _StartBlobs(ArchiveHandle *AH, TocEntry *te)
PrintTocDataPtrType PrintTocDataPtr
WriteDataPtrType WriteDataPtr
static void _EndData(ArchiveHandle *AH, TocEntry *te)
EndBlobsPtrType EndBlobsPtr
static int _WriteByte(ArchiveHandle *AH, const int i)
static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
#define fatal(...)
ClonePtrType ClonePtr
ClosePtrType ClosePtr
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
#define LOBBUFSIZE
static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)

◆ InitArchiveFmt_Tar()

void InitArchiveFmt_Tar ( ArchiveHandle AH)

Definition at line 126 of file pg_backup_tar.c.

References _ArchiveEntry(), _CloseArchive(), _EndBlob(), _EndBlobs(), _EndData(), _PrintExtraToc(), _PrintTocData(), _ReadBuf(), _ReadByte(), _ReadExtraToc(), _StartBlob(), _StartBlobs(), _StartData(), _WriteBuf(), _WriteByte(), _WriteData(), _WriteExtraToc(), archModeWrite, checkSeek(), fatal, lclContext::FH, lclContext::filePos, lclContext::hasSeek, lclContext::isSpecialScript, LOBBUFSIZE, PG_BINARY_R, PG_BINARY_W, pg_malloc(), pg_malloc0(), ReadHead(), ReadToc(), generate_unaccent_rules::stdout, tarClose(), lclContext::tarFH, lclContext::tarFHpos, and tarOpen().

Referenced by _allocAH().

127 {
128  lclContext *ctx;
129 
130  /* Assuming static functions, this can be copied for each format. */
132  AH->StartDataPtr = _StartData;
133  AH->WriteDataPtr = _WriteData;
134  AH->EndDataPtr = _EndData;
135  AH->WriteBytePtr = _WriteByte;
136  AH->ReadBytePtr = _ReadByte;
137  AH->WriteBufPtr = _WriteBuf;
138  AH->ReadBufPtr = _ReadBuf;
139  AH->ClosePtr = _CloseArchive;
140  AH->ReopenPtr = NULL;
145 
147  AH->StartBlobPtr = _StartBlob;
148  AH->EndBlobPtr = _EndBlob;
149  AH->EndBlobsPtr = _EndBlobs;
150  AH->ClonePtr = NULL;
151  AH->DeClonePtr = NULL;
152 
153  AH->WorkerJobDumpPtr = NULL;
154  AH->WorkerJobRestorePtr = NULL;
155 
156  /*
157  * Set up some special context used in compressing data.
158  */
159  ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
160  AH->formatData = (void *) ctx;
161  ctx->filePos = 0;
162  ctx->isSpecialScript = 0;
163 
164  /* Initialize LO buffering */
165  AH->lo_buf_size = LOBBUFSIZE;
166  AH->lo_buf = (void *) pg_malloc(LOBBUFSIZE);
167 
168  /*
169  * Now open the tar file, and load the TOC if we're in read mode.
170  */
171  if (AH->mode == archModeWrite)
172  {
173  if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
174  {
175  ctx->tarFH = fopen(AH->fSpec, PG_BINARY_W);
176  if (ctx->tarFH == NULL)
177  fatal("could not open TOC file \"%s\" for output: %m",
178  AH->fSpec);
179  }
180  else
181  {
182  ctx->tarFH = stdout;
183  if (ctx->tarFH == NULL)
184  fatal("could not open TOC file for output: %m");
185  }
186 
187  ctx->tarFHpos = 0;
188 
189  /*
190  * Make unbuffered since we will dup() it, and the buffers screw each
191  * other
192  */
193  /* setvbuf(ctx->tarFH, NULL, _IONBF, 0); */
194 
195  ctx->hasSeek = checkSeek(ctx->tarFH);
196 
197  /*
198  * We don't support compression because reading the files back is not
199  * possible since gzdopen uses buffered IO which totally screws file
200  * positioning.
201  */
202  if (AH->compression != 0)
203  fatal("compression is not supported by tar archive format");
204  }
205  else
206  { /* Read Mode */
207  if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
208  {
209  ctx->tarFH = fopen(AH->fSpec, PG_BINARY_R);
210  if (ctx->tarFH == NULL)
211  fatal("could not open TOC file \"%s\" for input: %m",
212  AH->fSpec);
213  }
214  else
215  {
216  ctx->tarFH = stdin;
217  if (ctx->tarFH == NULL)
218  fatal("could not open TOC file for input: %m");
219  }
220 
221  /*
222  * Make unbuffered since we will dup() it, and the buffers screw each
223  * other
224  */
225  /* setvbuf(ctx->tarFH, NULL, _IONBF, 0); */
226 
227  ctx->tarFHpos = 0;
228 
229  ctx->hasSeek = checkSeek(ctx->tarFH);
230 
231  /*
232  * Forcibly unmark the header as read since we use the lookahead
233  * buffer
234  */
235  AH->readHeader = 0;
236 
237  ctx->FH = (void *) tarOpen(AH, "toc.dat", 'r');
238  ReadHead(AH);
239  ReadToc(AH);
240  tarClose(AH, ctx->FH); /* Nothing else in the file... */
241  }
242 }
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
ReopenPtrType ReopenPtr
ReadBufPtrType ReadBufPtr
void ReadToc(ArchiveHandle *AH)
static void _StartData(ArchiveHandle *AH, TocEntry *te)
void ReadHead(ArchiveHandle *AH)
DeClonePtrType DeClonePtr
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
WriteBufPtrType WriteBufPtr
static TAR_MEMBER * tarOpen(ArchiveHandle *AH, const char *filename, char mode)
pgoff_t tarFHpos
Definition: pg_backup_tar.c:89
StartBlobPtrType StartBlobPtr
EndDataPtrType EndDataPtr
#define PG_BINARY_W
Definition: c.h:1214
static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
StartBlobsPtrType StartBlobsPtr
#define PG_BINARY_R
Definition: c.h:1213
ReadBytePtrType ReadBytePtr
EndBlobPtrType EndBlobPtr
int isSpecialScript
Definition: pg_backup_tar.c:92
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
FILE * tarFH
Definition: pg_backup_tar.c:88
static void _StartBlobs(ArchiveHandle *AH, TocEntry *te)
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
StartDataPtrType StartDataPtr
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
WriteBytePtrType WriteBytePtr
static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _EndBlobs(ArchiveHandle *AH, TocEntry *te)
PrintTocDataPtrType PrintTocDataPtr
WriteDataPtrType WriteDataPtr
static void _EndData(ArchiveHandle *AH, TocEntry *te)
EndBlobsPtrType EndBlobsPtr
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te)
static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
ArchiveEntryPtrType ArchiveEntryPtr
#define fatal(...)
static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
ClonePtrType ClonePtr
static void _CloseArchive(ArchiveHandle *AH)
static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
WorkerJobRestorePtrType WorkerJobRestorePtr
bool checkSeek(FILE *fp)
ClosePtrType ClosePtr
PrintExtraTocPtrType PrintExtraTocPtr
static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te)
pgoff_t filePos
Definition: pg_backup_tar.c:86
static int _ReadByte(ArchiveHandle *)
WriteExtraTocPtrType WriteExtraTocPtr
#define LOBBUFSIZE
static int _WriteByte(ArchiveHandle *AH, const int i)
ReadExtraTocPtrType ReadExtraTocPtr
static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
WorkerJobDumpPtrType WorkerJobDumpPtr

◆ isValidTarHeader()

bool isValidTarHeader ( char *  header)

Definition at line 1045 of file pg_backup_tar.c.

References read_tar_number(), and tarChecksum().

Referenced by _discoverArchiveFormat().

1046 {
1047  int sum;
1048  int chk = tarChecksum(header);
1049 
1050  sum = read_tar_number(&header[148], 8);
1051 
1052  if (sum != chk)
1053  return false;
1054 
1055  /* POSIX tar format */
1056  if (memcmp(&header[257], "ustar\0", 6) == 0 &&
1057  memcmp(&header[263], "00", 2) == 0)
1058  return true;
1059  /* GNU tar format */
1060  if (memcmp(&header[257], "ustar \0", 8) == 0)
1061  return true;
1062  /* not-quite-POSIX format written by pre-9.3 pg_dump */
1063  if (memcmp(&header[257], "ustar00\0", 8) == 0)
1064  return true;
1065 
1066  return false;
1067 }
uint64 read_tar_number(const char *s, int len)
Definition: tar.c:58
int tarChecksum(char *header)
Definition: tar.c:90
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:210

◆ on_exit_close_archive()

void on_exit_close_archive ( Archive AHX)

Definition at line 347 of file parallel.c.

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

Referenced by main().

348 {
349  shutdown_info.AHX = AHX;
351 }
void on_exit_nicely(on_exit_nicely_callback function, void *arg)
static ShutdownInformation shutdown_info
Definition: parallel.c:154
static void archive_close_connection(int code, void *arg)
Definition: parallel.c:358

◆ parallel_restore()

int parallel_restore ( ArchiveHandle AH,
TocEntry te 
)

Definition at line 4418 of file pg_backup_archiver.c.

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

Referenced by _WorkerJobRestoreCustom(), and _WorkerJobRestoreDirectory().

4419 {
4420  int status;
4421 
4422  Assert(AH->connection != NULL);
4423 
4424  /* Count only errors associated with this TOC entry */
4425  AH->public.n_errors = 0;
4426 
4427  /* Restore the TOC item */
4428  status = restore_toc_entry(AH, te, true);
4429 
4430  return status;
4431 }
int n_errors
Definition: pg_backup.h:214
static int restore_toc_entry(ArchiveHandle *AH, TocEntry *te, bool is_parallel)
#define Assert(condition)
Definition: c.h:745
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227

◆ ReadHead()

void ReadHead ( ArchiveHandle AH)

Definition at line 3758 of file pg_backup_archiver.c.

References _archiveHandle::archdbname, _archiveHandle::archiveDumpVersion, _archiveHandle::archiveRemoteVersion, _archiveHandle::compression, _archiveHandle::createDate, fatal, _archiveHandle::format, _archiveHandle::intSize, K_VERS_1_0, K_VERS_1_10, K_VERS_1_2, K_VERS_1_4, K_VERS_1_7, K_VERS_MAX, MAKE_ARCHIVE_VERSION, _archiveHandle::offSize, pg_log_warning, _archiveHandle::ReadBufPtr, _archiveHandle::ReadBytePtr, _archiveHandle::readHeader, ReadInt(), ReadStr(), tm, _archiveHandle::version, and Z_DEFAULT_COMPRESSION.

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

3759 {
3760  char tmpMag[7];
3761  int fmt;
3762  struct tm crtm;
3763 
3764  /*
3765  * If we haven't already read the header, do so.
3766  *
3767  * NB: this code must agree with _discoverArchiveFormat(). Maybe find a
3768  * way to unify the cases?
3769  */
3770  if (!AH->readHeader)
3771  {
3772  char vmaj,
3773  vmin,
3774  vrev;
3775 
3776  AH->ReadBufPtr(AH, tmpMag, 5);
3777 
3778  if (strncmp(tmpMag, "PGDMP", 5) != 0)
3779  fatal("did not find magic string in file header");
3780 
3781  vmaj = AH->ReadBytePtr(AH);
3782  vmin = AH->ReadBytePtr(AH);
3783 
3784  if (vmaj > 1 || (vmaj == 1 && vmin > 0)) /* Version > 1.0 */
3785  vrev = AH->ReadBytePtr(AH);
3786  else
3787  vrev = 0;
3788 
3789  AH->version = MAKE_ARCHIVE_VERSION(vmaj, vmin, vrev);
3790 
3791  if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX)
3792  fatal("unsupported version (%d.%d) in file header",
3793  vmaj, vmin);
3794 
3795  AH->intSize = AH->ReadBytePtr(AH);
3796  if (AH->intSize > 32)
3797  fatal("sanity check on integer size (%lu) failed",
3798  (unsigned long) AH->intSize);
3799 
3800  if (AH->intSize > sizeof(int))
3801  pg_log_warning("archive was made on a machine with larger integers, some operations might fail");
3802 
3803  if (AH->version >= K_VERS_1_7)
3804  AH->offSize = AH->ReadBytePtr(AH);
3805  else
3806  AH->offSize = AH->intSize;
3807 
3808  fmt = AH->ReadBytePtr(AH);
3809 
3810  if (AH->format != fmt)
3811  fatal("expected format (%d) differs from format found in file (%d)",
3812  AH->format, fmt);
3813  }
3814 
3815  if (AH->version >= K_VERS_1_2)
3816  {
3817  if (AH->version < K_VERS_1_4)
3818  AH->compression = AH->ReadBytePtr(AH);
3819  else
3820  AH->compression = ReadInt(AH);
3821  }
3822  else
3824 
3825 #ifndef HAVE_LIBZ
3826  if (AH->compression != 0)
3827  pg_log_warning("archive is compressed, but this installation does not support compression -- no data will be available");
3828 #endif
3829 
3830  if (AH->version >= K_VERS_1_4)
3831  {
3832  crtm.tm_sec = ReadInt(AH);
3833  crtm.tm_min = ReadInt(AH);
3834  crtm.tm_hour = ReadInt(AH);
3835  crtm.tm_mday = ReadInt(AH);
3836  crtm.tm_mon = ReadInt(AH);
3837  crtm.tm_year = ReadInt(AH);
3838  crtm.tm_isdst = ReadInt(AH);
3839 
3840  AH->archdbname = ReadStr(AH);
3841 
3842  AH->createDate = mktime(&crtm);
3843 
3844  if (AH->createDate == (time_t) -1)
3845  pg_log_warning("invalid creation date in header");
3846  }
3847 
3848  if (AH->version >= K_VERS_1_10)
3849  {
3850  AH->archiveRemoteVersion = ReadStr(AH);
3851  AH->archiveDumpVersion = ReadStr(AH);
3852  }
3853 }
ReadBufPtrType ReadBufPtr
#define Z_DEFAULT_COMPRESSION
#define K_VERS_1_0
int ReadInt(ArchiveHandle *AH)
#define K_VERS_1_4
ReadBytePtrType ReadBytePtr
static struct pg_tm tm
Definition: localtime.c:102
#define K_VERS_1_7
#define MAKE_ARCHIVE_VERSION(major, minor, rev)
#define K_VERS_1_2
#define K_VERS_1_10
ArchiveFormat format
#define K_VERS_MAX
char * ReadStr(ArchiveHandle *AH)
#define fatal(...)
#define pg_log_warning(...)
Definition: pgfnames.c:24

◆ ReadInt()

int ReadInt ( ArchiveHandle AH)

Definition at line 1994 of file pg_backup_archiver.c.

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

Referenced by _CustomReadFunc(), _LoadBlobs(), _readBlockHeader(), _ReadExtraToc(), _skipBlobs(), _skipData(), ReadHead(), ReadOffset(), ReadStr(), and ReadToc().

1995 {
1996  int res = 0;
1997  int bv,
1998  b;
1999  int sign = 0; /* Default positive */
2000  int bitShift = 0;
2001 
2002  if (AH->version > K_VERS_1_0)
2003  /* Read a sign byte */
2004  sign = AH->ReadBytePtr(AH);
2005 
2006  for (b = 0; b < AH->intSize; b++)
2007  {
2008  bv = AH->ReadBytePtr(AH) & 0xFF;
2009  if (bv != 0)
2010  res = res + (bv << bitShift);
2011  bitShift += 8;
2012  }
2013 
2014  if (sign)
2015  res = -res;
2016 
2017  return res;
2018 }
#define K_VERS_1_0
ReadBytePtrType ReadBytePtr
char sign
Definition: informix.c:668

◆ ReadOffset()

int ReadOffset ( ArchiveHandle ,
pgoff_t  
)

Definition at line 1899 of file pg_backup_archiver.c.

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

Referenced by _ReadExtraToc().

1900 {
1901  int i;
1902  int off;
1903  int offsetFlg;
1904 
1905  /* Initialize to zero */
1906  *o = 0;
1907 
1908  /* Check for old version */
1909  if (AH->version < K_VERS_1_7)
1910  {
1911  /* Prior versions wrote offsets using WriteInt */
1912  i = ReadInt(AH);
1913  /* -1 means not set */
1914  if (i < 0)
1915  return K_OFFSET_POS_NOT_SET;
1916  else if (i == 0)
1917  return K_OFFSET_NO_DATA;
1918 
1919  /* Cast to pgoff_t because it was written as an int. */
1920  *o = (pgoff_t) i;
1921  return K_OFFSET_POS_SET;
1922  }
1923 
1924  /*
1925  * Read the flag indicating the state of the data pointer. Check if valid
1926  * and die if not.
1927  *
1928  * This used to be handled by a negative or zero pointer, now we use an
1929  * extra byte specifically for the state.
1930  */
1931  offsetFlg = AH->ReadBytePtr(AH) & 0xFF;
1932 
1933  switch (offsetFlg)
1934  {
1935  case K_OFFSET_POS_NOT_SET:
1936  case K_OFFSET_NO_DATA:
1937  case K_OFFSET_POS_SET:
1938 
1939  break;
1940 
1941  default:
1942  fatal("unexpected data offset flag %d", offsetFlg);
1943  }
1944 
1945  /*
1946  * Read the bytes
1947  */
1948  for (off = 0; off < AH->offSize; off++)
1949  {
1950  if (off < sizeof(pgoff_t))
1951  *o |= ((pgoff_t) (AH->ReadBytePtr(AH))) << (off * 8);
1952  else
1953  {
1954  if (AH->ReadBytePtr(AH) != 0)
1955  fatal("file offset in dump file is too large");
1956  }
1957  }
1958 
1959  return offsetFlg;
1960 }
int ReadInt(ArchiveHandle *AH)
#define K_VERS_1_7
#define pgoff_t
Definition: win32_port.h:194
#define K_OFFSET_NO_DATA
#define fatal(...)
#define K_OFFSET_POS_SET
int i
#define K_OFFSET_POS_NOT_SET

◆ ReadStr()

char* ReadStr ( ArchiveHandle AH)

Definition at line 2040 of file pg_backup_archiver.c.

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

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

2041 {
2042  char *buf;
2043  int l;
2044 
2045  l = ReadInt(AH);
2046  if (l < 0)
2047  buf = NULL;
2048  else
2049  {
2050  buf = (char *) pg_malloc(l + 1);
2051  AH->ReadBufPtr(AH, (void *) buf, l);
2052 
2053  buf[l] = '\0';
2054  }
2055 
2056  return buf;
2057 }
ReadBufPtrType ReadBufPtr
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int ReadInt(ArchiveHandle *AH)
static char * buf
Definition: pg_test_fsync.c:68

◆ ReadToc()

void ReadToc ( ArchiveHandle AH)

Definition at line 2547 of file pg_backup_archiver.c.

References _tocEntry::catalogId, _tocEntry::copyStmt, _tocEntry::dataLength, _tocEntry::defn, _tocEntry::dependencies, _tocEntry::desc, _tocEntry::dropStmt, _tocEntry::dumpId, fatal, free, _tocEntry::hadDumper, i, InvalidOid, K_VERS_1_10, K_VERS_1_11, K_VERS_1_14, 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_log_debug, pg_log_warning, pg_malloc(), pg_malloc0(), pg_realloc(), _tocEntry::prev, processEncodingEntry(), processSearchPathEntry(), processStdStringsEntry(), _archiveHandle::ReadExtraTocPtr, ReadInt(), ReadStr(), _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().

2548 {
2549  int i;
2550  char *tmp;
2551  DumpId *deps;
2552  int depIdx;
2553  int depSize;
2554  TocEntry *te;
2555 
2556  AH->tocCount = ReadInt(AH);
2557  AH->maxDumpId = 0;
2558 
2559  for (i = 0; i < AH->tocCount; i++)
2560  {
2561  te = (TocEntry *) pg_malloc0(sizeof(TocEntry));
2562  te->dumpId = ReadInt(AH);
2563 
2564  if (te->dumpId > AH->maxDumpId)
2565  AH->maxDumpId = te->dumpId;
2566 
2567  /* Sanity check */
2568  if (te->dumpId <= 0)
2569  fatal("entry ID %d out of range -- perhaps a corrupt TOC",
2570  te->dumpId);
2571 
2572  te->hadDumper = ReadInt(AH);
2573 
2574  if (AH->version >= K_VERS_1_8)
2575  {
2576  tmp = ReadStr(AH);
2577  sscanf(tmp, "%u", &te->catalogId.tableoid);
2578  free(tmp);
2579  }
2580  else
2582  tmp = ReadStr(AH);
2583  sscanf(tmp, "%u", &te->catalogId.oid);
2584  free(tmp);
2585 
2586  te->tag = ReadStr(AH);
2587  te->desc = ReadStr(AH);
2588 
2589  if (AH->version >= K_VERS_1_11)
2590  {
2591  te->section = ReadInt(AH);
2592  }
2593  else
2594  {
2595  /*
2596  * Rules for pre-8.4 archives wherein pg_dump hasn't classified
2597  * the entries into sections. This list need not cover entry
2598  * types added later than 8.4.
2599  */
2600  if (strcmp(te->desc, "COMMENT") == 0 ||
2601  strcmp(te->desc, "ACL") == 0 ||
2602  strcmp(te->desc, "ACL LANGUAGE") == 0)
2603  te->section = SECTION_NONE;
2604  else if (strcmp(te->desc, "TABLE DATA") == 0 ||
2605  strcmp(te->desc, "BLOBS") == 0 ||
2606  strcmp(te->desc, "BLOB COMMENTS") == 0)
2607  te->section = SECTION_DATA;
2608  else if (strcmp(te->desc, "CONSTRAINT") == 0 ||
2609  strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
2610  strcmp(te->desc, "FK CONSTRAINT") == 0 ||
2611  strcmp(te->desc, "INDEX") == 0 ||
2612  strcmp(te->desc, "RULE") == 0 ||
2613  strcmp(te->desc, "TRIGGER") == 0)
2614  te->section = SECTION_POST_DATA;
2615  else
2616  te->section = SECTION_PRE_DATA;
2617  }
2618 
2619  te->defn = ReadStr(AH);
2620  te->dropStmt = ReadStr(AH);
2621 
2622  if (AH->version >= K_VERS_1_3)
2623  te->copyStmt = ReadStr(AH);
2624 
2625  if (AH->version >= K_VERS_1_6)
2626  te->namespace = ReadStr(AH);
2627 
2628  if (AH->version >= K_VERS_1_10)
2629  te->tablespace = ReadStr(AH);
2630 
2631  if (AH->version >= K_VERS_1_14)
2632  te->tableam = ReadStr(AH);
2633 
2634  te->owner = ReadStr(AH);
2635  if (AH->version < K_VERS_1_9 || strcmp(ReadStr(AH), "true") == 0)
2636  pg_log_warning("restoring tables WITH OIDS is not supported anymore");
2637 
2638  /* Read TOC entry dependencies */
2639  if (AH->version >= K_VERS_1_5)
2640  {
2641  depSize = 100;
2642  deps = (DumpId *) pg_malloc(sizeof(DumpId) * depSize);
2643  depIdx = 0;
2644  for (;;)
2645  {
2646  tmp = ReadStr(AH);
2647  if (!tmp)
2648  break; /* end of list */
2649  if (depIdx >= depSize)
2650  {
2651  depSize *= 2;
2652  deps = (DumpId *) pg_realloc(deps, sizeof(DumpId) * depSize);
2653  }
2654  sscanf(tmp, "%d", &deps[depIdx]);
2655  free(tmp);
2656  depIdx++;
2657  }
2658 
2659  if (depIdx > 0) /* We have a non-null entry */
2660  {
2661  deps = (DumpId *) pg_realloc(deps, sizeof(DumpId) * depIdx);
2662  te->dependencies = deps;
2663  te->nDeps = depIdx;
2664  }
2665  else
2666  {
2667  free(deps);
2668  te->dependencies = NULL;
2669  te->nDeps = 0;
2670  }
2671  }
2672  else
2673  {
2674  te->dependencies = NULL;
2675  te->nDeps = 0;
2676  }
2677  te->dataLength = 0;
2678 
2679  if (AH->ReadExtraTocPtr)
2680  AH->ReadExtraTocPtr(AH, te);
2681 
2682  pg_log_debug("read TOC entry %d (ID %d) for %s %s",
2683  i, te->dumpId, te->desc, te->tag);
2684 
2685  /* link completed entry into TOC circular list */
2686  te->prev = AH->toc->prev;
2687  AH->toc->prev->next = te;
2688  AH->toc->prev = te;
2689  te->next = AH->toc;
2690 
2691  /* special processing immediately upon read for some items */
2692  if (strcmp(te->desc, "ENCODING") == 0)
2693  processEncodingEntry(AH, te);
2694  else if (strcmp(te->desc, "STDSTRINGS") == 0)
2695  processStdStringsEntry(AH, te);
2696  else if (strcmp(te->desc, "SEARCHPATH") == 0)
2697  processSearchPathEntry(AH, te);
2698  }
2699 }
struct _tocEntry * next
static void processEncodingEntry(ArchiveHandle *AH, TocEntry *te)
int DumpId
Definition: pg_backup.h:242
Oid tableoid
Definition: pg_backup.h:238
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
CatalogId catalogId
#define K_VERS_1_9
int ReadInt(ArchiveHandle *AH)
static void processStdStringsEntry(ArchiveHandle *AH, TocEntry *te)
teSection section
#define K_VERS_1_5
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
#define pg_log_debug(...)
Definition: logging.h:92
struct _tocEntry * toc
DumpId * dependencies
#define K_VERS_1_8
static void processSearchPathEntry(ArchiveHandle *AH, TocEntry *te)
#define K_VERS_1_10
pgoff_t dataLength
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
#define K_VERS_1_3
struct _tocEntry * prev
#define InvalidOid
Definition: postgres_ext.h:36
#define free(a)
Definition: header.h:65
char * ReadStr(ArchiveHandle *AH)
#define fatal(...)
#define K_VERS_1_14
#define K_VERS_1_6
int i
#define K_VERS_1_11
#define pg_log_warning(...)
Definition: pgfnames.c:24
ReadExtraTocPtrType ReadExtraTocPtr

◆ ReconnectToServer()

void ReconnectToServer ( ArchiveHandle AH,
const char *  dbname 
)

Definition at line 78 of file pg_backup_db.c.

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

Referenced by _reconnectToDB().

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

◆ StartRestoreBlob()

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

Definition at line 1293 of file pg_backup_archiver.c.

References ahprintf(), _archiveHandle::blobCount, _archiveHandle::connection, DropBlobIfExists(), fatal, INV_WRITE, K_VERS_1_12, _archiveHandle::lo_buf_used, lo_create(), lo_open(), _archiveHandle::loFd, pg_log_info, PQerrorMessage(), _archiveHandle::version, and _archiveHandle::writingBlob.

Referenced by _LoadBlobs().

1294 {
1295  bool old_blob_style = (AH->version < K_VERS_1_12);
1296  Oid loOid;
1297 
1298  AH->blobCount++;
1299 
1300  /* Initialize the LO Buffer */
1301  AH->lo_buf_used = 0;
1302 
1303  pg_log_info("restoring large object with OID %u", oid);
1304 
1305  /* With an old archive we must do drop and create logic here */
1306  if (old_blob_style && drop)
1307  DropBlobIfExists(AH, oid);
1308 
1309  if (AH->connection)
1310  {
1311  if (old_blob_style)
1312  {
1313  loOid = lo_create(AH->connection, oid);
1314  if (loOid == 0 || loOid != oid)
1315  fatal("could not create large object %u: %s",
1316  oid, PQerrorMessage(AH->connection));
1317  }
1318  AH->loFd = lo_open(AH->connection, oid, INV_WRITE);
1319  if (AH->loFd == -1)
1320  fatal("could not open large object %u: %s",
1321  oid, PQerrorMessage(AH->connection));
1322  }
1323  else
1324  {
1325  if (old_blob_style)
1326  ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n",
1327  oid, INV_WRITE);
1328  else
1329  ahprintf(AH, "SELECT pg_catalog.lo_open('%u', %d);\n",
1330  oid, INV_WRITE);
1331  }
1332 
1333  AH->writingBlob = 1;
1334 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6666
void DropBlobIfExists(ArchiveHandle *AH, Oid oid)
Definition: pg_backup_db.c:551
unsigned int Oid
Definition: postgres_ext.h:31
#define K_VERS_1_12
int lo_open(PGconn *conn, Oid lobjId, int mode)
Definition: fe-lobj.c:57
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
Oid lo_create(PGconn *conn, Oid lobjId)
Definition: fe-lobj.c:504
#define fatal(...)
#define INV_WRITE
Definition: libpq-fs.h:21
#define pg_log_info(...)
Definition: logging.h:88

◆ StartRestoreBlobs()

void StartRestoreBlobs ( ArchiveHandle AH)

Definition at line 1251 of file pg_backup_archiver.c.

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

Referenced by _LoadBlobs().

1252 {
1253  RestoreOptions *ropt = AH->public.ropt;
1254 
1255  if (!ropt->single_txn)
1256  {
1257  if (AH->connection)
1258  StartTransaction(&AH->public);
1259  else
1260  ahprintf(AH, "BEGIN;\n\n");
1261  }
1262 
1263  AH->blobCount = 0;
1264 }
RestoreOptions * ropt
Definition: pg_backup.h:191
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
static void StartTransaction(void)
Definition: xact.c:1914

◆ TocIDRequired()

teReqs TocIDRequired ( ArchiveHandle AH,
DumpId  id 
)

Definition at line 1871 of file pg_backup_archiver.c.

References getTocEntryByDumpId(), and _tocEntry::reqs.

Referenced by _tarPositionTo(), _tocEntryRequired(), and findDumpableDependencies().

1872 {
1873  TocEntry *te = getTocEntryByDumpId(AH, id);
1874 
1875  if (!te)
1876  return 0;
1877 
1878  return te->reqs;
1879 }
TocEntry * getTocEntryByDumpId(ArchiveHandle *AH, DumpId id)

◆ 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 2373 of file pg_backup_archiver.c.

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, TocEntrySizeCompare(), WaitForWorkers(), WFW_ALL_IDLE, and WriteDataChunksForTocEntry().

Referenced by _CloseArchive().

2374 {
2375  TocEntry *te;
2376 
2377  if (pstate && pstate->numWorkers > 1)
2378  {
2379  /*
2380  * In parallel mode, this code runs in the leader process. We
2381  * construct an array of candidate TEs, then sort it into decreasing
2382  * size order, then dispatch each TE to a data-transfer worker. By
2383  * dumping larger tables first, we avoid getting into a situation
2384  * where we're down to one job and it's big, losing parallelism.
2385  */
2386  TocEntry **tes;
2387  int ntes;
2388 
2389  tes = (TocEntry **) pg_malloc(AH->tocCount * sizeof(TocEntry *));
2390  ntes = 0;
2391  for (te = AH->toc->next; te != AH->toc; te = te->next)
2392  {
2393  /* Consider only TEs with dataDumper functions ... */
2394  if (!te->dataDumper)
2395  continue;
2396  /* ... and ignore ones not enabled for dump */
2397  if ((te->reqs & REQ_DATA) == 0)
2398  continue;
2399 
2400  tes[ntes++] = te;
2401  }
2402 
2403  if (ntes > 1)
2404  qsort((void *) tes, ntes, sizeof(TocEntry *),
2406 
2407  for (int i = 0; i < ntes; i++)
2408  DispatchJobForTocEntry(AH, pstate, tes[i], ACT_DUMP,
2409  mark_dump_job_done, NULL);
2410 
2411  pg_free(tes);
2412 
2413  /* Now wait for workers to finish. */
2414  WaitForWorkers(AH, pstate, WFW_ALL_IDLE);
2415  }
2416  else
2417  {
2418  /* Non-parallel mode: just dump all candidate TEs sequentially. */
2419  for (te = AH->toc->next; te != AH->toc; te = te->next)
2420  {
2421  /* Must have same filter conditions as above */
2422  if (!te->dataDumper)
2423  continue;
2424  if ((te->reqs & REQ_DATA) == 0)
2425  continue;
2426 
2428  }
2429  }
2430 }
struct _tocEntry * next
static int TocEntrySizeCompare(const void *p1, const void *p2)
void DispatchJobForTocEntry(ArchiveHandle *AH, ParallelState *pstate, TocEntry *te, T_Action act, ParallelCompletionPtr callback, void *callback_data)
Definition: parallel.c:1224
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
DataDumperPtr dataDumper
struct _tocEntry * toc
void WaitForWorkers(ArchiveHandle *AH, ParallelState *pstate, WFW_WaitOption mode)
Definition: parallel.c:1470
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te)
int i
#define qsort(a, b, c, d)
Definition: port.h:475
static void mark_dump_job_done(ArchiveHandle *AH, TocEntry *te, int status, void *callback_data)
int numWorkers
Definition: parallel.h:42

◆ WriteDataChunksForTocEntry()

void WriteDataChunksForTocEntry ( ArchiveHandle AH,
TocEntry te 
)

Definition at line 2455 of file pg_backup_archiver.c.

References _archiveHandle::currToc, _tocEntry::dataDumper, _tocEntry::dataDumperArg, _tocEntry::desc, _archiveHandle::EndBlobsPtr, _archiveHandle::EndDataPtr, _archiveHandle::StartBlobsPtr, and _archiveHandle::StartDataPtr.

Referenced by _WorkerJobDumpDirectory(), and WriteDataChunks().

2456 {
2457  StartDataPtrType startPtr;
2458  EndDataPtrType endPtr;
2459 
2460  AH->currToc = te;
2461 
2462  if (strcmp(te->desc, "BLOBS") == 0)
2463  {
2464  startPtr = AH->StartBlobsPtr;
2465  endPtr = AH->EndBlobsPtr;
2466  }
2467  else
2468  {
2469  startPtr = AH->StartDataPtr;
2470  endPtr = AH->EndDataPtr;
2471  }
2472 
2473  if (startPtr != NULL)
2474  (*startPtr) (AH, te);
2475 
2476  /*
2477  * The user-provided DataDumper routine needs to call AH->WriteData
2478  */
2479  te->dataDumper((Archive *) AH, te->dataDumperArg);
2480 
2481  if (endPtr != NULL)
2482  (*endPtr) (AH, te);
2483 
2484  AH->currToc = NULL;
2485 }
void * dataDumperArg
EndDataPtrType EndDataPtr
DataDumperPtr dataDumper
StartBlobsPtrType StartBlobsPtr
struct _tocEntry * currToc
StartDataPtrType StartDataPtr
void(* StartDataPtrType)(ArchiveHandle *AH, TocEntry *te)
void(* EndDataPtrType)(ArchiveHandle *AH, TocEntry *te)
EndBlobsPtrType EndBlobsPtr

◆ WriteHead()

void WriteHead ( ArchiveHandle AH)

Definition at line 3732 of file pg_backup_archiver.c.

References ARCHIVE_MAJOR, ARCHIVE_MINOR, ARCHIVE_REV, _archiveHandle::compression, _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().

3733 {
3734  struct tm crtm;
3735 
3736  AH->WriteBufPtr(AH, "PGDMP", 5); /* Magic code */
3737  AH->WriteBytePtr(AH, ARCHIVE_MAJOR(AH->version));
3738  AH->WriteBytePtr(AH, ARCHIVE_MINOR(AH->version));
3739  AH->WriteBytePtr(AH, ARCHIVE_REV(AH->version));
3740  AH->WriteBytePtr(AH, AH->intSize);
3741  AH->WriteBytePtr(AH, AH->offSize);
3742  AH->WriteBytePtr(AH, AH->format);
3743  WriteInt(AH, AH->compression);
3744  crtm = *localtime(&AH->createDate);
3745  WriteInt(AH, crtm.tm_sec);
3746  WriteInt(AH, crtm.tm_min);
3747  WriteInt(AH, crtm.tm_hour);
3748  WriteInt(AH, crtm.tm_mday);
3749  WriteInt(AH, crtm.tm_mon);
3750  WriteInt(AH, crtm.tm_year);
3751  WriteInt(AH, crtm.tm_isdst);
3752  WriteStr(AH, PQdb(AH->connection));
3753  WriteStr(AH, AH->public.remoteVersionStr);
3754  WriteStr(AH, PG_VERSION);
3755 }
WriteBufPtrType WriteBufPtr
static struct pg_tm tm
Definition: localtime.c:102
#define ARCHIVE_REV(version)
WriteBytePtrType WriteBytePtr
#define ARCHIVE_MINOR(version)
ArchiveFormat format
#define ARCHIVE_MAJOR(version)
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6516
size_t WriteInt(ArchiveHandle *AH, int i)
char * remoteVersionStr
Definition: pg_backup.h:194
size_t WriteStr(ArchiveHandle *AH, const char *c)

◆ WriteInt()

size_t WriteInt ( ArchiveHandle AH,
int  i 
)

Definition at line 1963 of file pg_backup_archiver.c.

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

Referenced by _CustomWriteFunc(), _EndBlob(), _EndBlobs(), _EndData(), _StartBlob(), _StartBlobs(), _StartData(), WriteHead(), WriteStr(), and WriteToc().

1964 {
1965  int b;
1966 
1967  /*
1968  * This is a bit yucky, but I don't want to make the binary format very
1969  * dependent on representation, and not knowing much about it, I write out
1970  * a sign byte. If you change this, don't forget to change the file
1971  * version #, and modify ReadInt to read the new format AS WELL AS the old
1972  * formats.
1973  */
1974 
1975  /* SIGN byte */
1976  if (i < 0)
1977  {
1978  AH->WriteBytePtr(AH, 1);
1979  i = -i;
1980  }
1981  else
1982  AH->WriteBytePtr(AH, 0);
1983 
1984  for (b = 0; b < AH->intSize; b++)
1985  {
1986  AH->WriteBytePtr(AH, i & 0xFF);
1987  i >>= 8;
1988  }
1989 
1990  return AH->intSize + 1;
1991 }
WriteBytePtrType WriteBytePtr
int i

◆ WriteOffset()

size_t WriteOffset ( ArchiveHandle ,
pgoff_t  ,
int   
)

Definition at line 1882 of file pg_backup_archiver.c.

References pgoff_t, and _archiveHandle::WriteBytePtr.

Referenced by _WriteExtraToc().

1883 {
1884  int off;
1885 
1886  /* Save the flag */
1887  AH->WriteBytePtr(AH, wasSet);
1888 
1889  /* Write out pgoff_t smallest byte first, prevents endian mismatch */
1890  for (off = 0; off < sizeof(pgoff_t); off++)
1891  {
1892  AH->WriteBytePtr(AH, o & 0xFF);
1893  o >>= 8;
1894  }
1895  return sizeof(pgoff_t) + 1;
1896 }
#define pgoff_t
Definition: win32_port.h:194

◆ WriteStr()

size_t WriteStr ( ArchiveHandle AH,
const char *  s 
)

Definition at line 2021 of file pg_backup_archiver.c.

References _archiveHandle::WriteBufPtr, and WriteInt().

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

2022 {
2023  size_t res;
2024 
2025  if (c)
2026  {
2027  int len = strlen(c);
2028 
2029  res = WriteInt(AH, len);
2030  AH->WriteBufPtr(AH, c, len);
2031  res += len;
2032  }
2033  else
2034  res = WriteInt(AH, -1);
2035 
2036  return res;
2037 }
WriteBufPtrType WriteBufPtr
char * c
size_t WriteInt(ArchiveHandle *AH, int i)

◆ WriteToc()

void WriteToc ( ArchiveHandle AH)

Definition at line 2488 of file pg_backup_archiver.c.

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, 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().

2489 {
2490  TocEntry *te;
2491  char workbuf[32];
2492  int tocCount;
2493  int i;
2494 
2495  /* count entries that will actually be dumped */
2496  tocCount = 0;
2497  for (te = AH->toc->next; te != AH->toc; te = te->next)
2498  {
2499  if ((te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_SPECIAL)) != 0)
2500  tocCount++;
2501  }
2502 
2503  /* printf("%d TOC Entries to save\n", tocCount); */
2504 
2505  WriteInt(AH, tocCount);
2506 
2507  for (te = AH->toc->next; te != AH->toc; te = te->next)
2508  {
2509  if ((te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_SPECIAL)) == 0)
2510  continue;
2511 
2512  WriteInt(AH, te->dumpId);
2513  WriteInt(AH, te->dataDumper ? 1 : 0);
2514 
2515  /* OID is recorded as a string for historical reasons */
2516  sprintf(workbuf, "%u", te->catalogId.tableoid);
2517  WriteStr(AH, workbuf);
2518  sprintf(workbuf, "%u", te->catalogId.oid);
2519  WriteStr(AH, workbuf);
2520 
2521  WriteStr(AH, te->tag);
2522  WriteStr(AH, te->desc);
2523  WriteInt(AH, te->section);
2524  WriteStr(AH, te->defn);
2525  WriteStr(AH, te->dropStmt);
2526  WriteStr(AH, te->copyStmt);
2527  WriteStr(AH, te->namespace);
2528  WriteStr(AH, te->tablespace);
2529  WriteStr(AH, te->tableam);
2530  WriteStr(AH, te->owner);
2531  WriteStr(AH, "false");
2532 
2533  /* Dump list of dependencies */
2534  for (i = 0; i < te->nDeps; i++)
2535  {
2536  sprintf(workbuf, "%d", te->dependencies[i]);
2537  WriteStr(AH, workbuf);
2538  }
2539  WriteStr(AH, NULL); /* Terminate List */
2540 
2541  if (AH->WriteExtraTocPtr)
2542  AH->WriteExtraTocPtr(AH, te);
2543  }
2544 }
struct _tocEntry * next
Oid tableoid
Definition: pg_backup.h:238
CatalogId catalogId
DataDumperPtr dataDumper
teSection section
#define sprintf
Definition: port.h:195
struct _tocEntry * toc
DumpId * dependencies
size_t WriteInt(ArchiveHandle *AH, int i)
int i
size_t WriteStr(ArchiveHandle *AH, const char *c)
WriteExtraTocPtrType WriteExtraTocPtr