6
* Copyright (c) 2010-2011, PostgreSQL Global Development Group
7
* contrib/pg_upgrade/util.c
10
#include "pg_upgrade.h"
20
* Displays the result of an operation (ok, failed, error message,...)
23
report_status(eLogType type, const char *fmt,...)
26
char message[MAX_STRING];
29
vsnprintf(message, sizeof(message), fmt, args);
32
pg_log(type, "%s\n", message);
39
* Displays a message that describes an operation we are about to begin.
40
* We pad the message out to MESSAGE_WIDTH characters so that all of the "ok" and
41
* "failed" indicators line up nicely.
43
* A typical sequence would look like this:
44
* prep_status("about to flarb the next %d files", fileCount );
46
* if(( message = flarbFiles(fileCount)) == NULL)
47
* report_status(PG_REPORT, "ok" );
49
* pg_log(PG_FATAL, "failed - %s", message );
52
prep_status(const char *fmt,...)
55
char message[MAX_STRING];
58
vsnprintf(message, sizeof(message), fmt, args);
61
if (strlen(message) > 0 && message[strlen(message) - 1] == '\n')
62
pg_log(PG_REPORT, "%s", message);
64
pg_log(PG_REPORT, "%-" MESSAGE_WIDTH "s", message);
69
pg_log(eLogType type, char *fmt,...)
72
char message[MAX_STRING];
75
vsnprintf(message, sizeof(message), fmt, args);
78
if (log_opts.fd != NULL)
80
fwrite(message, strlen(message), 1, log_opts.fd);
81
/* if we are using OVERWRITE_MESSAGE, add newline */
82
if (strchr(message, '\r') != NULL)
83
fwrite("\n", 1, 1, log_opts.fd);
91
printf("%s", _(message));
96
printf("%s", _(message));
101
printf("%s", _(message));
102
printf("Failure, exiting\n");
108
fprintf(log_opts.debug_fd, "%s\n", _(message));
122
report_status(PG_REPORT, "ok");
129
* Properly double-quote a SQL identifier.
131
* The result should be pg_free'd, but most callers don't bother because
132
* memory leakage is not a big deal in this program.
135
quote_identifier(const char *s)
137
char *result = pg_malloc(strlen(s) * 2 + 3);
157
* (copied from initdb.c) find the current user
160
get_user_info(char **user_name)
165
struct passwd *pw = getpwuid(geteuid());
168
#else /* the windows code */
174
struct passwd_win32 *pw = &pass_win32;
175
DWORD pwname_size = sizeof(pass_win32.pw_name) - 1;
177
GetUserName(pw->pw_name, &pwname_size);
182
*user_name = pg_strdup(pw->pw_name);
194
pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
209
pg_strdup(const char *s)
211
char *result = strdup(s);
214
pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
223
* Returns the text of the error message for the given error number
225
* This feature is factored into a separate function because it is
229
getErrorText(int errNum)
232
_dosmaperr(GetLastError());
234
return strdup(strerror(errNum));
241
* convert string to oid
244
str2uint(const char *str)
246
return strtoul(str, NULL, 10);