From 0195e5c4ab1ac710b280f7834202a7164058379c Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 29 Nov 2011 20:41:06 -0500 Subject: [PATCH] Clean up after recent pg_dump patches. Fix entirely broken handling of va_list printing routines, update some out-of-date comments, fix some bogus inclusion orders, fix NLS declarations, fix missed realloc calls. --- src/bin/pg_dump/common.c | 7 +++-- src/bin/pg_dump/dumpmem.c | 14 +++++----- src/bin/pg_dump/dumpmem.h | 4 +-- src/bin/pg_dump/dumputils.c | 40 +++++++++++++++++++++------- src/bin/pg_dump/dumputils.h | 3 +++ src/bin/pg_dump/nls.mk | 4 +-- src/bin/pg_dump/pg_backup_archiver.c | 26 ++++++++++-------- src/bin/pg_dump/pg_backup_archiver.h | 2 -- src/bin/pg_dump/pg_dumpall.c | 3 --- src/bin/pg_dump/pg_restore.c | 3 ++- 10 files changed, 62 insertions(+), 44 deletions(-) diff --git a/src/bin/pg_dump/common.c b/src/bin/pg_dump/common.c index 6f921f7758..d4e906ddd0 100644 --- a/src/bin/pg_dump/common.c +++ b/src/bin/pg_dump/common.c @@ -1,7 +1,7 @@ /*------------------------------------------------------------------------- * * common.c - * catalog routines used by pg_dump; long ago these were shared + * Catalog routines used by pg_dump; long ago these were shared * by another dump tool, but not anymore. * * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group @@ -13,14 +13,13 @@ * *------------------------------------------------------------------------- */ -#include "postgres_fe.h" +#include "pg_backup_archiver.h" #include #include "catalog/pg_class.h" - -#include "pg_backup_archiver.h" #include "dumpmem.h" +#include "dumputils.h" /* diff --git a/src/bin/pg_dump/dumpmem.c b/src/bin/pg_dump/dumpmem.c index a71e217b70..570b68021b 100644 --- a/src/bin/pg_dump/dumpmem.c +++ b/src/bin/pg_dump/dumpmem.c @@ -1,8 +1,7 @@ /*------------------------------------------------------------------------- * * dumpmem.c - * memory routines used by pg_dump and pg_restore (but not pg_dumpall - * because there is no failure location to report). + * Memory allocation routines used by pg_dump, pg_dumpall, and pg_restore * * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California @@ -14,16 +13,15 @@ *------------------------------------------------------------------------- */ #include "postgres_fe.h" + #include "dumputils.h" #include "dumpmem.h" -#include /* * Safer versions of some standard C library functions. If an - * out-of-memory condition occurs, these functions will bail out - * safely; therefore, their return value is guaranteed to be non-NULL. - * We also report the program name and close the database connection. + * out-of-memory condition occurs, these functions will bail out via exit(); + *therefore, their return value is guaranteed to be non-NULL. */ char * @@ -57,7 +55,7 @@ pg_calloc(size_t nmemb, size_t size) tmp = calloc(nmemb, size); if (!tmp) - exit_horribly(NULL, _("out of memory\n")); + exit_horribly(NULL, "out of memory\n"); return tmp; } @@ -68,6 +66,6 @@ pg_realloc(void *ptr, size_t size) tmp = realloc(ptr, size); if (!tmp) - exit_horribly(NULL, _("out of memory\n")); + exit_horribly(NULL, "out of memory\n"); return tmp; } diff --git a/src/bin/pg_dump/dumpmem.h b/src/bin/pg_dump/dumpmem.h index f70b32065e..2b4a1eb24d 100644 --- a/src/bin/pg_dump/dumpmem.h +++ b/src/bin/pg_dump/dumpmem.h @@ -1,7 +1,7 @@ /*------------------------------------------------------------------------- * * dumpmem.h - * Common header file for the pg_dump and pg_restore + * Memory allocation routines used by pg_dump, pg_dumpall, and pg_restore * * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California @@ -14,8 +14,6 @@ #ifndef DUMPMEM_H #define DUMPMEM_H -#include "postgres_fe.h" - extern char *pg_strdup(const char *string); extern void *pg_malloc(size_t size); extern void *pg_calloc(size_t nmemb, size_t size); diff --git a/src/bin/pg_dump/dumputils.c b/src/bin/pg_dump/dumputils.c index 39601e6a5f..5dab9675fc 100644 --- a/src/bin/pg_dump/dumputils.c +++ b/src/bin/pg_dump/dumputils.c @@ -16,14 +16,14 @@ #include -#include "dumpmem.h" #include "dumputils.h" #include "parser/keywords.h" +/* Globals exported by this file */ int quote_all_identifiers = 0; -const char *progname; +const char *progname = NULL; #define supports_grant_options(version) ((version) >= 70400) @@ -1214,31 +1214,51 @@ emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, } +/* + * Write a printf-style message to stderr. + * + * The program name is prepended, if "progname" has been set. + * Also, if modulename isn't NULL, that's included too. + * Note that we'll try to translate the modulename and the fmt string. + */ 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); + vwrite_msg(modulename, fmt, ap); va_end(ap); } +/* + * As write_msg, but pass a va_list not variable arguments. + */ +void +vwrite_msg(const char *modulename, const char *fmt, va_list ap) +{ + if (progname) + { + if (modulename) + fprintf(stderr, "%s: [%s] ", progname, _(modulename)); + else + fprintf(stderr, "%s: ", progname); + } + vfprintf(stderr, _(fmt), ap); +} + +/* + * Fail and die, with a message to stderr. Parameters as for write_msg. + */ void exit_horribly(const char *modulename, const char *fmt,...) { va_list ap; va_start(ap, fmt); - write_msg(modulename, fmt, ap); + vwrite_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 62d8080585..c857f3a098 100644 --- a/src/bin/pg_dump/dumputils.h +++ b/src/bin/pg_dump/dumputils.h @@ -20,6 +20,7 @@ #include "pqexpbuffer.h" extern int quote_all_identifiers; +extern const char *progname; extern void init_parallel_dump_utils(void); extern const char *fmtId(const char *identifier); @@ -53,6 +54,8 @@ 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 vwrite_msg(const char *modulename, const char *fmt, va_list ap) + __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 0))); extern void exit_horribly(const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3))); diff --git a/src/bin/pg_dump/nls.mk b/src/bin/pg_dump/nls.mk index a4824af1b8..b1b4a0e175 100644 --- a/src/bin/pg_dump/nls.mk +++ b/src/bin/pg_dump/nls.mk @@ -5,10 +5,10 @@ GETTEXT_FILES = pg_dump.c common.c pg_backup_archiver.c pg_backup_custom.c \ pg_backup_db.c pg_backup_files.c pg_backup_null.c \ pg_backup_tar.c pg_restore.c pg_dumpall.c \ ../../port/exec.c -GETTEXT_TRIGGERS = write_msg:2 die_horribly:3 exit_horribly:3 simple_prompt \ +GETTEXT_TRIGGERS = write_msg:2 die_horribly:3 exit_horribly:2 simple_prompt \ ExecuteSqlCommand:3 ahlog:3 GETTEXT_FLAGS = \ write_msg:2:c-format \ die_horribly:3:c-format \ - exit_horribly:3:c-format \ + exit_horribly:2:c-format \ ahlog:3:c-format diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 1eb2b8b40e..66123094fe 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -118,7 +118,9 @@ static int _discoverArchiveFormat(ArchiveHandle *AH); static int RestoringToDB(ArchiveHandle *AH); static void dump_lo_buf(ArchiveHandle *AH); -static void _die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_list ap) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 0))); +static void vdie_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); static void SetOutput(ArchiveHandle *AH, char *filename, int compression); @@ -1299,7 +1301,7 @@ ahlog(ArchiveHandle *AH, int level, const char *fmt,...) return; va_start(ap, fmt); - write_msg(NULL, fmt, ap); + vwrite_msg(NULL, fmt, ap); va_end(ap); } @@ -1418,10 +1420,12 @@ ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH) } +/* Report a fatal error and exit(1) */ static void -_die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_list ap) +vdie_horribly(ArchiveHandle *AH, const char *modulename, + const char *fmt, va_list ap) { - write_msg(modulename, fmt, ap); + vwrite_msg(modulename, fmt, ap); if (AH) { @@ -1434,14 +1438,14 @@ _die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_lis exit(1); } -/* Archiver use (just different arg declaration) */ +/* As above, but with variable arg list */ void die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) { va_list ap; va_start(ap, fmt); - _die_horribly(AH, modulename, fmt, ap); + vdie_horribly(AH, modulename, fmt, ap); va_end(ap); } @@ -1486,10 +1490,10 @@ warn_or_die_horribly(ArchiveHandle *AH, va_start(ap, fmt); if (AH->public.exit_on_error) - _die_horribly(AH, modulename, fmt, ap); + vdie_horribly(AH, modulename, fmt, ap); else { - write_msg(modulename, fmt, ap); + vwrite_msg(modulename, fmt, ap); AH->public.n_errors++; } va_end(ap); @@ -2218,7 +2222,7 @@ ReadToc(ArchiveHandle *AH) if (depIdx >= depSize) { depSize *= 2; - deps = (DumpId *) realloc(deps, sizeof(DumpId) * depSize); + deps = (DumpId *) pg_realloc(deps, sizeof(DumpId) * depSize); } sscanf(tmp, "%d", &deps[depIdx]); free(tmp); @@ -2227,7 +2231,7 @@ ReadToc(ArchiveHandle *AH) if (depIdx > 0) /* We have a non-null entry */ { - deps = (DumpId *) realloc(deps, sizeof(DumpId) * depIdx); + deps = (DumpId *) pg_realloc(deps, sizeof(DumpId) * depIdx); te->dependencies = deps; te->nDeps = depIdx; } @@ -4062,7 +4066,7 @@ identify_locking_dependencies(TocEntry *te) return; } - te->lockDeps = realloc(lockids, nlockids * sizeof(DumpId)); + te->lockDeps = pg_realloc(lockids, nlockids * sizeof(DumpId)); te->nLockDeps = nlockids; } diff --git a/src/bin/pg_dump/pg_backup_archiver.h b/src/bin/pg_dump/pg_backup_archiver.h index d1e202f50f..07a4b6e50a 100644 --- a/src/bin/pg_dump/pg_backup_archiver.h +++ b/src/bin/pg_dump/pg_backup_archiver.h @@ -298,8 +298,6 @@ typedef struct _tocEntry int nLockDeps; /* number of such dependencies */ } TocEntry; -/* Used everywhere */ -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))); diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index 4833b2269c..76927093ac 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -61,9 +61,6 @@ static PGconn *connectDatabase(const char *dbname, const char *pghost, const cha static PGresult *executeQuery(PGconn *conn, const char *query); static void executeCommand(PGconn *conn, const char *query); -char *pg_strdup(const char *string); -void *pg_malloc(size_t size); - static char pg_dump_bin[MAXPGPATH]; static PQExpBuffer pgdumpopts; static bool skip_acls = false; diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index ea68f5175c..8d6edacd7d 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -39,8 +39,9 @@ *------------------------------------------------------------------------- */ -#include "dumpmem.h" #include "pg_backup_archiver.h" + +#include "dumpmem.h" #include "dumputils.h" #include -- 2.40.0