From 8b08deb0d1ee04a82130e640e48e302a3456817a Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Tue, 29 Nov 2011 16:34:45 -0500 Subject: [PATCH] Simplify the pg_dump/pg_restore error reporting macros, and allow pg_dumpall to use the same memory allocation functions as the others. --- src/bin/pg_dump/Makefile | 4 +-- src/bin/pg_dump/dumpmem.c | 12 ++++---- src/bin/pg_dump/dumputils.c | 31 +++++++++++++++++++++ src/bin/pg_dump/dumputils.h | 4 +++ src/bin/pg_dump/pg_backup.h | 4 --- src/bin/pg_dump/pg_backup_archiver.c | 41 ++-------------------------- src/bin/pg_dump/pg_backup_archiver.h | 1 - src/bin/pg_dump/pg_backup_custom.c | 1 + src/bin/pg_dump/pg_backup_files.c | 1 + src/bin/pg_dump/pg_dump_sort.c | 7 +++-- src/bin/pg_dump/pg_dumpall.c | 39 +------------------------- src/tools/msvc/Mkvcbuild.pm | 1 + 12 files changed, 54 insertions(+), 92 deletions(-) diff --git a/src/bin/pg_dump/Makefile b/src/bin/pg_dump/Makefile index d04c052994..49e3b513f6 100644 --- a/src/bin/pg_dump/Makefile +++ b/src/bin/pg_dump/Makefile @@ -35,8 +35,8 @@ pg_dump: pg_dump.o common.o pg_dump_sort.o $(OBJS) $(KEYWRDOBJS) | submake-libpq pg_restore: pg_restore.o $(OBJS) $(KEYWRDOBJS) | submake-libpq submake-libpgport $(CC) $(CFLAGS) pg_restore.o $(KEYWRDOBJS) $(OBJS) $(libpq_pgport) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X) -pg_dumpall: pg_dumpall.o dumputils.o $(KEYWRDOBJS) | submake-libpq submake-libpgport - $(CC) $(CFLAGS) pg_dumpall.o dumputils.o $(KEYWRDOBJS) $(WIN32RES) $(libpq_pgport) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X) +pg_dumpall: pg_dumpall.o dumputils.o dumpmem.o $(KEYWRDOBJS) | submake-libpq submake-libpgport + $(CC) $(CFLAGS) pg_dumpall.o dumputils.o dumpmem.o $(KEYWRDOBJS) $(WIN32RES) $(libpq_pgport) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X) install: all installdirs $(INSTALL_PROGRAM) pg_dump$(X) '$(DESTDIR)$(bindir)'/pg_dump$(X) diff --git a/src/bin/pg_dump/dumpmem.c b/src/bin/pg_dump/dumpmem.c index a50f4f596b..a71e217b70 100644 --- a/src/bin/pg_dump/dumpmem.c +++ b/src/bin/pg_dump/dumpmem.c @@ -14,7 +14,7 @@ *------------------------------------------------------------------------- */ #include "postgres_fe.h" -#include "pg_backup.h" +#include "dumputils.h" #include "dumpmem.h" #include @@ -32,10 +32,10 @@ pg_strdup(const char *string) char *tmp; if (!string) - exit_horribly(NULL, NULL, "cannot duplicate null pointer\n"); + exit_horribly(NULL, "cannot duplicate null pointer\n"); tmp = strdup(string); if (!tmp) - exit_horribly(NULL, NULL, "out of memory\n"); + exit_horribly(NULL, "out of memory\n"); return tmp; } @@ -46,7 +46,7 @@ pg_malloc(size_t size) tmp = malloc(size); if (!tmp) - exit_horribly(NULL, NULL, "out of memory\n"); + exit_horribly(NULL, "out of memory\n"); return tmp; } @@ -57,7 +57,7 @@ pg_calloc(size_t nmemb, size_t size) tmp = calloc(nmemb, size); if (!tmp) - exit_horribly(NULL, NULL, _("out of memory\n")); + exit_horribly(NULL, _("out of memory\n")); return tmp; } @@ -68,6 +68,6 @@ pg_realloc(void *ptr, size_t size) tmp = realloc(ptr, size); if (!tmp) - exit_horribly(NULL, NULL, _("out of memory\n")); + exit_horribly(NULL, _("out of memory\n")); return tmp; } diff --git a/src/bin/pg_dump/dumputils.c b/src/bin/pg_dump/dumputils.c index 92b9d28e7f..39601e6a5f 100644 --- a/src/bin/pg_dump/dumputils.c +++ b/src/bin/pg_dump/dumputils.c @@ -23,6 +23,7 @@ int quote_all_identifiers = 0; +const char *progname; #define supports_grant_options(version) ((version) >= 70400) @@ -1211,3 +1212,33 @@ emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, appendPQExpBuffer(buffer, ";\n"); } } + + +void +write_msg(const char *modulename, const char *fmt,...) +{ + va_list ap; + + va_start(ap, fmt); + if (modulename) + fprintf(stderr, "%s: [%s] ", progname, _(modulename)); + else + fprintf(stderr, "%s: ", progname); + vfprintf(stderr, _(fmt), ap); + va_end(ap); +} + + +void +exit_horribly(const char *modulename, const char *fmt,...) +{ + va_list ap; + + va_start(ap, fmt); + write_msg(modulename, fmt, ap); + va_end(ap); + + exit(1); +} + + diff --git a/src/bin/pg_dump/dumputils.h b/src/bin/pg_dump/dumputils.h index 40bbc81ae8..62d8080585 100644 --- a/src/bin/pg_dump/dumputils.h +++ b/src/bin/pg_dump/dumputils.h @@ -51,5 +51,9 @@ extern void buildShSecLabelQuery(PGconn *conn, const char *catalog_name, uint32 objectId, PQExpBuffer sql); extern void emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *target, const char *objname); +extern void write_msg(const char *modulename, const char *fmt,...) + __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3))); +extern void exit_horribly(const char *modulename, const char *fmt,...) + __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3))); #endif /* DUMPUTILS_H */ diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h index ce12a41ce3..8168cfffed 100644 --- a/src/bin/pg_dump/pg_backup.h +++ b/src/bin/pg_dump/pg_backup.h @@ -150,10 +150,6 @@ typedef struct _restoreOptions * Main archiver interface. */ -extern void -exit_horribly(Archive *AH, const char *modulename, const char *fmt,...) -__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4))); - /* Lets the archive know we have a DB connection to shutdown if it dies */ diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 164d593ff9..1eb2b8b40e 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -84,8 +84,6 @@ typedef struct _outputContext int gzOut; } OutputContext; -const char *progname; - static const char *modulename = gettext_noop("archiver"); /* index array created by fix_dependencies -- only used in parallel restore */ @@ -120,7 +118,6 @@ static int _discoverArchiveFormat(ArchiveHandle *AH); static int RestoringToDB(ArchiveHandle *AH); static void dump_lo_buf(ArchiveHandle *AH); -static void _write_msg(const char *modulename, const char *fmt, va_list ap) __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 0))); static void _die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_list ap) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 0))); static void dumpTimestamp(ArchiveHandle *AH, const char *msg, time_t tim); @@ -1302,7 +1299,7 @@ ahlog(ArchiveHandle *AH, int level, const char *fmt,...) return; va_start(ap, fmt); - _write_msg(NULL, fmt, ap); + write_msg(NULL, fmt, ap); va_end(ap); } @@ -1420,32 +1417,11 @@ ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH) } } -/* Common exit code */ -static void -_write_msg(const char *modulename, const char *fmt, va_list ap) -{ - if (modulename) - fprintf(stderr, "%s: [%s] ", progname, _(modulename)); - else - fprintf(stderr, "%s: ", progname); - vfprintf(stderr, _(fmt), ap); -} - -void -write_msg(const char *modulename, const char *fmt,...) -{ - va_list ap; - - va_start(ap, fmt); - _write_msg(modulename, fmt, ap); - va_end(ap); -} - static void _die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_list ap) { - _write_msg(modulename, fmt, ap); + write_msg(modulename, fmt, ap); if (AH) { @@ -1458,17 +1434,6 @@ _die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_lis exit(1); } -/* External use */ -void -exit_horribly(Archive *AH, const char *modulename, const char *fmt,...) -{ - va_list ap; - - va_start(ap, fmt); - _die_horribly((ArchiveHandle *) AH, modulename, fmt, ap); - va_end(ap); -} - /* Archiver use (just different arg declaration) */ void die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) @@ -1524,7 +1489,7 @@ warn_or_die_horribly(ArchiveHandle *AH, _die_horribly(AH, modulename, fmt, ap); else { - _write_msg(modulename, fmt, ap); + write_msg(modulename, fmt, ap); AH->public.n_errors++; } va_end(ap); diff --git a/src/bin/pg_dump/pg_backup_archiver.h b/src/bin/pg_dump/pg_backup_archiver.h index 8a3a6f9e22..d1e202f50f 100644 --- a/src/bin/pg_dump/pg_backup_archiver.h +++ b/src/bin/pg_dump/pg_backup_archiver.h @@ -303,7 +303,6 @@ extern const char *progname; extern void die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4))); extern void warn_or_die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4))); -extern void write_msg(const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3))); extern void WriteTOC(ArchiveHandle *AH); extern void ReadTOC(ArchiveHandle *AH); diff --git a/src/bin/pg_dump/pg_backup_custom.c b/src/bin/pg_dump/pg_backup_custom.c index b2f3196bf9..31fa3733f0 100644 --- a/src/bin/pg_dump/pg_backup_custom.c +++ b/src/bin/pg_dump/pg_backup_custom.c @@ -25,6 +25,7 @@ */ #include "compress_io.h" +#include "dumputils.h" #include "dumpmem.h" /*-------- diff --git a/src/bin/pg_dump/pg_backup_files.c b/src/bin/pg_dump/pg_backup_files.c index 85373b5f8c..ffcbb8f642 100644 --- a/src/bin/pg_dump/pg_backup_files.c +++ b/src/bin/pg_dump/pg_backup_files.c @@ -26,6 +26,7 @@ */ #include "pg_backup_archiver.h" +#include "dumputils.h" #include "dumpmem.h" static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te); diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c index 8023450675..368208da6f 100644 --- a/src/bin/pg_dump/pg_dump_sort.c +++ b/src/bin/pg_dump/pg_dump_sort.c @@ -14,6 +14,7 @@ *------------------------------------------------------------------------- */ #include "pg_backup_archiver.h" +#include "dumputils.h" #include "dumpmem.h" static const char *modulename = gettext_noop("sorter"); @@ -315,13 +316,13 @@ TopoSort(DumpableObject **objs, obj = objs[i]; j = obj->dumpId; if (j <= 0 || j > maxDumpId) - exit_horribly(NULL, modulename, "invalid dumpId %d\n", j); + exit_horribly(modulename, "invalid dumpId %d\n", j); idMap[j] = i; for (j = 0; j < obj->nDeps; j++) { k = obj->dependencies[j]; if (k <= 0 || k > maxDumpId) - exit_horribly(NULL, modulename, "invalid dependency %d\n", k); + exit_horribly(modulename, "invalid dependency %d\n", k); beforeConstraints[k]++; } } @@ -541,7 +542,7 @@ findDependencyLoops(DumpableObject **objs, int nObjs, int totObjs) /* We'd better have fixed at least one loop */ if (!fixedloop) - exit_horribly(NULL, modulename, "could not identify dependency loop\n"); + exit_horribly(modulename, "could not identify dependency loop\n"); free(workspace); } diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index 4782e6889b..4833b2269c 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -23,6 +23,7 @@ #include "getopt_long.h" #include "dumputils.h" +#include "dumpmem.h" #include "pg_backup.h" /* version string we expect back from pg_dump */ @@ -1908,41 +1909,3 @@ doShellQuoting(PQExpBuffer buf, const char *str) appendPQExpBufferChar(buf, '"'); #endif /* WIN32 */ } - - -/* - * Simpler versions of common.c functions. - */ - -char * -pg_strdup(const char *string) -{ - char *tmp; - - if (!string) - { - fprintf(stderr, "cannot duplicate null pointer\n"); - exit(1); - } - tmp = strdup(string); - if (!tmp) - { - fprintf(stderr, _("%s: out of memory\n"), progname); - exit(1); - } - return tmp; -} - -void * -pg_malloc(size_t size) -{ - void *tmp; - - tmp = malloc(size); - if (!tmp) - { - fprintf(stderr, _("%s: out of memory\n"), progname); - exit(1); - } - return tmp; -} diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm index 94ecb657cf..fb83224834 100644 --- a/src/tools/msvc/Mkvcbuild.pm +++ b/src/tools/msvc/Mkvcbuild.pm @@ -355,6 +355,7 @@ sub mkvcbuild $pgdumpall->AddIncludeDir('src\backend'); $pgdumpall->AddFile('src\bin\pg_dump\pg_dumpall.c'); $pgdumpall->AddFile('src\bin\pg_dump\dumputils.c'); + $pgdumpall->AddFile('src\bin\pg_dump\dumpmem.c'); $pgdumpall->AddFile('src\bin\pg_dump\keywords.c'); $pgdumpall->AddFile('src\backend\parser\kwlookup.c'); -- GitLab