~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/bin/pg_dump/pg_backup_archiver.h

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.62.4.1 2005-01-25 22:44:47 tgl Exp $
 
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 32768
 
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
#define Z_DEFAULT_COMPRESSION -1
 
53
 
 
54
typedef struct _z_stream
 
55
{
 
56
        void       *next_in;
 
57
        void       *next_out;
 
58
        size_t          avail_in;
 
59
        size_t          avail_out;
 
60
} z_stream;
 
61
typedef z_stream *z_streamp;
 
62
#endif
 
63
 
 
64
#define K_VERS_MAJOR 1
 
65
#define K_VERS_MINOR 10
 
66
#define K_VERS_REV 0
 
67
 
 
68
/* Data block types */
 
69
#define BLK_DATA 1
 
70
#define BLK_BLOB 2
 
71
#define BLK_BLOBS 3
 
72
 
 
73
/* Some important version numbers (checked in code) */
 
74
#define K_VERS_1_0 (( (1 * 256 + 0) * 256 + 0) * 256 + 0)
 
75
#define K_VERS_1_2 (( (1 * 256 + 2) * 256 + 0) * 256 + 0)               /* Allow No ZLIB */
 
76
#define K_VERS_1_3 (( (1 * 256 + 3) * 256 + 0) * 256 + 0)               /* BLOBs */
 
77
#define K_VERS_1_4 (( (1 * 256 + 4) * 256 + 0) * 256 + 0)               /* Date & name in header */
 
78
#define K_VERS_1_5 (( (1 * 256 + 5) * 256 + 0) * 256 + 0)               /* Handle dependencies */
 
79
#define K_VERS_1_6 (( (1 * 256 + 6) * 256 + 0) * 256 + 0)               /* Schema field in TOCs */
 
80
#define K_VERS_1_7 (( (1 * 256 + 7) * 256 + 0) * 256 + 0)               /* File Offset size in
 
81
                                                                                                                                 * header */
 
82
#define K_VERS_1_8 (( (1 * 256 + 8) * 256 + 0) * 256 + 0)               /* change interpretation
 
83
                                                                                                                                 * of ID numbers and
 
84
                                                                                                                                 * dependencies */
 
85
#define K_VERS_1_9 (( (1 * 256 + 9) * 256 + 0) * 256 + 0)               /* add default_with_oids
 
86
                                                                                                                                 * tracking */
 
87
#define K_VERS_1_10 (( (1 * 256 + 10) * 256 + 0) * 256 + 0)             /* add tablespace */
 
88
 
 
89
#define K_VERS_MAX (( (1 * 256 + 10) * 256 + 255) * 256 + 0)
 
90
 
 
91
/* No of BLOBs to restore in 1 TX */
 
92
#define BLOB_BATCH_SIZE 100
 
93
 
 
94
/* Flags to indicate disposition of offsets stored in files */
 
95
#define K_OFFSET_POS_NOT_SET 1
 
96
#define K_OFFSET_POS_SET 2
 
97
#define K_OFFSET_NO_DATA 3
 
98
 
 
99
struct _archiveHandle;
 
100
struct _tocEntry;
 
101
struct _restoreList;
 
102
 
 
103
typedef void (*ClosePtr) (struct _archiveHandle * AH);
 
104
typedef void (*ArchiveEntryPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
 
105
 
 
106
typedef void (*StartDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
 
107
typedef size_t (*WriteDataPtr) (struct _archiveHandle * AH, const void *data, size_t dLen);
 
108
typedef void (*EndDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
 
109
 
 
110
typedef void (*StartBlobsPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
 
111
typedef void (*StartBlobPtr) (struct _archiveHandle * AH, struct _tocEntry * te, Oid oid);
 
112
typedef void (*EndBlobPtr) (struct _archiveHandle * AH, struct _tocEntry * te, Oid oid);
 
113
typedef void (*EndBlobsPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
 
114
 
 
115
typedef int (*WriteBytePtr) (struct _archiveHandle * AH, const int i);
 
116
typedef int (*ReadBytePtr) (struct _archiveHandle * AH);
 
117
typedef size_t (*WriteBufPtr) (struct _archiveHandle * AH, const void *c, size_t len);
 
118
typedef size_t (*ReadBufPtr) (struct _archiveHandle * AH, void *buf, size_t len);
 
119
typedef void (*SaveArchivePtr) (struct _archiveHandle * AH);
 
120
typedef void (*WriteExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
 
121
typedef void (*ReadExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
 
122
typedef void (*PrintExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
 
123
typedef void (*PrintTocDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te, RestoreOptions *ropt);
 
124
 
 
125
typedef size_t (*CustomOutPtr) (struct _archiveHandle * AH, const void *buf, size_t len);
 
126
 
 
127
typedef enum _archiveMode
 
128
{
 
129
        archModeWrite,
 
130
        archModeRead
 
131
} ArchiveMode;
 
132
 
 
133
typedef struct _outputContext
 
134
{
 
135
        void       *OF;
 
136
        int                     gzOut;
 
137
} OutputContext;
 
138
 
 
139
typedef enum
 
140
{
 
141
        SQL_SCAN = 0,
 
142
        SQL_IN_SQL_COMMENT,
 
143
        SQL_IN_EXT_COMMENT,
 
144
        SQL_IN_QUOTE,
 
145
        SQL_IN_DOLLARTAG,
 
146
        SQL_IN_DOLLARQUOTE
 
147
} sqlparseState;
 
148
 
 
149
typedef struct
 
150
{
 
151
        int                     backSlash;
 
152
        sqlparseState state;
 
153
        char            lastChar;
 
154
        char            quoteChar;
 
155
        int                     braceDepth;
 
156
        PQExpBuffer tagBuf;
 
157
} sqlparseInfo;
 
158
 
 
159
typedef enum
 
160
{
 
161
        STAGE_NONE = 0,
 
162
        STAGE_INITIALIZING,
 
163
        STAGE_PROCESSING,
 
164
        STAGE_FINALIZING
 
165
} ArchiverStage;
 
166
 
 
167
typedef enum
 
168
{
 
169
        REQ_SCHEMA = 1,
 
170
        REQ_DATA = 2,
 
171
        REQ_ALL = REQ_SCHEMA + REQ_DATA
 
172
} teReqs;
 
173
 
 
174
typedef struct _archiveHandle
 
175
{
 
176
        Archive         public;                 /* Public part of archive */
 
177
        char            vmaj;                   /* Version of file */
 
178
        char            vmin;
 
179
        char            vrev;
 
180
        int                     version;                /* Conveniently formatted version */
 
181
 
 
182
        char       *archiveRemoteVersion;       /* When reading an archive,
 
183
                                                                                 * the version of the dumped DB */
 
184
        char       *archiveDumpVersion;         /* When reading an archive,
 
185
                                                                                 * the version of the dumper */
 
186
 
 
187
        int                     debugLevel;             /* Used for logging (currently only by
 
188
                                                                 * --verbose) */
 
189
        size_t          intSize;                /* Size of an integer in the archive */
 
190
        size_t          offSize;                /* Size of a file offset in the archive -
 
191
                                                                 * Added V1.7 */
 
192
        ArchiveFormat format;           /* Archive format */
 
193
 
 
194
        sqlparseInfo sqlparse;
 
195
        PQExpBuffer sqlBuf;
 
196
 
 
197
        time_t          createDate;             /* Date archive created */
 
198
 
 
199
        /*
 
200
         * Fields used when discovering header. A format can always get the
 
201
         * previous read bytes from here...
 
202
         */
 
203
        int                     readHeader;             /* Used if file header has been read
 
204
                                                                 * already */
 
205
        char       *lookahead;          /* Buffer used when reading header to
 
206
                                                                 * discover format */
 
207
        size_t          lookaheadSize;  /* Size of allocated buffer */
 
208
        size_t          lookaheadLen;   /* Length of data in lookahead */
 
209
        off_t           lookaheadPos;   /* Current read position in lookahead
 
210
                                                                 * buffer */
 
211
 
 
212
        ArchiveEntryPtr ArchiveEntryPtr;        /* Called for each metadata object */
 
213
        StartDataPtr StartDataPtr;      /* Called when table data is about to be
 
214
                                                                 * dumped */
 
215
        WriteDataPtr WriteDataPtr;      /* Called to send some table data to the
 
216
                                                                 * archive */
 
217
        EndDataPtr EndDataPtr;          /* Called when table data dump is finished */
 
218
        WriteBytePtr WriteBytePtr;      /* Write a byte to output */
 
219
        ReadBytePtr ReadBytePtr;        /* Read a byte from an archive */
 
220
        WriteBufPtr WriteBufPtr;        /* Write a buffer of output to the archive */
 
221
        ReadBufPtr ReadBufPtr;          /* Read a buffer of input from the archive */
 
222
        ClosePtr ClosePtr;                      /* Close the archive */
 
223
        WriteExtraTocPtr WriteExtraTocPtr;      /* Write extra TOC entry data
 
224
                                                                                 * associated with the current
 
225
                                                                                 * archive format */
 
226
        ReadExtraTocPtr ReadExtraTocPtr;        /* Read extr info associated with
 
227
                                                                                 * archie format */
 
228
        PrintExtraTocPtr PrintExtraTocPtr;      /* Extra TOC info for format */
 
229
        PrintTocDataPtr PrintTocDataPtr;
 
230
 
 
231
        StartBlobsPtr StartBlobsPtr;
 
232
        EndBlobsPtr EndBlobsPtr;
 
233
        StartBlobPtr StartBlobPtr;
 
234
        EndBlobPtr EndBlobPtr;
 
235
 
 
236
        CustomOutPtr CustomOutPtr;      /* Alternate script output routine */
 
237
 
 
238
        /* Stuff for direct DB connection */
 
239
        char       *archdbname;         /* DB name *read* from archive */
 
240
        bool            requirePassword;
 
241
        PGconn     *connection;
 
242
        PGconn     *blobConnection; /* Connection for BLOB xref */
 
243
        int                     txActive;               /* Flag set if TX active on connection */
 
244
        int                     blobTxActive;   /* Flag set if TX active on blobConnection */
 
245
        int                     connectToDB;    /* Flag to indicate if direct DB
 
246
                                                                 * connection is required */
 
247
        int                     pgCopyIn;               /* Currently in libpq 'COPY IN' mode. */
 
248
        PQExpBuffer pgCopyBuf;          /* Left-over data from incomplete lines in
 
249
                                                                 * COPY IN */
 
250
 
 
251
        int                     loFd;                   /* BLOB fd */
 
252
        int                     writingBlob;    /* Flag */
 
253
        int                     createdBlobXref;        /* Flag */
 
254
        int                     blobCount;              /* # of blobs restored */
 
255
 
 
256
        char       *fSpec;                      /* Archive File Spec */
 
257
        FILE       *FH;                         /* General purpose file handle */
 
258
        void       *OF;
 
259
        int                     gzOut;                  /* Output file */
 
260
 
 
261
        struct _tocEntry *toc;          /* List of TOC entries */
 
262
        int                     tocCount;               /* Number of TOC entries */
 
263
        DumpId          maxDumpId;              /* largest DumpId among all TOC entries */
 
264
 
 
265
        struct _tocEntry *currToc;      /* Used when dumping data */
 
266
        int                     compression;    /* Compression requested on open */
 
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
 
271
                                                                 * ahwrite etc */
 
272
 
 
273
        /* these vars track state to avoid sending redundant SET commands */
 
274
        char       *currUser;           /* current username */
 
275
        char       *currSchema;         /* current schema */
 
276
        char       *currTablespace;     /* current tablespace */
 
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
        bool            hadDumper;              /* Archiver was passed a dumper routine
 
297
                                                                 * (used in restore) */
 
298
        char       *tag;                        /* index tag */
 
299
        char       *namespace;          /* null or empty string if not in a schema */
 
300
        char       *tablespace;         /* null if not in a tablespace; empty string
 
301
                                                                 * means use database default */
 
302
        char       *owner;
 
303
        bool            withOids;               /* Used only by "TABLE" tags */
 
304
        char       *desc;
 
305
        char       *defn;
 
306
        char       *dropStmt;
 
307
        char       *copyStmt;
 
308
        DumpId     *dependencies;       /* dumpIds of objects this one depends on */
 
309
        int                     nDeps;                  /* number of dependencies */
 
310
 
 
311
        DataDumperPtr dataDumper;       /* Routine to dump data for object */
 
312
        void       *dataDumperArg;      /* Arg for above routine */
 
313
        void       *formatData;         /* TOC Entry data specific to file format */
 
314
} TocEntry;
 
315
 
 
316
/* Used everywhere */
 
317
extern const char *progname;
 
318
 
 
319
extern void die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(printf, 3, 4)));
 
320
extern void warn_or_die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(printf, 3, 4)));
 
321
extern void write_msg(const char *modulename, const char *fmt,...) __attribute__((format(printf, 2, 3)));
 
322
 
 
323
extern void WriteTOC(ArchiveHandle *AH);
 
324
extern void ReadTOC(ArchiveHandle *AH);
 
325
extern void WriteHead(ArchiveHandle *AH);
 
326
extern void ReadHead(ArchiveHandle *AH);
 
327
extern void WriteToc(ArchiveHandle *AH);
 
328
extern void ReadToc(ArchiveHandle *AH);
 
329
extern void WriteDataChunks(ArchiveHandle *AH);
 
330
 
 
331
extern teReqs TocIDRequired(ArchiveHandle *AH, DumpId id, RestoreOptions *ropt);
 
332
extern bool checkSeek(FILE *fp);
 
333
 
 
334
/*
 
335
 * Mandatory routines for each supported format
 
336
 */
 
337
 
 
338
extern size_t WriteInt(ArchiveHandle *AH, int i);
 
339
extern int      ReadInt(ArchiveHandle *AH);
 
340
extern char *ReadStr(ArchiveHandle *AH);
 
341
extern size_t WriteStr(ArchiveHandle *AH, const char *s);
 
342
 
 
343
int                     ReadOffset(ArchiveHandle *, off_t *);
 
344
size_t          WriteOffset(ArchiveHandle *, off_t, int);
 
345
 
 
346
extern void StartRestoreBlobs(ArchiveHandle *AH);
 
347
extern void StartRestoreBlob(ArchiveHandle *AH, Oid oid);
 
348
extern void EndRestoreBlob(ArchiveHandle *AH, Oid oid);
 
349
extern void EndRestoreBlobs(ArchiveHandle *AH);
 
350
 
 
351
extern void InitArchiveFmt_Custom(ArchiveHandle *AH);
 
352
extern void InitArchiveFmt_Files(ArchiveHandle *AH);
 
353
extern void InitArchiveFmt_Null(ArchiveHandle *AH);
 
354
extern void InitArchiveFmt_Tar(ArchiveHandle *AH);
 
355
 
 
356
extern bool isValidTarHeader(char *header);
 
357
 
 
358
extern OutputContext SetOutput(ArchiveHandle *AH, char *filename, int compression);
 
359
extern void ResetOutput(ArchiveHandle *AH, OutputContext savedContext);
 
360
extern int      RestoringToDB(ArchiveHandle *AH);
 
361
extern int      ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *newUser);
 
362
 
 
363
int                     ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH);
 
364
int                     ahprintf(ArchiveHandle *AH, const char *fmt,...) __attribute__((format(printf, 2, 3)));
 
365
 
 
366
void            ahlog(ArchiveHandle *AH, int level, const char *fmt,...) __attribute__((format(printf, 3, 4)));
 
367
 
 
368
#endif