initdb: Improve error recovery.

Started by Mats Erik Anderssonabout 11 years ago3 messages
#1Mats Erik Andersson
bsd@gisladisker.se

Hello there,

I would like to improve error recovery of initdb when the
password file is empty. The present code declares "Error 0"
as the cause of failure. It suffices to use ferrer() since
fgets() returns NULL also at a premature EOF.

In addition, a minor case is the need of a line feed in
order to print the error message on a line of its own
seems desirable.

Best regards,
Mats Erik Andersson

From 5ffe171fb63497a5e2d9d9233282504da0044b8e Mon Sep 17 00:00:00 2001
From: Mats Erik Andersson <bsd@gisladisker.se>
Date: Thu, 27 Nov 2014 12:08:31 +0100
Subject: [PATCH] initdb: Improve error recovery.

In case "--pwfile" encounters an empty file, this should be
pointed out, not reporting the cause as "Error 0" which is
the claim by strerror(). The return value of fgets() is NULL
at EOF, so ferror() must be used to tell the conditions apart.

Insert a line break to interrupt finalize the message "mmmmm ..."
whenever the bki_file is missing.
---
src/bin/initdb/initdb.c | 4 +++-
1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 3b52867..b76fb3b 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -1509,6 +1509,7 @@ bootstrap_template1(void)
 	if (strcmp(headerline, *bki_lines) != 0)
 	{
+		fprintf(stderr, "\n");
 		fprintf(stderr,
 				_("%s: input file \"%s\" does not belong to PostgreSQL %s\n"
 				  "Check your installation or specify the correct path "
@@ -1663,7 +1664,8 @@ get_set_pwd(void)
 		if (!fgets(pwdbuf, sizeof(pwdbuf), pwf))
 		{
 			fprintf(stderr, _("%s: could not read password from file \"%s\": %s\n"),
-					progname, pwfilename, strerror(errno));
+					progname, pwfilename,
+					ferror(pwf) ? strerror(errno) : _("empty file"));
 			exit_nicely();
 		}
 		fclose(pwf);
-- 
1.7.3.2

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#2Robert Haas
robertmhaas@gmail.com
In reply to: Mats Erik Andersson (#1)
Re: initdb: Improve error recovery.

On Thu, Nov 27, 2014 at 7:28 AM, Mats Erik Andersson <bsd@gisladisker.se> wrote:

I would like to improve error recovery of initdb when the
password file is empty. The present code declares "Error 0"
as the cause of failure. It suffices to use ferrer() since
fgets() returns NULL also at a premature EOF.

In addition, a minor case is the need of a line feed in
order to print the error message on a line of its own
seems desirable.

Best regards,
Mats Erik Andersson

Please add your patch here so that it doesn't get forgotten about:

https://commitfest.postgresql.org/action/commitfest_view/open

Also, note that the PostgreSQL project prefers for patches to be
attached rather than inline, as mailers have a tendency to mangle
them.

Thanks,

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#3Heikki Linnakangas
hlinnakangas@vmware.com
In reply to: Mats Erik Andersson (#1)
Re: initdb: Improve error recovery.

On 11/27/2014 02:28 PM, Mats Erik Andersson wrote:

Hello there,

I would like to improve error recovery of initdb when the
password file is empty. The present code declares "Error 0"
as the cause of failure. It suffices to use ferrer() since
fgets() returns NULL also at a premature EOF.

Thanks, committed.

In addition, a minor case is the need of a line feed in
order to print the error message on a line of its own
seems desirable.

Hmm. If you've piped stdout somewhere else, that produces a spurious
empty line in stderr:

$ bin/initdb -D data-foo > /dev/null

initdb: input file "/home/heikki/pgsql.master/share/postgres.bki" does
not belong to PostgreSQL 9.5devel
Check your installation or specify the correct path using the option -L.
initdb: removing data directory "data-foo"

I think the newline needs to go to stdout instead.

But in any case, that's just one error out of many similar ones that can
happen during initdb. If we care enough to fix this, we should fix them
all. Not sure it's worth the churn, though, unless you can come up with
some kind of a simple wholesale solution, rather than adding
fprintf(stdout, "\n") calls to every error condition.
- Heikki

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers