00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __PG_BACKUP_ARCHIVE__
00026 #define __PG_BACKUP_ARCHIVE__
00027
00028 #include "postgres_fe.h"
00029
00030 #include <time.h>
00031
00032 #include "pg_backup.h"
00033
00034 #include "libpq-fe.h"
00035 #include "pqexpbuffer.h"
00036
00037 #define LOBBUFSIZE 16384
00038
00039
00040
00041
00042
00043 #ifdef HAVE_LIBZ
00044 #include <zlib.h>
00045 #define GZCLOSE(fh) gzclose(fh)
00046 #define GZWRITE(p, s, n, fh) gzwrite(fh, p, (n) * (s))
00047 #define GZREAD(p, s, n, fh) gzread(fh, p, (n) * (s))
00048 #else
00049 #define GZCLOSE(fh) fclose(fh)
00050 #define GZWRITE(p, s, n, fh) (fwrite(p, s, n, fh) * (s))
00051 #define GZREAD(p, s, n, fh) fread(p, s, n, fh)
00052
00053 #define Z_DEFAULT_COMPRESSION (-1)
00054
00055 typedef struct _z_stream
00056 {
00057 void *next_in;
00058 void *next_out;
00059 size_t avail_in;
00060 size_t avail_out;
00061 } z_stream;
00062 typedef z_stream *z_streamp;
00063 #endif
00064
00065
00066 #define K_VERS_MAJOR 1
00067 #define K_VERS_MINOR 12
00068 #define K_VERS_REV 0
00069
00070
00071 #define BLK_DATA 1
00072 #define BLK_BLOBS 3
00073
00074
00075 #define K_VERS_1_0 (( (1 * 256 + 0) * 256 + 0) * 256 + 0)
00076 #define K_VERS_1_2 (( (1 * 256 + 2) * 256 + 0) * 256 + 0)
00077 #define K_VERS_1_3 (( (1 * 256 + 3) * 256 + 0) * 256 + 0)
00078 #define K_VERS_1_4 (( (1 * 256 + 4) * 256 + 0) * 256 + 0)
00079 #define K_VERS_1_5 (( (1 * 256 + 5) * 256 + 0) * 256 + 0)
00080 #define K_VERS_1_6 (( (1 * 256 + 6) * 256 + 0) * 256 + 0)
00081 #define K_VERS_1_7 (( (1 * 256 + 7) * 256 + 0) * 256 + 0)
00082
00083 #define K_VERS_1_8 (( (1 * 256 + 8) * 256 + 0) * 256 + 0)
00084
00085
00086 #define K_VERS_1_9 (( (1 * 256 + 9) * 256 + 0) * 256 + 0)
00087
00088 #define K_VERS_1_10 (( (1 * 256 + 10) * 256 + 0) * 256 + 0)
00089 #define K_VERS_1_11 (( (1 * 256 + 11) * 256 + 0) * 256 + 0)
00090
00091 #define K_VERS_1_12 (( (1 * 256 + 12) * 256 + 0) * 256 + 0)
00092
00093
00094
00095 #define K_VERS_MAX (( (1 * 256 + 12) * 256 + 255) * 256 + 0)
00096
00097
00098
00099 #define K_OFFSET_POS_NOT_SET 1
00100 #define K_OFFSET_POS_SET 2
00101 #define K_OFFSET_NO_DATA 3
00102
00103
00104
00105
00106
00107 #define WORKER_OK 0
00108 #define WORKER_CREATE_DONE 10
00109 #define WORKER_INHIBIT_DATA 11
00110 #define WORKER_IGNORED_ERRORS 12
00111
00112 struct _archiveHandle;
00113 struct _tocEntry;
00114 struct _restoreList;
00115 struct ParallelArgs;
00116 struct ParallelState;
00117
00118 typedef enum T_Action
00119 {
00120 ACT_DUMP,
00121 ACT_RESTORE
00122 } T_Action;
00123
00124 typedef void (*ClosePtr) (struct _archiveHandle * AH);
00125 typedef void (*ReopenPtr) (struct _archiveHandle * AH);
00126 typedef void (*ArchiveEntryPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00127
00128 typedef void (*StartDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00129 typedef size_t (*WriteDataPtr) (struct _archiveHandle * AH, const void *data, size_t dLen);
00130 typedef void (*EndDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00131
00132 typedef void (*StartBlobsPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00133 typedef void (*StartBlobPtr) (struct _archiveHandle * AH, struct _tocEntry * te, Oid oid);
00134 typedef void (*EndBlobPtr) (struct _archiveHandle * AH, struct _tocEntry * te, Oid oid);
00135 typedef void (*EndBlobsPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00136
00137 typedef int (*WriteBytePtr) (struct _archiveHandle * AH, const int i);
00138 typedef int (*ReadBytePtr) (struct _archiveHandle * AH);
00139 typedef size_t (*WriteBufPtr) (struct _archiveHandle * AH, const void *c, size_t len);
00140 typedef size_t (*ReadBufPtr) (struct _archiveHandle * AH, void *buf, size_t len);
00141 typedef void (*SaveArchivePtr) (struct _archiveHandle * AH);
00142 typedef void (*WriteExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00143 typedef void (*ReadExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00144 typedef void (*PrintExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00145 typedef void (*PrintTocDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te, RestoreOptions *ropt);
00146
00147 typedef void (*ClonePtr) (struct _archiveHandle * AH);
00148 typedef void (*DeClonePtr) (struct _archiveHandle * AH);
00149
00150 typedef char *(*WorkerJobRestorePtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00151 typedef char *(*WorkerJobDumpPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00152 typedef char *(*MasterStartParallelItemPtr) (struct _archiveHandle * AH, struct _tocEntry * te,
00153 T_Action act);
00154 typedef int (*MasterEndParallelItemPtr) (struct _archiveHandle * AH, struct _tocEntry * te,
00155 const char *str, T_Action act);
00156
00157 typedef size_t (*CustomOutPtr) (struct _archiveHandle * AH, const void *buf, size_t len);
00158
00159 typedef enum
00160 {
00161 SQL_SCAN = 0,
00162 SQL_IN_SINGLE_QUOTE,
00163 SQL_IN_DOUBLE_QUOTE
00164 } sqlparseState;
00165
00166 typedef struct
00167 {
00168 sqlparseState state;
00169 bool backSlash;
00170 PQExpBuffer curCmd;
00171 } sqlparseInfo;
00172
00173 typedef enum
00174 {
00175 STAGE_NONE = 0,
00176 STAGE_INITIALIZING,
00177 STAGE_PROCESSING,
00178 STAGE_FINALIZING
00179 } ArchiverStage;
00180
00181 typedef enum
00182 {
00183 OUTPUT_SQLCMDS = 0,
00184 OUTPUT_COPYDATA,
00185 OUTPUT_OTHERDATA
00186 } ArchiverOutput;
00187
00188 typedef enum
00189 {
00190 REQ_SCHEMA = 0x01,
00191 REQ_DATA = 0x02,
00192 REQ_SPECIAL = 0x04
00193 } teReqs;
00194
00195 typedef struct _archiveHandle
00196 {
00197 Archive public;
00198 char vmaj;
00199 char vmin;
00200 char vrev;
00201 int version;
00202
00203 char *archiveRemoteVersion;
00204
00205 char *archiveDumpVersion;
00206
00207
00208 int debugLevel;
00209
00210 size_t intSize;
00211 size_t offSize;
00212
00213 ArchiveFormat format;
00214
00215 sqlparseInfo sqlparse;
00216
00217 time_t createDate;
00218
00219
00220
00221
00222
00223 int readHeader;
00224 char *lookahead;
00225
00226 size_t lookaheadSize;
00227 size_t lookaheadLen;
00228 pgoff_t lookaheadPos;
00229
00230 ArchiveEntryPtr ArchiveEntryPtr;
00231 StartDataPtr StartDataPtr;
00232
00233 WriteDataPtr WriteDataPtr;
00234
00235 EndDataPtr EndDataPtr;
00236 WriteBytePtr WriteBytePtr;
00237 ReadBytePtr ReadBytePtr;
00238 WriteBufPtr WriteBufPtr;
00239 ReadBufPtr ReadBufPtr;
00240 ClosePtr ClosePtr;
00241 ReopenPtr ReopenPtr;
00242 WriteExtraTocPtr WriteExtraTocPtr;
00243
00244
00245 ReadExtraTocPtr ReadExtraTocPtr;
00246
00247 PrintExtraTocPtr PrintExtraTocPtr;
00248 PrintTocDataPtr PrintTocDataPtr;
00249
00250 StartBlobsPtr StartBlobsPtr;
00251 EndBlobsPtr EndBlobsPtr;
00252 StartBlobPtr StartBlobPtr;
00253 EndBlobPtr EndBlobPtr;
00254
00255 MasterStartParallelItemPtr MasterStartParallelItemPtr;
00256 MasterEndParallelItemPtr MasterEndParallelItemPtr;
00257
00258 SetupWorkerPtr SetupWorkerPtr;
00259 WorkerJobDumpPtr WorkerJobDumpPtr;
00260 WorkerJobRestorePtr WorkerJobRestorePtr;
00261
00262 ClonePtr ClonePtr;
00263 DeClonePtr DeClonePtr;
00264
00265 CustomOutPtr CustomOutPtr;
00266
00267
00268 char *archdbname;
00269 enum trivalue promptPassword;
00270 char *savedPassword;
00271 char *use_role;
00272 PGconn *connection;
00273 int connectToDB;
00274
00275 ArchiverOutput outputKind;
00276 bool pgCopyIn;
00277
00278 int loFd;
00279 int writingBlob;
00280 int blobCount;
00281
00282 char *fSpec;
00283 FILE *FH;
00284 void *OF;
00285 int gzOut;
00286
00287 struct _tocEntry *toc;
00288 int tocCount;
00289 DumpId maxDumpId;
00290
00291
00292 struct _tocEntry **tocsByDumpId;
00293 DumpId *tableDataId;
00294
00295 struct _tocEntry *currToc;
00296 int compression;
00297
00298
00299
00300 ArchiveMode mode;
00301 void *formatData;
00302
00303 RestoreOptions *ropt;
00304
00305
00306
00307 char *currUser;
00308 char *currSchema;
00309 char *currTablespace;
00310 bool currWithOids;
00311
00312 void *lo_buf;
00313 size_t lo_buf_used;
00314 size_t lo_buf_size;
00315
00316 int noTocComments;
00317 ArchiverStage stage;
00318 ArchiverStage lastErrorStage;
00319 struct _tocEntry *currentTE;
00320 struct _tocEntry *lastErrorTE;
00321 } ArchiveHandle;
00322
00323 typedef struct _tocEntry
00324 {
00325 struct _tocEntry *prev;
00326 struct _tocEntry *next;
00327 CatalogId catalogId;
00328 DumpId dumpId;
00329 teSection section;
00330 bool hadDumper;
00331
00332 char *tag;
00333 char *namespace;
00334 char *tablespace;
00335
00336 char *owner;
00337 bool withOids;
00338 char *desc;
00339 char *defn;
00340 char *dropStmt;
00341 char *copyStmt;
00342 DumpId *dependencies;
00343 int nDeps;
00344
00345 DataDumperPtr dataDumper;
00346 void *dataDumperArg;
00347 void *formatData;
00348
00349
00350 teReqs reqs;
00351 bool created;
00352
00353
00354 struct _tocEntry *par_prev;
00355 struct _tocEntry *par_next;
00356 int depCount;
00357 DumpId *revDeps;
00358 int nRevDeps;
00359 DumpId *lockDeps;
00360 int nLockDeps;
00361 } TocEntry;
00362
00363 extern int parallel_restore(struct ParallelArgs * args);
00364 extern void on_exit_close_archive(Archive *AHX);
00365
00366 extern void warn_or_exit_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
00367
00368 extern void WriteTOC(ArchiveHandle *AH);
00369 extern void ReadTOC(ArchiveHandle *AH);
00370 extern void WriteHead(ArchiveHandle *AH);
00371 extern void ReadHead(ArchiveHandle *AH);
00372 extern void WriteToc(ArchiveHandle *AH);
00373 extern void ReadToc(ArchiveHandle *AH);
00374 extern void WriteDataChunks(ArchiveHandle *AH, struct ParallelState *pstate);
00375 extern void WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te);
00376 extern ArchiveHandle *CloneArchive(ArchiveHandle *AH);
00377 extern void DeCloneArchive(ArchiveHandle *AH);
00378
00379 extern teReqs TocIDRequired(ArchiveHandle *AH, DumpId id);
00380 TocEntry *getTocEntryByDumpId(ArchiveHandle *AH, DumpId id);
00381 extern bool checkSeek(FILE *fp);
00382
00383 #define appendStringLiteralAHX(buf,str,AH) \
00384 appendStringLiteral(buf, str, (AH)->public.encoding, (AH)->public.std_strings)
00385
00386 #define appendByteaLiteralAHX(buf,str,len,AH) \
00387 appendByteaLiteral(buf, str, len, (AH)->public.std_strings)
00388
00389
00390
00391
00392
00393 extern size_t WriteInt(ArchiveHandle *AH, int i);
00394 extern int ReadInt(ArchiveHandle *AH);
00395 extern char *ReadStr(ArchiveHandle *AH);
00396 extern size_t WriteStr(ArchiveHandle *AH, const char *s);
00397
00398 int ReadOffset(ArchiveHandle *, pgoff_t *);
00399 size_t WriteOffset(ArchiveHandle *, pgoff_t, int);
00400
00401 extern void StartRestoreBlobs(ArchiveHandle *AH);
00402 extern void StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop);
00403 extern void EndRestoreBlob(ArchiveHandle *AH, Oid oid);
00404 extern void EndRestoreBlobs(ArchiveHandle *AH);
00405
00406 extern void InitArchiveFmt_Custom(ArchiveHandle *AH);
00407 extern void InitArchiveFmt_Null(ArchiveHandle *AH);
00408 extern void InitArchiveFmt_Directory(ArchiveHandle *AH);
00409 extern void InitArchiveFmt_Tar(ArchiveHandle *AH);
00410
00411 extern bool isValidTarHeader(char *header);
00412
00413 extern int ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *newUser);
00414 extern void DropBlobIfExists(ArchiveHandle *AH, Oid oid);
00415
00416 int ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH);
00417 int ahprintf(ArchiveHandle *AH, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
00418
00419 void ahlog(ArchiveHandle *AH, int level, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
00420
00421 #endif