Consistently use "superuser" instead of "super user"
[pgsql.git] / src / bin / pg_upgrade / util.c
blobfc20472fe7b8c9f3a3dc1f278246f463792665db
1 /*
2 * util.c
4 * utility functions
6 * Copyright (c) 2010-2021, PostgreSQL Global Development Group
7 * src/bin/pg_upgrade/util.c
8 */
10 #include "postgres_fe.h"
12 #include <signal.h>
14 #include "common/username.h"
15 #include "pg_upgrade.h"
17 LogOpts log_opts;
19 static void pg_log_v(eLogType type, const char *fmt, va_list ap) pg_attribute_printf(2, 0);
23 * report_status()
25 * Displays the result of an operation (ok, failed, error message,...)
27 void
28 report_status(eLogType type, const char *fmt,...)
30 va_list args;
31 char message[MAX_STRING];
33 va_start(args, fmt);
34 vsnprintf(message, sizeof(message), fmt, args);
35 va_end(args);
37 pg_log(type, "%s\n", message);
41 /* force blank output for progress display */
42 void
43 end_progress_output(void)
46 * In case nothing printed; pass a space so gcc doesn't complain about
47 * empty format string.
49 prep_status(" ");
54 * prep_status
56 * Displays a message that describes an operation we are about to begin.
57 * We pad the message out to MESSAGE_WIDTH characters so that all of the "ok" and
58 * "failed" indicators line up nicely.
60 * A typical sequence would look like this:
61 * prep_status("about to flarb the next %d files", fileCount );
63 * if(( message = flarbFiles(fileCount)) == NULL)
64 * report_status(PG_REPORT, "ok" );
65 * else
66 * pg_log(PG_FATAL, "failed - %s\n", message );
68 void
69 prep_status(const char *fmt,...)
71 va_list args;
72 char message[MAX_STRING];
74 va_start(args, fmt);
75 vsnprintf(message, sizeof(message), fmt, args);
76 va_end(args);
78 if (strlen(message) > 0 && message[strlen(message) - 1] == '\n')
79 pg_log(PG_REPORT, "%s", message);
80 else
81 /* trim strings that don't end in a newline */
82 pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
86 static void
87 pg_log_v(eLogType type, const char *fmt, va_list ap)
89 char message[QUERY_ALLOC];
91 vsnprintf(message, sizeof(message), _(fmt), ap);
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);
101 else
102 fprintf(log_opts.internal, "%s", message);
103 fflush(log_opts.internal);
106 switch (type)
108 case PG_VERBOSE:
109 if (log_opts.verbose)
110 printf("%s", message);
111 break;
113 case PG_STATUS:
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);
124 else
125 printf(" %s\n", message);
126 break;
128 case PG_REPORT:
129 case PG_WARNING:
130 printf("%s", message);
131 break;
133 case PG_FATAL:
134 printf("\n%s", message);
135 printf(_("Failure, exiting\n"));
136 exit(1);
137 break;
139 default:
140 break;
142 fflush(stdout);
146 void
147 pg_log(eLogType type, const char *fmt,...)
149 va_list args;
151 va_start(args, fmt);
152 pg_log_v(type, fmt, args);
153 va_end(args);
157 void
158 pg_fatal(const char *fmt,...)
160 va_list args;
162 va_start(args, fmt);
163 pg_log_v(PG_FATAL, fmt, args);
164 va_end(args);
165 printf(_("Failure, exiting\n"));
166 exit(1);
170 void
171 check_ok(void)
173 /* all seems well */
174 report_status(PG_REPORT, "ok");
175 fflush(stdout);
180 * quote_identifier()
181 * Properly double-quote a SQL identifier.
183 * The result should be pg_free'd, but most callers don't bother because
184 * memory leakage is not a big deal in this program.
186 char *
187 quote_identifier(const char *s)
189 char *result = pg_malloc(strlen(s) * 2 + 3);
190 char *r = result;
192 *r++ = '"';
193 while (*s)
195 if (*s == '"')
196 *r++ = *s;
197 *r++ = *s;
198 s++;
200 *r++ = '"';
201 *r++ = '\0';
203 return result;
208 * get_user_info()
211 get_user_info(char **user_name_p)
213 int user_id;
214 const char *user_name;
215 char *errstr;
217 #ifndef WIN32
218 user_id = geteuid();
219 #else
220 user_id = 1;
221 #endif
223 user_name = get_user_name(&errstr);
224 if (!user_name)
225 pg_fatal("%s\n", errstr);
227 /* make a copy */
228 *user_name_p = pg_strdup(user_name);
230 return user_id;
235 * str2uint()
237 * convert string to oid
239 unsigned int
240 str2uint(const char *str)
242 return strtoul(str, NULL, 10);