diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 1673a8dd4f..e25cf56756 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -118,7 +118,7 @@ void RestoreArchive(Archive* AHX, RestoreOptions *ropt) _printTocEntry(AH, te, ropt); if (AH->PrintTocDataPtr != NULL && (reqs & 2) != 0) { -#ifndef HAVE_ZLIB +#ifndef HAVE_LIBZ if (AH->compression != 0) die_horribly("%s: Unable to restore data from a compressed archive\n", progname); #endif @@ -415,11 +415,14 @@ int archprintf(Archive* AH, const char *fmt, ...) { char *p = NULL; va_list ap; - int bSize = strlen(fmt) + 1024; + int bSize = strlen(fmt) + 256; int cnt = -1; va_start(ap, fmt); - while (cnt < 0) { + + /* This is paranoid: deal with the possibility that vsnprintf is willing to ignore trailing null */ + /* or returns > 0 even if string does not fit. It may be the case that it returns cnt = bufsize */ + while (cnt < 0 || cnt >= (bSize-1) ) { if (p != NULL) free(p); bSize *= 2; if ((p = malloc(bSize)) == NULL) @@ -443,7 +446,7 @@ int archprintf(Archive* AH, const char *fmt, ...) OutputContext SetOutput(ArchiveHandle* AH, char *filename, int compression) { OutputContext sav; -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ char fmode[10]; #endif int fn = 0; @@ -464,7 +467,7 @@ OutputContext SetOutput(ArchiveHandle* AH, char *filename, int compression) } /* If compression explicitly requested, use gzopen */ -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ if (compression != 0) { sprintf(fmode, "wb%d", compression); @@ -482,7 +485,7 @@ OutputContext SetOutput(ArchiveHandle* AH, char *filename, int compression) AH->OF = fopen(filename, PG_BINARY_W); } AH->gzOut = 0; -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ } #endif @@ -509,11 +512,13 @@ int ahprintf(ArchiveHandle* AH, const char *fmt, ...) { char *p = NULL; va_list ap; - int bSize = strlen(fmt) + 1024; /* Should be enough */ + int bSize = strlen(fmt) + 256; /* Should be enough */ int cnt = -1; va_start(ap, fmt); - while (cnt < 0) { + /* This is paranoid: deal with the possibility that vsnprintf is willing to ignore trailing null */ + /* or returns > 0 even if string does not fit. It may be the case that it returns cnt = bufsize */ + while (cnt < 0 || cnt >= (bSize - 1) ) { if (p != NULL) free(p); bSize *= 2; p = (char*)malloc(bSize); @@ -681,6 +686,7 @@ int _discoverArchiveFormat(ArchiveHandle* AH) int cnt; int wantClose = 0; + if (AH->fSpec) { wantClose = 1; fh = fopen(AH->fSpec, PG_BINARY_R); @@ -693,16 +699,11 @@ int _discoverArchiveFormat(ArchiveHandle* AH) cnt = fread(sig, 1, 5, fh); - if (cnt != 5) { - fprintf(stderr, "Archiver: input file is too short, or is unreadable\n"); - exit(1); - } + if (cnt != 5) + die_horribly("%s: input file is too short, or is unreadable\n", progname); if (strncmp(sig, "PGDMP", 5) != 0) - { - fprintf(stderr, "Archiver: input file does not appear to be a valid archive\n"); - exit(1); - } + die_horribly("%s: input file does not appear to be a valid archive\n", progname); AH->vmaj = fgetc(fh); AH->vmin = fgetc(fh); @@ -739,7 +740,7 @@ static ArchiveHandle* _allocAH(const char* FileSpec, ArchiveFormat fmt, int compression, ArchiveMode mode) { ArchiveHandle* AH; - AH = (ArchiveHandle*)malloc(sizeof(ArchiveHandle)); + AH = (ArchiveHandle*)calloc(1, sizeof(ArchiveHandle)); if (!AH) die_horribly("Archiver: Could not allocate archive handle\n"); @@ -759,7 +760,7 @@ static ArchiveHandle* _allocAH(const char* FileSpec, ArchiveFormat fmt, AH->currToc = NULL; AH->currUser = ""; - AH->toc = (TocEntry*)malloc(sizeof(TocEntry)); + AH->toc = (TocEntry*)calloc(1, sizeof(TocEntry)); if (!AH->toc) die_horribly("Archiver: Could not allocate TOC header\n"); @@ -996,7 +997,7 @@ void WriteHead(ArchiveHandle* AH) (*AH->WriteBytePtr)(AH, AH->intSize); (*AH->WriteBytePtr)(AH, AH->format); -#ifndef HAVE_ZLIB +#ifndef HAVE_LIBZ if (AH->compression != 0) fprintf(stderr, "%s: WARNING - requested compression not available in this installation - " "archive will be uncompressed \n", progname); @@ -1016,44 +1017,44 @@ void ReadHead(ArchiveHandle* AH) char tmpMag[7]; int fmt; - if (AH->readHeader) - return; + if (!AH->readHeader) { - (*AH->ReadBufPtr)(AH, tmpMag, 5); + (*AH->ReadBufPtr)(AH, tmpMag, 5); - if (strncmp(tmpMag,"PGDMP", 5) != 0) - die_horribly("Archiver: Did not fing magic PGDMP in file header\n"); + if (strncmp(tmpMag,"PGDMP", 5) != 0) + die_horribly("Archiver: Did not fing magic PGDMP in file header\n"); - AH->vmaj = (*AH->ReadBytePtr)(AH); - AH->vmin = (*AH->ReadBytePtr)(AH); + AH->vmaj = (*AH->ReadBytePtr)(AH); + AH->vmin = (*AH->ReadBytePtr)(AH); - if (AH->vmaj > 1 || ( (AH->vmaj == 1) && (AH->vmin > 0) ) ) /* Version > 1.0 */ - { - AH->vrev = (*AH->ReadBytePtr)(AH); - } else { - AH->vrev = 0; + if (AH->vmaj > 1 || ( (AH->vmaj == 1) && (AH->vmin > 0) ) ) /* Version > 1.0 */ + { + AH->vrev = (*AH->ReadBytePtr)(AH); + } else { + AH->vrev = 0; + } + + AH->version = ( (AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev ) * 256 + 0; + + + if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX) + die_horribly("Archiver: unsupported version (%d.%d) in file header\n", AH->vmaj, AH->vmin); + + AH->intSize = (*AH->ReadBytePtr)(AH); + if (AH->intSize > 32) + die_horribly("Archiver: sanity check on integer size (%d) failes\n", AH->intSize); + + if (AH->intSize > sizeof(int)) + fprintf(stderr, "\nWARNING: Backup file was made on a machine with larger integers, " + "some operations may fail\n"); + + fmt = (*AH->ReadBytePtr)(AH); + + if (AH->format != fmt) + die_horribly("Archiver: expected format (%d) differs from format found in file (%d)\n", + AH->format, fmt); } - AH->version = ( (AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev ) * 256 + 0; - - - if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX) - die_horribly("Archiver: unsupported version (%d.%d) in file header\n", AH->vmaj, AH->vmin); - - AH->intSize = (*AH->ReadBytePtr)(AH); - if (AH->intSize > 32) - die_horribly("Archiver: sanity check on integer size (%d) failes\n", AH->intSize); - - if (AH->intSize > sizeof(int)) - fprintf(stderr, "\nWARNING: Backup file was made on a machine with larger integers, " - "some operations may fail\n"); - - fmt = (*AH->ReadBytePtr)(AH); - - if (AH->format != fmt) - die_horribly("Archiver: expected format (%d) differs from format found in file (%d)\n", - AH->format, fmt); - if (AH->version >= K_VERS_1_2) { AH->compression = (*AH->ReadBytePtr)(AH); @@ -1061,8 +1062,9 @@ void ReadHead(ArchiveHandle* AH) AH->compression = Z_DEFAULT_COMPRESSION; } -#ifndef HAVE_ZLIB - fprintf(stderr, "%s: WARNING - archive is compressed - any data will not be available\n", progname); +#ifndef HAVE_LIBZ + if (AH->compression != 0) + fprintf(stderr, "%s: WARNING - archive is compressed - any data will not be available\n", progname); #endif } diff --git a/src/bin/pg_dump/pg_backup_archiver.h b/src/bin/pg_dump/pg_backup_archiver.h index 6b0fd47f87..2153f3e707 100644 --- a/src/bin/pg_dump/pg_backup_archiver.h +++ b/src/bin/pg_dump/pg_backup_archiver.h @@ -30,7 +30,7 @@ #include -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ #include #define GZCLOSE(fh) gzclose(fh) #define GZWRITE(p, s, n, fh) gzwrite(fh, p, n * s) @@ -54,7 +54,7 @@ typedef z_stream *z_streamp; #define K_VERS_MAJOR 1 #define K_VERS_MINOR 2 -#define K_VERS_REV 0 +#define K_VERS_REV 1 /* Some important version numbers (checked in code) */ #define K_VERS_1_0 (( (1 * 256 + 0) * 256 + 0) * 256 + 0) diff --git a/src/bin/pg_dump/pg_backup_custom.c b/src/bin/pg_dump/pg_backup_custom.c index 95483f0e9f..3edbb751f9 100644 --- a/src/bin/pg_dump/pg_backup_custom.c +++ b/src/bin/pg_dump/pg_backup_custom.c @@ -200,7 +200,7 @@ static void _StartData(ArchiveHandle* AH, TocEntry* te) WriteInt(AH, te->id); /* For sanity check */ -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ if (AH->compression < 0 || AH->compression > 9) { AH->compression = Z_DEFAULT_COMPRESSION; @@ -230,7 +230,7 @@ static int _DoDeflate(ArchiveHandle* AH, lclContext* ctx, int flush) { z_streamp zp = ctx->zp; -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ char* out = ctx->zlibOut; int res = Z_OK; @@ -268,14 +268,14 @@ static int _DoDeflate(ArchiveHandle* AH, lclContext* ctx, int flush) ctx->filePos += zp->avail_in; zp->avail_in = 0; } else { -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ if (flush == Z_FINISH) res = Z_STREAM_END; #endif } -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ } return res; @@ -305,7 +305,7 @@ static void _EndData(ArchiveHandle* AH, TocEntry* te) lclContext* ctx = (lclContext*)AH->formatData; lclTocEntry* tctx = (lclTocEntry*) te->formatData; -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ z_streamp zp = ctx->zp; int res; @@ -385,7 +385,7 @@ static void _PrintData(ArchiveHandle* AH) char* in = ctx->zlibIn; int cnt; -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ int res; char* out = ctx->zlibOut; @@ -424,7 +424,7 @@ static void _PrintData(ArchiveHandle* AH) zp->next_in = in; zp->avail_in = blkLen; -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ if (AH->compression != 0) { @@ -443,14 +443,14 @@ static void _PrintData(ArchiveHandle* AH) ahwrite(in, 1, zp->avail_in, AH); zp->avail_in = 0; -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ } #endif blkLen = ReadInt(AH); } -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ if (AH->compression != 0) { zp->next_in = NULL; diff --git a/src/bin/pg_dump/pg_backup_files.c b/src/bin/pg_dump/pg_backup_files.c index bbf30de9aa..ef2ea57f2c 100644 --- a/src/bin/pg_dump/pg_backup_files.c +++ b/src/bin/pg_dump/pg_backup_files.c @@ -54,7 +54,7 @@ typedef struct { } lclContext; typedef struct { -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ gzFile *FH; #else FILE *FH; @@ -133,7 +133,7 @@ static void _ArchiveEntry(ArchiveHandle* AH, TocEntry* te) ctx = (lclTocEntry*)malloc(sizeof(lclTocEntry)); if (te->dataDumper) { -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ if (AH->compression == 0) { sprintf(fn, "%d.dat", te->id); } else { @@ -192,7 +192,7 @@ static void _StartData(ArchiveHandle* AH, TocEntry* te) sprintf(fmode, "wb%d", AH->compression); -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ tctx->FH = gzopen(tctx->filename, fmode); #else tctx->FH = fopen(tctx->filename, PG_BINARY_W); @@ -229,7 +229,7 @@ static void _PrintTocData(ArchiveHandle* AH, TocEntry* te, RestoreOptions *ropt) if (!tctx->filename) return; -#ifdef HAVE_ZLIB +#ifdef HAVE_LIBZ AH->FH = gzopen(tctx->filename,"rb"); #else AH->FH = fopen(tctx->filename,PG_BINARY_R); diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index a5fbdf8019..d444cb743f 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -22,7 +22,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.156 2000/07/04 16:57:49 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.157 2000/07/06 18:39:39 wieck Exp $ * * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb * @@ -3202,10 +3202,9 @@ dumpTables(Archive *fout, TableInfo *tblinfo, int numTables, { /* Skip VIEW relations */ - - /* - * if (isViewRule(tblinfo[i].relname)) continue; - */ + + /* if (isViewRule(tblinfo[i].relname)) continue; */ + parentRels = tblinfo[i].parentRels; numParents = tblinfo[i].numParents; diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 7097a8e069..7b1a17c3f7 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -108,7 +108,7 @@ int main(int argc, char **argv) char *progname; int c; Archive* AH; - char *fileSpec; + char *fileSpec = NULL; opts = NewRestoreOptions();