Skip to content

Commit 8b08deb

Browse files
committed
Simplify the pg_dump/pg_restore error reporting macros, and allow
pg_dumpall to use the same memory allocation functions as the others.
1 parent b60f37b commit 8b08deb

File tree

12 files changed

+54
-92
lines changed

12 files changed

+54
-92
lines changed

src/bin/pg_dump/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ pg_dump: pg_dump.o common.o pg_dump_sort.o $(OBJS) $(KEYWRDOBJS) | submake-libpq
3535
pg_restore: pg_restore.o $(OBJS) $(KEYWRDOBJS) | submake-libpq submake-libpgport
3636
$(CC) $(CFLAGS) pg_restore.o $(KEYWRDOBJS) $(OBJS) $(libpq_pgport) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
3737

38-
pg_dumpall: pg_dumpall.o dumputils.o $(KEYWRDOBJS) | submake-libpq submake-libpgport
39-
$(CC) $(CFLAGS) pg_dumpall.o dumputils.o $(KEYWRDOBJS) $(WIN32RES) $(libpq_pgport) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
38+
pg_dumpall: pg_dumpall.o dumputils.o dumpmem.o $(KEYWRDOBJS) | submake-libpq submake-libpgport
39+
$(CC) $(CFLAGS) pg_dumpall.o dumputils.o dumpmem.o $(KEYWRDOBJS) $(WIN32RES) $(libpq_pgport) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
4040

4141
install: all installdirs
4242
$(INSTALL_PROGRAM) pg_dump$(X) '$(DESTDIR)$(bindir)'/pg_dump$(X)

src/bin/pg_dump/dumpmem.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*-------------------------------------------------------------------------
1515
*/
1616
#include "postgres_fe.h"
17-
#include "pg_backup.h"
17+
#include "dumputils.h"
1818
#include "dumpmem.h"
1919

2020
#include <ctype.h>
@@ -32,10 +32,10 @@ pg_strdup(const char *string)
3232
char *tmp;
3333

3434
if (!string)
35-
exit_horribly(NULL, NULL, "cannot duplicate null pointer\n");
35+
exit_horribly(NULL, "cannot duplicate null pointer\n");
3636
tmp = strdup(string);
3737
if (!tmp)
38-
exit_horribly(NULL, NULL, "out of memory\n");
38+
exit_horribly(NULL, "out of memory\n");
3939
return tmp;
4040
}
4141

@@ -46,7 +46,7 @@ pg_malloc(size_t size)
4646

4747
tmp = malloc(size);
4848
if (!tmp)
49-
exit_horribly(NULL, NULL, "out of memory\n");
49+
exit_horribly(NULL, "out of memory\n");
5050
return tmp;
5151
}
5252

@@ -57,7 +57,7 @@ pg_calloc(size_t nmemb, size_t size)
5757

5858
tmp = calloc(nmemb, size);
5959
if (!tmp)
60-
exit_horribly(NULL, NULL, _("out of memory\n"));
60+
exit_horribly(NULL, _("out of memory\n"));
6161
return tmp;
6262
}
6363

@@ -68,6 +68,6 @@ pg_realloc(void *ptr, size_t size)
6868

6969
tmp = realloc(ptr, size);
7070
if (!tmp)
71-
exit_horribly(NULL, NULL, _("out of memory\n"));
71+
exit_horribly(NULL, _("out of memory\n"));
7272
return tmp;
7373
}

src/bin/pg_dump/dumputils.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424

2525
int quote_all_identifiers = 0;
26+
const char *progname;
2627

2728

2829
#define supports_grant_options(version) ((version) >= 70400)
@@ -1211,3 +1212,33 @@ emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer,
12111212
appendPQExpBuffer(buffer, ";\n");
12121213
}
12131214
}
1215+
1216+
1217+
void
1218+
write_msg(const char *modulename, const char *fmt,...)
1219+
{
1220+
va_list ap;
1221+
1222+
va_start(ap, fmt);
1223+
if (modulename)
1224+
fprintf(stderr, "%s: [%s] ", progname, _(modulename));
1225+
else
1226+
fprintf(stderr, "%s: ", progname);
1227+
vfprintf(stderr, _(fmt), ap);
1228+
va_end(ap);
1229+
}
1230+
1231+
1232+
void
1233+
exit_horribly(const char *modulename, const char *fmt,...)
1234+
{
1235+
va_list ap;
1236+
1237+
va_start(ap, fmt);
1238+
write_msg(modulename, fmt, ap);
1239+
va_end(ap);
1240+
1241+
exit(1);
1242+
}
1243+
1244+

src/bin/pg_dump/dumputils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,9 @@ extern void buildShSecLabelQuery(PGconn *conn, const char *catalog_name,
5151
uint32 objectId, PQExpBuffer sql);
5252
extern void emitShSecLabels(PGconn *conn, PGresult *res,
5353
PQExpBuffer buffer, const char *target, const char *objname);
54+
extern void write_msg(const char *modulename, const char *fmt,...)
55+
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
56+
extern void exit_horribly(const char *modulename, const char *fmt,...)
57+
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
5458

5559
#endif /* DUMPUTILS_H */

src/bin/pg_dump/pg_backup.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,6 @@ typedef struct _restoreOptions
150150
* Main archiver interface.
151151
*/
152152

153-
extern void
154-
exit_horribly(Archive *AH, const char *modulename, const char *fmt,...)
155-
__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
156-
157153

158154
/* Lets the archive know we have a DB connection to shutdown if it dies */
159155

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ typedef struct _outputContext
8484
int gzOut;
8585
} OutputContext;
8686

87-
const char *progname;
88-
8987
static const char *modulename = gettext_noop("archiver");
9088

9189
/* index array created by fix_dependencies -- only used in parallel restore */
@@ -120,7 +118,6 @@ static int _discoverArchiveFormat(ArchiveHandle *AH);
120118

121119
static int RestoringToDB(ArchiveHandle *AH);
122120
static void dump_lo_buf(ArchiveHandle *AH);
123-
static void _write_msg(const char *modulename, const char *fmt, va_list ap) __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 0)));
124121
static void _die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_list ap) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 0)));
125122

126123
static void dumpTimestamp(ArchiveHandle *AH, const char *msg, time_t tim);
@@ -1302,7 +1299,7 @@ ahlog(ArchiveHandle *AH, int level, const char *fmt,...)
13021299
return;
13031300

13041301
va_start(ap, fmt);
1305-
_write_msg(NULL, fmt, ap);
1302+
write_msg(NULL, fmt, ap);
13061303
va_end(ap);
13071304
}
13081305

@@ -1420,32 +1417,11 @@ ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
14201417
}
14211418
}
14221419

1423-
/* Common exit code */
1424-
static void
1425-
_write_msg(const char *modulename, const char *fmt, va_list ap)
1426-
{
1427-
if (modulename)
1428-
fprintf(stderr, "%s: [%s] ", progname, _(modulename));
1429-
else
1430-
fprintf(stderr, "%s: ", progname);
1431-
vfprintf(stderr, _(fmt), ap);
1432-
}
1433-
1434-
void
1435-
write_msg(const char *modulename, const char *fmt,...)
1436-
{
1437-
va_list ap;
1438-
1439-
va_start(ap, fmt);
1440-
_write_msg(modulename, fmt, ap);
1441-
va_end(ap);
1442-
}
1443-
14441420

14451421
static void
14461422
_die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_list ap)
14471423
{
1448-
_write_msg(modulename, fmt, ap);
1424+
write_msg(modulename, fmt, ap);
14491425

14501426
if (AH)
14511427
{
@@ -1458,17 +1434,6 @@ _die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_lis
14581434
exit(1);
14591435
}
14601436

1461-
/* External use */
1462-
void
1463-
exit_horribly(Archive *AH, const char *modulename, const char *fmt,...)
1464-
{
1465-
va_list ap;
1466-
1467-
va_start(ap, fmt);
1468-
_die_horribly((ArchiveHandle *) AH, modulename, fmt, ap);
1469-
va_end(ap);
1470-
}
1471-
14721437
/* Archiver use (just different arg declaration) */
14731438
void
14741439
die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...)
@@ -1524,7 +1489,7 @@ warn_or_die_horribly(ArchiveHandle *AH,
15241489
_die_horribly(AH, modulename, fmt, ap);
15251490
else
15261491
{
1527-
_write_msg(modulename, fmt, ap);
1492+
write_msg(modulename, fmt, ap);
15281493
AH->public.n_errors++;
15291494
}
15301495
va_end(ap);

src/bin/pg_dump/pg_backup_archiver.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ extern const char *progname;
303303

304304
extern void die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
305305
extern void warn_or_die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
306-
extern void write_msg(const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
307306

308307
extern void WriteTOC(ArchiveHandle *AH);
309308
extern void ReadTOC(ArchiveHandle *AH);

src/bin/pg_dump/pg_backup_custom.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "compress_io.h"
28+
#include "dumputils.h"
2829
#include "dumpmem.h"
2930

3031
/*--------

src/bin/pg_dump/pg_backup_files.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727

2828
#include "pg_backup_archiver.h"
29+
#include "dumputils.h"
2930
#include "dumpmem.h"
3031

3132
static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te);

src/bin/pg_dump/pg_dump_sort.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*-------------------------------------------------------------------------
1515
*/
1616
#include "pg_backup_archiver.h"
17+
#include "dumputils.h"
1718
#include "dumpmem.h"
1819

1920
static const char *modulename = gettext_noop("sorter");
@@ -315,13 +316,13 @@ TopoSort(DumpableObject **objs,
315316
obj = objs[i];
316317
j = obj->dumpId;
317318
if (j <= 0 || j > maxDumpId)
318-
exit_horribly(NULL, modulename, "invalid dumpId %d\n", j);
319+
exit_horribly(modulename, "invalid dumpId %d\n", j);
319320
idMap[j] = i;
320321
for (j = 0; j < obj->nDeps; j++)
321322
{
322323
k = obj->dependencies[j];
323324
if (k <= 0 || k > maxDumpId)
324-
exit_horribly(NULL, modulename, "invalid dependency %d\n", k);
325+
exit_horribly(modulename, "invalid dependency %d\n", k);
325326
beforeConstraints[k]++;
326327
}
327328
}
@@ -541,7 +542,7 @@ findDependencyLoops(DumpableObject **objs, int nObjs, int totObjs)
541542

542543
/* We'd better have fixed at least one loop */
543544
if (!fixedloop)
544-
exit_horribly(NULL, modulename, "could not identify dependency loop\n");
545+
exit_horribly(modulename, "could not identify dependency loop\n");
545546

546547
free(workspace);
547548
}

src/bin/pg_dump/pg_dumpall.c

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "getopt_long.h"
2424

2525
#include "dumputils.h"
26+
#include "dumpmem.h"
2627
#include "pg_backup.h"
2728

2829
/* version string we expect back from pg_dump */
@@ -1908,41 +1909,3 @@ doShellQuoting(PQExpBuffer buf, const char *str)
19081909
appendPQExpBufferChar(buf, '"');
19091910
#endif /* WIN32 */
19101911
}
1911-
1912-
1913-
/*
1914-
* Simpler versions of common.c functions.
1915-
*/
1916-
1917-
char *
1918-
pg_strdup(const char *string)
1919-
{
1920-
char *tmp;
1921-
1922-
if (!string)
1923-
{
1924-
fprintf(stderr, "cannot duplicate null pointer\n");
1925-
exit(1);
1926-
}
1927-
tmp = strdup(string);
1928-
if (!tmp)
1929-
{
1930-
fprintf(stderr, _("%s: out of memory\n"), progname);
1931-
exit(1);
1932-
}
1933-
return tmp;
1934-
}
1935-
1936-
void *
1937-
pg_malloc(size_t size)
1938-
{
1939-
void *tmp;
1940-
1941-
tmp = malloc(size);
1942-
if (!tmp)
1943-
{
1944-
fprintf(stderr, _("%s: out of memory\n"), progname);
1945-
exit(1);
1946-
}
1947-
return tmp;
1948-
}

src/tools/msvc/Mkvcbuild.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ sub mkvcbuild
355355
$pgdumpall->AddIncludeDir('src\backend');
356356
$pgdumpall->AddFile('src\bin\pg_dump\pg_dumpall.c');
357357
$pgdumpall->AddFile('src\bin\pg_dump\dumputils.c');
358+
$pgdumpall->AddFile('src\bin\pg_dump\dumpmem.c');
358359
$pgdumpall->AddFile('src\bin\pg_dump\keywords.c');
359360
$pgdumpall->AddFile('src\backend\parser\kwlookup.c');
360361

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy