6 * Copyright (c) 2010, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/util.c
10 #include "pg_upgrade.h"
18 * Displays the result of an operation (ok, failed, error message,...)
21 report_status(migratorContext *ctx, eLogType type, const char *fmt,...)
24 char message[MAX_STRING];
27 vsnprintf(message, sizeof(message), fmt, args);
30 pg_log(ctx, type, "%s\n", message);
37 * Displays a message that describes an operation we are about to begin.
38 * We pad the message out to MESSAGE_WIDTH characters so that all of the "ok" and
39 * "failed" indicators line up nicely.
41 * A typical sequence would look like this:
42 * prep_status(&ctx, "about to flarb the next %d files", fileCount );
44 * if(( message = flarbFiles(fileCount)) == NULL)
45 * report_status(ctx, PG_REPORT, "ok" );
47 * pg_log(ctx, PG_FATAL, "failed - %s", message );
50 prep_status(migratorContext *ctx, const char *fmt,...)
53 char message[MAX_STRING];
56 vsnprintf(message, sizeof(message), fmt, args);
59 if (strlen(message) > 0 && message[strlen(message) - 1] == '\n')
60 pg_log(ctx, PG_REPORT, "%s", message);
62 pg_log(ctx, PG_REPORT, "%-" MESSAGE_WIDTH "s", message);
67 pg_log(migratorContext *ctx, eLogType type, char *fmt,...)
70 char message[MAX_STRING];
73 vsnprintf(message, sizeof(message), fmt, args);
76 if (ctx->log_fd != NULL)
78 fwrite(message, strlen(message), 1, ctx->log_fd);
79 /* if we are using OVERWRITE_MESSAGE, add newline */
80 if (strchr(message, '\r') != NULL)
81 fwrite("\n", 1, 1, ctx->log_fd);
89 printf("%s", _(message));
94 printf("%s", _(message));
99 printf("%s", _(message));
100 exit_nicely(ctx, true);
105 fprintf(ctx->debug_fd, "%s\n", _(message));
116 check_ok(migratorContext *ctx)
119 report_status(ctx, PG_REPORT, "ok");
126 * Properly double-quote a SQL identifier.
128 * The result should be pg_free'd, but most callers don't bother because
129 * memory leakage is not a big deal in this program.
132 quote_identifier(migratorContext *ctx, const char *s)
134 char *result = pg_malloc(ctx, strlen(s) * 2 + 3);
154 * (copied from initdb.c) find the current user
157 get_user_info(migratorContext *ctx, char **user_name)
162 struct passwd *pw = getpwuid(geteuid());
165 #else /* the windows code */
171 struct passwd_win32 *pw = &pass_win32;
172 DWORD pwname_size = sizeof(pass_win32.pw_name) - 1;
174 GetUserName(pw->pw_name, &pwname_size);
179 *user_name = pg_strdup(ctx, pw->pw_name);
186 exit_nicely(migratorContext *ctx, bool need_cleanup)
188 stop_postmaster(ctx, true, true);
190 pg_free(ctx->logfile);
196 fclose(ctx->debug_fd);
198 /* terminate any running instance of postmaster */
199 if (ctx->postmasterPID != 0)
200 kill(ctx->postmasterPID, SIGTERM);
205 * FIXME must delete intermediate files
215 pg_malloc(migratorContext *ctx, int n)
220 pg_log(ctx, PG_FATAL, "%s: out of memory\n", ctx->progname);
235 pg_strdup(migratorContext *ctx, const char *s)
237 char *result = strdup(s);
240 pg_log(ctx, PG_FATAL, "%s: out of memory\n", ctx->progname);
249 * Returns the text of the error message for the given error number
251 * This feature is factored into a separate function because it is
255 getErrorText(int errNum)
258 _dosmaperr(GetLastError());
260 return strdup(strerror(errNum));
267 * convert string to oid
270 str2uint(const char *str)
272 return strtoul(str, NULL, 10);