|
27 | 27 | * Modifications - 30-Oct-2000 - pjw@rhyme.com.au
|
28 | 28 | * Added {Start,End}RestoreBlobs to allow extended TX during BLOB restore.
|
29 | 29 | *
|
| 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 | + * |
30 | 37 | *-------------------------------------------------------------------------
|
31 | 38 | */
|
32 | 39 |
|
@@ -99,14 +106,18 @@ Archive* OpenArchive(const char* FileSpec, const ArchiveFormat fmt)
|
99 | 106 | /* Public */
|
100 | 107 | void CloseArchive(Archive* AHX)
|
101 | 108 | {
|
| 109 | + int res = 0; |
102 | 110 | ArchiveHandle* AH = (ArchiveHandle*)AHX;
|
103 | 111 | (*AH->ClosePtr)(AH);
|
104 | 112 |
|
105 | 113 | /* Close the output */
|
106 | 114 | if (AH->gzOut)
|
107 |
| - GZCLOSE(AH->OF); |
| 115 | + res = GZCLOSE(AH->OF); |
108 | 116 | 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); |
110 | 121 | }
|
111 | 122 |
|
112 | 123 | /* Public */
|
@@ -791,8 +802,8 @@ void SortTocFromFile(Archive* AHX, RestoreOptions *ropt)
|
791 | 802 |
|
792 | 803 | /* Setup the file */
|
793 | 804 | 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); |
796 | 807 |
|
797 | 808 | while (fgets(buf, 1024, fh) != NULL)
|
798 | 809 | {
|
@@ -828,7 +839,8 @@ void SortTocFromFile(Archive* AHX, RestoreOptions *ropt)
|
828 | 839 | tePrev = te;
|
829 | 840 | }
|
830 | 841 |
|
831 |
| - fclose(fh); |
| 842 | + if (fclose(fh) != 0) |
| 843 | + die_horribly(AH, "%s: could not close TOC file\n", progname); |
832 | 844 | }
|
833 | 845 |
|
834 | 846 | /**********************
|
@@ -906,34 +918,42 @@ OutputContext SetOutput(ArchiveHandle* AH, char *filename, int compression)
|
906 | 918 | #ifdef HAVE_LIBZ
|
907 | 919 | if (compression != 0)
|
908 | 920 | {
|
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; |
916 | 928 | } else { /* Use fopen */
|
917 | 929 | #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; |
924 | 936 | #ifdef HAVE_LIBZ
|
925 | 937 | }
|
926 | 938 | #endif
|
927 | 939 |
|
| 940 | + if (!AH->OF) |
| 941 | + die_horribly(AH, "%s: could not set output\n", progname); |
| 942 | + |
928 | 943 | return sav;
|
929 | 944 | }
|
930 | 945 |
|
931 | 946 | void ResetOutput(ArchiveHandle* AH, OutputContext sav)
|
932 | 947 | {
|
| 948 | + int res; |
| 949 | + |
933 | 950 | if (AH->gzOut)
|
934 |
| - GZCLOSE(AH->OF); |
| 951 | + res = GZCLOSE(AH->OF); |
935 | 952 | 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); |
937 | 957 |
|
938 | 958 | AH->gzOut = sav.gzOut;
|
939 | 959 | AH->OF = sav.OF;
|
@@ -1012,19 +1032,34 @@ int ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle* AH)
|
1012 | 1032 | return res;
|
1013 | 1033 | }
|
1014 | 1034 | 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 | + } |
1016 | 1041 | 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 | + } |
1018 | 1048 | else
|
1019 | 1049 | {
|
1020 | 1050 | /*
|
1021 | 1051 | * If we're doing a restore, and it's direct to DB, and we're connected
|
1022 | 1052 | * then send it to the DB.
|
1023 | 1053 | */
|
1024 | 1054 | if (RestoringToDB(AH))
|
1025 |
| - return ExecuteSqlCommandBuf(AH, (void*)ptr, size*nmemb); |
| 1055 | + return ExecuteSqlCommandBuf(AH, (void*)ptr, size*nmemb); /* Always 1, currently */ |
1026 | 1056 | 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 | + } |
1028 | 1063 | }
|
1029 | 1064 | }
|
1030 | 1065 |
|
@@ -1299,7 +1334,8 @@ _discoverArchiveFormat(ArchiveHandle* AH)
|
1299 | 1334 |
|
1300 | 1335 | /* Close the file */
|
1301 | 1336 | 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); |
1303 | 1339 |
|
1304 | 1340 | return AH->format;
|
1305 | 1341 | }
|
@@ -1342,7 +1378,7 @@ static ArchiveHandle* _allocAH(const char* FileSpec, const ArchiveFormat fmt,
|
1342 | 1378 | AH->fSpec = NULL;
|
1343 | 1379 | }
|
1344 | 1380 |
|
1345 |
| - AH->currUser = ""; |
| 1381 | + AH->currUser = strdup(""); /* So it's valid, but we can free() it later if necessary */ |
1346 | 1382 |
|
1347 | 1383 | AH->toc = (TocEntry*)calloc(1, sizeof(TocEntry));
|
1348 | 1384 | if (!AH->toc)
|
@@ -1455,7 +1491,7 @@ void WriteToc(ArchiveHandle* AH)
|
1455 | 1491 | if (AH->WriteExtraTocPtr) {
|
1456 | 1492 | (*AH->WriteExtraTocPtr)(AH, te);
|
1457 | 1493 | }
|
1458 |
| - te = te->next; |
| 1494 | + te = te->next; |
1459 | 1495 | }
|
1460 | 1496 | }
|
1461 | 1497 |
|
@@ -1585,7 +1621,12 @@ static void _reconnectAsUser(ArchiveHandle* AH, const char *dbname, char *user)
|
1585 | 1621 | {
|
1586 | 1622 | ahprintf(AH, "\\connect %s %s\n", dbname, user);
|
1587 | 1623 | }
|
1588 |
| - AH->currUser = user; |
| 1624 | + if (AH->currUser) |
| 1625 | + { |
| 1626 | + free(AH->currUser); |
| 1627 | + } |
| 1628 | + |
| 1629 | + AH->currUser = strdup(user); |
1589 | 1630 | }
|
1590 | 1631 | }
|
1591 | 1632 |
|
|
0 commit comments