diff options
author | Tom Lane | 2007-12-11 02:08:59 +0000 |
---|---|---|
committer | Tom Lane | 2007-12-11 02:08:59 +0000 |
commit | 46f7c42bd45c65fc470b834c93580e91e1323a18 (patch) | |
tree | c8ce9184bdd0cc59873cb2dacbc84fe523de81c8 | |
parent | 172871e38f3cff672fc050d9b87f8f1b470dd1c1 (diff) |
Make vacuumlo prompt for password when needed, thus making its -W
switch optional, as is the case for every other one of our programs.
I had already documented its -W as being optional, so this is bringing
the code into line with the docs ...
-rw-r--r-- | contrib/vacuumlo/vacuumlo.c | 52 |
1 files changed, 35 insertions, 17 deletions
diff --git a/contrib/vacuumlo/vacuumlo.c b/contrib/vacuumlo/vacuumlo.c index 6ddb19867f..a6711ce5bd 100644 --- a/contrib/vacuumlo/vacuumlo.c +++ b/contrib/vacuumlo/vacuumlo.c @@ -61,32 +61,50 @@ vacuumlo(char *database, struct _param * param) int matched; int deleted; int i; - char *password = NULL; + static char *password = NULL; + bool new_pass; - if (param->pg_prompt) + if (param->pg_prompt && password == NULL) + password = simple_prompt("Password: ", 100, false); + + /* + * Start the connection. Loop until we have a password if requested by + * backend. + */ + do { - password = simple_prompt("Password: ", 32, 0); - if (!password) + new_pass = false; + + conn = PQsetdbLogin(param->pg_host, + param->pg_port, + NULL, + NULL, + database, + param->pg_user, + password); + if (!conn) { - fprintf(stderr, "failed to get password\n"); - exit(1); + fprintf(stderr, "Connection to database \"%s\" failed\n", + database); + return -1; } - } - conn = PQsetdbLogin(param->pg_host, - param->pg_port, - NULL, - NULL, - database, - param->pg_user, - password - ); + if (PQstatus(conn) == CONNECTION_BAD && + PQconnectionNeedsPassword(conn) && + password == NULL && + !feof(stdin)) + { + PQfinish(conn); + password = simple_prompt("Password: ", 100, false); + new_pass = true; + } + } while (new_pass); /* check to see that the backend connection was successfully made */ if (PQstatus(conn) == CONNECTION_BAD) { - fprintf(stderr, "Connection to database '%s' failed:\n", database); - fprintf(stderr, "%s", PQerrorMessage(conn)); + fprintf(stderr, "Connection to database \"%s\" failed:\n%s", + database, PQerrorMessage(conn)); PQfinish(conn); return -1; } |