]> granicus.if.org Git - postgresql/blob - src/bin/pg_dump/pg_backup_archiver.h
Run pgindent on 9.2 source tree in preparation for first 9.3
[postgresql] / src / bin / pg_dump / pg_backup_archiver.h
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  *              src/bin/pg_dump/pg_backup_archiver.h
21  *
22  *-------------------------------------------------------------------------
23  */
24
25 #ifndef __PG_BACKUP_ARCHIVE__
26 #define __PG_BACKUP_ARCHIVE__
27
28 #include "postgres_fe.h"
29
30 #include <time.h>
31
32 #include "pg_backup.h"
33
34 #include "libpq-fe.h"
35 #include "pqexpbuffer.h"
36
37 #define LOBBUFSIZE 16384
38
39 /*
40  * Note: zlib.h must be included *after* libpq-fe.h, because the latter may
41  * include ssl.h, which has a naming conflict with zlib.h.
42  */
43 #ifdef HAVE_LIBZ
44 #include <zlib.h>
45 #define GZCLOSE(fh) gzclose(fh)
46 #define GZWRITE(p, s, n, fh) gzwrite(fh, p, (n) * (s))
47 #define GZREAD(p, s, n, fh) gzread(fh, p, (n) * (s))
48 #else
49 #define GZCLOSE(fh) fclose(fh)
50 #define GZWRITE(p, s, n, fh) (fwrite(p, s, n, fh) * (s))
51 #define GZREAD(p, s, n, fh) fread(p, s, n, fh)
52 /* this is just the redefinition of a libz constant */
53 #define Z_DEFAULT_COMPRESSION (-1)
54
55 typedef struct _z_stream
56 {
57         void       *next_in;
58         void       *next_out;
59         size_t          avail_in;
60         size_t          avail_out;
61 } z_stream;
62 typedef z_stream *z_streamp;
63 #endif
64
65 /* Current archive version number (the format we can output) */
66 #define K_VERS_MAJOR 1
67 #define K_VERS_MINOR 12
68 #define K_VERS_REV 0
69
70 /* Data block types */
71 #define BLK_DATA 1
72 #define BLK_BLOBS 3
73
74 /* Historical version numbers (checked in code) */
75 #define K_VERS_1_0 (( (1 * 256 + 0) * 256 + 0) * 256 + 0)
76 #define K_VERS_1_2 (( (1 * 256 + 2) * 256 + 0) * 256 + 0)               /* Allow No ZLIB */
77 #define K_VERS_1_3 (( (1 * 256 + 3) * 256 + 0) * 256 + 0)               /* BLOBs */
78 #define K_VERS_1_4 (( (1 * 256 + 4) * 256 + 0) * 256 + 0)               /* Date & name in header */
79 #define K_VERS_1_5 (( (1 * 256 + 5) * 256 + 0) * 256 + 0)               /* Handle dependencies */
80 #define K_VERS_1_6 (( (1 * 256 + 6) * 256 + 0) * 256 + 0)               /* Schema field in TOCs */
81 #define K_VERS_1_7 (( (1 * 256 + 7) * 256 + 0) * 256 + 0)               /* File Offset size in
82                                                                                                                                  * header */
83 #define K_VERS_1_8 (( (1 * 256 + 8) * 256 + 0) * 256 + 0)               /* change interpretation
84                                                                                                                                  * of ID numbers and
85                                                                                                                                  * dependencies */
86 #define K_VERS_1_9 (( (1 * 256 + 9) * 256 + 0) * 256 + 0)               /* add default_with_oids
87                                                                                                                                  * tracking */
88 #define K_VERS_1_10 (( (1 * 256 + 10) * 256 + 0) * 256 + 0)             /* add tablespace */
89 #define K_VERS_1_11 (( (1 * 256 + 11) * 256 + 0) * 256 + 0)             /* add toc section
90                                                                                                                                  * indicator */
91 #define K_VERS_1_12 (( (1 * 256 + 12) * 256 + 0) * 256 + 0)             /* add separate BLOB
92                                                                                                                                  * entries */
93
94 /* Newest format we can read */
95 #define K_VERS_MAX (( (1 * 256 + 12) * 256 + 255) * 256 + 0)
96
97
98 /* Flags to indicate disposition of offsets stored in files */
99 #define K_OFFSET_POS_NOT_SET 1
100 #define K_OFFSET_POS_SET 2
101 #define K_OFFSET_NO_DATA 3
102
103 struct _archiveHandle;
104 struct _tocEntry;
105
106 typedef void (*ClosePtr) (struct _archiveHandle * AH);
107 typedef void (*ReopenPtr) (struct _archiveHandle * AH);
108 typedef void (*ArchiveEntryPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
109
110 typedef void (*StartDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
111 typedef size_t (*WriteDataPtr) (struct _archiveHandle * AH, const void *data, size_t dLen);
112 typedef void (*EndDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
113
114 typedef void (*StartBlobsPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
115 typedef void (*StartBlobPtr) (struct _archiveHandle * AH, struct _tocEntry * te, Oid oid);
116 typedef void (*EndBlobPtr) (struct _archiveHandle * AH, struct _tocEntry * te, Oid oid);
117 typedef void (*EndBlobsPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
118
119 typedef int (*WriteBytePtr) (struct _archiveHandle * AH, const int i);
120 typedef int (*ReadBytePtr) (struct _archiveHandle * AH);
121 typedef size_t (*WriteBufPtr) (struct _archiveHandle * AH, const void *c, size_t len);
122 typedef size_t (*ReadBufPtr) (struct _archiveHandle * AH, void *buf, size_t len);
123 typedef void (*SaveArchivePtr) (struct _archiveHandle * AH);
124 typedef void (*WriteExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
125 typedef void (*ReadExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
126 typedef void (*PrintExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
127 typedef void (*PrintTocDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te, RestoreOptions *ropt);
128
129 typedef void (*ClonePtr) (struct _archiveHandle * AH);
130 typedef void (*DeClonePtr) (struct _archiveHandle * AH);
131
132 typedef size_t (*CustomOutPtr) (struct _archiveHandle * AH, const void *buf, size_t len);
133
134 typedef enum
135 {
136         SQL_SCAN = 0,                           /* normal */
137         SQL_IN_SINGLE_QUOTE,            /* '...' literal */
138         SQL_IN_DOUBLE_QUOTE                     /* "..." identifier */
139 } sqlparseState;
140
141 typedef struct
142 {
143         sqlparseState state;            /* see above */
144         bool            backSlash;              /* next char is backslash quoted? */
145         PQExpBuffer curCmd;                     /* incomplete line (NULL if not created) */
146 } sqlparseInfo;
147
148 typedef enum
149 {
150         STAGE_NONE = 0,
151         STAGE_INITIALIZING,
152         STAGE_PROCESSING,
153         STAGE_FINALIZING
154 } ArchiverStage;
155
156 typedef enum
157 {
158         OUTPUT_SQLCMDS = 0,                     /* emitting general SQL commands */
159         OUTPUT_COPYDATA,                        /* writing COPY data */
160         OUTPUT_OTHERDATA                        /* writing data as INSERT commands */
161 } ArchiverOutput;
162
163 typedef enum
164 {
165         REQ_SCHEMA = 0x01,                      /* want schema */
166         REQ_DATA = 0x02,                        /* want data */
167         REQ_SPECIAL = 0x04                      /* for special TOC entries */
168 } teReqs;
169
170 typedef struct _archiveHandle
171 {
172         Archive         public;                 /* Public part of archive */
173         char            vmaj;                   /* Version of file */
174         char            vmin;
175         char            vrev;
176         int                     version;                /* Conveniently formatted version */
177
178         char       *archiveRemoteVersion;       /* When reading an archive, the
179                                                                                  * version of the dumped DB */
180         char       *archiveDumpVersion;         /* When reading an archive, the
181                                                                                  * version of the dumper */
182
183         int                     debugLevel;             /* Used for logging (currently only by
184                                                                  * --verbose) */
185         size_t          intSize;                /* Size of an integer in the archive */
186         size_t          offSize;                /* Size of a file offset in the archive -
187                                                                  * Added V1.7 */
188         ArchiveFormat format;           /* Archive format */
189
190         sqlparseInfo sqlparse;          /* state for parsing INSERT data */
191
192         time_t          createDate;             /* Date archive created */
193
194         /*
195          * Fields used when discovering header. A format can always get the
196          * previous read bytes from here...
197          */
198         int                     readHeader;             /* Used if file header has been read already */
199         char       *lookahead;          /* Buffer used when reading header to discover
200                                                                  * format */
201         size_t          lookaheadSize;  /* Size of allocated buffer */
202         size_t          lookaheadLen;   /* Length of data in lookahead */
203         pgoff_t         lookaheadPos;   /* Current read position in lookahead buffer */
204
205         ArchiveEntryPtr ArchiveEntryPtr;        /* Called for each metadata object */
206         StartDataPtr StartDataPtr;      /* Called when table data is about to be
207                                                                  * dumped */
208         WriteDataPtr WriteDataPtr;      /* Called to send some table data to the
209                                                                  * archive */
210         EndDataPtr EndDataPtr;          /* Called when table data dump is finished */
211         WriteBytePtr WriteBytePtr;      /* Write a byte to output */
212         ReadBytePtr ReadBytePtr;        /* Read a byte from an archive */
213         WriteBufPtr WriteBufPtr;        /* Write a buffer of output to the archive */
214         ReadBufPtr ReadBufPtr;          /* Read a buffer of input from the archive */
215         ClosePtr ClosePtr;                      /* Close the archive */
216         ReopenPtr ReopenPtr;            /* Reopen the archive */
217         WriteExtraTocPtr WriteExtraTocPtr;      /* Write extra TOC entry data
218                                                                                  * associated with the current archive
219                                                                                  * format */
220         ReadExtraTocPtr ReadExtraTocPtr;        /* Read extr info associated with
221                                                                                  * archie format */
222         PrintExtraTocPtr PrintExtraTocPtr;      /* Extra TOC info for format */
223         PrintTocDataPtr PrintTocDataPtr;
224
225         StartBlobsPtr StartBlobsPtr;
226         EndBlobsPtr EndBlobsPtr;
227         StartBlobPtr StartBlobPtr;
228         EndBlobPtr EndBlobPtr;
229
230         ClonePtr ClonePtr;                      /* Clone format-specific fields */
231         DeClonePtr DeClonePtr;          /* Clean up cloned fields */
232
233         CustomOutPtr CustomOutPtr;      /* Alternative script output routine */
234
235         /* Stuff for direct DB connection */
236         char       *archdbname;         /* DB name *read* from archive */
237         enum trivalue promptPassword;
238         char       *savedPassword;      /* password for ropt->username, if known */
239         PGconn     *connection;
240         int                     connectToDB;    /* Flag to indicate if direct DB connection is
241                                                                  * required */
242         ArchiverOutput outputKind;      /* Flag for what we're currently writing */
243         bool            pgCopyIn;               /* Currently in libpq 'COPY IN' mode. */
244
245         int                     loFd;                   /* BLOB fd */
246         int                     writingBlob;    /* Flag */
247         int                     blobCount;              /* # of blobs restored */
248
249         char       *fSpec;                      /* Archive File Spec */
250         FILE       *FH;                         /* General purpose file handle */
251         void       *OF;
252         int                     gzOut;                  /* Output file */
253
254         struct _tocEntry *toc;          /* Header of circular list of TOC entries */
255         int                     tocCount;               /* Number of TOC entries */
256         DumpId          maxDumpId;              /* largest DumpId among all TOC entries */
257
258         /* arrays created after the TOC list is complete: */
259         struct _tocEntry **tocsByDumpId;        /* TOCs indexed by dumpId */
260         DumpId     *tableDataId;        /* TABLE DATA ids, indexed by table dumpId */
261
262         struct _tocEntry *currToc;      /* Used when dumping data */
263         int                     compression;    /* Compression requested on open Possible
264                                                                  * values for compression: -1
265                                                                  * Z_DEFAULT_COMPRESSION 0      COMPRESSION_NONE
266                                                                  * 1-9 levels for gzip compression */
267         ArchiveMode mode;                       /* File mode - r or w */
268         void       *formatData;         /* Header data specific to file format */
269
270         RestoreOptions *ropt;           /* Used to check restore options in ahwrite
271                                                                  * etc */
272
273         /* these vars track state to avoid sending redundant SET commands */
274         char       *currUser;           /* current username, or NULL if unknown */
275         char       *currSchema;         /* current schema, or NULL */
276         char       *currTablespace; /* current tablespace, or NULL */
277         bool            currWithOids;   /* current default_with_oids setting */
278
279         void       *lo_buf;
280         size_t          lo_buf_used;
281         size_t          lo_buf_size;
282
283         int                     noTocComments;
284         ArchiverStage stage;
285         ArchiverStage lastErrorStage;
286         struct _tocEntry *currentTE;
287         struct _tocEntry *lastErrorTE;
288 } ArchiveHandle;
289
290 typedef struct _tocEntry
291 {
292         struct _tocEntry *prev;
293         struct _tocEntry *next;
294         CatalogId       catalogId;
295         DumpId          dumpId;
296         teSection       section;
297         bool            hadDumper;              /* Archiver was passed a dumper routine (used
298                                                                  * in restore) */
299         char       *tag;                        /* index tag */
300         char       *namespace;          /* null or empty string if not in a schema */
301         char       *tablespace;         /* null if not in a tablespace; empty string
302                                                                  * means use database default */
303         char       *owner;
304         bool            withOids;               /* Used only by "TABLE" tags */
305         char       *desc;
306         char       *defn;
307         char       *dropStmt;
308         char       *copyStmt;
309         DumpId     *dependencies;       /* dumpIds of objects this one depends on */
310         int                     nDeps;                  /* number of dependencies */
311
312         DataDumperPtr dataDumper;       /* Routine to dump data for object */
313         void       *dataDumperArg;      /* Arg for above routine */
314         void       *formatData;         /* TOC Entry data specific to file format */
315
316         /* working state while dumping/restoring */
317         teReqs          reqs;                   /* do we need schema and/or data of object */
318         bool            created;                /* set for DATA member if TABLE was created */
319
320         /* working state (needed only for parallel restore) */
321         struct _tocEntry *par_prev; /* list links for pending/ready items; */
322         struct _tocEntry *par_next; /* these are NULL if not in either list */
323         int                     depCount;               /* number of dependencies not yet restored */
324         DumpId     *revDeps;            /* dumpIds of objects depending on this one */
325         int                     nRevDeps;               /* number of such dependencies */
326         DumpId     *lockDeps;           /* dumpIds of objects this one needs lock on */
327         int                     nLockDeps;              /* number of such dependencies */
328 } TocEntry;
329
330 extern void on_exit_close_archive(Archive *AHX);
331
332 extern void warn_or_exit_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
333
334 extern void WriteTOC(ArchiveHandle *AH);
335 extern void ReadTOC(ArchiveHandle *AH);
336 extern void WriteHead(ArchiveHandle *AH);
337 extern void ReadHead(ArchiveHandle *AH);
338 extern void WriteToc(ArchiveHandle *AH);
339 extern void ReadToc(ArchiveHandle *AH);
340 extern void WriteDataChunks(ArchiveHandle *AH);
341
342 extern teReqs TocIDRequired(ArchiveHandle *AH, DumpId id);
343 extern bool checkSeek(FILE *fp);
344
345 #define appendStringLiteralAHX(buf,str,AH) \
346         appendStringLiteral(buf, str, (AH)->public.encoding, (AH)->public.std_strings)
347
348 #define appendByteaLiteralAHX(buf,str,len,AH) \
349         appendByteaLiteral(buf, str, len, (AH)->public.std_strings)
350
351 /*
352  * Mandatory routines for each supported format
353  */
354
355 extern size_t WriteInt(ArchiveHandle *AH, int i);
356 extern int      ReadInt(ArchiveHandle *AH);
357 extern char *ReadStr(ArchiveHandle *AH);
358 extern size_t WriteStr(ArchiveHandle *AH, const char *s);
359
360 int                     ReadOffset(ArchiveHandle *, pgoff_t *);
361 size_t          WriteOffset(ArchiveHandle *, pgoff_t, int);
362
363 extern void StartRestoreBlobs(ArchiveHandle *AH);
364 extern void StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop);
365 extern void EndRestoreBlob(ArchiveHandle *AH, Oid oid);
366 extern void EndRestoreBlobs(ArchiveHandle *AH);
367
368 extern void InitArchiveFmt_Custom(ArchiveHandle *AH);
369 extern void InitArchiveFmt_Null(ArchiveHandle *AH);
370 extern void InitArchiveFmt_Directory(ArchiveHandle *AH);
371 extern void InitArchiveFmt_Tar(ArchiveHandle *AH);
372
373 extern bool isValidTarHeader(char *header);
374
375 extern int      ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *newUser);
376 extern void DropBlobIfExists(ArchiveHandle *AH, Oid oid);
377
378 int                     ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH);
379 int                     ahprintf(ArchiveHandle *AH, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
380
381 void            ahlog(ArchiveHandle *AH, int level, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
382
383 #endif