1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*
* util.c
*
* utility functions
*
* Copyright (c) 2010-2023, PostgreSQL Global Development Group
* src/bin/pg_upgrade/util.c
*/
#include "postgres_fe.h"
#include <signal.h>
#include "common/username.h"
#include "pg_upgrade.h"
LogOpts log_opts;
static void pg_log_v(eLogType type, const char *fmt, va_list ap) pg_attribute_printf(2, 0);
/*
* report_status()
*
* Displays the result of an operation (ok, failed, error message,...)
*
* This is no longer functionally different from pg_log(), but we keep
* it around to maintain a notational distinction between operation
* results and other messages.
*/
void
report_status(eLogType type, const char *fmt,...)
{
va_list args;
va_start(args, fmt);
pg_log_v(type, fmt, args);
va_end(args);
}
void
end_progress_output(void)
{
/*
* For output to a tty, erase prior contents of progress line. When either
* tty or verbose, indent so that report_status() output will align
* nicely.
*/
if (log_opts.isatty)
{
printf("\r");
pg_log(PG_REPORT_NONL, "%-*s", MESSAGE_WIDTH, "");
}
else if (log_opts.verbose)
pg_log(PG_REPORT_NONL, "%-*s", MESSAGE_WIDTH, "");
}
/*
* Remove any logs generated internally. To be used once when exiting.
*/
void
cleanup_output_dirs(void)
{
fclose(log_opts.internal);
/* Remove dump and log files? */
if (log_opts.retain)
return;
/*
* Try twice. The second time might wait for files to finish being
* unlinked, on Windows.
*/
if (!rmtree(log_opts.basedir, true))
rmtree(log_opts.basedir, true);
/* Remove pg_upgrade_output.d only if empty */
switch (pg_check_dir(log_opts.rootdir))
{
case 0: /* non-existent */
case 3: /* exists and contains a mount point */
Assert(false);
break;
case 1: /* exists and empty */
case 2: /* exists and contains only dot files */
/*
* Try twice. The second time might wait for files to finish
* being unlinked, on Windows.
*/
if (!rmtree(log_opts.rootdir, true))
rmtree(log_opts.rootdir, true);
break;
case 4: /* exists */
/*
* Keep the root directory as this includes some past log
* activity.
*/
break;
default:
/* different failure, just report it */
pg_log(PG_WARNING, "could not access directory \"%s\": %m",
log_opts.rootdir);
break;
}
}
/*
* prep_status
*
* Displays a message that describes an operation we are about to begin.
* We pad the message out to MESSAGE_WIDTH characters so that all of the
* "ok" and "failed" indicators line up nicely. (Overlength messages
* will be truncated, so don't get too verbose.)
*
* A typical sequence would look like this:
* prep_status("about to flarb the next %d files", fileCount);
* if ((message = flarbFiles(fileCount)) == NULL)
* report_status(PG_REPORT, "ok");
* else
* pg_log(PG_FATAL, "failed: %s", message);
*/
void
prep_status(const char *fmt,...)
{
va_list args;
char message[MAX_STRING];
va_start(args, fmt);
vsnprintf(message, sizeof(message), fmt, args);
va_end(args);
/* trim strings */
pg_log(PG_REPORT_NONL, "%-*s", MESSAGE_WIDTH, message);
}
/*
* prep_status_progress
*
* Like prep_status(), but for potentially longer running operations.
* Details about what item is currently being processed can be displayed
* with pg_log(PG_STATUS, ...). A typical sequence would look like this:
*
* prep_status_progress("copying files");
* for (...)
* pg_log(PG_STATUS, "%s", filename);
* end_progress_output();
* report_status(PG_REPORT, "ok");
*/
void
prep_status_progress(const char *fmt,...)
{
va_list args;
char message[MAX_STRING];
va_start(args, fmt);
vsnprintf(message, sizeof(message), fmt, args);
va_end(args);
/*
* If outputting to a tty or in verbose, append newline. pg_log_v() will
* put the individual progress items onto the next line.
*/
if (log_opts.isatty || log_opts.verbose)
pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
else
pg_log(PG_REPORT_NONL, "%-*s", MESSAGE_WIDTH, message);
}
static void
pg_log_v(eLogType type, const char *fmt, va_list ap)
{
char message[QUERY_ALLOC];
/* No incoming message should end in newline; we add that here. */
Assert(fmt);
Assert(fmt[0] == '\0' || fmt[strlen(fmt) - 1] != '\n');
vsnprintf(message, sizeof(message), _(fmt), ap);
/* PG_VERBOSE and PG_STATUS are only output in verbose mode */
/* fopen() on log_opts.internal might have failed, so check it */
if (((type != PG_VERBOSE && type != PG_STATUS) || log_opts.verbose) &&
log_opts.internal != NULL)
{
if (type == PG_STATUS)
/* status messages get two leading spaces, see below */
fprintf(log_opts.internal, " %s\n", message);
else if (type == PG_REPORT_NONL)
fprintf(log_opts.internal, "%s", message);
else
fprintf(log_opts.internal, "%s\n", message);
fflush(log_opts.internal);
}
switch (type)
{
case PG_VERBOSE:
if (log_opts.verbose)
printf("%s\n", message);
break;
case PG_STATUS:
/*
* For output to a terminal, we add two leading spaces and no
* newline; instead append \r so that the next message is output
* on the same line. Truncate on the left to fit into
* MESSAGE_WIDTH (counting the spaces as part of that).
*
* If going to non-interactive output, only display progress if
* verbose is enabled. Otherwise the output gets unreasonably
* large by default.
*/
if (log_opts.isatty)
{
bool itfits = (strlen(message) <= MESSAGE_WIDTH - 2);
/* prefix with "..." if we do leading truncation */
printf(" %s%-*.*s\r",
itfits ? "" : "...",
MESSAGE_WIDTH - 2, MESSAGE_WIDTH - 2,
itfits ? message :
message + strlen(message) - MESSAGE_WIDTH + 3 + 2);
}
else if (log_opts.verbose)
printf(" %s\n", message);
break;
case PG_REPORT_NONL:
/* This option is for use by prep_status and friends */
printf("%s", message);
break;
case PG_REPORT:
case PG_WARNING:
printf("%s\n", message);
break;
case PG_FATAL:
/* Extra newline in case we're interrupting status output */
printf("\n%s\n", message);
printf(_("Failure, exiting\n"));
exit(1);
break;
/* No default:, we want a warning for omitted cases */
}
fflush(stdout);
}
void
pg_log(eLogType type, const char *fmt,...)
{
va_list args;
va_start(args, fmt);
pg_log_v(type, fmt, args);
va_end(args);
}
void
pg_fatal(const char *fmt,...)
{
va_list args;
va_start(args, fmt);
pg_log_v(PG_FATAL, fmt, args);
va_end(args);
/* NOTREACHED */
printf(_("Failure, exiting\n"));
exit(1);
}
void
check_ok(void)
{
/* all seems well */
report_status(PG_REPORT, "ok");
}
/*
* quote_identifier()
* Properly double-quote a SQL identifier.
*
* The result should be pg_free'd, but most callers don't bother because
* memory leakage is not a big deal in this program.
*/
char *
quote_identifier(const char *s)
{
char *result = pg_malloc(strlen(s) * 2 + 3);
char *r = result;
*r++ = '"';
while (*s)
{
if (*s == '"')
*r++ = *s;
*r++ = *s;
s++;
}
*r++ = '"';
*r++ = '\0';
return result;
}
/*
* get_user_info()
*/
int
get_user_info(char **user_name_p)
{
int user_id;
const char *user_name;
char *errstr;
#ifndef WIN32
user_id = geteuid();
#else
user_id = 1;
#endif
user_name = get_user_name(&errstr);
if (!user_name)
pg_fatal("%s", errstr);
/* make a copy */
*user_name_p = pg_strdup(user_name);
return user_id;
}
/*
* str2uint()
*
* convert string to oid
*/
unsigned int
str2uint(const char *str)
{
return strtoul(str, NULL, 10);
}
|