From 17a16eeb7c4fd0c6dce80521247a20d76706b2bb Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Wed, 20 Jul 2011 18:31:03 -0400 Subject: [PATCH] In pg_upgrade, fix the -l/log option to work on Windows. Also, double-quote the log file name in all places, to allow (on all platforms) log file names with spaces. Back patch to 9.0 and 9.1. --- contrib/pg_upgrade/check.c | 2 +- contrib/pg_upgrade/option.c | 9 ++++++++- contrib/pg_upgrade/pg_upgrade.c | 14 +++++++------- contrib/pg_upgrade/pg_upgrade.h | 10 ++++++++++ contrib/pg_upgrade/server.c | 28 ++++------------------------ 5 files changed, 30 insertions(+), 33 deletions(-) diff --git a/contrib/pg_upgrade/check.c b/contrib/pg_upgrade/check.c index fa4ef8dcc7..528b674b4f 100644 --- a/contrib/pg_upgrade/check.c +++ b/contrib/pg_upgrade/check.c @@ -170,7 +170,7 @@ issue_warnings(char *sequence_script_file_name) "--no-psqlrc --port %d --username \"%s\" " "-f \"%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE, new_cluster.bindir, new_cluster.port, os_info.user, - sequence_script_file_name, log_opts.filename); + sequence_script_file_name, log_opts.filename2); unlink(sequence_script_file_name); check_ok(); } diff --git a/contrib/pg_upgrade/option.c b/contrib/pg_upgrade/option.c index 1793bb320e..32bd3b896e 100644 --- a/contrib/pg_upgrade/option.c +++ b/contrib/pg_upgrade/option.c @@ -192,8 +192,15 @@ parseCommandLine(int argc, char *argv[]) pg_log(PG_FATAL, "cannot write to log file %s\n", log_opts.filename); } else - log_opts.filename = strdup(DEVNULL); + log_opts.filename = pg_strdup(DEVNULL); + /* WIN32 files do not accept writes from multiple processes */ +#ifndef WIN32 + log_opts.filename2 = pg_strdup(log_opts.filename); +#else + log_opts.filename2 = pg_strdup(DEVNULL); +#endif + /* if no debug file name, output to the terminal */ if (log_opts.debug && !log_opts.debug_fd) { diff --git a/contrib/pg_upgrade/pg_upgrade.c b/contrib/pg_upgrade/pg_upgrade.c index efa5c0f177..4912f7727f 100644 --- a/contrib/pg_upgrade/pg_upgrade.c +++ b/contrib/pg_upgrade/pg_upgrade.c @@ -193,8 +193,8 @@ prepare_new_cluster(void) prep_status("Analyzing all rows in the new cluster"); exec_prog(true, SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" " - "--all --analyze >> %s 2>&1" SYSTEMQUOTE, - new_cluster.bindir, new_cluster.port, os_info.user, log_opts.filename); + "--all --analyze >> \"%s\" 2>&1" SYSTEMQUOTE, + new_cluster.bindir, new_cluster.port, os_info.user, log_opts.filename2); check_ok(); /* @@ -206,8 +206,8 @@ prepare_new_cluster(void) prep_status("Freezing all rows on the new cluster"); exec_prog(true, SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" " - "--all --freeze >> %s 2>&1" SYSTEMQUOTE, - new_cluster.bindir, new_cluster.port, os_info.user, log_opts.filename); + "--all --freeze >> \"%s\" 2>&1" SYSTEMQUOTE, + new_cluster.bindir, new_cluster.port, os_info.user, log_opts.filename2); check_ok(); get_pg_database_relfilenode(&new_cluster); @@ -245,7 +245,7 @@ prepare_new_databases(void) "--no-psqlrc --port %d --username \"%s\" " "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE, new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd, - GLOBALS_DUMP_FILE, log_opts.filename); + GLOBALS_DUMP_FILE, log_opts.filename2); check_ok(); /* we load this to get a current list of databases */ @@ -276,7 +276,7 @@ create_new_objects(void) "--no-psqlrc --port %d --username \"%s\" " "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE, new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd, - DB_DUMP_FILE, log_opts.filename); + DB_DUMP_FILE, log_opts.filename2); check_ok(); /* regenerate now that we have objects in the databases */ @@ -324,7 +324,7 @@ copy_clog_xlog_xid(void) exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -l %u,%u,%u \"%s\" >> \"%s\" 2>&1" SYSTEMQUOTE, new_cluster.bindir, old_cluster.controldata.chkpnt_tli, old_cluster.controldata.logid, old_cluster.controldata.nxtlogseg, - new_cluster.pgdata, log_opts.filename); + new_cluster.pgdata, log_opts.filename2); check_ok(); } diff --git a/contrib/pg_upgrade/pg_upgrade.h b/contrib/pg_upgrade/pg_upgrade.h index 4729ac39d9..df884bd684 100644 --- a/contrib/pg_upgrade/pg_upgrade.h +++ b/contrib/pg_upgrade/pg_upgrade.h @@ -199,6 +199,16 @@ typedef struct typedef struct { char *filename; /* name of log file (may be /dev/null) */ + /* + * WIN32 files do not accept writes from multiple processes + * + * On Win32, we can't send both pg_upgrade output and command output to the + * same file because we get the error: "The process cannot access the file + * because it is being used by another process." so we have to send all + * other output to 'nul'. Therefore, we set this to DEVNULL on Win32, and + * it equals 'filename' on all other platforms. + */ + char *filename2; FILE *fd; /* log FILE */ bool debug; /* TRUE -> log more information */ FILE *debug_fd; /* debug-level log FILE */ diff --git a/contrib/pg_upgrade/server.c b/contrib/pg_upgrade/server.c index 59eaad9dcf..17231da790 100644 --- a/contrib/pg_upgrade/server.c +++ b/contrib/pg_upgrade/server.c @@ -147,19 +147,6 @@ start_postmaster(ClusterInfo *cluster) bool exit_hook_registered = false; int pg_ctl_return = 0; -#ifndef WIN32 - char *output_filename = log_opts.filename; -#else - - /* - * On Win32, we can't send both pg_upgrade output and pg_ctl output to the - * same file because we get the error: "The process cannot access the file - * because it is being used by another process." so we have to send all - * other output to 'nul'. - */ - char *output_filename = DEVNULL; -#endif - if (!exit_hook_registered) { #ifdef HAVE_ATEXIT @@ -180,11 +167,11 @@ start_postmaster(ClusterInfo *cluster) snprintf(cmd, sizeof(cmd), SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" " "-o \"-p %d %s\" start >> \"%s\" 2>&1" SYSTEMQUOTE, - cluster->bindir, output_filename, cluster->pgdata, cluster->port, + cluster->bindir, log_opts.filename2, cluster->pgdata, cluster->port, (cluster->controldata.cat_ver >= BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? "-b" : "-c autovacuum=off -c autovacuum_freeze_max_age=2000000000", - log_opts.filename); + log_opts.filename2); /* * Don't throw an error right away, let connecting throw the error because @@ -221,13 +208,6 @@ stop_postmaster(bool fast) const char *bindir; const char *datadir; -#ifndef WIN32 - char *output_filename = log_opts.filename; -#else - /* See comment in start_postmaster() about why win32 output is ignored. */ - char *output_filename = DEVNULL; -#endif - if (os_info.running_cluster == &old_cluster) { bindir = old_cluster.bindir; @@ -244,8 +224,8 @@ stop_postmaster(bool fast) snprintf(cmd, sizeof(cmd), SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" %s stop >> " "\"%s\" 2>&1" SYSTEMQUOTE, - bindir, output_filename, datadir, fast ? "-m fast" : "", - output_filename); + bindir, log_opts.filename2, datadir, fast ? "-m fast" : "", + log_opts.filename2); exec_prog(fast ? false : true, "%s", cmd); -- 2.40.0