Simplify the pg_dump/pg_restore error reporting macros, and allow

pg_dumpall to use the same memory allocation functions as the others.
This commit is contained in:
Bruce Momjian 2011-11-29 16:34:45 -05:00
parent b60f37bf44
commit 8b08deb0d1
12 changed files with 54 additions and 92 deletions

View File

@ -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)

View File

@ -14,7 +14,7 @@
*-------------------------------------------------------------------------
*/
#include "postgres_fe.h"
#include "pg_backup.h"
#include "dumputils.h"
#include "dumpmem.h"
#include <ctype.h>
@ -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;
}

View File

@ -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);
}

View File

@ -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 */

View File

@ -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 */

View File

@ -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);

View File

@ -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);

View File

@ -25,6 +25,7 @@
*/
#include "compress_io.h"
#include "dumputils.h"
#include "dumpmem.h"
/*--------

View File

@ -26,6 +26,7 @@
*/
#include "pg_backup_archiver.h"
#include "dumputils.h"
#include "dumpmem.h"
static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te);

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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');