PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_backup_utils.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_backup_utils.c
4  * Utility routines shared by pg_dump and pg_restore
5  *
6  *
7  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/bin/pg_dump/pg_backup_utils.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres_fe.h"
15 
16 #include "parallel.h"
17 #include "pg_backup_utils.h"
18 
19 /* Globals exported by this file */
20 const char *progname = NULL;
21 
22 #define MAX_ON_EXIT_NICELY 20
23 
24 static struct
25 {
27  void *arg;
29 
31 
32 /*
33  * Parse a --section=foo command line argument.
34  *
35  * Set or update the bitmask in *dumpSections according to arg.
36  * dumpSections is initialised as DUMP_UNSECTIONED by pg_dump and
37  * pg_restore so they can know if this has even been called.
38  */
39 void
40 set_dump_section(const char *arg, int *dumpSections)
41 {
42  /* if this is the first call, clear all the bits */
43  if (*dumpSections == DUMP_UNSECTIONED)
44  *dumpSections = 0;
45 
46  if (strcmp(arg, "pre-data") == 0)
47  *dumpSections |= DUMP_PRE_DATA;
48  else if (strcmp(arg, "data") == 0)
49  *dumpSections |= DUMP_DATA;
50  else if (strcmp(arg, "post-data") == 0)
51  *dumpSections |= DUMP_POST_DATA;
52  else
53  {
54  fprintf(stderr, _("%s: unrecognized section name: \"%s\"\n"),
55  progname, arg);
56  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
57  progname);
58  exit_nicely(1);
59  }
60 }
61 
62 
63 /*
64  * Write a printf-style message to stderr.
65  *
66  * The program name is prepended, if "progname" has been set.
67  * Also, if modulename isn't NULL, that's included too.
68  * Note that we'll try to translate the modulename and the fmt string.
69  */
70 void
71 write_msg(const char *modulename, const char *fmt,...)
72 {
73  va_list ap;
74 
75  va_start(ap, fmt);
76  vwrite_msg(modulename, fmt, ap);
77  va_end(ap);
78 }
79 
80 /*
81  * As write_msg, but pass a va_list not variable arguments.
82  */
83 void
84 vwrite_msg(const char *modulename, const char *fmt, va_list ap)
85 {
86  if (progname)
87  {
88  if (modulename)
89  fprintf(stderr, "%s: [%s] ", progname, _(modulename));
90  else
91  fprintf(stderr, "%s: ", progname);
92  }
93  vfprintf(stderr, _(fmt), ap);
94 }
95 
96 /* Register a callback to be run when exit_nicely is invoked. */
97 void
99 {
101  exit_horribly(NULL, "out of on_exit_nicely slots\n");
102  on_exit_nicely_list[on_exit_nicely_index].function = function;
105 }
106 
107 /*
108  * Run accumulated on_exit_nicely callbacks in reverse order and then exit
109  * quietly. This needs to be thread-safe.
110  */
111 void
112 exit_nicely(int code)
113 {
114  int i;
115 
116  for (i = on_exit_nicely_index - 1; i >= 0; i--)
117  (*on_exit_nicely_list[i].function) (code,
118  on_exit_nicely_list[i].arg);
119 
120 #ifdef WIN32
121  if (parallel_init_done && GetCurrentThreadId() != mainThreadId)
122  ExitThread(code);
123 #endif
124 
125  exit(code);
126 }
void on_exit_nicely(on_exit_nicely_callback function, void *arg)
#define MAX_ON_EXIT_NICELY
static const char * modulename
Definition: compress_io.c:79
void exit_nicely(int code)
static struct @30 on_exit_nicely_list[MAX_ON_EXIT_NICELY]
void(* on_exit_nicely_callback)(int code, void *arg)
const char * progname
void set_dump_section(const char *arg, int *dumpSections)
void write_msg(const char *modulename, const char *fmt,...)
#define NULL
Definition: c.h:215
int i
static int on_exit_nicely_index
void * arg
#define _(x)
Definition: elog.c:83
void vwrite_msg(const char *modulename, const char *fmt, va_list ap)
void exit_horribly(const char *modulename, const char *fmt,...)
Definition: parallel.c:175