mirror of https://github.com/postgres/postgres
Move functions used only by pg_dump and pg_restore from dumputils.c to a new file, pg_backup_utils.c. dumputils.c is linked into psql and some programs in bin/scripts, so it seems good to keep it slim. The parallel functionality is moved to parallel.c, as is exit_horribly, because the interesting code in exit_horribly is parallel-related. This refactoring gets rid of the on_exit_msg_func function pointer. It was problematic, because a modern gcc version with -Wmissing-format-attribute complained if it wasn't marked with PF_PRINTF_ATTRIBUTE, but the ancient gcc version that Tom Lane's old HP-UX box has didn't accept that attribute on a function pointer, and gave an error. We still use a similar function pointer trick for getLocalPQBuffer() function, to use a thread-local version of that in parallel mode on Windows, but that dodges the problem because it doesn't take printf-like arguments.pull/4/head
parent
1cea9bbb21
commit
7800a71291
@ -0,0 +1,126 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* pg_backup_utils.c |
||||
* Utility routines shared by pg_dump and pg_restore |
||||
* |
||||
* |
||||
* Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* src/bin/pg_dump/pg_backup_utils.c |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#include "postgres_fe.h" |
||||
|
||||
#include "pg_backup_utils.h" |
||||
#include "parallel.h" |
||||
|
||||
/* Globals exported by this file */ |
||||
const char *progname = NULL; |
||||
|
||||
#define MAX_ON_EXIT_NICELY 20 |
||||
|
||||
static struct |
||||
{ |
||||
on_exit_nicely_callback function; |
||||
void *arg; |
||||
} on_exit_nicely_list[MAX_ON_EXIT_NICELY]; |
||||
|
||||
static int on_exit_nicely_index; |
||||
|
||||
/*
|
||||
* Parse a --section=foo command line argument. |
||||
* |
||||
* Set or update the bitmask in *dumpSections according to arg. |
||||
* dumpSections is initialised as DUMP_UNSECTIONED by pg_dump and |
||||
* pg_restore so they can know if this has even been called. |
||||
*/ |
||||
void |
||||
set_dump_section(const char *arg, int *dumpSections) |
||||
{ |
||||
/* if this is the first call, clear all the bits */ |
||||
if (*dumpSections == DUMP_UNSECTIONED) |
||||
*dumpSections = 0; |
||||
|
||||
if (strcmp(arg, "pre-data") == 0) |
||||
*dumpSections |= DUMP_PRE_DATA; |
||||
else if (strcmp(arg, "data") == 0) |
||||
*dumpSections |= DUMP_DATA; |
||||
else if (strcmp(arg, "post-data") == 0) |
||||
*dumpSections |= DUMP_POST_DATA; |
||||
else |
||||
{ |
||||
fprintf(stderr, _("%s: unrecognized section name: \"%s\"\n"), |
||||
progname, arg); |
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), |
||||
progname); |
||||
exit_nicely(1); |
||||
} |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* 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); |
||||
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); |
||||
} |
||||
|
||||
/* Register a callback to be run when exit_nicely is invoked. */ |
||||
void |
||||
on_exit_nicely(on_exit_nicely_callback function, void *arg) |
||||
{ |
||||
if (on_exit_nicely_index >= MAX_ON_EXIT_NICELY) |
||||
exit_horribly(NULL, "out of on_exit_nicely slots\n"); |
||||
on_exit_nicely_list[on_exit_nicely_index].function = function; |
||||
on_exit_nicely_list[on_exit_nicely_index].arg = arg; |
||||
on_exit_nicely_index++; |
||||
} |
||||
|
||||
/*
|
||||
* Run accumulated on_exit_nicely callbacks in reverse order and then exit |
||||
* quietly. This needs to be thread-safe. |
||||
*/ |
||||
void |
||||
exit_nicely(int code) |
||||
{ |
||||
int i; |
||||
|
||||
for (i = on_exit_nicely_index - 1; i >= 0; i--) |
||||
(*on_exit_nicely_list[i].function) (code, |
||||
on_exit_nicely_list[i].arg); |
||||
|
||||
#ifdef WIN32 |
||||
if (parallel_init_done && GetCurrentThreadId() != mainThreadId) |
||||
ExitThread(code); |
||||
#endif |
||||
|
||||
exit(code); |
||||
} |
||||
@ -0,0 +1,40 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* pg_backup_utils.h |
||||
* Utility routines shared by pg_dump and pg_restore. |
||||
* |
||||
* |
||||
* Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* src/bin/pg_dump/pg_backup_utils.h |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
|
||||
#ifndef PG_BACKUP_UTILS_H |
||||
#define PG_BACKUP_UTILS_H |
||||
|
||||
typedef enum /* bits returned by set_dump_section */ |
||||
{ |
||||
DUMP_PRE_DATA = 0x01, |
||||
DUMP_DATA = 0x02, |
||||
DUMP_POST_DATA = 0x04, |
||||
DUMP_UNSECTIONED = 0xff |
||||
} DumpSections; |
||||
|
||||
typedef void (*on_exit_nicely_callback) (int code, void *arg); |
||||
|
||||
extern const char *progname; |
||||
|
||||
extern void set_dump_section(const char *arg, int *dumpSections); |
||||
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 on_exit_nicely(on_exit_nicely_callback function, void *arg); |
||||
extern void exit_nicely(int code) __attribute__((noreturn)); |
||||
|
||||
#endif /* PG_BACKUP_UTILS_H */ |
||||
Loading…
Reference in new issue