option.c 9.1 KB
Newer Older
1 2 3 4
/*
 *	opt.c
 *
 *	options functions
5
 *
6
 *	Copyright (c) 2010, PostgreSQL Global Development Group
7
 *	contrib/pg_upgrade/option.c
8 9 10 11 12 13 14 15 16 17 18
 */

#include "pg_upgrade.h"

#include "getopt_long.h"

#ifdef WIN32
#include <io.h>
#endif


19 20
static void usage(void);
static void validateDirectoryOption(char **dirpath,
21
				   char *envVarName, char *cmdLineOption, char *description);
22 23 24 25 26
static void get_pkglibdirs(void);
static char *get_pkglibdir(const char *bindir);


UserOpts     user_opts;
27 28 29 30 31 32 33 34 35


/*
 * parseCommandLine()
 *
 *	Parses the command line (argc, argv[]) into the given migratorContext object
 *	and initializes the rest of the object.
 */
void
36
parseCommandLine(int argc, char *argv[])
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
{
	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}
	};
55
	int			option;			/* Command line option */
56
	int			optindex = 0;	/* used by getopt_long */
57
	int			user_id;
B
Bruce Momjian 已提交
58

59 60
	if (getenv("PGUSER"))
	{
61 62
		pg_free(os_info.user);
		os_info.user = pg_strdup(getenv("PGUSER"));
63 64
	}

65 66 67
	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;
68 69
	/* must save value, getenv()'s pointer is not stable */

70
	user_opts.transfer_mode = TRANSFER_MODE_COPY;
71

72
	/* user lookup and 'root' test must be split because of usage() */
73
	user_id = get_user_info(&os_info.user);
B
Bruce Momjian 已提交
74

75 76 77 78 79
	if (argc > 1)
	{
		if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 ||
			strcmp(argv[1], "-?") == 0)
		{
80 81
			usage();
			exit_nicely(false);
82 83 84
		}
		if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
		{
85 86
			pg_log(PG_REPORT, "pg_upgrade " PG_VERSION "\n");
			exit_nicely(false);
87 88 89
		}
	}

90
	if (user_id == 0)
91
		pg_log(PG_FATAL, "%s: cannot be run as root\n", os_info.progname);
92

93
	getcwd(os_info.cwd, MAXPGPATH);
94 95 96 97 98 99 100

	while ((option = getopt_long(argc, argv, "d:D:b:B:cgG:kl:p:P:u:v",
								 long_options, &optindex)) != -1)
	{
		switch (option)
		{
			case 'd':
101
				old_cluster.pgdata = pg_strdup(optarg);
102 103 104
				break;

			case 'D':
105
				new_cluster.pgdata = pg_strdup(optarg);
106 107 108
				break;

			case 'b':
109
				old_cluster.bindir = pg_strdup(optarg);
110 111 112
				break;

			case 'B':
113
				new_cluster.bindir = pg_strdup(optarg);
114 115 116
				break;

			case 'c':
117
				user_opts.check = true;
118 119 120
				break;

			case 'g':
121 122
				pg_log(PG_REPORT, "Running in debug mode\n");
				log.debug = true;
123 124 125
				break;

			case 'G':
126
				if ((log.debug_fd = fopen(optarg, "w")) == NULL)
127
				{
128 129
					pg_log(PG_FATAL, "cannot open debug file\n");
					exit_nicely(false);
130 131 132 133
				}
				break;

			case 'k':
134
				user_opts.transfer_mode = TRANSFER_MODE_LINK;
135 136 137
				break;

			case 'l':
138
				log.filename = pg_strdup(optarg);
139 140 141
				break;

			case 'p':
142
				if ((old_cluster.port = atoi(optarg)) <= 0)
143
				{
144 145
					pg_log(PG_FATAL, "invalid old port number\n");
					exit_nicely(false);
146 147 148 149
				}
				break;

			case 'P':
150
				if ((new_cluster.port = atoi(optarg)) <= 0)
151
				{
152 153
					pg_log(PG_FATAL, "invalid new port number\n");
					exit_nicely(false);
154 155 156 157
				}
				break;

			case 'u':
158 159
				pg_free(os_info.user);
				os_info.user = pg_strdup(optarg);
160 161 162
				break;

			case 'v':
163 164
				pg_log(PG_REPORT, "Running in verbose mode\n");
				log.verbose = true;
165 166 167
				break;

			default:
168
				pg_log(PG_FATAL,
169
					   "Try \"%s --help\" for more information.\n",
170
					   os_info.progname);
171 172 173 174
				break;
		}
	}

175
	if (log.filename != NULL)
176 177 178 179 180 181 182
	{
		/*
		 * 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 */
183 184 185 186 187
		if ((log.fd = fopen(log.filename, "w")) == NULL)
			pg_log(PG_FATAL, "Cannot write to log file %s\n", log.filename);
		fclose(log.fd);
		if ((log.fd = fopen(log.filename, "a")) == NULL)
			pg_log(PG_FATAL, "Cannot write to log file %s\n", log.filename);
188 189
	}
	else
190
		log.filename = strdup(DEVNULL);
191 192

	/* if no debug file name, output to the terminal */
193
	if (log.debug && !log.debug_fd)
194
	{
195 196 197
		log.debug_fd = fopen(DEVTTY, "w");
		if (!log.debug_fd)
			pg_log(PG_FATAL, "Cannot write to terminal\n");
198 199 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 209
							"new cluster binaries reside");

210
	get_pkglibdirs();
211 212 213 214
}


static void
215
usage(void)
216 217 218 219
{
	printf(_("\nUsage: pg_upgrade [OPTIONS]...\n\
\n\
Options:\n\
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
 -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\
235 236 237 238 239 240 241 242 243 244 245 246 247 248
\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\
249
or\n"), old_cluster.port, new_cluster.port, os_info.user);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
#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
281
validateDirectoryOption(char **dirpath,
282 283 284 285 286 287 288
					char *envVarName, char *cmdLineOption, char *description)
{
	if (*dirpath == NULL || (strlen(*dirpath) == 0))
	{
		const char *envVar;

		if ((envVar = getenv(envVarName)) && strlen(envVar))
289
			*dirpath = pg_strdup(envVar);
290 291
		else
		{
292
			pg_log(PG_FATAL, "You must identify the directory where the %s\n"
293 294 295 296 297 298 299 300
				   "Please use the %s command-line option or the %s environment variable\n",
				   description, cmdLineOption, envVarName);
		}
	}

	/*
	 * Trim off any trailing path separators
	 */
301 302 303 304
#ifndef WIN32
	if ((*dirpath)[strlen(*dirpath) - 1] == '/')
#else
	if ((*dirpath)[strlen(*dirpath) - 1] == '/' ||
B
Bruce Momjian 已提交
305
		(*dirpath)[strlen(*dirpath) - 1] == '\\')
306
#endif
307 308 309 310 311
		(*dirpath)[strlen(*dirpath) - 1] = 0;
}


static void
312
get_pkglibdirs(void)
313
{
314 315
	old_cluster.libpath = get_pkglibdir(old_cluster.bindir);
	new_cluster.libpath = get_pkglibdir(new_cluster.bindir);
316 317 318 319
}


static char *
320
get_pkglibdir(const char *bindir)
321 322 323 324 325 326 327 328 329
{
	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)
330
		pg_log(PG_FATAL, "Could not get pkglibdir data: %s\n",
331 332 333 334 335 336 337 338 339 340 341 342 343
			   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';

344
	return pg_strdup(bufin);
345
}