pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
To: pgsql-hackers <pgsql-hackers@postgresql.org>
Subject: pg_upgrade: Improve invalid option handling
Date: Thu, 13 Jun 2019 10:19:05 +0200
Message-ID: <24c8bd05-aed1-6301-919d-8acbabdb8c24@2ndquadrant.com> (raw)

Currently, calling pg_upgrade with an invalid command-line option aborts
pg_upgrade but leaves a pg_upgrade_internal.log file lying around.  This
patch reorder things a bit so that that file is not created until all
the options have been parsed.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
From 69a1f9259c025fc38280b41ced25571feeb90ef8 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 13 Jun 2019 10:12:34 +0200
Subject: [PATCH] pg_upgrade: Improve invalid option handling

Currently, calling pg_upgrade with an invalid command-line option
aborts pg_upgrade but leaves a pg_upgrade_internal.log file lying
around.  Reorder things a bit so that that file is not created until
all the options have been parsed.
---
 src/bin/pg_upgrade/option.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/bin/pg_upgrade/option.c b/src/bin/pg_upgrade/option.c
index 171aaa8f13..873d1d07e4 100644
--- a/src/bin/pg_upgrade/option.c
+++ b/src/bin/pg_upgrade/option.c
@@ -101,9 +101,6 @@ parseCommandLine(int argc, char *argv[])
 	if (os_user_effective_id == 0)
 		pg_fatal("%s: cannot be run as root\n", os_info.progname);
 
-	if ((log_opts.internal = fopen_priv(INTERNAL_LOG_FILE, "a")) == NULL)
-		pg_fatal("could not write to log file \"%s\"\n", INTERNAL_LOG_FILE);
-
 	while ((option = getopt_long(argc, argv, "d:D:b:B:cj:ko:O:p:P:rs:U:v",
 								 long_options, &optindex)) != -1)
 	{
@@ -205,7 +202,6 @@ parseCommandLine(int argc, char *argv[])
 				break;
 
 			case 'v':
-				pg_log(PG_REPORT, "Running in verbose mode\n");
 				log_opts.verbose = true;
 				break;
 
@@ -214,12 +210,18 @@ parseCommandLine(int argc, char *argv[])
 				break;
 
 			default:
-				pg_fatal("Try \"%s --help\" for more information.\n",
-						 os_info.progname);
-				break;
+				fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
+						os_info.progname);
+				exit(1);
 		}
 	}
 
+	if ((log_opts.internal = fopen_priv(INTERNAL_LOG_FILE, "a")) == NULL)
+		pg_fatal("could not write to log file \"%s\"\n", INTERNAL_LOG_FILE);
+
+	if (log_opts.verbose)
+		pg_log(PG_REPORT, "Running in verbose mode\n");
+
 	/* label start of upgrade in logfiles */
 	for (filename = output_files; *filename != NULL; filename++)
 	{
-- 
2.22.0



Attachments:

  [text/plain] 0001-pg_upgrade-Improve-invalid-option-handling.patch (1.9K, ../24c8bd05-aed1-6301-919d-8acbabdb8c24@2ndquadrant.com/2-0001-pg_upgrade-Improve-invalid-option-handling.patch)
  download | inline diff:
From 69a1f9259c025fc38280b41ced25571feeb90ef8 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 13 Jun 2019 10:12:34 +0200
Subject: [PATCH] pg_upgrade: Improve invalid option handling

Currently, calling pg_upgrade with an invalid command-line option
aborts pg_upgrade but leaves a pg_upgrade_internal.log file lying
around.  Reorder things a bit so that that file is not created until
all the options have been parsed.
---
 src/bin/pg_upgrade/option.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/bin/pg_upgrade/option.c b/src/bin/pg_upgrade/option.c
index 171aaa8f13..873d1d07e4 100644
--- a/src/bin/pg_upgrade/option.c
+++ b/src/bin/pg_upgrade/option.c
@@ -101,9 +101,6 @@ parseCommandLine(int argc, char *argv[])
 	if (os_user_effective_id == 0)
 		pg_fatal("%s: cannot be run as root\n", os_info.progname);
 
-	if ((log_opts.internal = fopen_priv(INTERNAL_LOG_FILE, "a")) == NULL)
-		pg_fatal("could not write to log file \"%s\"\n", INTERNAL_LOG_FILE);
-
 	while ((option = getopt_long(argc, argv, "d:D:b:B:cj:ko:O:p:P:rs:U:v",
 								 long_options, &optindex)) != -1)
 	{
@@ -205,7 +202,6 @@ parseCommandLine(int argc, char *argv[])
 				break;
 
 			case 'v':
-				pg_log(PG_REPORT, "Running in verbose mode\n");
 				log_opts.verbose = true;
 				break;
 
@@ -214,12 +210,18 @@ parseCommandLine(int argc, char *argv[])
 				break;
 
 			default:
-				pg_fatal("Try \"%s --help\" for more information.\n",
-						 os_info.progname);
-				break;
+				fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
+						os_info.progname);
+				exit(1);
 		}
 	}
 
+	if ((log_opts.internal = fopen_priv(INTERNAL_LOG_FILE, "a")) == NULL)
+		pg_fatal("could not write to log file \"%s\"\n", INTERNAL_LOG_FILE);
+
+	if (log_opts.verbose)
+		pg_log(PG_REPORT, "Running in verbose mode\n");
+
 	/* label start of upgrade in logfiles */
 	for (filename = output_files; *filename != NULL; filename++)
 	{
-- 
2.22.0



view thread (9+ messages)  latest in thread

Message-ID: <24c8bd05-aed1-6301-919d-8acbabdb8c24@2ndquadrant.com>
Permalink:  ../24c8bd05-aed1-6301-919d-8acbabdb8c24@2ndquadrant.com/
Also on:    postgresql.org/message-id/24c8bd05-aed1-6301-919d-8acbabdb8c24@2ndquadrant.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: peter.eisentraut@2ndquadrant.com
  Subject: Re: pg_upgrade: Improve invalid option handling
  In-Reply-To: <24c8bd05-aed1-6301-919d-8acbabdb8c24@2ndquadrant.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox