Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 06ef1ef

Browse files
committed
- Check ntuples == 1 for various SELECT statements.
- Fix handling of --tables=* (multiple tables never worked properly, AFAICT) - strdup() the current user in DB routines - Check results of IO routines more carefully. - Check results of PQ routines more carefully. Have not fixed index output yet.
1 parent 565639c commit 06ef1ef

8 files changed

+257
-71
lines changed

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
* Modifications - 30-Oct-2000 - pjw@rhyme.com.au
2828
* Added {Start,End}RestoreBlobs to allow extended TX during BLOB restore.
2929
*
30+
* Modifications - 04-Jan-2001 - pjw@rhyme.com.au
31+
* - strdup() the current user just in case it's deallocated from it's TOC
32+
* entry. Should *never* happen, but that's what they said about the
33+
* Titanic...
34+
*
35+
* - Check results of IO routines more carefully.
36+
*
3037
*-------------------------------------------------------------------------
3138
*/
3239

@@ -99,14 +106,18 @@ Archive* OpenArchive(const char* FileSpec, const ArchiveFormat fmt)
99106
/* Public */
100107
void CloseArchive(Archive* AHX)
101108
{
109+
int res = 0;
102110
ArchiveHandle* AH = (ArchiveHandle*)AHX;
103111
(*AH->ClosePtr)(AH);
104112

105113
/* Close the output */
106114
if (AH->gzOut)
107-
GZCLOSE(AH->OF);
115+
res = GZCLOSE(AH->OF);
108116
else if (AH->OF != stdout)
109-
fclose(AH->OF);
117+
res = fclose(AH->OF);
118+
119+
if (res != 0)
120+
die_horribly(AH, "%s: could not close the output file in CloseArchive\n", progname);
110121
}
111122

112123
/* Public */
@@ -791,8 +802,8 @@ void SortTocFromFile(Archive* AHX, RestoreOptions *ropt)
791802

792803
/* Setup the file */
793804
fh = fopen(ropt->tocFile, PG_BINARY_R);
794-
if (!fh)
795-
die_horribly(AH, "%s: could not open TOC file\n", progname);
805+
if (!fh)
806+
die_horribly(AH, "%s: could not open TOC file\n", progname);
796807

797808
while (fgets(buf, 1024, fh) != NULL)
798809
{
@@ -828,7 +839,8 @@ void SortTocFromFile(Archive* AHX, RestoreOptions *ropt)
828839
tePrev = te;
829840
}
830841

831-
fclose(fh);
842+
if (fclose(fh) != 0)
843+
die_horribly(AH, "%s: could not close TOC file\n", progname);
832844
}
833845

834846
/**********************
@@ -906,34 +918,42 @@ OutputContext SetOutput(ArchiveHandle* AH, char *filename, int compression)
906918
#ifdef HAVE_LIBZ
907919
if (compression != 0)
908920
{
909-
sprintf(fmode, "wb%d", compression);
910-
if (fn) {
911-
AH->OF = gzdopen(dup(fn), fmode); /* Don't use PG_BINARY_x since this is zlib */
912-
} else {
913-
AH->OF = gzopen(filename, fmode);
914-
}
915-
AH->gzOut = 1;
921+
sprintf(fmode, "wb%d", compression);
922+
if (fn) {
923+
AH->OF = gzdopen(dup(fn), fmode); /* Don't use PG_BINARY_x since this is zlib */
924+
} else {
925+
AH->OF = gzopen(filename, fmode);
926+
}
927+
AH->gzOut = 1;
916928
} else { /* Use fopen */
917929
#endif
918-
if (fn) {
919-
AH->OF = fdopen(dup(fn), PG_BINARY_W);
920-
} else {
921-
AH->OF = fopen(filename, PG_BINARY_W);
922-
}
923-
AH->gzOut = 0;
930+
if (fn) {
931+
AH->OF = fdopen(dup(fn), PG_BINARY_W);
932+
} else {
933+
AH->OF = fopen(filename, PG_BINARY_W);
934+
}
935+
AH->gzOut = 0;
924936
#ifdef HAVE_LIBZ
925937
}
926938
#endif
927939

940+
if (!AH->OF)
941+
die_horribly(AH, "%s: could not set output\n", progname);
942+
928943
return sav;
929944
}
930945

