diff options
Diffstat (limited to 'src/bin/pg_dump/pg_backup_custom.c')
-rw-r--r-- | src/bin/pg_dump/pg_backup_custom.c | 827 |
1 files changed, 445 insertions, 382 deletions
diff --git a/src/bin/pg_dump/pg_backup_custom.c b/src/bin/pg_dump/pg_backup_custom.c index e9117b8320c..e58a9f15c36 100644 --- a/src/bin/pg_dump/pg_backup_custom.c +++ b/src/bin/pg_dump/pg_backup_custom.c @@ -4,30 +4,30 @@ * * 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. + * 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. + * 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. + * related bug. * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.8 2001/03/19 02:35:28 pjw Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.9 2001/03/22 04:00:12 momjian Exp $ * * Modifications - 28-Jun-2000 - pjw@rhyme.com.au * - * Initial version. + * Initial version. * * Modifications - 04-Jan-2001 - pjw@rhyme.com.au * - * - Check results of IO routines more carefully. + * - Check results of IO routines more carefully. * *------------------------------------------------------------------------- */ @@ -43,161 +43,165 @@ *-------- */ -static void _ArchiveEntry(ArchiveHandle* AH, TocEntry* te); -static void _StartData(ArchiveHandle* AH, TocEntry* te); -static int _WriteData(ArchiveHandle* AH, const void* data, int dLen); -static void _EndData(ArchiveHandle* AH, TocEntry* te); -static int _WriteByte(ArchiveHandle* AH, const int i); -static int _ReadByte(ArchiveHandle* ); -static int _WriteBuf(ArchiveHandle* AH, const void* buf, int len); -static int _ReadBuf(ArchiveHandle* AH, void* buf, int 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, int oid); -static void _EndBlob(ArchiveHandle* AH, TocEntry* te, int oid); -static void _EndBlobs(ArchiveHandle* AH, TocEntry* te); -static void _LoadBlobs(ArchiveHandle* AH); +static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te); +static void _StartData(ArchiveHandle *AH, TocEntry *te); +static int _WriteData(ArchiveHandle *AH, const void *data, int dLen); +static void _EndData(ArchiveHandle *AH, TocEntry *te); +static int _WriteByte(ArchiveHandle *AH, const int i); +static int _ReadByte(ArchiveHandle *); +static int _WriteBuf(ArchiveHandle *AH, const void *buf, int len); +static int _ReadBuf(ArchiveHandle *AH, void *buf, int 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, int oid); +static void _EndBlob(ArchiveHandle *AH, TocEntry *te, int 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 zlibOutSize 4096 #define zlibInSize 4096 -typedef struct { - z_streamp zp; - char* zlibOut; - char* zlibIn; - int inSize; - int hasSeek; - int filePos; - int dataStart; +typedef struct +{ + z_streamp zp; + char *zlibOut; + char *zlibIn; + int inSize; + int hasSeek; + int filePos; + int dataStart; } lclContext; -typedef struct { - int dataPos; - int dataLen; +typedef struct +{ + int dataPos; + int dataLen; } lclTocEntry; /*------ - * Static declarations + * 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 int _getFilePos(ArchiveHandle* AH, lclContext* ctx); -static int _DoDeflate(ArchiveHandle* AH, lclContext* ctx, int flush); +static void _readBlockHeader(ArchiveHandle *AH, int *type, int *id); +static void _StartDataCompressor(ArchiveHandle *AH, TocEntry *te); +static void _EndDataCompressor(ArchiveHandle *AH, TocEntry *te); +static int _getFilePos(ArchiveHandle *AH, lclContext *ctx); +static int _DoDeflate(ArchiveHandle *AH, lclContext *ctx, int flush); -static char* progname = "Archiver(custom)"; +static char *progname = "Archiver(custom)"; /* - * Init routine required by ALL formats. This is a global routine + * 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. + * and in the case of a read mode connection, it should load the Header & TOC. */ -void InitArchiveFmt_Custom(ArchiveHandle* AH) +void +InitArchiveFmt_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*)malloc(sizeof(lclContext)); - if (ctx == NULL) - die_horribly(AH, "%s: Unable to allocate archive context",progname); - AH->formatData = (void*)ctx; - - ctx->zp = (z_streamp)malloc(sizeof(z_stream)); - if (ctx->zp == NULL) - die_horribly(AH, "%s: unable to allocate zlib stream archive context",progname); + 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 *) malloc(sizeof(lclContext)); + if (ctx == NULL) + die_horribly(AH, "%s: Unable to allocate archive context", progname); + AH->formatData = (void *) ctx; + + ctx->zp = (z_streamp) malloc(sizeof(z_stream)); + if (ctx->zp == NULL) + die_horribly(AH, "%s: unable to allocate zlib stream archive context", progname); /* * 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. + * 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; + 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, "%s: unable to allocate buffers in archive context",progname); + if (ctx->zlibOut == NULL || ctx->zlibIn == NULL) + die_horribly(AH, "%s: unable to allocate buffers in archive context", progname); - /* - * Now open the file - */ - if (AH->mode == archModeWrite) { + /* + * Now open the file + */ + if (AH->mode == archModeWrite) + { - if (AH->fSpec && strcmp(AH->fSpec,"") != 0) { + if (AH->fSpec && strcmp(AH->fSpec, "") != 0) AH->FH = fopen(AH->fSpec, PG_BINARY_W); - } else { + else AH->FH = stdout; - } if (!AH->FH) - die_horribly(AH, "%s: unable to open archive file %s",progname, AH->fSpec); + die_horribly(AH, "%s: unable to open archive file %s", progname, AH->fSpec); ctx->hasSeek = (fseek(AH->FH, 0, SEEK_CUR) == 0); - } else { + } + else + { - if (AH->fSpec && strcmp(AH->fSpec,"") != 0) { + if (AH->fSpec && strcmp(AH->fSpec, "") != 0) AH->FH = fopen(AH->fSpec, PG_BINARY_R); - } else { + else AH->FH = stdin; - } if (!AH->FH) - die_horribly(AH, "%s: unable to open archive file %s",progname, AH->fSpec); + die_horribly(AH, "%s: unable to open archive file %s", progname, AH->fSpec); ctx->hasSeek = (fseek(AH->FH, 0, SEEK_CUR) == 0); ReadHead(AH); ReadToc(AH); ctx->dataStart = _getFilePos(AH, ctx); - } + } } @@ -206,20 +210,20 @@ void InitArchiveFmt_Custom(ArchiveHandle* AH) * * Optional. * - * Set up extrac format-related TOC data. + * Set up extrac format-related TOC data. */ -static void _ArchiveEntry(ArchiveHandle* AH, TocEntry* te) +static void +_ArchiveEntry(ArchiveHandle *AH, TocEntry *te) { - lclTocEntry* ctx; + lclTocEntry *ctx; - ctx = (lclTocEntry*)calloc(1, sizeof(lclTocEntry)); - if (te->dataDumper) { + ctx = (lclTocEntry *) calloc(1, sizeof(lclTocEntry)); + if (te->dataDumper) ctx->dataPos = -1; - } else { + else ctx->dataPos = 0; - } - ctx->dataLen = 0; - te->formatData = (void*)ctx; + ctx->dataLen = 0; + te->formatData = (void *) ctx; } @@ -232,12 +236,13 @@ static void _ArchiveEntry(ArchiveHandle* AH, TocEntry* te) * Use the Archiver routines to write data - they are non-endian, and * maintain other important file information. */ -static void _WriteExtraToc(ArchiveHandle* AH, TocEntry* te) +static void +_WriteExtraToc(ArchiveHandle *AH, TocEntry *te) { - lclTocEntry* ctx = (lclTocEntry*)te->formatData; + lclTocEntry *ctx = (lclTocEntry *) te->formatData; - WriteInt(AH, ctx->dataPos); - WriteInt(AH, ctx->dataLen); + WriteInt(AH, ctx->dataPos); + WriteInt(AH, ctx->dataLen); } /* @@ -248,17 +253,19 @@ static void _WriteExtraToc(ArchiveHandle* AH, TocEntry* te) * Needs to match the order defined in _WriteExtraToc, and sould also * use the Archiver input routines. */ -static void _ReadExtraToc(ArchiveHandle* AH, TocEntry* te) +static void +_ReadExtraToc(ArchiveHandle *AH, TocEntry *te) { - lclTocEntry* ctx = (lclTocEntry*)te->formatData; + lclTocEntry *ctx = (lclTocEntry *) te->formatData; - if (ctx == NULL) { - ctx = (lclTocEntry*)malloc(sizeof(lclTocEntry)); - te->formatData = (void*)ctx; - } + if (ctx == NULL) + { + ctx = (lclTocEntry *) malloc(sizeof(lclTocEntry)); + te->formatData = (void *) ctx; + } - ctx->dataPos = ReadInt( AH ); - ctx->dataLen = ReadInt( AH ); + ctx->dataPos = ReadInt(AH); + ctx->dataLen = ReadInt(AH); } /* @@ -268,11 +275,12 @@ static void _ReadExtraToc(ArchiveHandle* AH, TocEntry* te) * Optional. * */ -static void _PrintExtraToc(ArchiveHandle* AH, TocEntry* te) +static void +_PrintExtraToc(ArchiveHandle *AH, TocEntry *te) { - lclTocEntry* ctx = (lclTocEntry*)te->formatData; + lclTocEntry *ctx = (lclTocEntry *) te->formatData; - ahprintf(AH, "-- Data Pos: %d (Length %d)\n", ctx->dataPos, ctx->dataLen); + ahprintf(AH, "-- Data Pos: %d (Length %d)\n", ctx->dataPos, ctx->dataLen); } /* @@ -285,22 +293,23 @@ static void _PrintExtraToc(ArchiveHandle* AH, TocEntry* te) * Optional, but strongly recommended. * */ -static void _StartData(ArchiveHandle* AH, TocEntry* te) +static void +_StartData(ArchiveHandle *AH, TocEntry *te) { - lclContext* ctx = (lclContext*)AH->formatData; - lclTocEntry* tctx = (lclTocEntry*)te->formatData; + lclContext *ctx = (lclContext *) AH->formatData; + lclTocEntry *tctx = (lclTocEntry *) te->formatData; - tctx->dataPos = _getFilePos(AH, ctx); + tctx->dataPos = _getFilePos(AH, ctx); - _WriteByte(AH, BLK_DATA); /* Block type */ - WriteInt(AH, te->id); /* For sanity check */ + _WriteByte(AH, BLK_DATA); /* Block type */ + WriteInt(AH, te->id); /* For sanity check */ - _StartDataCompressor(AH, te); + _StartDataCompressor(AH, te); } /* - * Called by archiver when dumper calls WriteData. This routine is + * 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. * @@ -309,19 +318,21 @@ static void _StartData(ArchiveHandle* AH, TocEntry* te) * Mandatory. * */ -static int _WriteData(ArchiveHandle* AH, const void* data, int dLen) +static int +_WriteData(ArchiveHandle *AH, const void *data, int dLen) { - lclContext* ctx = (lclContext*)AH->formatData; - z_streamp zp = ctx->zp; + lclContext *ctx = (lclContext *) AH->formatData; + z_streamp zp = ctx->zp; - zp->next_in = (void*)data; - zp->avail_in = dLen; + zp->next_in = (void *) data; + zp->avail_in = dLen; - while (zp->avail_in != 0) { - /* printf("Deflating %d bytes\n", dLen); */ + while (zp->avail_in != 0) + { + /* printf("Deflating %d bytes\n", dLen); */ _DoDeflate(AH, ctx, 0); - } - return dLen; + } + return dLen; } /* @@ -331,35 +342,37 @@ static int _WriteData(ArchiveHandle* AH, const void* data, int dLen) * Optional. * */ -static void _EndData(ArchiveHandle* AH, TocEntry* te) +static void +_EndData(ArchiveHandle *AH, TocEntry *te) { - lclContext* ctx = (lclContext*)AH->formatData; - lclTocEntry* tctx = (lclTocEntry*) te->formatData; + lclContext *ctx = (lclContext *) AH->formatData; + lclTocEntry *tctx = (lclTocEntry *) te->formatData; - _EndDataCompressor(AH, te); + _EndDataCompressor(AH, te); - tctx->dataLen = _getFilePos(AH, ctx) - tctx->dataPos; + tctx->dataLen = _getFilePos(AH, ctx) - tctx->dataPos; } /* - * Called by the archiver when starting to save all BLOB DATA (not schema). + * 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. + * 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) +static void +_StartBlobs(ArchiveHandle *AH, TocEntry *te) { - lclContext* ctx = (lclContext*)AH->formatData; - lclTocEntry* tctx = (lclTocEntry*)te->formatData; + lclContext *ctx = (lclContext *) AH->formatData; + lclTocEntry *tctx = (lclTocEntry *) te->formatData; - tctx->dataPos = _getFilePos(AH, ctx); + tctx->dataPos = _getFilePos(AH, ctx); - _WriteByte(AH, BLK_BLOBS); /* Block type */ - WriteInt(AH, te->id); /* For sanity check */ + _WriteByte(AH, BLK_BLOBS); /* Block type */ + WriteInt(AH, te->id); /* For sanity check */ } @@ -370,13 +383,14 @@ static void _StartBlobs(ArchiveHandle* AH, TocEntry* te) * * Must save the passed OID for retrieval at restore-time. */ -static void _StartBlob(ArchiveHandle* AH, TocEntry* te, int oid) +static void +_StartBlob(ArchiveHandle *AH, TocEntry *te, int oid) { - if (oid == 0) - die_horribly(AH, "%s: illegal OID for BLOB (%d)\n", progname, oid); + if (oid == 0) + die_horribly(AH, "%s: illegal OID for BLOB (%d)\n", progname, oid); - WriteInt(AH, oid); - _StartDataCompressor(AH, te); + WriteInt(AH, oid); + _StartDataCompressor(AH, te); } /* @@ -385,91 +399,100 @@ static void _StartBlob(ArchiveHandle* AH, TocEntry* te, int oid) * Optional. * */ -static void _EndBlob(ArchiveHandle* AH, TocEntry* te, int oid) +static void +_EndBlob(ArchiveHandle *AH, TocEntry *te, int oid) { - _EndDataCompressor(AH, te); + _EndDataCompressor(AH, te); } /* - * Called by the archiver when finishing saving all BLOB DATA. + * Called by the archiver when finishing saving all BLOB DATA. * * Optional. * */ -static void _EndBlobs(ArchiveHandle* AH, TocEntry* te) +static void +_EndBlobs(ArchiveHandle *AH, TocEntry *te) { /* Write out a fake zero OID to mark end-of-blobs. */ - WriteInt(AH, 0); + WriteInt(AH, 0); } /* * Print data for a gievn TOC entry */ -static void _PrintTocData(ArchiveHandle* AH, TocEntry* te, RestoreOptions *ropt) +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; + lclContext *ctx = (lclContext *) AH->formatData; + int id; + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + int blkType; + int found = 0; - if (tctx->dataPos == 0) + if (tctx->dataPos == 0) return; - if (!ctx->hasSeek || tctx->dataPos < 0) { + if (!ctx->hasSeek || tctx->dataPos < 0) + { - /* Skip over unnecessary blocks until we get the one we want. */ + /* Skip over unnecessary blocks until we get the one we want. */ - found = 0; + found = 0; - _readBlockHeader(AH, &blkType, &id); + _readBlockHeader(AH, &blkType, &id); - while (id != te->id) { + while (id != te->id) + { - if ( (TocIDRequired(AH, id, ropt) & 2) != 0) - die_horribly(AH, "%s: Dumping a specific TOC data block out of order is not supported" - " without on this input stream (fseek required)\n", progname); + if ((TocIDRequired(AH, id, ropt) & 2) != 0) + die_horribly(AH, "%s: Dumping a specific TOC data block out of order is not supported" + " without on this input stream (fseek required)\n", progname); - switch (blkType) { + switch (blkType) + { - case BLK_DATA: + case BLK_DATA: - _skipData(AH); - break; + _skipData(AH); + break; - case BLK_BLOBS: + case BLK_BLOBS: - _skipBlobs(AH); - break; + _skipBlobs(AH); + break; - default: /* Always have a default */ + default: /* Always have a default */ - die_horribly(AH, "%s: unrecognized data block type while searching archive %d\n", - progname, blkType); - break; - } + die_horribly(AH, "%s: unrecognized data block type while searching archive %d\n", + progname, blkType); + break; + } - _readBlockHeader(AH, &blkType, &id); + _readBlockHeader(AH, &blkType, &id); - } + } - } else { + } + else + { - /* Grab it */ + /* Grab it */ if (fseek(AH->FH, tctx->dataPos, SEEK_SET) != 0) - die_horribly(AH, "%s: error %d in file seek\n",progname, errno); + die_horribly(AH, "%s: error %d in file seek\n", progname, errno); _readBlockHeader(AH, &blkType, &id); - } + } - /* Are we sane? */ - if (id != te->id) + /* Are we sane? */ + if (id != te->id) die_horribly(AH, "%s: Found unexpected block ID (%d) when reading data - expected %d\n", - progname, id, te->id); + progname, id, te->id); - switch (blkType) { + switch (blkType) + { case BLK_DATA: @@ -484,61 +507,67 @@ static void _PrintTocData(ArchiveHandle* AH, TocEntry* te, RestoreOptions *ropt) _LoadBlobs(AH); break; - default: /* Always have a default */ + default: /* Always have a default */ - die_horribly(AH, "%s: unrecognized data block type %d while restoring archive\n", - progname, blkType); + die_horribly(AH, "%s: unrecognized data block type %d while restoring archive\n", + progname, blkType); break; - } + } - ahprintf(AH, "\n\n"); + ahprintf(AH, "\n\n"); } /* * Print data from current file position. */ -static void _PrintData(ArchiveHandle* AH) +static void +_PrintData(ArchiveHandle *AH) { - lclContext* ctx = (lclContext*)AH->formatData; - z_streamp zp = ctx->zp; - int blkLen; - char* in = ctx->zlibIn; - int cnt; + lclContext *ctx = (lclContext *) AH->formatData; + z_streamp zp = ctx->zp; + int blkLen; + char *in = ctx->zlibIn; + int cnt; + #ifdef HAVE_LIBZ - int res; - char* out = ctx->zlibOut; + int res; + char *out = ctx->zlibOut; + #endif #ifdef HAVE_LIBZ - res = Z_OK; + res = Z_OK; - if (AH->compression != 0) { + if (AH->compression != 0) + { zp->zalloc = Z_NULL; zp->zfree = Z_NULL; zp->opaque = Z_NULL; if (inflateInit(zp) != Z_OK) die_horribly(AH, "%s: could not initialize compression library - %s\n", progname, zp->msg); - } + } #endif - blkLen = ReadInt(AH); - while (blkLen != 0) { - if (blkLen+1 > ctx->inSize) { + blkLen = ReadInt(AH); + while (blkLen != 0) + { + if (blkLen + 1 > ctx->inSize) + { free(ctx->zlibIn); ctx->zlibIn = NULL; - ctx->zlibIn = (char*)malloc(blkLen+1); + ctx->zlibIn = (char *) malloc(blkLen + 1); if (!ctx->zlibIn) die_horribly(AH, "%s: failed to allocate decompression buffer\n", progname); - ctx->inSize = blkLen+1; + ctx->inSize = blkLen + 1; in = ctx->zlibIn; } cnt = fread(in, 1, blkLen, AH->FH); - if (cnt != blkLen) + if (cnt != blkLen) die_horribly(AH, "%s: could not read data block - expected %d, got %d\n", progname, blkLen, cnt); ctx->filePos += blkLen; @@ -548,9 +577,11 @@ static void _PrintData(ArchiveHandle* AH) #ifdef HAVE_LIBZ - if (AH->compression != 0) { + if (AH->compression != 0) + { - while (zp->avail_in != 0) { + while (zp->avail_in != 0) + { zp->next_out = out; zp->avail_out = zlibOutSize; res = inflate(zp, 0); @@ -560,7 +591,9 @@ static void _PrintData(ArchiveHandle* AH) out[zlibOutSize - zp->avail_out] = '\0'; ahwrite(out, 1, zlibOutSize - zp->avail_out, AH); } - } else { + } + else + { #endif in[zp->avail_in] = '\0'; ahwrite(in, 1, zp->avail_in, AH); @@ -572,14 +605,15 @@ static void _PrintData(ArchiveHandle* AH) blkLen = ReadInt(AH); - } + } #ifdef HAVE_LIBZ - if (AH->compression != 0) - { + if (AH->compression != 0) + { zp->next_in = NULL; zp->avail_in = 0; - while (res != Z_STREAM_END) { + while (res != Z_STREAM_END) + { zp->next_out = out; zp->avail_out = zlibOutSize; res = inflate(zp, 0); @@ -589,25 +623,26 @@ static void _PrintData(ArchiveHandle* AH) out[zlibOutSize - zp->avail_out] = '\0'; ahwrite(out, 1, zlibOutSize - zp->avail_out, AH); } - } + } #endif } -static void _LoadBlobs(ArchiveHandle* AH) +static void +_LoadBlobs(ArchiveHandle *AH) { - int oid; + int oid; StartRestoreBlobs(AH); - oid = ReadInt(AH); - while(oid != 0) - { + oid = ReadInt(AH); + while (oid != 0) + { StartRestoreBlob(AH, oid); _PrintData(AH); EndRestoreBlob(AH, oid); oid = ReadInt(AH); - } + } EndRestoreBlobs(AH); @@ -616,49 +651,53 @@ static void _LoadBlobs(ArchiveHandle* AH) /* * Skip the BLOBs from the current file position. * BLOBS are written sequentially as data blocks (see below). - * Each BLOB is preceded by it's original OID. + * Each BLOB is preceded by it's original OID. * A zero OID indicated the end of the BLOBS */ -static void _skipBlobs(ArchiveHandle* AH) +static void +_skipBlobs(ArchiveHandle *AH) { - int oid; + int oid; - oid = ReadInt(AH); - while(oid != 0) - { + oid = ReadInt(AH); + while (oid != 0) + { _skipData(AH); oid = ReadInt(AH); - } + } } /* * Skip data from current file position. - * Data blocks are formatted as an integer length, followed by data. + * Data blocks are formatted as an integer length, followed by data. * A zero length denoted the end of the block. */ -static void _skipData(ArchiveHandle* AH) +static void +_skipData(ArchiveHandle *AH) { - lclContext* ctx = (lclContext*)AH->formatData; - int blkLen; - char* in = ctx->zlibIn; - int cnt; - - blkLen = ReadInt(AH); - while (blkLen != 0) { - if (blkLen > ctx->inSize) { - free(ctx->zlibIn); - ctx->zlibIn = (char*)malloc(blkLen); - ctx->inSize = blkLen; - in = ctx->zlibIn; - } - cnt = fread(in, 1, blkLen, AH->FH); - if (cnt != blkLen) - die_horribly(AH, "%s: could not read data block - expected %d, got %d\n", progname, blkLen, cnt); - - ctx->filePos += blkLen; + lclContext *ctx = (lclContext *) AH->formatData; + int blkLen; + char *in = ctx->zlibIn; + int cnt; blkLen = ReadInt(AH); - } + while (blkLen != 0) + { + if (blkLen > ctx->inSize) + { + free(ctx->zlibIn); + ctx->zlibIn = (char *) malloc(blkLen); + ctx->inSize = blkLen; + in = ctx->zlibIn; + } + cnt = fread(in, 1, blkLen, AH->FH); + if (cnt != blkLen) + die_horribly(AH, "%s: could not read data block - expected %d, got %d\n", progname, blkLen, cnt); + + ctx->filePos += blkLen; + + blkLen = ReadInt(AH); + } } @@ -671,18 +710,18 @@ static void _skipData(ArchiveHandle* AH) * These routines are only used to read & write headers & TOC. * */ -static int _WriteByte(ArchiveHandle* AH, const int i) +static int +_WriteByte(ArchiveHandle *AH, const int i) { - lclContext* ctx = (lclContext*)AH->formatData; - int res; + lclContext *ctx = (lclContext *) AH->formatData; + int res; - res = fputc(i, AH->FH); - if (res != EOF) { + res = fputc(i, AH->FH); + if (res != EOF) ctx->filePos += 1; - } else { - die_horribly(AH, "%s: could not write byte./n",progname); - } - return res; + else + die_horribly(AH, "%s: could not write byte./n", progname); + return res; } /* @@ -694,16 +733,16 @@ static int _WriteByte(ArchiveHandle* AH, const int i) * These routines are only used to read & write headers & TOC. * */ -static int _ReadByte(ArchiveHandle* AH) +static int +_ReadByte(ArchiveHandle *AH) { - lclContext* ctx = (lclContext*)AH->formatData; - int res; + lclContext *ctx = (lclContext *) AH->formatData; + int res; - res = fgetc(AH->FH); - if (res != EOF) { + res = fgetc(AH->FH); + if (res != EOF) ctx->filePos += 1; - } - return res; + return res; } /* @@ -715,35 +754,39 @@ static int _ReadByte(ArchiveHandle* AH) * These routines are only used to read & write headers & TOC. * */ -static int _WriteBuf(ArchiveHandle* AH, const void* buf, int len) +static int +_WriteBuf(ArchiveHandle *AH, const void *buf, int len) { - lclContext* ctx = (lclContext*)AH->formatData; - int res; - res = fwrite(buf, 1, len, AH->FH); + lclContext *ctx = (lclContext *) AH->formatData; + int res; + + res = fwrite(buf, 1, len, AH->FH); if (res != len) die_horribly(AH, "%s: write error in _WriteBuf (%d != %d)\n", progname, res, len); - ctx->filePos += res; - return res; + ctx->filePos += res; + return res; } /* * Read a block of bytes from the archive. - * + * * Mandatory. - * + * * Called by the archiver to read a block of bytes from the archive * These routines are only used to read & write headers & TOC. * */ -static int _ReadBuf(ArchiveHandle* AH, void* buf, int len) +static int +_ReadBuf(ArchiveHandle *AH, void *buf, int len) { - lclContext* ctx = (lclContext*)AH->formatData; - int res; - res = fread(buf, 1, len, AH->FH); - ctx->filePos += res; - return res; + lclContext *ctx = (lclContext *) AH->formatData; + int res; + + res = fread(buf, 1, len, AH->FH); + ctx->filePos += res; + return res; } /* @@ -751,42 +794,47 @@ static int _ReadBuf(ArchiveHandle* AH, void* buf, int len) * * Mandatory. * - * When writing the archive, this is the routine that actually starts - * the process of saving it to files. No data should be written prior + * When writing the archive, this is the routine that actually starts + * the process of saving it to files. No data should be written prior * to this point, since the user could sort the TOC after creating it. - * + * * If an archive is to be written, this toutine must call: * WriteHead to save the archive header - * WriteToc to save the TOC entries + * WriteToc to save the TOC entries * WriteDataChunks to save all DATA & BLOBs. * */ -static void _CloseArchive(ArchiveHandle* AH) +static void +_CloseArchive(ArchiveHandle *AH) { - lclContext* ctx = (lclContext*)AH->formatData; - int tpos; + lclContext *ctx = (lclContext *) AH->formatData; + int tpos; - if (AH->mode == archModeWrite) { + if (AH->mode == archModeWrite) + { WriteHead(AH); tpos = ftell(AH->FH); WriteToc(AH); ctx->dataStart = _getFilePos(AH, ctx); WriteDataChunks(AH); - /* This is not an essential operation - it is really only - * needed if we expect to be doing seeks to read the data back - * - it may be ok to just use the existing self-consistent block + + /* + * This is not an essential operation - it is really only needed + * if we expect to be doing seeks to read the data back - it may + * be ok to just use the existing self-consistent block * formatting. - */ - if (ctx->hasSeek) { + */ + if (ctx->hasSeek) + { fseek(AH->FH, tpos, SEEK_SET); WriteToc(AH); } - } + } - if (fclose(AH->FH) != 0) - die_horribly(AH, "%s: could not close archive file\n",progname); + if (fclose(AH->FH) != 0) + die_horribly(AH, "%s: could not close archive file\n", progname); - AH->FH = NULL; + AH->FH = NULL; } /*-------------------------------------------------- @@ -797,32 +845,35 @@ static void _CloseArchive(ArchiveHandle* AH) /* * Get the current position in the archive file. */ -static int _getFilePos(ArchiveHandle* AH, lclContext* ctx) +static int +_getFilePos(ArchiveHandle *AH, lclContext *ctx) { - int pos; - if (ctx->hasSeek) { + int pos; + + if (ctx->hasSeek) + { pos = ftell(AH->FH); - if (pos != ctx->filePos) { + if (pos != ctx->filePos) fprintf(stderr, "Warning: ftell mismatch with filePos\n"); - } - } else { + } + else pos = ctx->filePos; - } - return pos; + return pos; } /* - * Read a data block header. The format changed in V1.3, so we + * Read a data block header. The format changed in V1.3, so we * put the code here for simplicity. */ -static void _readBlockHeader(ArchiveHandle *AH, int *type, int *id) +static void +_readBlockHeader(ArchiveHandle *AH, int *type, int *id) { - if (AH->version < K_VERS_1_3) + if (AH->version < K_VERS_1_3) *type = BLK_DATA; - else + else *type = _ReadByte(AH);; - *id = ReadInt(AH); + *id = ReadInt(AH); } /* @@ -830,35 +881,36 @@ static void _readBlockHeader(ArchiveHandle *AH, int *type, int *id) * StartData & StartBlob. The buffers are setup in the Init routine. * */ -static void _StartDataCompressor(ArchiveHandle* AH, TocEntry* te) +static void +_StartDataCompressor(ArchiveHandle *AH, TocEntry *te) { - lclContext* ctx = (lclContext*)AH->formatData; - z_streamp zp = ctx->zp; + lclContext *ctx = (lclContext *) AH->formatData; + z_streamp zp = ctx->zp; #ifdef HAVE_LIBZ - if (AH->compression < 0 || AH->compression > 9) { + if (AH->compression < 0 || AH->compression > 9) AH->compression = Z_DEFAULT_COMPRESSION; - } - if (AH->compression != 0) { - zp->zalloc = Z_NULL; - zp->zfree = Z_NULL; - zp->opaque = Z_NULL; + if (AH->compression != 0) + { + zp->zalloc = Z_NULL; + zp->zfree = Z_NULL; + zp->opaque = Z_NULL; - if (deflateInit(zp, AH->compression) != Z_OK) - die_horribly(AH, "%s: could not initialize compression library - %s\n",progname, zp->msg); - } + if (deflateInit(zp, AH->compression) != Z_OK) + die_horribly(AH, "%s: could not initialize compression library - %s\n", progname, zp->msg); + } #else - AH->compression = 0; + AH->compression = 0; #endif - /* Just be paranoid - maybe End is called after Start, with no Write */ - zp->next_out = ctx->zlibOut; - zp->avail_out = zlibOutSize; + /* Just be paranoid - maybe End is called after Start, with no Write */ + zp->next_out = ctx->zlibOut; + zp->avail_out = zlibOutSize; } /* @@ -867,42 +919,51 @@ static void _StartDataCompressor(ArchiveHandle* AH, TocEntry* te) * In the case of Z0, or no zlib, just write the raw data. * */ -static int _DoDeflate(ArchiveHandle* AH, lclContext* ctx, int flush) +static int +_DoDeflate(ArchiveHandle *AH, lclContext *ctx, int flush) { - z_streamp zp = ctx->zp; + z_streamp zp = ctx->zp; #ifdef HAVE_LIBZ - char* out = ctx->zlibOut; - int res = Z_OK; + char *out = ctx->zlibOut; + int res = Z_OK; - if (AH->compression != 0) - { + if (AH->compression != 0) + { res = deflate(zp, flush); if (res == Z_STREAM_ERROR) - die_horribly(AH, "%s: could not compress data - %s\n",progname, zp->msg); + die_horribly(AH, "%s: could not compress data - %s\n", progname, zp->msg); - if ( ( (flush == Z_FINISH) && (zp->avail_out < zlibOutSize) ) - || (zp->avail_out == 0) + if (((flush == Z_FINISH) && (zp->avail_out < zlibOutSize)) + || (zp->avail_out == 0) || (zp->avail_in != 0) - ) + ) { + /* - * Extra paranoia: avoid zero-length chunks since a zero + * Extra paranoia: avoid zero-length chunks since a zero * length chunk is the EOF marker. This should never happen - * but... + * but... */ - if (zp->avail_out < zlibOutSize) { - /* printf("Wrote %d byte deflated chunk\n", zlibOutSize - zp->avail_out); */ + if (zp->avail_out < zlibOutSize) + { + + /* + * printf("Wrote %d byte deflated chunk\n", zlibOutSize - + * zp->avail_out); + */ WriteInt(AH, zlibOutSize - zp->avail_out); if (fwrite(out, 1, zlibOutSize - zp->avail_out, AH->FH) != (zlibOutSize - zp->avail_out)) - die_horribly(AH, "%s: could not write compressed chunk\n",progname); + die_horribly(AH, "%s: could not write compressed chunk\n", progname); ctx->filePos += zlibOutSize - zp->avail_out; } zp->next_out = out; zp->avail_out = zlibOutSize; } - } else { + } + else #endif + { if (zp->avail_in > 0) { WriteInt(AH, zp->avail_in); @@ -910,7 +971,9 @@ static int _DoDeflate(ArchiveHandle* AH, lclContext* ctx, int flush) die_horribly(AH, "%s: could not write uncompressed chunk\n", progname); ctx->filePos += zp->avail_in; zp->avail_in = 0; - } else { + } + else + { #ifdef HAVE_LIBZ if (flush == Z_FINISH) res = Z_STREAM_END; @@ -918,35 +981,37 @@ static int _DoDeflate(ArchiveHandle* AH, lclContext* ctx, int flush) } -#ifdef HAVE_LIBZ - } + } - return res; +#ifdef HAVE_LIBZ + return res; #else - return 1; + return 1; #endif } /* - * Terminate zlib context and flush it's buffers. If no zlib + * Terminate zlib context and flush it's buffers. If no zlib * then just return. * */ -static void _EndDataCompressor(ArchiveHandle* AH, TocEntry* te) +static void +_EndDataCompressor(ArchiveHandle *AH, TocEntry *te) { #ifdef HAVE_LIBZ - lclContext* ctx = (lclContext*)AH->formatData; - z_streamp zp = ctx->zp; - int res; + lclContext *ctx = (lclContext *) AH->formatData; + z_streamp zp = ctx->zp; + int res; - if (AH->compression != 0) - { + if (AH->compression != 0) + { zp->next_in = NULL; zp->avail_in = 0; - do { + do + { /* printf("Ending data output\n"); */ res = _DoDeflate(AH, ctx, Z_FINISH); } while (res != Z_STREAM_END); @@ -959,5 +1024,3 @@ static void _EndDataCompressor(ArchiveHandle* AH, TocEntry* te) /* Send the end marker */ WriteInt(AH, 0); } - - |