6
* Copyright (c) 2010-2013, PostgreSQL Global Development Group
7
* contrib/pg_upgrade/util.c
10
#include "postgres_fe.h"
12
#include "pg_upgrade.h"
22
* Displays the result of an operation (ok, failed, error message,...)
25
report_status(eLogType type, const char *fmt,...)
28
char message[MAX_STRING];
31
vsnprintf(message, sizeof(message), fmt, args);
34
pg_log(type, "%s\n", message);
38
/* force blank output for progress display */
40
end_progress_output(void)
43
* In case nothing printed; pass a space so gcc doesn't complain about
44
* empty format string.
53
* Displays a message that describes an operation we are about to begin.
54
* We pad the message out to MESSAGE_WIDTH characters so that all of the "ok" and
55
* "failed" indicators line up nicely.
57
* A typical sequence would look like this:
58
* prep_status("about to flarb the next %d files", fileCount );
60
* if(( message = flarbFiles(fileCount)) == NULL)
61
* report_status(PG_REPORT, "ok" );
63
* pg_log(PG_FATAL, "failed - %s\n", message );
66
prep_status(const char *fmt,...)
69
char message[MAX_STRING];
72
vsnprintf(message, sizeof(message), fmt, args);
75
if (strlen(message) > 0 && message[strlen(message) - 1] == '\n')
76
pg_log(PG_REPORT, "%s", message);
78
/* trim strings that don't end in a newline */
79
pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
84
pg_log(eLogType type, char *fmt,...)
87
char message[MAX_STRING];
90
vsnprintf(message, sizeof(message), fmt, args);
93
/* PG_VERBOSE and PG_STATUS are only output in verbose mode */
94
/* fopen() on log_opts.internal might have failed, so check it */
95
if (((type != PG_VERBOSE && type != PG_STATUS) || log_opts.verbose) &&
96
log_opts.internal != NULL)
98
if (type == PG_STATUS)
99
/* status messages need two leading spaces and a newline */
100
fprintf(log_opts.internal, " %s\n", message);
102
fprintf(log_opts.internal, "%s", message);
103
fflush(log_opts.internal);
109
if (log_opts.verbose)
110
printf("%s", _(message));
114
/* for output to a display, do leading truncation and append \r */
115
if (isatty(fileno(stdout)))
116
/* -2 because we use a 2-space indent */
117
printf(" %s%-*.*s\r",
118
/* prefix with "..." if we do leading truncation */
119
strlen(message) <= MESSAGE_WIDTH - 2 ? "" : "...",
120
MESSAGE_WIDTH - 2, MESSAGE_WIDTH - 2,
121
/* optional leading truncation */
122
strlen(message) <= MESSAGE_WIDTH - 2 ? message :
123
message + strlen(message) - MESSAGE_WIDTH + 3 + 2);
125
printf(" %s\n", _(message));
130
printf("%s", _(message));
134
printf("\n%s", _(message));
135
printf("Failure, exiting\n");
150
report_status(PG_REPORT, "ok");
157
* Properly double-quote a SQL identifier.
159
* The result should be pg_free'd, but most callers don't bother because
160
* memory leakage is not a big deal in this program.
163
quote_identifier(const char *s)
165
char *result = pg_malloc(strlen(s) * 2 + 3);
185
* (copied from initdb.c) find the current user
188
get_user_info(char **user_name)
193
struct passwd *pw = getpwuid(geteuid());
196
#else /* the windows code */
202
struct passwd_win32 *pw = &pass_win32;
203
DWORD pwname_size = sizeof(pass_win32.pw_name) - 1;
205
GetUserName(pw->pw_name, &pwname_size);
210
*user_name = pg_strdup(pw->pw_name);
219
* Returns the text of the error message for the given error number
221
* This feature is factored into a separate function because it is
225
getErrorText(int errNum)
228
_dosmaperr(GetLastError());
230
return pg_strdup(strerror(errNum));
237
* convert string to oid
240
str2uint(const char *str)
242
return strtoul(str, NULL, 10);
249
* This is like putenv(), but takes two arguments.
250
* It also does unsetenv() if val is NULL.
253
pg_putenv(const char *var, const char *val)
258
char *envstr = (char *) pg_malloc(strlen(var) +
261
sprintf(envstr, "%s=%s", var, val);
265
* Do not free envstr because it becomes part of the environment on
266
* some operating systems. See port/unsetenv.c::unsetenv.
269
SetEnvironmentVariableA(var, val);
277
SetEnvironmentVariableA(var, "");