931946
void ResetOutput(ArchiveHandle* AH, OutputContext sav)
932947
{
948+
int res;
949+
933950
if (AH->gzOut)
934-
GZCLOSE(AH->OF);
951+
res = GZCLOSE(AH->OF);
935952
else
936-
fclose(AH->OF);
953+
res = fclose(AH->OF);
954+
955+
if (res != 0)
956+
die_horribly(AH, "%s: could not reset the output file\n", progname);
937957

938958
AH->gzOut = sav.gzOut;
939959
AH->OF = sav.OF;
@@ -1012,19 +1032,34 @@ int ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle* AH)
10121032
return res;
10131033
}
10141034
else if (AH->gzOut)
1015-
return GZWRITE((void*)ptr, size, nmemb, AH->OF);
1035+
{
1036+
res = GZWRITE((void*)ptr, size, nmemb, AH->OF);
1037+
if (res != (nmemb * size))
1038+
die_horribly(AH, "%s: could not write to archive\n", progname);
1039+
return res;
1040+
}
10161041
else if (AH->CustomOutPtr)
1017-
return AH->CustomOutPtr(AH, ptr, size * nmemb);
1042+
{
1043+
res = AH->CustomOutPtr(AH, ptr, size * nmemb);
1044+
if (res != (nmemb * size))
1045+
die_horribly(AH, "%s: could not write to custom output routine\n", progname);
1046+
return res;
1047+
}
10181048
else
10191049
{
10201050
/*
10211051
* If we're doing a restore, and it's direct to DB, and we're connected
10221052
* then send it to the DB.
10231053
*/
10241054
if (RestoringToDB(AH))
1025-
return ExecuteSqlCommandBuf(AH, (void*)ptr, size*nmemb);
1055+
return ExecuteSqlCommandBuf(AH, (void*)ptr, size*nmemb); /* Always 1, currently */
10261056
else
1027-
return fwrite((void*)ptr, size, nmemb, AH->OF);
1057+
{
1058+
res = fwrite((void*)ptr, size, nmemb, AH->OF);
1059+
if (res != nmemb)
1060+
die_horribly(AH, "%s: could not write to output file (%d != %d)\n", progname, res, nmemb);
1061+
return res;
1062+
}
10281063
}
10291064
}
10301065

@@ -1299,7 +1334,8 @@ _discoverArchiveFormat(ArchiveHandle* AH)
12991334

13001335
/* Close the file */
13011336
if (wantClose)
1302-
fclose(fh);
1337+
if (fclose(fh) != 0)
1338+
die_horribly(AH, "%s: could not close the input file after reading header\n", progname);
13031339

13041340
return AH->format;
13051341
}
@@ -1342,7 +1378,7 @@ static ArchiveHandle* _allocAH(const char* FileSpec, const ArchiveFormat fmt,
13421378
AH->fSpec = NULL;
13431379
}
13441380

1345-
AH->currUser = "";
1381+
AH->currUser = strdup(""); /* So it's valid, but we can free() it later if necessary */
13461382

13471383
AH->toc = (TocEntry*)calloc(1, sizeof(TocEntry));
13481384
if (!AH->toc)
@@ -1455,7 +1491,7 @@ void WriteToc(ArchiveHandle* AH)
14551491
if (AH->WriteExtraTocPtr) {
14561492
(*AH->WriteExtraTocPtr)(AH, te);
14571493
}
1458-
te = te->next;
1494+
te = te->next;
14591495
}
14601496
}
14611497

@@ -1585,7 +1621,12 @@ static void _reconnectAsUser(ArchiveHandle* AH, const char *dbname, char *user)
15851621
{
15861622
ahprintf(AH, "\\connect %s %s\n", dbname, user);
15871623
}
1588-
AH->currUser = user;
1624+
if (AH->currUser)
1625+
{
1626+
free(AH->currUser);
1627+
}
1628+
1629+
AH->currUser = strdup(user);
15891630
}
15901631
}
15911632

src/bin/pg_dump/pg_backup_archiver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#define GZREAD(p, s, n, fh) gzread(fh, p, n * s)
4545
#else
4646
#define GZCLOSE(fh) fclose(fh)
47-
#define GZWRITE(p, s, n, fh) fwrite(p, s, n, fh)
47+
#define GZWRITE(p, s, n, fh) (fwrite(p, s, n, fh) * s)
4848
#define GZREAD(p, s, n, fh) fread(p, s, n, fh)
4949
#define Z_DEFAULT_COMPRESSION -1
5050

@@ -62,7 +62,7 @@ typedef z_stream *z_streamp;
6262

6363
#define K_VERS_MAJOR 1
6464
#define K_VERS_MINOR 4
65-
#define K_VERS_REV 22
65+
#define K_VERS_REV 23
6666

6767
/* Data block types */
6868
#define BLK_DATA 1

src/bin/pg_dump/pg_backup_custom.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
*
2525
* Initial version.
2626
*
27+
* Modifications - 04-Jan-2001 - pjw@rhyme.com.au
28+
*
29+
* - Check results of IO routines more carefully.
30+
*
2731
*-------------------------------------------------------------------------
2832
*/
2933

@@ -164,7 +168,7 @@ void InitArchiveFmt_Custom(ArchiveHandle* AH)
164168
AH->FH = stdout;
165169
}
166170

