1 /*-------------------------------------------------------------------------
5 * Implements the custom output format.
7 * The comments with the routined in this code are a good place to
8 * understand how to write a new format.
10 * See the headers to pg_restore for more details.
12 * Copyright (c) 2000, Philip Warner
13 * Rights are granted to use this software in any way so long
14 * as this notice is not removed.
16 * The author is not responsible for loss or damages that may
17 * and any liability will be limited to the time taken to fix any
22 * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.17 2001/11/27 23:48:12 tgl Exp $
24 * Modifications - 28-Jun-2000 - pjw@rhyme.com.au
28 * Modifications - 04-Jan-2001 - pjw@rhyme.com.au
30 * - Check results of IO routines more carefully.
32 *-------------------------------------------------------------------------
35 #include "pg_backup.h"
36 #include "pg_backup_archiver.h"
41 * Routines in the format interface
45 static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te);
46 static void _StartData(ArchiveHandle *AH, TocEntry *te);
47 static int _WriteData(ArchiveHandle *AH, const void *data, int dLen);
48 static void _EndData(ArchiveHandle *AH, TocEntry *te);
49 static int _WriteByte(ArchiveHandle *AH, const int i);
50 static int _ReadByte(ArchiveHandle *);
51 static int _WriteBuf(ArchiveHandle *AH, const void *buf, int len);
52 static int _ReadBuf(ArchiveHandle *AH, void *buf, int len);
53 static void _CloseArchive(ArchiveHandle *AH);
54 static void _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
55 static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te);
56 static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te);
57 static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te);
59 static void _PrintData(ArchiveHandle *AH);
60 static void _skipData(ArchiveHandle *AH);
61 static void _skipBlobs(ArchiveHandle *AH);
63 static void _StartBlobs(ArchiveHandle *AH, TocEntry *te);
64 static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);
65 static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);
66 static void _EndBlobs(ArchiveHandle *AH, TocEntry *te);
67 static void _LoadBlobs(ArchiveHandle *AH);
70 * Buffers used in zlib compression and extra data stored in archive and
74 #define zlibOutSize 4096
75 #define zlibInSize 4096
99 static void _readBlockHeader(ArchiveHandle *AH, int *type, int *id);
100 static void _StartDataCompressor(ArchiveHandle *AH, TocEntry *te);
101 static void _EndDataCompressor(ArchiveHandle *AH, TocEntry *te);
102 static int _getFilePos(ArchiveHandle *AH, lclContext *ctx);
103 static int _DoDeflate(ArchiveHandle *AH, lclContext *ctx, int flush);
105 static char *modulename = gettext_noop("custom archiver");
110 * Init routine required by ALL formats. This is a global routine
111 * and should be declared in pg_backup_archiver.h
113 * It's task is to create any extra archive context (using AH->formatData),
114 * and to initialize the supported function pointers.
116 * It should also prepare whatever it's input source is for reading/writing,
117 * and in the case of a read mode connection, it should load the Header & TOC.
120 InitArchiveFmt_Custom(ArchiveHandle *AH)
124 /* Assuming static functions, this can be copied for each format. */
125 AH->ArchiveEntryPtr = _ArchiveEntry;
126 AH->StartDataPtr = _StartData;
127 AH->WriteDataPtr = _WriteData;
128 AH->EndDataPtr = _EndData;
129 AH->WriteBytePtr = _WriteByte;
130 AH->ReadBytePtr = _ReadByte;
131 AH->WriteBufPtr = _WriteBuf;
132 AH->ReadBufPtr = _ReadBuf;
133 AH->ClosePtr = _CloseArchive;
134 AH->PrintTocDataPtr = _PrintTocData;
135 AH->ReadExtraTocPtr = _ReadExtraToc;
136 AH->WriteExtraTocPtr = _WriteExtraToc;
137 AH->PrintExtraTocPtr = _PrintExtraToc;
139 AH->StartBlobsPtr = _StartBlobs;
140 AH->StartBlobPtr = _StartBlob;
141 AH->EndBlobPtr = _EndBlob;
142 AH->EndBlobsPtr = _EndBlobs;
145 * Set up some special context used in compressing data.
147 ctx = (lclContext *) malloc(sizeof(lclContext));
149 die_horribly(AH, modulename, "out of memory\n");
150 AH->formatData = (void *) ctx;
152 ctx->zp = (z_streamp) malloc(sizeof(z_stream));
154 die_horribly(AH, modulename, "out of memory\n");
157 * zlibOutSize is the buffer size we tell zlib it can output to. We
158 * actually allocate one extra byte because some routines want to
159 * append a trailing zero byte to the zlib output. The input buffer
160 * is expansible and is always of size ctx->inSize; zlibInSize is just
161 * the initial default size for it.
163 ctx->zlibOut = (char *) malloc(zlibOutSize + 1);
164 ctx->zlibIn = (char *) malloc(zlibInSize);
165 ctx->inSize = zlibInSize;
168 if (ctx->zlibOut == NULL || ctx->zlibIn == NULL)
169 die_horribly(AH, modulename, "out of memory\n");
174 if (AH->mode == archModeWrite)
177 if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
178 AH->FH = fopen(AH->fSpec, PG_BINARY_W);
183 die_horribly(AH, modulename, "could not open archive file %s: %s\n", AH->fSpec, strerror(errno));
185 ctx->hasSeek = (fseek(AH->FH, 0, SEEK_CUR) == 0);
191 if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
192 AH->FH = fopen(AH->fSpec, PG_BINARY_R);
196 die_horribly(AH, modulename, "could not open archive file %s: %s\n", AH->fSpec, strerror(errno));
198 ctx->hasSeek = (fseek(AH->FH, 0, SEEK_CUR) == 0);
202 ctx->dataStart = _getFilePos(AH, ctx);
208 * Called by the Archiver when the dumper creates a new TOC entry.
212 * Set up extrac format-related TOC data.
215 _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
219 ctx = (lclTocEntry *) calloc(1, sizeof(lclTocEntry));
225 te->formatData = (void *) ctx;
230 * Called by the Archiver to save any extra format-related TOC entry
235 * Use the Archiver routines to write data - they are non-endian, and
236 * maintain other important file information.
239 _WriteExtraToc(ArchiveHandle *AH, TocEntry *te)
241 lclTocEntry *ctx = (lclTocEntry *) te->formatData;
243 WriteInt(AH, ctx->dataPos);
244 WriteInt(AH, ctx->dataLen);
248 * Called by the Archiver to read any extra format-related TOC data.
252 * Needs to match the order defined in _WriteExtraToc, and sould also
253 * use the Archiver input routines.
256 _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
258 lclTocEntry *ctx = (lclTocEntry *) te->formatData;
262 ctx = (lclTocEntry *) malloc(sizeof(lclTocEntry));
263 te->formatData = (void *) ctx;
266 ctx->dataPos = ReadInt(AH);
267 ctx->dataLen = ReadInt(AH);
271 * Called by the Archiver when restoring an archive to output a comment
272 * that includes useful information about the TOC entry.
278 _PrintExtraToc(ArchiveHandle *AH, TocEntry *te)
280 lclTocEntry *ctx = (lclTocEntry *) te->formatData;
282 ahprintf(AH, "-- Data Pos: %d (Length %d)\n", ctx->dataPos, ctx->dataLen);
286 * Called by the archiver when saving TABLE DATA (not schema). This routine
287 * should save whatever format-specific information is needed to read
290 * It is called just prior to the dumper's 'DataDumper' routine being called.
292 * Optional, but strongly recommended.
296 _StartData(ArchiveHandle *AH, TocEntry *te)
298 lclContext *ctx = (lclContext *) AH->formatData;
299 lclTocEntry *tctx = (lclTocEntry *) te->formatData;
301 tctx->dataPos = _getFilePos(AH, ctx);
303 _WriteByte(AH, BLK_DATA); /* Block type */
304 WriteInt(AH, te->id); /* For sanity check */
306 _StartDataCompressor(AH, te);
311 * Called by archiver when dumper calls WriteData. This routine is
312 * called for both BLOB and TABLE data; it is the responsibility of
313 * the format to manage each kind of data using StartBlob/StartData.
315 * It should only be called from withing a DataDumper routine.
321 _WriteData(ArchiveHandle *AH, const void *data, int dLen)
323 lclContext *ctx = (lclContext *) AH->formatData;
324 z_streamp zp = ctx->zp;
326 zp->next_in = (void *) data;
329 while (zp->avail_in != 0)
331 /* printf("Deflating %d bytes\n", dLen); */
332 _DoDeflate(AH, ctx, 0);
338 * Called by the archiver when a dumper's 'DataDumper' routine has
345 _EndData(ArchiveHandle *AH, TocEntry *te)
347 lclContext *ctx = (lclContext *) AH->formatData;
348 lclTocEntry *tctx = (lclTocEntry *) te->formatData;
350 _EndDataCompressor(AH, te);
352 tctx->dataLen = _getFilePos(AH, ctx) - tctx->dataPos;
356 * Called by the archiver when starting to save all BLOB DATA (not schema).
357 * This routine should save whatever format-specific information is needed
358 * to read the BLOBs back into memory.
360 * It is called just prior to the dumper's DataDumper routine.
362 * Optional, but strongly recommended.
366 _StartBlobs(ArchiveHandle *AH, TocEntry *te)
368 lclContext *ctx = (lclContext *) AH->formatData;
369 lclTocEntry *tctx = (lclTocEntry *) te->formatData;
371 tctx->dataPos = _getFilePos(AH, ctx);
373 _WriteByte(AH, BLK_BLOBS); /* Block type */
374 WriteInt(AH, te->id); /* For sanity check */
379 * Called by the archiver when the dumper calls StartBlob.
383 * Must save the passed OID for retrieval at restore-time.
386 _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
389 die_horribly(AH, modulename, "invalid OID for large object\n");
392 _StartDataCompressor(AH, te);
396 * Called by the archiver when the dumper calls EndBlob.
402 _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
404 _EndDataCompressor(AH, te);
408 * Called by the archiver when finishing saving all BLOB DATA.
414 _EndBlobs(ArchiveHandle *AH, TocEntry *te)
416 /* Write out a fake zero OID to mark end-of-blobs. */
421 * Print data for a gievn TOC entry
424 _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
426 lclContext *ctx = (lclContext *) AH->formatData;
428 lclTocEntry *tctx = (lclTocEntry *) te->formatData;
432 if (tctx->dataPos == 0)
435 if (!ctx->hasSeek || tctx->dataPos < 0)
438 /* Skip over unnecessary blocks until we get the one we want. */
442 _readBlockHeader(AH, &blkType, &id);
447 if ((TocIDRequired(AH, id, ropt) & 2) != 0)
448 die_horribly(AH, modulename,
449 "Dumping a specific TOC data block out of order is not supported"
450 " without id on this input stream (fseek required)\n");
465 default: /* Always have a default */
467 die_horribly(AH, modulename,
468 "unrecognized data block type (%d) while searching archive\n",
473 _readBlockHeader(AH, &blkType, &id);
483 if (fseek(AH->FH, tctx->dataPos, SEEK_SET) != 0)
484 die_horribly(AH, modulename, "error during file seek: %s\n", strerror(errno));
486 _readBlockHeader(AH, &blkType, &id);
492 die_horribly(AH, modulename, "found unexpected block ID (%d) when reading data - expected %d\n",
506 die_horribly(AH, modulename, "large objects cannot be loaded without a database connection\n");
511 default: /* Always have a default */
513 die_horribly(AH, modulename, "unrecognized data block type %d while restoring archive\n",
518 ahprintf(AH, "\n\n");
522 * Print data from current file position.
525 _PrintData(ArchiveHandle *AH)
527 lclContext *ctx = (lclContext *) AH->formatData;
528 z_streamp zp = ctx->zp;
530 char *in = ctx->zlibIn;
535 char *out = ctx->zlibOut;
542 if (AH->compression != 0)
548 if (inflateInit(zp) != Z_OK)
549 die_horribly(AH, modulename, "could not initialize compression library: %s\n", zp->msg);
553 blkLen = ReadInt(AH);
556 if (blkLen + 1 > ctx->inSize)
560 ctx->zlibIn = (char *) malloc(blkLen + 1);
562 die_horribly(AH, modulename, "out of memory\n");
564 ctx->inSize = blkLen + 1;
568 cnt = fread(in, 1, blkLen, AH->FH);
570 die_horribly(AH, modulename, "could not read data block - expected %d, got %d\n", blkLen, cnt);
572 ctx->filePos += blkLen;
575 zp->avail_in = blkLen;
579 if (AH->compression != 0)
582 while (zp->avail_in != 0)
585 zp->avail_out = zlibOutSize;
586 res = inflate(zp, 0);
587 if (res != Z_OK && res != Z_STREAM_END)
588 die_horribly(AH, modulename, "unable to uncompress data: %s\n", zp->msg);
590 out[zlibOutSize - zp->avail_out] = '\0';
591 ahwrite(out, 1, zlibOutSize - zp->avail_out, AH);
597 in[zp->avail_in] = '\0';
598 ahwrite(in, 1, zp->avail_in, AH);
605 blkLen = ReadInt(AH);
610 if (AH->compression != 0)
614 while (res != Z_STREAM_END)
617 zp->avail_out = zlibOutSize;
618 res = inflate(zp, 0);
619 if (res != Z_OK && res != Z_STREAM_END)
620 die_horribly(AH, modulename, "unable to uncompress data: %s\n", zp->msg);
622 out[zlibOutSize - zp->avail_out] = '\0';
623 ahwrite(out, 1, zlibOutSize - zp->avail_out, AH);
625 if (inflateEnd(zp) != Z_OK)
626 die_horribly(AH, modulename, "could not close compression library: %s\n", zp->msg);
633 _LoadBlobs(ArchiveHandle *AH)
637 StartRestoreBlobs(AH);
642 StartRestoreBlob(AH, oid);
644 EndRestoreBlob(AH, oid);
653 * Skip the BLOBs from the current file position.
654 * BLOBS are written sequentially as data blocks (see below).
655 * Each BLOB is preceded by it's original OID.
656 * A zero OID indicated the end of the BLOBS
659 _skipBlobs(ArchiveHandle *AH)
672 * Skip data from current file position.
673 * Data blocks are formatted as an integer length, followed by data.
674 * A zero length denoted the end of the block.
677 _skipData(ArchiveHandle *AH)
679 lclContext *ctx = (lclContext *) AH->formatData;
681 char *in = ctx->zlibIn;
684 blkLen = ReadInt(AH);
687 if (blkLen > ctx->inSize)
690 ctx->zlibIn = (char *) malloc(blkLen);
691 ctx->inSize = blkLen;
694 cnt = fread(in, 1, blkLen, AH->FH);
696 die_horribly(AH, modulename, "could not read data block - expected %d, got %d\n", blkLen, cnt);
698 ctx->filePos += blkLen;
700 blkLen = ReadInt(AH);
706 * Write a byte of data to the archive.
710 * Called by the archiver to do integer & byte output to the archive.
711 * These routines are only used to read & write headers & TOC.
715 _WriteByte(ArchiveHandle *AH, const int i)
717 lclContext *ctx = (lclContext *) AH->formatData;
720 res = fputc(i, AH->FH);
724 die_horribly(AH, modulename, "could not write byte: %s\n", strerror(errno));
729 * Read a byte of data from the archive.
733 * Called by the archiver to read bytes & integers from the archive.
734 * These routines are only used to read & write headers & TOC.
738 _ReadByte(ArchiveHandle *AH)
740 lclContext *ctx = (lclContext *) AH->formatData;
750 * Write a buffer of data to the archive.
754 * Called by the archiver to write a block of bytes to the archive.
755 * These routines are only used to read & write headers & TOC.
759 _WriteBuf(ArchiveHandle *AH, const void *buf, int len)
761 lclContext *ctx = (lclContext *) AH->formatData;
764 res = fwrite(buf, 1, len, AH->FH);
767 die_horribly(AH, modulename, "write error in _WriteBuf (%d != %d)\n", res, len);
774 * Read a block of bytes from the archive.
778 * Called by the archiver to read a block of bytes from the archive
779 * These routines are only used to read & write headers & TOC.
783 _ReadBuf(ArchiveHandle *AH, void *buf, int len)
785 lclContext *ctx = (lclContext *) AH->formatData;
788 res = fread(buf, 1, len, AH->FH);
799 * When writing the archive, this is the routine that actually starts
800 * the process of saving it to files. No data should be written prior
801 * to this point, since the user could sort the TOC after creating it.
803 * If an archive is to be written, this toutine must call:
804 * WriteHead to save the archive header
805 * WriteToc to save the TOC entries
806 * WriteDataChunks to save all DATA & BLOBs.
810 _CloseArchive(ArchiveHandle *AH)
812 lclContext *ctx = (lclContext *) AH->formatData;
815 if (AH->mode == archModeWrite)
818 tpos = ftell(AH->FH);
820 ctx->dataStart = _getFilePos(AH, ctx);
824 * This is not an essential operation - it is really only needed
825 * if we expect to be doing seeks to read the data back - it may
826 * be ok to just use the existing self-consistent block
831 fseek(AH->FH, tpos, SEEK_SET);
836 if (fclose(AH->FH) != 0)
837 die_horribly(AH, modulename, "could not close archive file: %s\n", strerror(errno));
842 /*--------------------------------------------------
843 * END OF FORMAT CALLBACKS
844 *--------------------------------------------------
848 * Get the current position in the archive file.
851 _getFilePos(ArchiveHandle *AH, lclContext *ctx)
858 if (pos != ctx->filePos)
860 write_msg(modulename, "WARNING: ftell mismatch with expected position -- ftell ignored\n");
870 * Read a data block header. The format changed in V1.3, so we
871 * put the code here for simplicity.
874 _readBlockHeader(ArchiveHandle *AH, int *type, int *id)
876 if (AH->version < K_VERS_1_3)
879 *type = _ReadByte(AH);;
885 * If zlib is available, then startit up. This is called from
886 * StartData & StartBlob. The buffers are setup in the Init routine.
890 _StartDataCompressor(ArchiveHandle *AH, TocEntry *te)
892 lclContext *ctx = (lclContext *) AH->formatData;
893 z_streamp zp = ctx->zp;
897 if (AH->compression < 0 || AH->compression > 9)
898 AH->compression = Z_DEFAULT_COMPRESSION;
900 if (AH->compression != 0)
906 if (deflateInit(zp, AH->compression) != Z_OK)
907 die_horribly(AH, modulename, "could not initialize compression library: %s\n", zp->msg);
915 /* Just be paranoid - maybe End is called after Start, with no Write */
916 zp->next_out = ctx->zlibOut;
917 zp->avail_out = zlibOutSize;
921 * Send compressed data to the output stream (via ahwrite).
922 * Each data chunk is preceded by it's length.
923 * In the case of Z0, or no zlib, just write the raw data.
927 _DoDeflate(ArchiveHandle *AH, lclContext *ctx, int flush)
929 z_streamp zp = ctx->zp;
932 char *out = ctx->zlibOut;
935 if (AH->compression != 0)
937 res = deflate(zp, flush);
938 if (res == Z_STREAM_ERROR)
939 die_horribly(AH, modulename, "could not compress data: %s\n", zp->msg);
941 if (((flush == Z_FINISH) && (zp->avail_out < zlibOutSize))
942 || (zp->avail_out == 0)
943 || (zp->avail_in != 0)
947 * Extra paranoia: avoid zero-length chunks since a zero
948 * length chunk is the EOF marker. This should never happen
951 if (zp->avail_out < zlibOutSize)
954 * printf("Wrote %d byte deflated chunk\n", zlibOutSize -
957 WriteInt(AH, zlibOutSize - zp->avail_out);
958 if (fwrite(out, 1, zlibOutSize - zp->avail_out, AH->FH) != (zlibOutSize - zp->avail_out))
959 die_horribly(AH, modulename, "could not write compressed chunk\n");
960 ctx->filePos += zlibOutSize - zp->avail_out;
963 zp->avail_out = zlibOutSize;
969 if (zp->avail_in > 0)
971 WriteInt(AH, zp->avail_in);
972 if (fwrite(zp->next_in, 1, zp->avail_in, AH->FH) != zp->avail_in)
973 die_horribly(AH, modulename, "could not write uncompressed chunk\n");
974 ctx->filePos += zp->avail_in;
980 if (flush == Z_FINISH)
997 * Terminate zlib context and flush it's buffers. If no zlib
1002 _EndDataCompressor(ArchiveHandle *AH, TocEntry *te)
1006 lclContext *ctx = (lclContext *) AH->formatData;
1007 z_streamp zp = ctx->zp;
1010 if (AH->compression != 0)
1017 /* printf("Ending data output\n"); */
1018 res = _DoDeflate(AH, ctx, Z_FINISH);
1019 } while (res != Z_STREAM_END);
1021 if (deflateEnd(zp) != Z_OK)
1022 die_horribly(AH, modulename, "could not close compression stream: %s\n", zp->msg);
1026 /* Send the end marker */