2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* opt.c
|
|
|
|
*
|
|
|
|
* options functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2011-01-02 02:18:15 +08:00
|
|
|
* Copyright (c) 2010-2011, PostgreSQL Global Development Group
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pg_upgrade/option.c
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
|
|
|
#include "getopt_long.h"
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
static void usage(void);
|
|
|
|
static void validateDirectoryOption(char **dirpath,
|
2010-05-12 10:19:11 +08:00
|
|
|
char *envVarName, char *cmdLineOption, char *description);
|
2010-10-20 05:38:16 +08:00
|
|
|
static void get_pkglibdirs(void);
|
|
|
|
static char *get_pkglibdir(const char *bindir);
|
|
|
|
|
|
|
|
|
2010-10-20 06:37:04 +08:00
|
|
|
UserOpts user_opts;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parseCommandLine()
|
|
|
|
*
|
2010-12-15 20:11:31 +08:00
|
|
|
* Parses the command line (argc, argv[]) and loads structures
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
void
|
2010-10-20 05:38:16 +08:00
|
|
|
parseCommandLine(int argc, char *argv[])
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"old-datadir", required_argument, NULL, 'd'},
|
|
|
|
{"new-datadir", required_argument, NULL, 'D'},
|
|
|
|
{"old-bindir", required_argument, NULL, 'b'},
|
|
|
|
{"new-bindir", required_argument, NULL, 'B'},
|
|
|
|
{"old-port", required_argument, NULL, 'p'},
|
|
|
|
{"new-port", required_argument, NULL, 'P'},
|
|
|
|
|
|
|
|
{"user", required_argument, NULL, 'u'},
|
|
|
|
{"check", no_argument, NULL, 'c'},
|
|
|
|
{"debug", no_argument, NULL, 'g'},
|
|
|
|
{"debugfile", required_argument, NULL, 'G'},
|
|
|
|
{"link", no_argument, NULL, 'k'},
|
|
|
|
{"logfile", required_argument, NULL, 'l'},
|
|
|
|
{"verbose", no_argument, NULL, 'v'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
2010-06-16 07:25:01 +08:00
|
|
|
int option; /* Command line option */
|
2010-05-12 10:19:11 +08:00
|
|
|
int optindex = 0; /* used by getopt_long */
|
2010-05-25 00:34:35 +08:00
|
|
|
int user_id;
|
2010-07-07 03:19:02 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
if (getenv("PGUSER"))
|
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_free(os_info.user);
|
|
|
|
os_info.user = pg_strdup(getenv("PGUSER"));
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
os_info.progname = get_progname(argv[0]);
|
|
|
|
old_cluster.port = getenv("PGPORT") ? atoi(getenv("PGPORT")) : DEF_PGPORT;
|
|
|
|
new_cluster.port = getenv("PGPORT") ? atoi(getenv("PGPORT")) : DEF_PGPORT;
|
2010-05-12 10:19:11 +08:00
|
|
|
/* must save value, getenv()'s pointer is not stable */
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
user_opts.transfer_mode = TRANSFER_MODE_COPY;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-05-25 00:34:35 +08:00
|
|
|
/* user lookup and 'root' test must be split because of usage() */
|
2010-10-20 05:38:16 +08:00
|
|
|
user_id = get_user_info(&os_info.user);
|
2010-07-07 03:19:02 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 ||
|
|
|
|
strcmp(argv[1], "-?") == 0)
|
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
usage();
|
2011-04-07 04:00:44 +08:00
|
|
|
exit(0);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
|
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_REPORT, "pg_upgrade " PG_VERSION "\n");
|
2011-04-07 04:00:44 +08:00
|
|
|
exit(0);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-25 00:34:35 +08:00
|
|
|
if (user_id == 0)
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_FATAL, "%s: cannot be run as root\n", os_info.progname);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
getcwd(os_info.cwd, MAXPGPATH);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
while ((option = getopt_long(argc, argv, "d:D:b:B:cgG:kl:p:P:u:v",
|
|
|
|
long_options, &optindex)) != -1)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'd':
|
2010-10-20 05:38:16 +08:00
|
|
|
old_cluster.pgdata = pg_strdup(optarg);
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
2010-10-20 05:38:16 +08:00
|
|
|
new_cluster.pgdata = pg_strdup(optarg);
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
2010-10-20 05:38:16 +08:00
|
|
|
old_cluster.bindir = pg_strdup(optarg);
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B':
|
2010-10-20 05:38:16 +08:00
|
|
|
new_cluster.bindir = pg_strdup(optarg);
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
2010-10-20 05:38:16 +08:00
|
|
|
user_opts.check = true;
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'g':
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_REPORT, "Running in debug mode\n");
|
2010-10-20 10:31:17 +08:00
|
|
|
log_opts.debug = true;
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'G':
|
2010-10-20 10:31:17 +08:00
|
|
|
if ((log_opts.debug_fd = fopen(optarg, "w")) == NULL)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_FATAL, "cannot open debug file\n");
|
2011-04-07 04:00:44 +08:00
|
|
|
exit(1);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'k':
|
2010-10-20 05:38:16 +08:00
|
|
|
user_opts.transfer_mode = TRANSFER_MODE_LINK;
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
2010-10-20 10:31:17 +08:00
|
|
|
log_opts.filename = pg_strdup(optarg);
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
2010-10-20 05:38:16 +08:00
|
|
|
if ((old_cluster.port = atoi(optarg)) <= 0)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_FATAL, "invalid old port number\n");
|
2011-04-07 04:00:44 +08:00
|
|
|
exit(1);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'P':
|
2010-10-20 05:38:16 +08:00
|
|
|
if ((new_cluster.port = atoi(optarg)) <= 0)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_FATAL, "invalid new port number\n");
|
2011-04-07 04:00:44 +08:00
|
|
|
exit(1);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_free(os_info.user);
|
|
|
|
os_info.user = pg_strdup(optarg);
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_REPORT, "Running in verbose mode\n");
|
2010-10-20 10:31:17 +08:00
|
|
|
log_opts.verbose = true;
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_FATAL,
|
2010-05-12 10:19:11 +08:00
|
|
|
"Try \"%s --help\" for more information.\n",
|
2010-10-20 05:38:16 +08:00
|
|
|
os_info.progname);
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:31:17 +08:00
|
|
|
if (log_opts.filename != NULL)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We must use append mode so output generated by child processes via
|
|
|
|
* ">>" will not be overwritten, and we want the file truncated on
|
|
|
|
* start.
|
|
|
|
*/
|
|
|
|
/* truncate */
|
2010-10-20 10:31:17 +08:00
|
|
|
if ((log_opts.fd = fopen(log_opts.filename, "w")) == NULL)
|
|
|
|
pg_log(PG_FATAL, "Cannot write to log file %s\n", log_opts.filename);
|
|
|
|
fclose(log_opts.fd);
|
|
|
|
if ((log_opts.fd = fopen(log_opts.filename, "a")) == NULL)
|
|
|
|
pg_log(PG_FATAL, "Cannot write to log file %s\n", log_opts.filename);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
else
|
2010-10-20 10:31:17 +08:00
|
|
|
log_opts.filename = strdup(DEVNULL);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* if no debug file name, output to the terminal */
|
2010-10-20 10:31:17 +08:00
|
|
|
if (log_opts.debug && !log_opts.debug_fd)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-10-20 10:31:17 +08:00
|
|
|
log_opts.debug_fd = fopen(DEVTTY, "w");
|
|
|
|
if (!log_opts.debug_fd)
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_FATAL, "Cannot write to terminal\n");
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get values from env if not already set */
|
2010-10-20 05:38:16 +08:00
|
|
|
validateDirectoryOption(&old_cluster.pgdata, "OLDDATADIR", "-d",
|
2010-05-12 10:19:11 +08:00
|
|
|
"old cluster data resides");
|
2010-10-20 05:38:16 +08:00
|
|
|
validateDirectoryOption(&new_cluster.pgdata, "NEWDATADIR", "-D",
|
2010-05-12 10:19:11 +08:00
|
|
|
"new cluster data resides");
|
2010-10-20 05:38:16 +08:00
|
|
|
validateDirectoryOption(&old_cluster.bindir, "OLDBINDIR", "-b",
|
2010-05-12 10:19:11 +08:00
|
|
|
"old cluster binaries reside");
|
2010-10-20 05:38:16 +08:00
|
|
|
validateDirectoryOption(&new_cluster.bindir, "NEWBINDIR", "-B",
|
2010-05-12 10:19:11 +08:00
|
|
|
"new cluster binaries reside");
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
get_pkglibdirs();
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
usage(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
printf(_("\nUsage: pg_upgrade [OPTIONS]...\n\
|
|
|
|
\n\
|
|
|
|
Options:\n\
|
2010-05-25 23:55:28 +08:00
|
|
|
-b, --old-bindir=old_bindir old cluster executable directory\n\
|
|
|
|
-B, --new-bindir=new_bindir new cluster executable directory\n\
|
|
|
|
-c, --check check clusters only, don't change any data\n\
|
|
|
|
-d, --old-datadir=old_datadir old cluster data directory\n\
|
|
|
|
-D, --new-datadir=new_datadir new cluster data directory\n\
|
|
|
|
-g, --debug enable debugging\n\
|
|
|
|
-G, --debugfile=debug_filename output debugging activity to file\n\
|
|
|
|
-k, --link link instead of copying files to new cluster\n\
|
|
|
|
-l, --logfile=log_filename log session activity to file\n\
|
|
|
|
-p, --old-port=old_portnum old cluster port number (default %d)\n\
|
|
|
|
-P, --new-port=new_portnum new cluster port number (default %d)\n\
|
|
|
|
-u, --user=username clusters superuser (default \"%s\")\n\
|
|
|
|
-v, --verbose enable verbose output\n\
|
|
|
|
-V, --version display version information, then exit\n\
|
|
|
|
-h, --help show this help, then exit\n\
|
2010-05-12 10:19:11 +08:00
|
|
|
\n\
|
|
|
|
Before running pg_upgrade you must:\n\
|
|
|
|
create a new database cluster (using the new version of initdb)\n\
|
|
|
|
shutdown the postmaster servicing the old cluster\n\
|
|
|
|
shutdown the postmaster servicing the new cluster\n\
|
|
|
|
\n\
|
|
|
|
When you run pg_upgrade, you must provide the following information:\n\
|
|
|
|
the data directory for the old cluster (-d OLDDATADIR)\n\
|
|
|
|
the data directory for the new cluster (-D NEWDATADIR)\n\
|
|
|
|
the 'bin' directory for the old version (-b OLDBINDIR)\n\
|
|
|
|
the 'bin' directory for the new version (-B NEWBINDIR)\n\
|
|
|
|
\n\
|
|
|
|
For example:\n\
|
|
|
|
pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin\n\
|
2010-10-20 05:38:16 +08:00
|
|
|
or\n"), old_cluster.port, new_cluster.port, os_info.user);
|
2010-05-12 10:19:11 +08:00
|
|
|
#ifndef WIN32
|
|
|
|
printf(_("\
|
|
|
|
$ export OLDDATADIR=oldCluster/data\n\
|
|
|
|
$ export NEWDATADIR=newCluster/data\n\
|
|
|
|
$ export OLDBINDIR=oldCluster/bin\n\
|
|
|
|
$ export NEWBINDIR=newCluster/bin\n\
|
|
|
|
$ pg_upgrade\n"));
|
|
|
|
#else
|
|
|
|
printf(_("\
|
|
|
|
C:\\> set OLDDATADIR=oldCluster/data\n\
|
|
|
|
C:\\> set NEWDATADIR=newCluster/data\n\
|
|
|
|
C:\\> set OLDBINDIR=oldCluster/bin\n\
|
|
|
|
C:\\> set NEWBINDIR=newCluster/bin\n\
|
|
|
|
C:\\> pg_upgrade\n"));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* validateDirectoryOption()
|
|
|
|
*
|
|
|
|
* Validates a directory option.
|
|
|
|
* dirpath - the directory name supplied on the command line
|
|
|
|
* envVarName - the name of an environment variable to get if dirpath is NULL
|
|
|
|
* cmdLineOption - the command line option corresponds to this directory (-o, -O, -n, -N)
|
|
|
|
* description - a description of this directory option
|
|
|
|
*
|
|
|
|
* We use the last two arguments to construct a meaningful error message if the
|
|
|
|
* user hasn't provided the required directory name.
|
|
|
|
*/
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
validateDirectoryOption(char **dirpath,
|
2010-05-12 10:19:11 +08:00
|
|
|
char *envVarName, char *cmdLineOption, char *description)
|
|
|
|
{
|
|
|
|
if (*dirpath == NULL || (strlen(*dirpath) == 0))
|
|
|
|
{
|
|
|
|
const char *envVar;
|
|
|
|
|
|
|
|
if ((envVar = getenv(envVarName)) && strlen(envVar))
|
2010-10-20 05:38:16 +08:00
|
|
|
*dirpath = pg_strdup(envVar);
|
2010-05-12 10:19:11 +08:00
|
|
|
else
|
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_FATAL, "You must identify the directory where the %s\n"
|
2010-05-12 10:19:11 +08:00
|
|
|
"Please use the %s command-line option or the %s environment variable\n",
|
|
|
|
description, cmdLineOption, envVarName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Trim off any trailing path separators
|
|
|
|
*/
|
2010-05-14 08:13:38 +08:00
|
|
|
#ifndef WIN32
|
|
|
|
if ((*dirpath)[strlen(*dirpath) - 1] == '/')
|
|
|
|
#else
|
|
|
|
if ((*dirpath)[strlen(*dirpath) - 1] == '/' ||
|
2010-07-07 03:19:02 +08:00
|
|
|
(*dirpath)[strlen(*dirpath) - 1] == '\\')
|
2010-05-14 08:13:38 +08:00
|
|
|
#endif
|
2010-05-12 10:19:11 +08:00
|
|
|
(*dirpath)[strlen(*dirpath) - 1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
get_pkglibdirs(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-12-30 02:43:53 +08:00
|
|
|
/*
|
|
|
|
* we do not need to know the libpath in the old cluster, and might not
|
|
|
|
* have a working pg_config to ask for it anyway.
|
|
|
|
*/
|
|
|
|
old_cluster.libpath = NULL;
|
2010-10-20 05:38:16 +08:00
|
|
|
new_cluster.libpath = get_pkglibdir(new_cluster.bindir);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2010-10-20 05:38:16 +08:00
|
|
|
get_pkglibdir(const char *bindir)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
char cmd[MAXPGPATH];
|
|
|
|
char bufin[MAX_STRING];
|
|
|
|
FILE *output;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
snprintf(cmd, sizeof(cmd), "\"%s/pg_config\" --pkglibdir", bindir);
|
|
|
|
|
|
|
|
if ((output = popen(cmd, "r")) == NULL)
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_FATAL, "Could not get pkglibdir data: %s\n",
|
2010-05-12 10:19:11 +08:00
|
|
|
getErrorText(errno));
|
|
|
|
|
|
|
|
fgets(bufin, sizeof(bufin), output);
|
|
|
|
|
|
|
|
if (output)
|
|
|
|
pclose(output);
|
|
|
|
|
|
|
|
/* Remove trailing newline */
|
|
|
|
i = strlen(bufin) - 1;
|
|
|
|
|
|
|
|
if (bufin[i] == '\n')
|
|
|
|
bufin[i] = '\0';
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
return pg_strdup(bufin);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|