6 * Copyright (c) 2010-2011, 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[]) and loads structures
35 parseCommandLine(int argc, char *argv[])
37 static struct option long_options[] = {
38 {"old-datadir", required_argument, NULL, 'd'},
39 {"new-datadir", required_argument, NULL, 'D'},
40 {"old-bindir", required_argument, NULL, 'b'},
41 {"new-bindir", required_argument, NULL, 'B'},
42 {"old-port", required_argument, NULL, 'p'},
43 {"new-port", required_argument, NULL, 'P'},
45 {"user", required_argument, NULL, 'u'},
46 {"check", no_argument, NULL, 'c'},
47 {"debug", no_argument, NULL, 'g'},
48 {"debugfile", required_argument, NULL, 'G'},
49 {"link", no_argument, NULL, 'k'},
50 {"logfile", required_argument, NULL, 'l'},
51 {"verbose", no_argument, NULL, 'v'},
54 int option; /* Command line option */
55 int optindex = 0; /* used by getopt_long */
60 pg_free(os_info.user);
61 os_info.user = pg_strdup(getenv("PGUSER"));
64 os_info.progname = get_progname(argv[0]);
65 old_cluster.port = getenv("PGPORT") ? atoi(getenv("PGPORT")) : DEF_PGPORT;
66 new_cluster.port = getenv("PGPORT") ? atoi(getenv("PGPORT")) : DEF_PGPORT;
67 /* must save value, getenv()'s pointer is not stable */
69 user_opts.transfer_mode = TRANSFER_MODE_COPY;
71 /* user lookup and 'root' test must be split because of usage() */
72 user_id = get_user_info(&os_info.user);
76 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 ||
77 strcmp(argv[1], "-?") == 0)
82 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
84 pg_log(PG_REPORT, "pg_upgrade " PG_VERSION "\n");
90 pg_log(PG_FATAL, "%s: cannot be run as root\n", os_info.progname);
92 getcwd(os_info.cwd, MAXPGPATH);
94 while ((option = getopt_long(argc, argv, "d:D:b:B:cgG:kl:p:P:u:v",
95 long_options, &optindex)) != -1)
100 old_cluster.pgdata = pg_strdup(optarg);
104 new_cluster.pgdata = pg_strdup(optarg);
108 old_cluster.bindir = pg_strdup(optarg);
112 new_cluster.bindir = pg_strdup(optarg);
116 user_opts.check = true;
120 pg_log(PG_REPORT, "Running in debug mode\n");
121 log_opts.debug = true;
125 if ((log_opts.debug_fd = fopen(optarg, "w")) == NULL)
127 pg_log(PG_FATAL, "cannot open debug file\n");
133 user_opts.transfer_mode = TRANSFER_MODE_LINK;
137 log_opts.filename = pg_strdup(optarg);
141 if ((old_cluster.port = atoi(optarg)) <= 0)
143 pg_log(PG_FATAL, "invalid old port number\n");
149 if ((new_cluster.port = atoi(optarg)) <= 0)
151 pg_log(PG_FATAL, "invalid new port number\n");
157 pg_free(os_info.user);
158 os_info.user = pg_strdup(optarg);
162 pg_log(PG_REPORT, "Running in verbose mode\n");
163 log_opts.verbose = true;
168 "Try \"%s --help\" for more information.\n",
174 if (log_opts.filename != NULL)
177 * We must use append mode so output generated by child processes via
178 * ">>" will not be overwritten, and we want the file truncated on
182 if ((log_opts.fd = fopen(log_opts.filename, "w")) == NULL)
183 pg_log(PG_FATAL, "Cannot write to log file %s\n", log_opts.filename);
185 if ((log_opts.fd = fopen(log_opts.filename, "a")) == NULL)
186 pg_log(PG_FATAL, "Cannot write to log file %s\n", log_opts.filename);
189 log_opts.filename = strdup(DEVNULL);
191 /* if no debug file name, output to the terminal */
192 if (log_opts.debug && !log_opts.debug_fd)
194 log_opts.debug_fd = fopen(DEVTTY, "w");
195 if (!log_opts.debug_fd)
196 pg_log(PG_FATAL, "Cannot write to terminal\n");
199 /* Get values from env if not already set */
200 validateDirectoryOption(&old_cluster.pgdata, "OLDDATADIR", "-d",
201 "old cluster data resides");
202 validateDirectoryOption(&new_cluster.pgdata, "NEWDATADIR", "-D",
203 "new cluster data resides");
204 validateDirectoryOption(&old_cluster.bindir, "OLDBINDIR", "-b",
205 "old cluster binaries reside");
206 validateDirectoryOption(&new_cluster.bindir, "NEWBINDIR", "-B",
207 "new cluster binaries reside");
216 printf(_("\nUsage: pg_upgrade [OPTIONS]...\n\
219 -b, --old-bindir=old_bindir old cluster executable directory\n\
220 -B, --new-bindir=new_bindir new cluster executable directory\n\
221 -c, --check check clusters only, don't change any data\n\
222 -d, --old-datadir=old_datadir old cluster data directory\n\
223 -D, --new-datadir=new_datadir new cluster data directory\n\
224 -g, --debug enable debugging\n\
225 -G, --debugfile=debug_filename output debugging activity to file\n\
226 -k, --link link instead of copying files to new cluster\n\
227 -l, --logfile=log_filename log session activity to file\n\
228 -p, --old-port=old_portnum old cluster port number (default %d)\n\
229 -P, --new-port=new_portnum new cluster port number (default %d)\n\
230 -u, --user=username clusters superuser (default \"%s\")\n\
231 -v, --verbose enable verbose output\n\
232 -V, --version display version information, then exit\n\
233 -h, --help show this help, then exit\n\
235 Before running pg_upgrade you must:\n\
236 create a new database cluster (using the new version of initdb)\n\
237 shutdown the postmaster servicing the old cluster\n\
238 shutdown the postmaster servicing the new cluster\n\
240 When you run pg_upgrade, you must provide the following information:\n\
241 the data directory for the old cluster (-d OLDDATADIR)\n\
242 the data directory for the new cluster (-D NEWDATADIR)\n\
243 the 'bin' directory for the old version (-b OLDBINDIR)\n\
244 the 'bin' directory for the new version (-B NEWBINDIR)\n\
247 pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin\n\
248 or\n"), old_cluster.port, new_cluster.port, os_info.user);
251 $ export OLDDATADIR=oldCluster/data\n\
252 $ export NEWDATADIR=newCluster/data\n\
253 $ export OLDBINDIR=oldCluster/bin\n\
254 $ export NEWBINDIR=newCluster/bin\n\
258 C:\\> set OLDDATADIR=oldCluster/data\n\
259 C:\\> set NEWDATADIR=newCluster/data\n\
260 C:\\> set OLDBINDIR=oldCluster/bin\n\
261 C:\\> set NEWBINDIR=newCluster/bin\n\
262 C:\\> pg_upgrade\n"));
268 * validateDirectoryOption()
270 * Validates a directory option.
271 * dirpath - the directory name supplied on the command line
272 * envVarName - the name of an environment variable to get if dirpath is NULL
273 * cmdLineOption - the command line option corresponds to this directory (-o, -O, -n, -N)
274 * description - a description of this directory option
276 * We use the last two arguments to construct a meaningful error message if the
277 * user hasn't provided the required directory name.
280 validateDirectoryOption(char **dirpath,
281 char *envVarName, char *cmdLineOption, char *description)
283 if (*dirpath == NULL || (strlen(*dirpath) == 0))
287 if ((envVar = getenv(envVarName)) && strlen(envVar))
288 *dirpath = pg_strdup(envVar);
291 pg_log(PG_FATAL, "You must identify the directory where the %s\n"
292 "Please use the %s command-line option or the %s environment variable\n",
293 description, cmdLineOption, envVarName);
298 * Trim off any trailing path separators
301 if ((*dirpath)[strlen(*dirpath) - 1] == '/')
303 if ((*dirpath)[strlen(*dirpath) - 1] == '/' ||
304 (*dirpath)[strlen(*dirpath) - 1] == '\\')
306 (*dirpath)[strlen(*dirpath) - 1] = 0;
314 * we do not need to know the libpath in the old cluster, and might not
315 * have a working pg_config to ask for it anyway.
317 old_cluster.libpath = NULL;
318 new_cluster.libpath = get_pkglibdir(new_cluster.bindir);
323 get_pkglibdir(const char *bindir)
326 char bufin[MAX_STRING];
330 snprintf(cmd, sizeof(cmd), "\"%s/pg_config\" --pkglibdir", bindir);
332 if ((output = popen(cmd, "r")) == NULL)
333 pg_log(PG_FATAL, "Could not get pkglibdir data: %s\n",
334 getErrorText(errno));
336 fgets(bufin, sizeof(bufin), output);
341 /* Remove trailing newline */
342 i = strlen(bufin) - 1;
344 if (bufin[i] == '\n')
347 return pg_strdup(bufin);