Make pg_dump and friends consistently report both the filename and the
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 28 Oct 2007 21:55:52 +0000 (21:55 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 28 Oct 2007 21:55:52 +0000 (21:55 +0000)
errno string when complaining of fopen failures.  Per gripe from Bob
Pawley, it's not always instantly obvious to the user which name we
tried to open.

src/bin/pg_dump/pg_backup_archiver.c
src/bin/pg_dump/pg_backup_custom.c
src/bin/pg_dump/pg_backup_files.c
src/bin/pg_dump/pg_backup_tar.c
src/bin/pg_dump/pg_dumpall.c

index c35d5436fdbd1b0a2551261ecf8ba1b192d17d71..16ed5f0e313c4adda3daf4d9fd31ff0bf87abda3 100644 (file)
@@ -15,7 +15,7 @@
  *
  *
  * IDENTIFICATION
- *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.147 2007/10/13 20:18:41 tgl Exp $
+ *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.148 2007/10/28 21:55:52 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -799,8 +799,8 @@ SortTocFromFile(Archive *AHX, RestoreOptions *ropt)
        /* Setup the file */
        fh = fopen(ropt->tocFile, PG_BINARY_R);
        if (!fh)
-               die_horribly(AH, modulename, "could not open TOC file: %s\n",
-                                        strerror(errno));
+               die_horribly(AH, modulename, "could not open TOC file \"%s\": %s\n",
+                                        ropt->tocFile, strerror(errno));
 
        while (fgets(buf, sizeof(buf), fh) != NULL)
        {
@@ -957,7 +957,14 @@ SetOutput(ArchiveHandle *AH, char *filename, int compression)
        }
 
        if (!AH->OF)
-               die_horribly(AH, modulename, "could not open output file: %s\n", strerror(errno));
+       {
+               if (filename)
+                       die_horribly(AH, modulename, "could not open output file \"%s\": %s\n",
+                                                filename, strerror(errno));
+               else
+                       die_horribly(AH, modulename, "could not open output file: %s\n",
+                                                strerror(errno));
+       }
 
        return sav;
 }
@@ -1512,12 +1519,17 @@ _discoverArchiveFormat(ArchiveHandle *AH)
        {
                wantClose = 1;
                fh = fopen(AH->fSpec, PG_BINARY_R);
+               if (!fh)
+                       die_horribly(AH, modulename, "could not open input file \"%s\": %s\n",
+                                                AH->fSpec, strerror(errno));
        }
        else
+       {
                fh = stdin;
-
-       if (!fh)
-               die_horribly(AH, modulename, "could not open input file: %s\n", strerror(errno));
+               if (!fh)
+                       die_horribly(AH, modulename, "could not open input file: %s\n",
+                                                strerror(errno));
+       }
 
        cnt = fread(sig, 1, 5, fh);
 
index 923acce55a9499067305bad4d25dde67ef90260d..5791ec7812816f02eb8e2f2e51a05e6f176a8d57 100644 (file)
@@ -19,7 +19,7 @@
  *
  *
  * IDENTIFICATION
- *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.39 2007/08/06 01:38:14 tgl Exp $
+ *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.40 2007/10/28 21:55:52 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -169,23 +169,38 @@ InitArchiveFmt_Custom(ArchiveHandle *AH)
        if (AH->mode == archModeWrite)
        {
                if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
+               {
                        AH->FH = fopen(AH->fSpec, PG_BINARY_W);
+                       if (!AH->FH)
+                               die_horribly(AH, modulename, "could not open output file \"%s\": %s\n",
+                                                        AH->fSpec, strerror(errno));
+               }
                else
+               {
                        AH->FH = stdout;
-
-               if (!AH->FH)
-                       die_horribly(AH, modulename, "could not open output file \"%s\": %s\n", AH->fSpec, strerror(errno));
+                       if (!AH->FH)
+                               die_horribly(AH, modulename, "could not open output file: %s\n",
+                                                        strerror(errno));
+               }
 
                ctx->hasSeek = checkSeek(AH->FH);
        }
        else
        {
                if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
+               {
                        AH->FH = fopen(AH->fSpec, PG_BINARY_R);
+                       if (!AH->FH)
+                               die_horribly(AH, modulename, "could not open input file \"%s\": %s\n",
+                                                        AH->fSpec, strerror(errno));
+               }
                else
+               {
                        AH->FH = stdin;
-               if (!AH->FH)
-                       die_horribly(AH, modulename, "could not open input file \"%s\": %s\n", AH->fSpec, strerror(errno));
+                       if (!AH->FH)
+                               die_horribly(AH, modulename, "could not open input file: %s\n",
+                                                        strerror(errno));
+               }
 
                ctx->hasSeek = checkSeek(AH->FH);
 
index 6520ca227aa8c57e4d096980775b1fd1d94cff84..47a4ddd77cb3d23e11999ca904db861f8a2e2e0d 100644 (file)
@@ -20,7 +20,7 @@
  *
  *
  * IDENTIFICATION
- *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.33 2007/08/06 01:38:15 tgl Exp $
+ *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.34 2007/10/28 21:55:52 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -121,12 +121,19 @@ InitArchiveFmt_Files(ArchiveHandle *AH)
                                  "  normal use. Files will be written in the current working directory.\n");
 
                if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
+               {
                        AH->FH = fopen(AH->fSpec, PG_BINARY_W);
+                       if (AH->FH == NULL)
+                               die_horribly(NULL, modulename, "could not open output file \"%s\": %s\n",
+                                                        AH->fSpec, strerror(errno));
+               }
                else
