PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/postgresql-9.0.0/src/bin/pg_dump/pg_backup_archiver.h

https://github.com/srush/postgres-provanence
C Header | 384 lines | 256 code | 64 blank | 64 comment | 0 complexity | 3f17cdd41003d404e475de8823b6f4f2 MD5 | raw file
  1. /*-------------------------------------------------------------------------
  2. *
  3. * pg_backup_archiver.h
  4. *
  5. * Private interface to the pg_dump archiver routines.
  6. * It is NOT intended that these routines be called by any
  7. * dumper directly.
  8. *
  9. * See the headers to pg_restore for more details.
  10. *
  11. * Copyright (c) 2000, Philip Warner
  12. * Rights are granted to use this software in any way so long
  13. * as this notice is not removed.
  14. *
  15. * The author is not responsible for loss or damages that may
  16. * result from it's use.
  17. *
  18. *
  19. * IDENTIFICATION
  20. * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.85 2010/02/26 02:01:16 momjian Exp $
  21. *
  22. *-------------------------------------------------------------------------
  23. */
  24. #ifndef __PG_BACKUP_ARCHIVE__
  25. #define __PG_BACKUP_ARCHIVE__
  26. #include "postgres_fe.h"
  27. #include <time.h>
  28. #include "pg_backup.h"
  29. #include "libpq-fe.h"
  30. #include "pqexpbuffer.h"
  31. #define LOBBUFSIZE 16384
  32. /*
  33. * Note: zlib.h must be included *after* libpq-fe.h, because the latter may
  34. * include ssl.h, which has a naming conflict with zlib.h.
  35. */
  36. #ifdef HAVE_LIBZ
  37. #include <zlib.h>
  38. #define GZCLOSE(fh) gzclose(fh)
  39. #define GZWRITE(p, s, n, fh) gzwrite(fh, p, (n) * (s))
  40. #define GZREAD(p, s, n, fh) gzread(fh, p, (n) * (s))
  41. #else
  42. #define GZCLOSE(fh) fclose(fh)
  43. #define GZWRITE(p, s, n, fh) (fwrite(p, s, n, fh) * (s))
  44. #define GZREAD(p, s, n, fh) fread(p, s, n, fh)
  45. #define Z_DEFAULT_COMPRESSION (-1)
  46. typedef struct _z_stream
  47. {
  48. void *next_in;
  49. void *next_out;
  50. size_t avail_in;
  51. size_t avail_out;
  52. } z_stream;
  53. typedef z_stream *z_streamp;
  54. #endif
  55. /* Current archive version number (the format we can output) */
  56. #define K_VERS_MAJOR 1
  57. #define K_VERS_MINOR 12
  58. #define K_VERS_REV 0
  59. /* Data block types */
  60. #define BLK_DATA 1
  61. #define BLK_BLOBS 3
  62. /* Historical version numbers (checked in code) */
  63. #define K_VERS_1_0 (( (1 * 256 + 0) * 256 + 0) * 256 + 0)
  64. #define K_VERS_1_2 (( (1 * 256 + 2) * 256 + 0) * 256 + 0) /* Allow No ZLIB */
  65. #define K_VERS_1_3 (( (1 * 256 + 3) * 256 + 0) * 256 + 0) /* BLOBs */
  66. #define K_VERS_1_4 (( (1 * 256 + 4) * 256 + 0) * 256 + 0) /* Date & name in header */
  67. #define K_VERS_1_5 (( (1 * 256 + 5) * 256 + 0) * 256 + 0) /* Handle dependencies */
  68. #define K_VERS_1_6 (( (1 * 256 + 6) * 256 + 0) * 256 + 0) /* Schema field in TOCs */
  69. #define K_VERS_1_7 (( (1 * 256 + 7) * 256 + 0) * 256 + 0) /* File Offset size in
  70. * header */
  71. #define K_VERS_1_8 (( (1 * 256 + 8) * 256 + 0) * 256 + 0) /* change interpretation
  72. * of ID numbers and
  73. * dependencies */
  74. #define K_VERS_1_9 (( (1 * 256 + 9) * 256 + 0) * 256 + 0) /* add default_with_oids
  75. * tracking */
  76. #define K_VERS_1_10 (( (1 * 256 + 10) * 256 + 0) * 256 + 0) /* add tablespace */
  77. #define K_VERS_1_11 (( (1 * 256 + 11) * 256 + 0) * 256 + 0) /* add toc section
  78. * indicator */
  79. #define K_VERS_1_12 (( (1 * 256 + 12) * 256 + 0) * 256 + 0) /* add separate BLOB
  80. * entries */
  81. /* Newest format we can read */
  82. #define K_VERS_MAX (( (1 * 256 + 12) * 256 + 255) * 256 + 0)
  83. /* Flags to indicate disposition of offsets stored in files */
  84. #define K_OFFSET_POS_NOT_SET 1
  85. #define K_OFFSET_POS_SET 2
  86. #define K_OFFSET_NO_DATA 3
  87. struct _archiveHandle;
  88. struct _tocEntry;
  89. struct _restoreList;
  90. typedef void (*ClosePtr) (struct _archiveHandle * AH);
  91. typedef void (*ReopenPtr) (struct _archiveHandle * AH);
  92. typedef void (*ArchiveEntryPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
  93. typedef void (*StartDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
  94. typedef size_t (*WriteDataPtr) (struct _archiveHandle * AH, const void *data, size_t dLen);
  95. typedef void (*EndDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
  96. typedef void (*StartBlobsPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
  97. typedef void (*StartBlobPtr) (struct _archiveHandle * AH, struct _tocEntry * te, Oid oid);
  98. typedef void (*EndBlobPtr) (struct _archiveHandle * AH, struct _tocEntry * te, Oid oid);
  99. typedef void (*EndBlobsPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
  100. typedef int (*WriteBytePtr) (struct _archiveHandle * AH, const int i);
  101. typedef int (*ReadBytePtr) (struct _archiveHandle * AH);
  102. typedef size_t (*WriteBufPtr) (struct _archiveHandle * AH, const void *c, size_t len);
  103. typedef size_t (*ReadBufPtr) (struct _archiveHandle * AH, void *buf, size_t len);
  104. typedef void (*SaveArchivePtr) (struct _archiveHandle * AH);
  105. typedef void (*WriteExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
  106. typedef void (*ReadExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
  107. typedef void (*PrintExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
  108. typedef void (*PrintTocDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te, RestoreOptions *ropt);
  109. typedef void (*ClonePtr) (struct _archiveHandle * AH);
  110. typedef void (*DeClonePtr) (struct _archiveHandle * AH);
  111. typedef size_t (*CustomOutPtr) (struct _archiveHandle * AH, const void *buf, size_t len);
  112. typedef struct _outputContext
  113. {
  114. void *OF;
  115. int gzOut;
  116. } OutputContext;
  117. typedef enum
  118. {
  119. SQL_SCAN = 0, /* normal */
  120. SQL_IN_SQL_COMMENT, /* -- comment */
  121. SQL_IN_EXT_COMMENT, /* slash-star comment */
  122. SQL_IN_SINGLE_QUOTE, /* '...' literal */
  123. SQL_IN_E_QUOTE, /* E'...' literal */
  124. SQL_IN_DOUBLE_QUOTE, /* "..." identifier */
  125. SQL_IN_DOLLAR_TAG, /* possible dollar-quote starting tag */
  126. SQL_IN_DOLLAR_QUOTE /* body of dollar quote */
  127. } sqlparseState;
  128. typedef struct
  129. {
  130. sqlparseState state; /* see above */
  131. char lastChar; /* preceding char, or '\0' initially */
  132. bool backSlash; /* next char is backslash quoted? */
  133. int braceDepth; /* parenthesis nesting depth */
  134. PQExpBuffer tagBuf; /* dollar quote tag (NULL if not created) */
  135. int minTagEndPos; /* first possible end position of $-quote */
  136. } sqlparseInfo;
  137. typedef enum
  138. {
  139. STAGE_NONE = 0,
  140. STAGE_INITIALIZING,
  141. STAGE_PROCESSING,
  142. STAGE_FINALIZING
  143. } ArchiverStage;
  144. typedef enum
  145. {
  146. REQ_SCHEMA = 1,
  147. REQ_DATA = 2,
  148. REQ_ALL = REQ_SCHEMA + REQ_DATA
  149. } teReqs;
  150. typedef struct _archiveHandle
  151. {
  152. Archive public; /* Public part of archive */
  153. char vmaj; /* Version of file */
  154. char vmin;
  155. char vrev;
  156. int version; /* Conveniently formatted version */
  157. char *archiveRemoteVersion; /* When reading an archive, the
  158. * version of the dumped DB */
  159. char *archiveDumpVersion; /* When reading an archive, the
  160. * version of the dumper */
  161. int debugLevel; /* Used for logging (currently only by
  162. * --verbose) */
  163. size_t intSize; /* Size of an integer in the archive */
  164. size_t offSize; /* Size of a file offset in the archive -
  165. * Added V1.7 */
  166. ArchiveFormat format; /* Archive format */
  167. sqlparseInfo sqlparse;
  168. PQExpBuffer sqlBuf;
  169. time_t createDate; /* Date archive created */
  170. /*
  171. * Fields used when discovering header. A format can always get the
  172. * previous read bytes from here...
  173. */
  174. int readHeader; /* Used if file header has been read already */
  175. char *lookahead; /* Buffer used when reading header to discover
  176. * format */
  177. size_t lookaheadSize; /* Size of allocated buffer */
  178. size_t lookaheadLen; /* Length of data in lookahead */
  179. pgoff_t lookaheadPos; /* Current read position in lookahead buffer */
  180. ArchiveEntryPtr ArchiveEntryPtr; /* Called for each metadata object */
  181. StartDataPtr StartDataPtr; /* Called when table data is about to be
  182. * dumped */
  183. WriteDataPtr WriteDataPtr; /* Called to send some table data to the
  184. * archive */
  185. EndDataPtr EndDataPtr; /* Called when table data dump is finished */
  186. WriteBytePtr WriteBytePtr; /* Write a byte to output */
  187. ReadBytePtr ReadBytePtr; /* Read a byte from an archive */
  188. WriteBufPtr WriteBufPtr; /* Write a buffer of output to the archive */
  189. ReadBufPtr ReadBufPtr; /* Read a buffer of input from the archive */
  190. ClosePtr ClosePtr; /* Close the archive */
  191. ReopenPtr ReopenPtr; /* Reopen the archive */
  192. WriteExtraTocPtr WriteExtraTocPtr; /* Write extra TOC entry data
  193. * associated with the current archive
  194. * format */
  195. ReadExtraTocPtr ReadExtraTocPtr; /* Read extr info associated with
  196. * archie format */
  197. PrintExtraTocPtr PrintExtraTocPtr; /* Extra TOC info for format */
  198. PrintTocDataPtr PrintTocDataPtr;
  199. StartBlobsPtr StartBlobsPtr;
  200. EndBlobsPtr EndBlobsPtr;
  201. StartBlobPtr StartBlobPtr;
  202. EndBlobPtr EndBlobPtr;
  203. ClonePtr ClonePtr; /* Clone format-specific fields */
  204. DeClonePtr DeClonePtr; /* Clean up cloned fields */
  205. CustomOutPtr CustomOutPtr; /* Alternative script output routine */
  206. /* Stuff for direct DB connection */
  207. char *archdbname; /* DB name *read* from archive */
  208. enum trivalue promptPassword;
  209. char *savedPassword; /* password for ropt->username, if known */
  210. PGconn *connection;
  211. int connectToDB; /* Flag to indicate if direct DB connection is
  212. * required */
  213. bool writingCopyData; /* True when we are sending COPY data */
  214. bool pgCopyIn; /* Currently in libpq 'COPY IN' mode. */
  215. PQExpBuffer pgCopyBuf; /* Left-over data from incomplete lines in
  216. * COPY IN */
  217. int loFd; /* BLOB fd */
  218. int writingBlob; /* Flag */
  219. int blobCount; /* # of blobs restored */
  220. char *fSpec; /* Archive File Spec */
  221. FILE *FH; /* General purpose file handle */
  222. void *OF;
  223. int gzOut; /* Output file */
  224. struct _tocEntry *toc; /* List of TOC entries */
  225. int tocCount; /* Number of TOC entries */
  226. DumpId maxDumpId; /* largest DumpId among all TOC entries */
  227. struct _tocEntry *currToc; /* Used when dumping data */
  228. int compression; /* Compression requested on open */
  229. ArchiveMode mode; /* File mode - r or w */
  230. void *formatData; /* Header data specific to file format */
  231. RestoreOptions *ropt; /* Used to check restore options in ahwrite
  232. * etc */
  233. /* these vars track state to avoid sending redundant SET commands */
  234. char *currUser; /* current username, or NULL if unknown */
  235. char *currSchema; /* current schema, or NULL */
  236. char *currTablespace; /* current tablespace, or NULL */
  237. bool currWithOids; /* current default_with_oids setting */
  238. void *lo_buf;
  239. size_t lo_buf_used;
  240. size_t lo_buf_size;
  241. int noTocComments;
  242. ArchiverStage stage;
  243. ArchiverStage lastErrorStage;
  244. struct _tocEntry *currentTE;
  245. struct _tocEntry *lastErrorTE;
  246. } ArchiveHandle;
  247. typedef struct _tocEntry
  248. {
  249. struct _tocEntry *prev;
  250. struct _tocEntry *next;
  251. CatalogId catalogId;
  252. DumpId dumpId;
  253. teSection section;
  254. bool hadDumper; /* Archiver was passed a dumper routine (used
  255. * in restore) */
  256. char *tag; /* index tag */
  257. char *namespace; /* null or empty string if not in a schema */
  258. char *tablespace; /* null if not in a tablespace; empty string
  259. * means use database default */
  260. char *owner;
  261. bool withOids; /* Used only by "TABLE" tags */
  262. char *desc;
  263. char *defn;
  264. char *dropStmt;
  265. char *copyStmt;
  266. DumpId *dependencies; /* dumpIds of objects this one depends on */
  267. int nDeps; /* number of dependencies */
  268. DataDumperPtr dataDumper; /* Routine to dump data for object */
  269. void *dataDumperArg; /* Arg for above routine */
  270. void *formatData; /* TOC Entry data specific to file format */
  271. /* working state (needed only for parallel restore) */
  272. struct _tocEntry *par_prev; /* list links for pending/ready items; */
  273. struct _tocEntry *par_next; /* these are NULL if not in either list */
  274. bool created; /* set for DATA member if TABLE was created */
  275. int depCount; /* number of dependencies not yet restored */
  276. DumpId *lockDeps; /* dumpIds of objects this one needs lock on */
  277. int nLockDeps; /* number of such dependencies */
  278. } TocEntry;
  279. /* Used everywhere */
  280. extern const char *progname;
  281. extern void die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(printf, 3, 4)));
  282. extern void warn_or_die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(printf, 3, 4)));
  283. extern void write_msg(const char *modulename, const char *fmt,...) __attribute__((format(printf, 2, 3)));
  284. extern void WriteTOC(ArchiveHandle *AH);
  285. extern void ReadTOC(ArchiveHandle *AH);
  286. extern void WriteHead(ArchiveHandle *AH);
  287. extern void ReadHead(ArchiveHandle *AH);
  288. extern void WriteToc(ArchiveHandle *AH);
  289. extern void ReadToc(ArchiveHandle *AH);
  290. extern void WriteDataChunks(ArchiveHandle *AH);
  291. extern teReqs TocIDRequired(ArchiveHandle *AH, DumpId id, RestoreOptions *ropt);
  292. extern bool checkSeek(FILE *fp);
  293. #define appendStringLiteralAHX(buf,str,AH) \
  294. appendStringLiteral(buf, str, (AH)->public.encoding, (AH)->public.std_strings)
  295. #define appendByteaLiteralAHX(buf,str,len,AH) \
  296. appendByteaLiteral(buf, str, len, (AH)->public.std_strings)
  297. /*
  298. * Mandatory routines for each supported format
  299. */
  300. extern size_t WriteInt(ArchiveHandle *AH, int i);
  301. extern int ReadInt(ArchiveHandle *AH);
  302. extern char *ReadStr(ArchiveHandle *AH);
  303. extern size_t WriteStr(ArchiveHandle *AH, const char *s);
  304. int ReadOffset(ArchiveHandle *, pgoff_t *);
  305. size_t WriteOffset(ArchiveHandle *, pgoff_t, int);
  306. extern void StartRestoreBlobs(ArchiveHandle *AH);
  307. extern void StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop);
  308. extern void EndRestoreBlob(ArchiveHandle *AH, Oid oid);
  309. extern void EndRestoreBlobs(ArchiveHandle *AH);
  310. extern void InitArchiveFmt_Custom(ArchiveHandle *AH);
  311. extern void InitArchiveFmt_Files(ArchiveHandle *AH);
  312. extern void InitArchiveFmt_Null(ArchiveHandle *AH);
  313. extern void InitArchiveFmt_Tar(ArchiveHandle *AH);
  314. extern bool isValidTarHeader(char *header);
  315. extern int ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *newUser);
  316. extern void DropBlobIfExists(ArchiveHandle *AH, Oid oid);
  317. int ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH);
  318. int ahprintf(ArchiveHandle *AH, const char *fmt,...) __attribute__((format(printf, 2, 3)));
  319. void ahlog(ArchiveHandle *AH, int level, const char *fmt,...) __attribute__((format(printf, 3, 4)));
  320. #endif