6 * Copyright (c) 2010, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/option.c
10 #include "pg_upgrade.h"
12 #include "getopt_long.h"
19 static void usage(void);
20 static void validateDirectoryOption(char **dirpath,
21 char *envVarName, char *cmdLineOption, char *description);
22 static void get_pkglibdirs(void);
23 static char *get_pkglibdir(const char *bindir);
32 * Parses the command line (argc, argv[]) into the given migratorContext object
33 * and initializes the rest of the object.
36 parseCommandLine(int argc, char *argv[])
38 static struct option long_options[] = {
39 {"old-datadir", required_argument, NULL, 'd'},
40 {"new-datadir", required_argument, NULL, 'D'},
41 {"old-bindir", required_argument, NULL, 'b'},
42 {"new-bindir", required_argument, NULL, 'B'},
43 {"old-port", required_argument, NULL, 'p'},
44 {"new-port", required_argument, NULL, 'P'},
46 {"user", required_argument, NULL, 'u'},
47 {"check", no_argument, NULL, 'c'},
48 {"debug", no_argument, NULL, 'g'},
49 {"debugfile", required_argument, NULL, 'G'},
50 {"link", no_argument, NULL, 'k'},
51 {"logfile", required_argument, NULL, 'l'},
52 {"verbose", no_argument, NULL, 'v'},
55 int option; /* Command line option */
56 int optindex = 0; /* used by getopt_long */
61 pg_free(os_info.user);
62 os_info.user = pg_strdup(getenv("PGUSER"));
65 os_info.progname = get_progname(argv[0]);
66 old_cluster.port = getenv("PGPORT") ? atoi(getenv("PGPORT")) : DEF_PGPORT;
67 new_cluster.port = getenv("PGPORT") ? atoi(getenv("PGPORT")) : DEF_PGPORT;
68 /* must save value, getenv()'s pointer is not stable */
70 user_opts.transfer_mode = TRANSFER_MODE_COPY;
72 /* user lookup and 'root' test must be split because of usage() */
73 user_id = get_user_info(&os_info.user);
77 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 ||
78 strcmp(argv[1], "-?") == 0)
83 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
85 pg_log(PG_REPORT, "pg_upgrade " PG_VERSION "\n");
91 pg_log(PG_FATAL, "%s: cannot be run as root\n", os_info.progname);
93 getcwd(os_info.cwd, MAXPGPATH);
95 while ((option = getopt_long(argc, argv, "d:D:b:B:cgG:kl:p:P:u:v",
96 long_options, &optindex)) != -1)
101 old_cluster.pgdata = pg_strdup(optarg);
105 new_cluster.pgdata = pg_strdup(optarg);
109 old_cluster.bindir = pg_strdup(optarg);
113 new_cluster.bindir = pg_strdup(optarg);
117 user_opts.check = true;
121 pg_log(PG_REPORT, "Running in debug mode\n");
126 if ((log.debug_fd = fopen(optarg, "w")) == NULL)
128 pg_log(PG_FATAL, "cannot open debug file\n");
134 user_opts.transfer_mode = TRANSFER_MODE_LINK;
138 log.filename = pg_strdup(optarg);
142 if ((old_cluster.port = atoi(optarg)) <= 0)
144 pg_log(PG_FATAL, "invalid old port number\n");
150 if ((new_cluster.port = atoi(optarg)) <= 0)
152 pg_log(PG_FATAL, "invalid new port number\n");
158 pg_free(os_info.user);
159 os_info.user = pg_strdup(optarg);
163 pg_log(PG_REPORT, "Running in verbose mode\n");
169 "Try \"%s --help\" for more information.\n",
175 if (log.filename != NULL)
178 * We must use append mode so output generated by child processes via
179 * ">>" will not be overwritten, and we want the file truncated on
183 if ((log.fd = fopen(log.filename, "w")) == NULL)
184 pg_log(PG_FATAL, "Cannot write to log file %s\n", log.filename);
186 if ((log.fd = fopen(log.filename, "a")) == NULL)
187 pg_log(PG_FATAL, "Cannot write to log file %s\n", log.filename);
190 log.filename = strdup(DEVNULL);
192 /* if no debug file name, output to the terminal */
193 if (log.debug && !log.debug_fd)
195 log.debug_fd = fopen(DEVTTY, "w");
197 pg_log(PG_FATAL, "Cannot write to terminal\n");
200 /* Get values from env if not already set */
201 validateDirectoryOption(&old_cluster.pgdata, "OLDDATADIR", "-d",
202 "old cluster data resides");
203 validateDirectoryOption(&new_cluster.pgdata, "NEWDATADIR", "-D",
204 "new cluster data resides");
205 validateDirectoryOption(&old_cluster.bindir, "OLDBINDIR", "-b",
206 "old cluster binaries reside");
207 validateDirectoryOption(&new_cluster.bindir, "NEWBINDIR", "-B",
208 "new cluster binaries reside");
217 printf(_("\nUsage: pg_upgrade [OPTIONS]...\n\
220 -b, --old-bindir=old_bindir old cluster executable directory\n\
221 -B, --new-bindir=new_bindir new cluster executable directory\n\
222 -c, --check check clusters only, don't change any data\n\
223 -d, --old-datadir=old_datadir old cluster data directory\n\
224 -D, --new-datadir=new_datadir new cluster data directory\n\
225 -g, --debug enable debugging\n\
226 -G, --debugfile=debug_filename output debugging activity to file\n\
227 -k, --link link instead of copying files to new cluster\n\
228 -l, --logfile=log_filename log session activity to file\n\
229 -p, --old-port=old_portnum old cluster port number (default %d)\n\
230 -P, --new-port=new_portnum new cluster port number (default %d)\n\
231 -u, --user=username clusters superuser (default \"%s\")\n\
232 -v, --verbose enable verbose output\n\
233 -V, --version display version information, then exit\n\
234 -h, --help show this help, then exit\n\
236 Before running pg_upgrade you must:\n\
237 create a new database cluster (using the new version of initdb)\n\
238 shutdown the postmaster servicing the old cluster\n\
239 shutdown the postmaster servicing the new cluster\n\
241 When you run pg_upgrade, you must provide the following information:\n\
242 the data directory for the old cluster (-d OLDDATADIR)\n\
243 the data directory for the new cluster (-D NEWDATADIR)\n\
244 the 'bin' directory for the old version (-b OLDBINDIR)\n\
245 the 'bin' directory for the new version (-B NEWBINDIR)\n\
248 pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin\n\
249 or\n"), old_cluster.port, new_cluster.port, os_info.user);
252 $ export OLDDATADIR=oldCluster/data\n\
253 $ export NEWDATADIR=newCluster/data\n\
254 $ export OLDBINDIR=oldCluster/bin\n\
255 $ export NEWBINDIR=newCluster/bin\n\
259 C:\\> set OLDDATADIR=oldCluster/data\n\
260 C:\\> set NEWDATADIR=newCluster/data\n\
261 C:\\> set OLDBINDIR=oldCluster/bin\n\
262 C:\\> set NEWBINDIR=newCluster/bin\n\
263 C:\\> pg_upgrade\n"));
269 * validateDirectoryOption()
271 * Validates a directory option.
272 * dirpath - the directory name supplied on the command line
273 * envVarName - the name of an environment variable to get if dirpath is NULL
274 * cmdLineOption - the command line option corresponds to this directory (-o, -O, -n, -N)
275 * description - a description of this directory option
277 * We use the last two arguments to construct a meaningful error message if the
278 * user hasn't provided the required directory name.
281 validateDirectoryOption(char **dirpath,
282 char *envVarName, char *cmdLineOption, char *description)
284 if (*dirpath == NULL || (strlen(*dirpath) == 0))
288 if ((envVar = getenv(envVarName)) && strlen(envVar))
289 *dirpath = pg_strdup(envVar);
292 pg_log(PG_FATAL, "You must identify the directory where the %s\n"
293 "Please use the %s command-line option or the %s environment variable\n",
294 description, cmdLineOption, envVarName);
299 * Trim off any trailing path separators
302 if ((*dirpath)[strlen(*dirpath) - 1] == '/')
304 if ((*dirpath)[strlen(*dirpath) - 1] == '/' ||
305 (*dirpath)[strlen(*dirpath) - 1] == '\\')
307 (*dirpath)[strlen(*dirpath) - 1] = 0;
314 old_cluster.libpath = get_pkglibdir(old_cluster.bindir);
315 new_cluster.libpath = get_pkglibdir(new_cluster.bindir);
320 get_pkglibdir(const char *bindir)
323 char bufin[MAX_STRING];
327 snprintf(cmd, sizeof(cmd), "\"%s/pg_config\" --pkglibdir", bindir);
329 if ((output = popen(cmd, "r")) == NULL)
330 pg_log(PG_FATAL, "Could not get pkglibdir data: %s\n",
331 getErrorText(errno));
333 fgets(bufin, sizeof(bufin), output);
338 /* Remove trailing newline */
339 i = strlen(bufin) - 1;
341 if (bufin[i] == '\n')
344 return pg_strdup(bufin);