7 #include "pg_upgrade.h"
9 #include "getopt_long.h"
16 static void usage(migratorContext *ctx);
17 static void validateDirectoryOption(migratorContext *ctx, char **dirpath,
18 char *envVarName, char *cmdLineOption, char *description);
19 static void get_pkglibdirs(migratorContext *ctx);
20 static char *get_pkglibdir(migratorContext *ctx, const char *bindir);
26 * Parses the command line (argc, argv[]) into the given migratorContext object
27 * and initializes the rest of the object.
30 parseCommandLine(migratorContext *ctx, int argc, char *argv[])
32 static struct option long_options[] = {
33 {"old-datadir", required_argument, NULL, 'd'},
34 {"new-datadir", required_argument, NULL, 'D'},
35 {"old-bindir", required_argument, NULL, 'b'},
36 {"new-bindir", required_argument, NULL, 'B'},
37 {"old-port", required_argument, NULL, 'p'},
38 {"new-port", required_argument, NULL, 'P'},
40 {"user", required_argument, NULL, 'u'},
41 {"check", no_argument, NULL, 'c'},
42 {"debug", no_argument, NULL, 'g'},
43 {"debugfile", required_argument, NULL, 'G'},
44 {"link", no_argument, NULL, 'k'},
45 {"logfile", required_argument, NULL, 'l'},
46 {"verbose", no_argument, NULL, 'v'},
49 char option; /* Command line option */
50 int optindex = 0; /* used by getopt_long */
56 ctx->user = pg_strdup(ctx, getenv("PGUSER"));
59 ctx->progname = get_progname(argv[0]);
60 ctx->old.port = getenv("PGPORT") ? atoi(getenv("PGPORT")) : DEF_PGPORT;
61 ctx->new.port = getenv("PGPORT") ? atoi(getenv("PGPORT")) : DEF_PGPORT;
62 /* must save value, getenv()'s pointer is not stable */
64 ctx->transfer_mode = TRANSFER_MODE_COPY;
66 /* user lookup and 'root' test must be split because of usage() */
67 user_id = get_user_info(ctx, &ctx->user);
71 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 ||
72 strcmp(argv[1], "-?") == 0)
75 exit_nicely(ctx, false);
77 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
79 pg_log(ctx, PG_REPORT, "pg_upgrade " PG_VERSION "\n");
80 exit_nicely(ctx, false);
85 pg_log(ctx, PG_FATAL, "%s: cannot be run as root\n", ctx->progname);
88 get_home_path(ctx->home_dir);
93 /* TMP is the best place on Windows, rather than APPDATA */
94 if ((tmppath = getenv("TMP")) == NULL)
95 pg_log(ctx, PG_FATAL, "TMP environment variable is not set.\n");
96 snprintf(ctx->home_dir, MAXPGPATH, "%s", tmppath);
100 snprintf(ctx->output_dir, MAXPGPATH, "%s/" OUTPUT_SUBDIR, ctx->home_dir);
102 while ((option = getopt_long(argc, argv, "d:D:b:B:cgG:kl:p:P:u:v",
103 long_options, &optindex)) != -1)
108 ctx->old.pgdata = pg_strdup(ctx, optarg);
112 ctx->new.pgdata = pg_strdup(ctx, optarg);
116 ctx->old.bindir = pg_strdup(ctx, optarg);
120 ctx->new.bindir = pg_strdup(ctx, optarg);
128 pg_log(ctx, PG_REPORT, "Running in debug mode\n");
133 if ((ctx->debug_fd = fopen(optarg, "w")) == NULL)
135 pg_log(ctx, PG_FATAL, "cannot open debug file\n");
136 exit_nicely(ctx, false);
141 ctx->transfer_mode = TRANSFER_MODE_LINK;
145 ctx->logfile = pg_strdup(ctx, optarg);
149 if ((ctx->old.port = atoi(optarg)) <= 0)
151 pg_log(ctx, PG_FATAL, "invalid old port number\n");
152 exit_nicely(ctx, false);
157 if ((ctx->new.port = atoi(optarg)) <= 0)
159 pg_log(ctx, PG_FATAL, "invalid new port number\n");
160 exit_nicely(ctx, false);
166 ctx->user = pg_strdup(ctx, optarg);
170 pg_log(ctx, PG_REPORT, "Running in verbose mode\n");
175 pg_log(ctx, PG_FATAL,
176 "Try \"%s --help\" for more information.\n",
182 if (ctx->logfile != NULL)
185 * We must use append mode so output generated by child processes via
186 * ">>" will not be overwritten, and we want the file truncated on
190 ctx->log_fd = fopen(ctx->logfile, "w");
192 pg_log(ctx, PG_FATAL, "Cannot write to log file %s\n", ctx->logfile);
194 ctx->log_fd = fopen(ctx->logfile, "a");
196 pg_log(ctx, PG_FATAL, "Cannot write to log file %s\n", ctx->logfile);
199 ctx->logfile = strdup(DEVNULL);
201 /* if no debug file name, output to the terminal */
202 if (ctx->debug && !ctx->debug_fd)
204 ctx->debug_fd = fopen(DEVTTY, "w");
206 pg_log(ctx, PG_FATAL, "Cannot write to terminal\n");
209 /* Get values from env if not already set */
210 validateDirectoryOption(ctx, &ctx->old.pgdata, "OLDDATADIR", "-d",
211 "old cluster data resides");
212 validateDirectoryOption(ctx, &ctx->new.pgdata, "NEWDATADIR", "-D",
213 "new cluster data resides");
214 validateDirectoryOption(ctx, &ctx->old.bindir, "OLDBINDIR", "-b",
215 "old cluster binaries reside");
216 validateDirectoryOption(ctx, &ctx->new.bindir, "NEWBINDIR", "-B",
217 "new cluster binaries reside");
224 usage(migratorContext *ctx)
226 printf(_("\nUsage: pg_upgrade [OPTIONS]...\n\
229 -d, --old-datadir=OLDDATADIR old cluster data directory\n\
230 -D, --new-datadir=NEWDATADIR new cluster data directory\n\
231 -b, --old-bindir=OLDBINDIR old cluster executable directory\n\
232 -B, --new-bindir=NEWBINDIR new cluster executable directory\n\
233 -p, --old-port=portnum old cluster port number (default %d)\n\
234 -P, --new-port=portnum new cluster port number (default %d)\n\
236 -u, --user=username clusters superuser (default \"%s\")\n\
237 -c, --check check clusters only, don't change any data\n\
238 -g, --debug enable debugging\n\
239 -G, --debugfile=DEBUGFILENAME output debugging activity to file\n\
240 -k, --link link instead of copying files to new cluster\n\
241 -l, --logfile=LOGFILENAME log session activity to file\n\
242 -v, --verbose enable verbose output\n\
243 -V, --version display version information, then exit\n\
244 -h, --help show this help, then exit\n\
246 Before running pg_upgrade you must:\n\
247 create a new database cluster (using the new version of initdb)\n\
248 shutdown the postmaster servicing the old cluster\n\
249 shutdown the postmaster servicing the new cluster\n\
251 When you run pg_upgrade, you must provide the following information:\n\
252 the data directory for the old cluster (-d OLDDATADIR)\n\
253 the data directory for the new cluster (-D NEWDATADIR)\n\
254 the 'bin' directory for the old version (-b OLDBINDIR)\n\
255 the 'bin' directory for the new version (-B NEWBINDIR)\n\
258 pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin\n\
259 or\n"), ctx->old.port, ctx->new.port, ctx->user);
262 $ export OLDDATADIR=oldCluster/data\n\
263 $ export NEWDATADIR=newCluster/data\n\
264 $ export OLDBINDIR=oldCluster/bin\n\
265 $ export NEWBINDIR=newCluster/bin\n\
269 C:\\> set OLDDATADIR=oldCluster/data\n\
270 C:\\> set NEWDATADIR=newCluster/data\n\
271 C:\\> set OLDBINDIR=oldCluster/bin\n\
272 C:\\> set NEWBINDIR=newCluster/bin\n\
273 C:\\> pg_upgrade\n"));
276 You may find it useful to save the preceding 5 commands in a shell script\n\
278 Report bugs to <pg-migrator-general@lists.pgfoundry.org>\n"));
283 * validateDirectoryOption()
285 * Validates a directory option.
286 * dirpath - the directory name supplied on the command line
287 * envVarName - the name of an environment variable to get if dirpath is NULL
288 * cmdLineOption - the command line option corresponds to this directory (-o, -O, -n, -N)
289 * description - a description of this directory option
291 * We use the last two arguments to construct a meaningful error message if the
292 * user hasn't provided the required directory name.
295 validateDirectoryOption(migratorContext *ctx, char **dirpath,
296 char *envVarName, char *cmdLineOption, char *description)
298 if (*dirpath == NULL || (strlen(*dirpath) == 0))
302 if ((envVar = getenv(envVarName)) && strlen(envVar))
303 *dirpath = pg_strdup(ctx, envVar);
306 pg_log(ctx, PG_FATAL, "You must identify the directory where the %s\n"
307 "Please use the %s command-line option or the %s environment variable\n",
308 description, cmdLineOption, envVarName);
313 * Trim off any trailing path separators
316 if ((*dirpath)[strlen(*dirpath) - 1] == '/')
318 if ((*dirpath)[strlen(*dirpath) - 1] == '/' ||
319 (*dirpath)[strlen(*dirpath) - 1] == '\\')
321 (*dirpath)[strlen(*dirpath) - 1] = 0;
326 get_pkglibdirs(migratorContext *ctx)
328 ctx->old.libpath = get_pkglibdir(ctx, ctx->old.bindir);
329 ctx->new.libpath = get_pkglibdir(ctx, ctx->new.bindir);
334 get_pkglibdir(migratorContext *ctx, const char *bindir)
337 char bufin[MAX_STRING];
341 snprintf(cmd, sizeof(cmd), "\"%s/pg_config\" --pkglibdir", bindir);
343 if ((output = popen(cmd, "r")) == NULL)
344 pg_log(ctx, PG_FATAL, "Could not get pkglibdir data: %s\n",
345 getErrorText(errno));
347 fgets(bufin, sizeof(bufin), output);
352 /* Remove trailing newline */
353 i = strlen(bufin) - 1;
355 if (bufin[i] == '\n')
358 return pg_strdup(ctx, bufin);