+               {
                        AH->FH = stdout;
-
-               if (AH->FH == NULL)
-                       die_horribly(NULL, modulename, "could not open output file: %s\n", strerror(errno));
+                       if (AH->FH == NULL)
+                               die_horribly(NULL, modulename, "could not open output file: %s\n",
+                                                        strerror(errno));
+               }
 
                ctx->hasSeek = checkSeek(AH->FH);
 
@@ -139,12 +146,19 @@ InitArchiveFmt_Files(ArchiveHandle *AH)
        {                                                       /* Read Mode */
 
                if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
+               {
                        AH->FH = fopen(AH->fSpec, PG_BINARY_R);
+                       if (AH->FH == NULL)
+                               die_horribly(NULL, modulename, "could not open input file \"%s\": %s\n",
+                                                        AH->fSpec, strerror(errno));
+               }
                else
+               {
                        AH->FH = stdin;
-
-               if (AH->FH == NULL)
-                       die_horribly(NULL, modulename, "could not open input file: %s\n", strerror(errno));
+                       if (AH->FH == NULL)
+                               die_horribly(NULL, modulename, "could not open input file: %s\n",
+                                                        strerror(errno));
+               }
 
                ctx->hasSeek = checkSeek(AH->FH);
 
@@ -242,7 +256,8 @@ _StartData(ArchiveHandle *AH, TocEntry *te)
 #endif
 
        if (tctx->FH == NULL)
-               die_horribly(AH, modulename, "could not open output file: %s\n", strerror(errno));
+               die_horribly(AH, modulename, "could not open output file \"%s\": %s\n",
+                                        tctx->filename, strerror(errno));
 }
 
 static size_t
@@ -286,7 +301,8 @@ _PrintFileData(ArchiveHandle *AH, char *filename, RestoreOptions *ropt)
 #endif
 
        if (AH->FH == NULL)
-               die_horribly(AH, modulename, "could not open input file: %s\n", strerror(errno));
+               die_horribly(AH, modulename, "could not open input file \"%s\": %s\n",
+                                        filename, strerror(errno));
 
        while ((cnt = GZREAD(buf, 1, 4095, AH->FH)) > 0)
        {
@@ -507,7 +523,8 @@ _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
 #endif
 
        if (tctx->FH == NULL)
-               die_horribly(AH, modulename, "could not open large object file for input: %s\n", strerror(errno));
+               die_horribly(AH, modulename, "could not open large object file \"%s\" for input: %s\n",
+                                        fname, strerror(errno));
 }
 
 /*
index 8be6e5edf78d74ec4cccdf62cecef3ca06e371bb..380e0184ef452dba97cf3d51afb4b6c80a851ec0 100644 (file)
@@ -16,7 +16,7 @@
  *
  *
  * IDENTIFICATION
- *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.60 2007/08/29 16:31:36 tgl Exp $
+ *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.61 2007/10/28 21:55:52 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -172,15 +172,22 @@ InitArchiveFmt_Tar(ArchiveHandle *AH)
         */
        if (AH->mode == archModeWrite)
        {
-
                if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
+               {
                        ctx->tarFH = fopen(AH->fSpec, PG_BINARY_W);
+                       if (ctx->tarFH == NULL)
+                               die_horribly(NULL, modulename,
+                                                        "could not open TOC file \"%s\" for output: %s\n",
+                                                        AH->fSpec, strerror(errno));
+               }
                else
+               {
                        ctx->tarFH = stdout;
-
-               if (ctx->tarFH == NULL)
-                       die_horribly(NULL, modulename,
-                               "could not open TOC file for output: %s\n", strerror(errno));
+                       if (ctx->tarFH == NULL)
+                               die_horribly(NULL, modulename,
+                                                        "could not open TOC file for output: %s\n",
+                                                        strerror(errno));
+               }
 
                ctx->tarFHpos = 0;
 
@@ -210,14 +217,20 @@ InitArchiveFmt_Tar(ArchiveHandle *AH)
        }
        else
        {                                                       /* Read Mode */
-
                if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
+               {
                        ctx->tarFH = fopen(AH->fSpec, PG_BINARY_R);
+                       if (ctx->tarFH == NULL)
+                               die_horribly(NULL, modulename, "could not open TOC file \"%s\" for input: %s\n",
+                                                        AH->fSpec, strerror(errno));
+               }
                else
+               {
                        ctx->tarFH = stdin;
-
-               if (ctx->tarFH == NULL)
-                       die_horribly(NULL, modulename, "could not open TOC file for input: %s\n", strerror(errno));
+                       if (ctx->tarFH == NULL)
+                               die_horribly(NULL, modulename, "could not open TOC file for input: %s\n",
+                                                        strerror(errno));
+               }
 
                /*
                 * Make unbuffered since we will dup() it, and the buffers screw each
index 59f1ddbcfbaba96463db181a06293f58cff7ef2b..319f29522819eff63b175615684ce122aa9c9dc3 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.93 2007/10/13 20:18:41 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.94 2007/10/28 21:55:52 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -398,8 +398,8 @@ main(int argc, char *argv[])
                OPF = fopen(filename, PG_BINARY_W);
                if (!OPF)
                {
-                       fprintf(stderr, _("%s: could not open the output file \"%s\"\n"),
-                                       progname, filename);
+                       fprintf(stderr, _("%s: could not open the output file \"%s\": %s\n"),
+                                       progname, filename, strerror(errno));
                        exit(1);
                }
        }
@@ -1210,8 +1210,8 @@ dumpDatabases(PGconn *conn)
                        OPF = fopen(filename, PG_BINARY_A);
                        if (!OPF)
                        {
-                               fprintf(stderr, _("%s: could not re-open the output file \"%s\"\n"),
-                                               progname, filename);
+                               fprintf(stderr, _("%s: could not re-open the output file \"%s\": %s\n"),
+                                               progname, filename, strerror(errno));
                                exit(1);
                        }
                }