167-
if (!AH)
171+
if (!AH->FH)
168172
die_horribly(AH, "%s: unable to open archive file %s",progname, AH->fSpec);
169173

170174
ctx->hasSeek = (fseek(AH->FH, 0, SEEK_CUR) == 0);
@@ -176,7 +180,7 @@ void InitArchiveFmt_Custom(ArchiveHandle* AH)
176180
} else {
177181
AH->FH = stdin;
178182
}
179-
if (!AH)
183+
if (!AH->FH)
180184
die_horribly(AH, "%s: unable to open archive file %s",progname, AH->fSpec);
181185

182186
ctx->hasSeek = (fseek(AH->FH, 0, SEEK_CUR) == 0);
@@ -666,6 +670,8 @@ static int _WriteByte(ArchiveHandle* AH, const int i)
666670
res = fputc(i, AH->FH);
667671
if (res != EOF) {
668672
ctx->filePos += 1;
673+
} else {
674+
die_horribly(AH, "%s: could not write byte./n",progname);
669675
}
670676
return res;
671677
}
@@ -705,6 +711,10 @@ static int _WriteBuf(ArchiveHandle* AH, const void* buf, int len)
705711
lclContext* ctx = (lclContext*)AH->formatData;
706712
int res;
707713
res = fwrite(buf, 1, len, AH->FH);
714+
715+
if (res != len)
716+
die_horribly(AH, "%s: write error in _WriteBuf (%d != %d)\n", progname, res, len);
717+
708718
ctx->filePos += res;
709719
return res;
710720
}
@@ -764,7 +774,9 @@ static void _CloseArchive(ArchiveHandle* AH)
764774
}
765775
}
766776

767-
fclose(AH->FH);
777+
if (fclose(AH->FH) != 0)
778+
die_horribly(AH, "%s: could not close archive file\n",progname);
779+
768780
AH->FH = NULL;
769781
}
770782

@@ -873,7 +885,8 @@ static int _DoDeflate(ArchiveHandle* AH, lclContext* ctx, int flush)
873885
if (zp->avail_out < zlibOutSize) {
874886
/* printf("Wrote %d byte deflated chunk\n", zlibOutSize - zp->avail_out); */
875887
WriteInt(AH, zlibOutSize - zp->avail_out);
876-
fwrite(out, 1, zlibOutSize - zp->avail_out, AH->FH);
888+
if (fwrite(out, 1, zlibOutSize - zp->avail_out, AH->FH) != (zlibOutSize - zp->avail_out))
889+
die_horribly(AH, "%s: could write compressed chunk\n",progname);
877890
ctx->filePos += zlibOutSize - zp->avail_out;
878891
}
879892
zp->next_out = out;
@@ -884,7 +897,8 @@ static int _DoDeflate(ArchiveHandle* AH, lclContext* ctx, int flush)
884897
if (zp->avail_in > 0)
885898
{
886899
WriteInt(AH, zp->avail_in);
887-
fwrite(zp->next_in, 1, zp->avail_in, AH->FH);
900+
if (fwrite(zp->next_in, 1, zp->avail_in, AH->FH) != zp->avail_in)
901+
die_horribly(AH, "%s: could write uncompressed chunk\n", progname);
888902
ctx->filePos += zp->avail_in;
889903
zp->avail_in = 0;
890904
} else {

src/bin/pg_dump/pg_backup_db.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
/*-------------------------------------------------------------------------
22
*
3+
* pg_backup_db.c
4+
*
5+
* Implements the basic DB functions used by the archiver.
6+
*
7+
* IDENTIFICATION
8+
*
9+
* Modifications - 04-Jan-2001 - pjw@rhyme.com.au
10+
*
11+
* - Check results of PQ routines more carefully.
312
*
413
*-------------------------------------------------------------------------
514
*/
@@ -449,14 +458,17 @@ int ExecuteSqlCommandBuf(ArchiveHandle* AH, void *qryv, int bufLen)
449458

450459
/* fprintf(stderr, "Sending '%s' via COPY (at end = %d)\n\n", AH->pgCopyBuf->data, isEnd); */
451460

452-
PQputline(AH->connection, AH->pgCopyBuf->data);
461+
if (PQputline(AH->connection, AH->pgCopyBuf->data) != 0)
462+
die_horribly(AH, "%s: error returned by PQputline\n", progname);
453463

454464
resetPQExpBuffer(AH->pgCopyBuf);
455465

456466
/* fprintf(stderr, "Buffer is '%s'\n", AH->pgCopyBuf->data); */
457467

458468
if(isEnd) {
459-
PQendcopy(AH->connection);
469+
if (PQendcopy(AH->connection) != 0)
470+
die_horribly(AH, "%s: error returned by PQendcopy\n", progname);
471+
460472
AH->pgCopyIn = 0;
461473
break;
462474
}

0 commit comments

Comments
 (0)