📄 pg_backup_custom.c
字号:
/*------------------------------------------------------------------------- * * pg_backup_custom.c * * Implements the custom output format. * * The comments with the routined in this code are a good place to * understand how to write a new format. * * See the headers to pg_restore for more details. * * Copyright (c) 2000, Philip Warner * Rights are granted to use this software in any way so long * as this notice is not removed. * * The author is not responsible for loss or damages that may * and any liability will be limited to the time taken to fix any * related bug. * * * IDENTIFICATION * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.33 2005/10/15 02:49:38 momjian Exp $ * *------------------------------------------------------------------------- */#include "pg_backup.h"#include "pg_backup_archiver.h"/*-------- * Routines in the format interface *-------- */static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te);static void _StartData(ArchiveHandle *AH, TocEntry *te);static size_t _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);static void _EndData(ArchiveHandle *AH, TocEntry *te);static int _WriteByte(ArchiveHandle *AH, const int i);static int _ReadByte(ArchiveHandle *);static size_t _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);static size_t _ReadBuf(ArchiveHandle *AH, void *buf, size_t len);static void _CloseArchive(ArchiveHandle *AH);static void _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te);static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te);static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te);static void _PrintData(ArchiveHandle *AH);static void _skipData(ArchiveHandle *AH);static void _skipBlobs(ArchiveHandle *AH);static void _StartBlobs(ArchiveHandle *AH, TocEntry *te);static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);static void _EndBlobs(ArchiveHandle *AH, TocEntry *te);static void _LoadBlobs(ArchiveHandle *AH);/*------------ * Buffers used in zlib compression and extra data stored in archive and * in TOC entries. *------------ */#define zlibOutSize 4096#define zlibInSize 4096typedef struct{ z_streamp zp; char *zlibOut; char *zlibIn; size_t inSize; int hasSeek; off_t filePos; off_t dataStart;} lclContext;typedef struct{ int dataState; off_t dataPos;} lclTocEntry;/*------ * Static declarations *------ */static void _readBlockHeader(ArchiveHandle *AH, int *type, int *id);static void _StartDataCompressor(ArchiveHandle *AH, TocEntry *te);static void _EndDataCompressor(ArchiveHandle *AH, TocEntry *te);static off_t _getFilePos(ArchiveHandle *AH, lclContext *ctx);static int _DoDeflate(ArchiveHandle *AH, lclContext *ctx, int flush);static char *modulename = gettext_noop("custom archiver");/* * Init routine required by ALL formats. This is a global routine * and should be declared in pg_backup_archiver.h * * It's task is to create any extra archive context (using AH->formatData), * and to initialize the supported function pointers. * * It should also prepare whatever it's input source is for reading/writing, * and in the case of a read mode connection, it should load the Header & TOC. */voidInitArchiveFmt_Custom(ArchiveHandle *AH){ lclContext *ctx; /* Assuming static functions, this can be copied for each format. */ AH->ArchiveEntryPtr = _ArchiveEntry; AH->StartDataPtr = _StartData; AH->WriteDataPtr = _WriteData; AH->EndDataPtr = _EndData; AH->WriteBytePtr = _WriteByte; AH->ReadBytePtr = _ReadByte; AH->WriteBufPtr = _WriteBuf; AH->ReadBufPtr = _ReadBuf; AH->ClosePtr = _CloseArchive; AH->PrintTocDataPtr = _PrintTocData; AH->ReadExtraTocPtr = _ReadExtraToc; AH->WriteExtraTocPtr = _WriteExtraToc; AH->PrintExtraTocPtr = _PrintExtraToc; AH->StartBlobsPtr = _StartBlobs; AH->StartBlobPtr = _StartBlob; AH->EndBlobPtr = _EndBlob; AH->EndBlobsPtr = _EndBlobs; /* * Set up some special context used in compressing data. */ ctx = (lclContext *) calloc(1, sizeof(lclContext)); if (ctx == NULL) die_horribly(AH, modulename, "out of memory\n"); AH->formatData = (void *) ctx; ctx->zp = (z_streamp) malloc(sizeof(z_stream)); if (ctx->zp == NULL) die_horribly(AH, modulename, "out of memory\n"); /* Initialize LO buffering */ AH->lo_buf_size = LOBBUFSIZE; AH->lo_buf = (void *) malloc(LOBBUFSIZE); if (AH->lo_buf == NULL) die_horribly(AH, modulename, "out of memory\n"); /* * zlibOutSize is the buffer size we tell zlib it can output to. We * actually allocate one extra byte because some routines want to append a * trailing zero byte to the zlib output. The input buffer is expansible * and is always of size ctx->inSize; zlibInSize is just the initial * default size for it. */ ctx->zlibOut = (char *) malloc(zlibOutSize + 1); ctx->zlibIn = (char *) malloc(zlibInSize); ctx->inSize = zlibInSize; ctx->filePos = 0; if (ctx->zlibOut == NULL || ctx->zlibIn == NULL) die_horribly(AH, modulename, "out of memory\n"); /* * Now open the file */ if (AH->mode == archModeWrite) { if (AH->fSpec && strcmp(AH->fSpec, "") != 0) AH->FH = fopen(AH->fSpec, PG_BINARY_W); else AH->FH = stdout; if (!AH->FH) die_horribly(AH, modulename, "could not open archive file \"%s\": %s\n", AH->fSpec, strerror(errno)); ctx->hasSeek = checkSeek(AH->FH); } else { if (AH->fSpec && strcmp(AH->fSpec, "") != 0) AH->FH = fopen(AH->fSpec, PG_BINARY_R); else AH->FH = stdin; if (!AH->FH) die_horribly(AH, modulename, "could not open archive file \"%s\": %s\n", AH->fSpec, strerror(errno)); ctx->hasSeek = checkSeek(AH->FH); ReadHead(AH); ReadToc(AH); ctx->dataStart = _getFilePos(AH, ctx); }}/* * Called by the Archiver when the dumper creates a new TOC entry. * * Optional. * * Set up extrac format-related TOC data.*/static void_ArchiveEntry(ArchiveHandle *AH, TocEntry *te){ lclTocEntry *ctx; ctx = (lclTocEntry *) calloc(1, sizeof(lclTocEntry)); if (te->dataDumper) ctx->dataState = K_OFFSET_POS_NOT_SET; else ctx->dataState = K_OFFSET_NO_DATA; te->formatData = (void *) ctx;}/* * Called by the Archiver to save any extra format-related TOC entry * data. * * Optional. * * Use the Archiver routines to write data - they are non-endian, and * maintain other important file information. */static void_WriteExtraToc(ArchiveHandle *AH, TocEntry *te){ lclTocEntry *ctx = (lclTocEntry *) te->formatData; WriteOffset(AH, ctx->dataPos, ctx->dataState);}/* * Called by the Archiver to read any extra format-related TOC data. * * Optional. * * Needs to match the order defined in _WriteExtraToc, and sould also * use the Archiver input routines. */static void_ReadExtraToc(ArchiveHandle *AH, TocEntry *te){ int junk; lclTocEntry *ctx = (lclTocEntry *) te->formatData; if (ctx == NULL) { ctx = (lclTocEntry *) calloc(1, sizeof(lclTocEntry)); te->formatData = (void *) ctx; } ctx->dataState = ReadOffset(AH, &(ctx->dataPos)); /* * Prior to V1.7 (pg7.3), we dumped the data size as an int now we don't * dump it at all. */ if (AH->version < K_VERS_1_7) junk = ReadInt(AH);}/* * Called by the Archiver when restoring an archive to output a comment * that includes useful information about the TOC entry. * * Optional. * */static void_PrintExtraToc(ArchiveHandle *AH, TocEntry *te){ lclTocEntry *ctx = (lclTocEntry *) te->formatData; if (AH->public.verbose) ahprintf(AH, "-- Data Pos: " INT64_FORMAT "\n", (int64) ctx->dataPos);}/* * Called by the archiver when saving TABLE DATA (not schema). This routine * should save whatever format-specific information is needed to read * the archive back. * * It is called just prior to the dumper's 'DataDumper' routine being called. * * Optional, but strongly recommended. * */static void_StartData(ArchiveHandle *AH, TocEntry *te){ lclContext *ctx = (lclContext *) AH->formatData; lclTocEntry *tctx = (lclTocEntry *) te->formatData; tctx->dataPos = _getFilePos(AH, ctx); tctx->dataState = K_OFFSET_POS_SET; _WriteByte(AH, BLK_DATA); /* Block type */ WriteInt(AH, te->dumpId); /* For sanity check */ _StartDataCompressor(AH, te);}/* * Called by archiver when dumper calls WriteData. This routine is * called for both BLOB and TABLE data; it is the responsibility of * the format to manage each kind of data using StartBlob/StartData. * * It should only be called from within a DataDumper routine. * * Mandatory. */static size_t_WriteData(ArchiveHandle *AH, const void *data, size_t dLen){ lclContext *ctx = (lclContext *) AH->formatData; z_streamp zp = ctx->zp; zp->next_in = (void *) data; zp->avail_in = dLen; while (zp->avail_in != 0) { /* printf("Deflating %lu bytes\n", (unsigned long) dLen); */ _DoDeflate(AH, ctx, 0); } return dLen;}/* * Called by the archiver when a dumper's 'DataDumper' routine has * finished. * * Optional. * */static void_EndData(ArchiveHandle *AH, TocEntry *te){/* lclContext *ctx = (lclContext *) AH->formatData; *//* lclTocEntry *tctx = (lclTocEntry *) te->formatData; */ _EndDataCompressor(AH, te);}/* * Called by the archiver when starting to save all BLOB DATA (not schema). * This routine should save whatever format-specific information is needed * to read the BLOBs back into memory. * * It is called just prior to the dumper's DataDumper routine. * * Optional, but strongly recommended. */static void_StartBlobs(ArchiveHandle *AH, TocEntry *te){ lclContext *ctx = (lclContext *) AH->formatData; lclTocEntry *tctx = (lclTocEntry *) te->formatData; tctx->dataPos = _getFilePos(AH, ctx); tctx->dataState = K_OFFSET_POS_SET; _WriteByte(AH, BLK_BLOBS); /* Block type */ WriteInt(AH, te->dumpId); /* For sanity check */}/* * Called by the archiver when the dumper calls StartBlob. * * Mandatory. * * Must save the passed OID for retrieval at restore-time. */static void_StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid){ if (oid == 0) die_horribly(AH, modulename, "invalid OID for large object\n"); WriteInt(AH, oid); _StartDataCompressor(AH, te);}/* * Called by the archiver when the dumper calls EndBlob. * * Optional. */static void_EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid){ _EndDataCompressor(AH, te);}/* * Called by the archiver when finishing saving all BLOB DATA. * * Optional. */static void_EndBlobs(ArchiveHandle *AH, TocEntry *te){ /* Write out a fake zero OID to mark end-of-blobs. */ WriteInt(AH, 0);}/* * Print data for a given TOC entry */static void_PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt){ lclContext *ctx = (lclContext *) AH->formatData; int id; lclTocEntry *tctx = (lclTocEntry *) te->formatData; int blkType; int found = 0; if (tctx->dataState == K_OFFSET_NO_DATA) return; if (!ctx->hasSeek || tctx->dataState == K_OFFSET_POS_NOT_SET) { /* Skip over unnecessary blocks until we get the one we want. */ found = 0; _readBlockHeader(AH, &blkType, &id); while (id != te->dumpId) { if ((TocIDRequired(AH, id, ropt) & REQ_DATA) != 0) die_horribly(AH, modulename, "Dumping a specific TOC data block out of order is not supported" " without ID on this input stream (fseek required)\n"); switch (blkType) { case BLK_DATA: _skipData(AH); break; case BLK_BLOBS: _skipBlobs(AH); break; default: /* Always have a default */ die_horribly(AH, modulename, "unrecognized data block type (%d) while searching archive\n", blkType); break; } _readBlockHeader(AH, &blkType, &id); } } else { /* Grab it */ if (fseeko(AH->FH, tctx->dataPos, SEEK_SET) != 0) die_horribly(AH, modulename, "error during file seek: %s\n", strerror(errno)); _readBlockHeader(AH, &blkType, &id); } /* Are we sane? */ if (id != te->dumpId) die_horribly(AH, modulename, "found unexpected block ID (%d) when reading data -- expected %d\n", id, te->dumpId); switch (blkType) { case BLK_DATA: _PrintData(AH); break; case BLK_BLOBS: _LoadBlobs(AH); break; default: /* Always have a default */ die_horribly(AH, modulename, "unrecognized data block type %d while restoring archive\n", blkType); break; }}/* * Print data from current file position.*/static void_PrintData(ArchiveHandle *AH){ lclContext *ctx = (lclContext *) AH->formatData;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -