From 27c033ed98d0ed8ee05caf440ae648f42fa5f9d6 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 28 Oct 2007 21:55:52 +0000 Subject: [PATCH] Make pg_dump and friends consistently report both the filename and the 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 | 26 +++++++++++++------ src/bin/pg_dump/pg_backup_custom.c | 27 +++++++++++++++----- src/bin/pg_dump/pg_backup_files.c | 37 ++++++++++++++++++++-------- src/bin/pg_dump/pg_backup_tar.c | 33 +++++++++++++++++-------- src/bin/pg_dump/pg_dumpall.c | 10 ++++---- 5 files changed, 95 insertions(+), 38 deletions(-) diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index c35d5436fd..16ed5f0e31 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -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); diff --git a/src/bin/pg_dump/pg_backup_custom.c b/src/bin/pg_dump/pg_backup_custom.c index 923acce55a..5791ec7812 100644 --- a/src/bin/pg_dump/pg_backup_custom.c +++ b/src/bin/pg_dump/pg_backup_custom.c @@ -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); diff --git a/src/bin/pg_dump/pg_backup_files.c b/src/bin/pg_dump/pg_backup_files.c index 6520ca227a..47a4ddd77c 100644 --- a/src/bin/pg_dump/pg_backup_files.c +++ b/src/bin/pg_dump/pg_backup_files.c @@ -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)); } /* diff --git a/src/bin/pg_dump/pg_backup_tar.c b/src/bin/pg_dump/pg_backup_tar.c index 8be6e5edf7..380e0184ef 100644 --- a/src/bin/pg_dump/pg_backup_tar.c +++ b/src/bin/pg_dump/pg_backup_tar.c @@ -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 diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index 59f1ddbcfb..319f295228 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -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); } } -- 2.49.0