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 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 0)))
86 pg_log_v(eLogType type, const char *fmt, va_list ap)
88 char message[MAX_STRING];
90 vsnprintf(message, sizeof(message), fmt, ap);
92 /* PG_VERBOSE and PG_STATUS are only output in verbose mode */
93 /* fopen() on log_opts.internal might have failed, so check it */
94 if (((type != PG_VERBOSE && type != PG_STATUS) || log_opts.verbose) &&
95 log_opts.internal != NULL)
97 if (type == PG_STATUS)
98 /* status messages need two leading spaces and a newline */
99 fprintf(log_opts.internal, " %s\n", message);
101 fprintf(log_opts.internal, "%s", message);
102 fflush(log_opts.internal);
108 if (log_opts.verbose)
109 printf("%s", _(message));
113 /* for output to a display, do leading truncation and append \r */
114 if (isatty(fileno(stdout)))
115 /* -2 because we use a 2-space indent */
116 printf(" %s%-*.*s\r",
117 /* prefix with "..." if we do leading truncation */
118 strlen(message) <= MESSAGE_WIDTH - 2 ? "" : "...",
119 MESSAGE_WIDTH - 2, MESSAGE_WIDTH - 2,
120 /* optional leading truncation */
121 strlen(message) <= MESSAGE_WIDTH - 2 ? message :
122 message + strlen(message) - MESSAGE_WIDTH + 3 + 2);
124 printf(" %s\n", _(message));
129 printf("%s", _(message));
133 printf("\n%s", _(message));
144 pg_log(eLogType type, const char *fmt,...)
149 pg_log_v(type, fmt, args);
155 pg_fatal(const char *fmt,...)
160 pg_log_v(PG_FATAL, fmt, args);
162 printf("Failure, exiting\n");
171 report_status(PG_REPORT, "ok");
178 * Properly double-quote a SQL identifier.
180 * The result should be pg_free'd, but most callers don't bother because
181 * memory leakage is not a big deal in this program.
184 quote_identifier(const char *s)
186 char *result = pg_malloc(strlen(s) * 2 + 3);
206 * (copied from initdb.c) find the current user
209 get_user_info(char **user_name)
214 struct passwd *pw = getpwuid(geteuid());
217 #else /* the windows code */
223 struct passwd_win32 *pw = &pass_win32;
224 DWORD pwname_size = sizeof(pass_win32.pw_name) - 1;
226 GetUserName(pw->pw_name, &pwname_size);
231 *user_name = pg_strdup(pw->pw_name);
240 * Returns the text of the error message for the given error number
242 * This feature is factored into a separate function because it is
246 getErrorText(int errNum)
249 _dosmaperr(GetLastError());
251 return pg_strdup(strerror(errNum));
258 * convert string to oid
261 str2uint(const char *str)
263 return strtoul(str, NULL, 10);
270 * This is like putenv(), but takes two arguments.
271 * It also does unsetenv() if val is NULL.
274 pg_putenv(const char *var, const char *val)
281 envstr = psprintf("%s=%s", var, val);
285 * Do not free envstr because it becomes part of the environment on
286 * some operating systems. See port/unsetenv.c::unsetenv.
289 SetEnvironmentVariableA(var, val);
297 SetEnvironmentVariableA(var, "");