PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
util.c File Reference
#include "postgres_fe.h"
#include <signal.h>
#include "common/username.h"
#include "pg_upgrade.h"
Include dependency graph for util.c:

Go to the source code of this file.

Functions

static void pg_log_v (eLogType type, const char *fmt, va_list ap) pg_attribute_printf(2
 
static void void report_status (eLogType type, const char *fmt,...)
 
void end_progress_output (void)
 
void cleanup_output_dirs (void)
 
void prep_status (const char *fmt,...)
 
void prep_status_progress (const char *fmt,...)
 
void pg_log (eLogType type, const char *fmt,...)
 
void pg_fatal (const char *fmt,...)
 
void check_ok (void)
 
char * quote_identifier (const char *s)
 
int get_user_info (char **user_name_p)
 
unsigned int str2uint (const char *str)
 

Variables

LogOpts log_opts
 

Function Documentation

◆ check_ok()

void check_ok ( void  )

Definition at line 284 of file util.c.

285{
286 /* all seems well */
288}
static void void report_status(eLogType type, const char *fmt,...)
Definition: util.c:32
@ PG_REPORT
Definition: pg_upgrade.h:276

References PG_REPORT, and report_status().

◆ cleanup_output_dirs()

void cleanup_output_dirs ( void  )

Definition at line 63 of file util.c.

64{
65 fclose(log_opts.internal);
66
67 /* Remove dump and log files? */
68 if (log_opts.retain)
69 return;
70
71 /*
72 * Try twice. The second time might wait for files to finish being
73 * unlinked, on Windows.
74 */
75 if (!rmtree(log_opts.basedir, true))
76 rmtree(log_opts.basedir, true);
77
78 /* Remove pg_upgrade_output.d only if empty */
80 {
81 case 0: /* non-existent */
82 case 3: /* exists and contains a mount point */
83 Assert(false);
84 break;
85
86 case 1: /* exists and empty */
87 case 2: /* exists and contains only dot files */
88
89 /*
90 * Try twice. The second time might wait for files to finish
91 * being unlinked, on Windows.
92 */
93 if (!rmtree(log_opts.rootdir, true))
94 rmtree(log_opts.rootdir, true);
95 break;
96
97 case 4: /* exists */
98
99 /*
100 * Keep the root directory as this includes some past log
101 * activity.
102 */
103 break;
104
105 default:
106 /* different failure, just report it */
107 pg_log(PG_WARNING, "could not access directory \"%s\": %m",
109 break;
110 }
111}
void pg_log(eLogType type, const char *fmt,...)
Definition: util.c:259
LogOpts log_opts
Definition: util.c:17
Assert(PointerIsAligned(start, uint64))
@ PG_WARNING
Definition: pg_upgrade.h:277
int pg_check_dir(const char *dir)
Definition: pgcheckdir.c:33
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:50
char * rootdir
Definition: pg_upgrade.h:317
bool retain
Definition: pg_upgrade.h:315
FILE * internal
Definition: pg_upgrade.h:313
char * basedir
Definition: pg_upgrade.h:318

References Assert(), LogOpts::basedir, LogOpts::internal, log_opts, pg_check_dir(), pg_log(), PG_WARNING, LogOpts::retain, rmtree(), and LogOpts::rootdir.

Referenced by main(), and report_clusters_compatible().

◆ end_progress_output()

void end_progress_output ( void  )

Definition at line 43 of file util.c.

44{
45 /*
46 * For output to a tty, erase prior contents of progress line. When either
47 * tty or verbose, indent so that report_status() output will align
48 * nicely.
49 */
50 if (log_opts.isatty)
51 {
52 printf("\r");
54 }
55 else if (log_opts.verbose)
57}
#define MESSAGE_WIDTH
Definition: pg_upgrade.h:25
@ PG_REPORT_NONL
Definition: pg_upgrade.h:275
#define printf(...)
Definition: port.h:245
bool isatty
Definition: pg_upgrade.h:321
bool verbose
Definition: pg_upgrade.h:314

References LogOpts::isatty, log_opts, MESSAGE_WIDTH, pg_log(), PG_REPORT_NONL, printf, and LogOpts::verbose.

Referenced by create_logical_replication_slots(), create_new_objects(), generate_old_dump(), and transfer_all_new_tablespaces().

◆ get_user_info()

int get_user_info ( char **  user_name_p)

Definition at line 323 of file util.c.

324{
325 int user_id;
326 const char *user_name;
327 char *errstr;
328
329#ifndef WIN32
330 user_id = geteuid();
331#else
332 user_id = 1;
333#endif
334
335 user_name = get_user_name(&errstr);
336 if (!user_name)
337 pg_fatal("%s", errstr);
338
339 /* make a copy */
340 *user_name_p = pg_strdup(user_name);
341
342 return user_id;
343}
void pg_fatal(const char *fmt,...)
Definition: util.c:270
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
const char * get_user_name(char **errstr)
Definition: username.c:31

References get_user_name(), pg_fatal(), and pg_strdup().

Referenced by parseCommandLine().

◆ pg_fatal()

void pg_fatal ( const char *  fmt,
  ... 
)

Definition at line 270 of file util.c.

271{
272 va_list args;
273
274 va_start(args, fmt);
275 pg_log_v(PG_FATAL, fmt, args);
276 va_end(args);
277 /* NOTREACHED */
278 printf(_("Failure, exiting\n"));
279 exit(1);
280}
static void pg_log_v(eLogType type, const char *fmt, va_list ap) pg_attribute_printf(2
Definition: util.c:176
#define _(x)
Definition: elog.c:90
@ PG_FATAL
Definition: pg_upgrade.h:278

References _, generate_unaccent_rules::args, PG_FATAL, pg_log_v(), and printf.

Referenced by get_user_info().

◆ pg_log()

void pg_log ( eLogType  type,
const char *  fmt,
  ... 
)

Definition at line 259 of file util.c.

260{
261 va_list args;
262
263 va_start(args, fmt);
264 pg_log_v(type, fmt, args);
265 va_end(args);
266}
const char * type

References generate_unaccent_rules::args, pg_log_v(), and type.

Referenced by cleanup_output_dirs(), end_progress_output(), prep_status(), and prep_status_progress().

◆ pg_log_v()

static void pg_log_v ( eLogType  type,
const char *  fmt,
va_list  ap 
)
static

Definition at line 176 of file util.c.

177{
178 char message[QUERY_ALLOC];
179
180 /* No incoming message should end in newline; we add that here. */
181 Assert(fmt);
182 Assert(fmt[0] == '\0' || fmt[strlen(fmt) - 1] != '\n');
183
184 vsnprintf(message, sizeof(message), _(fmt), ap);
185
186 /* PG_VERBOSE and PG_STATUS are only output in verbose mode */
187 /* fopen() on log_opts.internal might have failed, so check it */
188 if (((type != PG_VERBOSE && type != PG_STATUS) || log_opts.verbose) &&
189 log_opts.internal != NULL)
190 {
191 if (type == PG_STATUS)
192 /* status messages get two leading spaces, see below */
193 fprintf(log_opts.internal, " %s\n", message);
194 else if (type == PG_REPORT_NONL)
195 fprintf(log_opts.internal, "%s", message);
196 else
197 fprintf(log_opts.internal, "%s\n", message);
198 fflush(log_opts.internal);
199 }
200
201 switch (type)
202 {
203 case PG_VERBOSE:
204 if (log_opts.verbose)
205 printf("%s\n", message);
206 break;
207
208 case PG_STATUS:
209
210 /*
211 * For output to a terminal, we add two leading spaces and no
212 * newline; instead append \r so that the next message is output
213 * on the same line. Truncate on the left to fit into
214 * MESSAGE_WIDTH (counting the spaces as part of that).
215 *
216 * If going to non-interactive output, only display progress if
217 * verbose is enabled. Otherwise the output gets unreasonably
218 * large by default.
219 */
220 if (log_opts.isatty)
221 {
222 bool itfits = (strlen(message) <= MESSAGE_WIDTH - 2);
223
224 /* prefix with "..." if we do leading truncation */
225 printf(" %s%-*.*s\r",
226 itfits ? "" : "...",
228 itfits ? message :
229 message + strlen(message) - MESSAGE_WIDTH + 3 + 2);
230 }
231 else if (log_opts.verbose)
232 printf(" %s\n", message);
233 break;
234
235 case PG_REPORT_NONL:
236 /* This option is for use by prep_status and friends */
237 printf("%s", message);
238 break;
239
240 case PG_REPORT:
241 case PG_WARNING:
242 printf("%s\n", message);
243 break;
244
245 case PG_FATAL:
246 /* Extra newline in case we're interrupting status output */
247 printf("\n%s\n", message);
248 printf(_("Failure, exiting\n"));
249 exit(1);
250 break;
251
252 /* No default:, we want a warning for omitted cases */
253 }
254 fflush(stdout);
255}
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
#define QUERY_ALLOC
Definition: pg_upgrade.h:23
@ PG_STATUS
Definition: pg_upgrade.h:274
@ PG_VERBOSE
Definition: pg_upgrade.h:273
#define vsnprintf
Definition: port.h:238

References _, Assert(), fprintf, LogOpts::internal, LogOpts::isatty, log_opts, MESSAGE_WIDTH, PG_FATAL, PG_REPORT, PG_REPORT_NONL, PG_STATUS, PG_VERBOSE, PG_WARNING, printf, QUERY_ALLOC, generate_unaccent_rules::stdout, type, LogOpts::verbose, and vsnprintf.

Referenced by pg_fatal(), pg_log(), and report_status().

◆ prep_status()

void prep_status ( const char *  fmt,
  ... 
)

Definition at line 129 of file util.c.

130{
131 va_list args;
132 char message[MAX_STRING];
133
134 va_start(args, fmt);
135 vsnprintf(message, sizeof(message), fmt, args);
136 va_end(args);
137
138 /* trim strings */
139 pg_log(PG_REPORT_NONL, "%-*s", MESSAGE_WIDTH, message);
140}
#define MAX_STRING
Definition: pg_upgrade.h:22

References generate_unaccent_rules::args, MAX_STRING, MESSAGE_WIDTH, pg_log(), PG_REPORT_NONL, and vsnprintf.

◆ prep_status_progress()

void prep_status_progress ( const char *  fmt,
  ... 
)

Definition at line 156 of file util.c.

157{
158 va_list args;
159 char message[MAX_STRING];
160
161 va_start(args, fmt);
162 vsnprintf(message, sizeof(message), fmt, args);
163 va_end(args);
164
165 /*
166 * If outputting to a tty or in verbose, append newline. pg_log_v() will
167 * put the individual progress items onto the next line.
168 */
170 pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
171 else
172 pg_log(PG_REPORT_NONL, "%-*s", MESSAGE_WIDTH, message);
173}

References generate_unaccent_rules::args, LogOpts::isatty, log_opts, MAX_STRING, MESSAGE_WIDTH, pg_log(), PG_REPORT, PG_REPORT_NONL, LogOpts::verbose, and vsnprintf.

◆ quote_identifier()

char * quote_identifier ( const char *  s)

Definition at line 299 of file util.c.

300{
301 char *result = pg_malloc(strlen(s) * 2 + 3);
302 char *r = result;
303
304 *r++ = '"';
305 while (*s)
306 {
307 if (*s == '"')
308 *r++ = *s;
309 *r++ = *s;
310 s++;
311 }
312 *r++ = '"';
313 *r++ = '\0';
314
315 return result;
316}
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47

References pg_malloc().

◆ report_status()

static void void report_status ( eLogType  type,
const char *  fmt,
  ... 
)

Definition at line 32 of file util.c.

33{
34 va_list args;
35
36 va_start(args, fmt);
37 pg_log_v(type, fmt, args);
38 va_end(args);
39}

References generate_unaccent_rules::args, pg_log_v(), and type.

Referenced by check_ok().

◆ str2uint()

unsigned int str2uint ( const char *  str)

Definition at line 352 of file util.c.

353{
354 return strtoul(str, NULL, 10);
355}
const char * str

References str.

Referenced by get_control_data().

Variable Documentation

◆ log_opts