Commit 0104fc11 authored by Tom Lane's avatar Tom Lane

Add missing logic to handle fixing permissions on an already-existing

data directory.  Also fix handling of error conditions associated with
data directory checking step (can't use a boolean to distinguish four
possible result states...)
parent c38ff526
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Header: /cvsroot/pgsql/src/bin/initdb/initdb.c,v 1.7 2003/11/13 23:46:31 tgl Exp $ * $Header: /cvsroot/pgsql/src/bin/initdb/initdb.c,v 1.8 2003/11/14 17:19:35 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -163,7 +163,7 @@ static char *get_id(void); ...@@ -163,7 +163,7 @@ static char *get_id(void);
static char *get_encoding_id(char *); static char *get_encoding_id(char *);
static char *get_short_version(void); static char *get_short_version(void);
static int mkdir_p(char *, mode_t); static int mkdir_p(char *, mode_t);
static bool check_data_dir(void); static int check_data_dir(void);
static bool mkdatadir(char *); static bool mkdatadir(char *);
static bool chklocale(const char *); static bool chklocale(const char *);
static void setlocales(void); static void setlocales(void);
...@@ -274,8 +274,8 @@ rmtree(char *path, bool rmtopdir) ...@@ -274,8 +274,8 @@ rmtree(char *path, bool rmtopdir)
char buf[MAXPGPATH + 64]; char buf[MAXPGPATH + 64];
#ifndef WIN32 #ifndef WIN32
/* doesn't handle .* files */ /* doesn't handle .* files, but we don't make any... */
snprintf(buf, sizeof(buf), "rm -rf '%s%s'", path, snprintf(buf, sizeof(buf), "rm -rf \"%s\"%s", path,
rmtopdir ? "" : "/*"); rmtopdir ? "" : "/*");
#else #else
snprintf(buf, sizeof(buf), "%s /s /q \"%s\"", snprintf(buf, sizeof(buf), "%s /s /q \"%s\"",
...@@ -707,18 +707,23 @@ get_short_version(void) ...@@ -707,18 +707,23 @@ get_short_version(void)
/* /*
* make sure the data directory either doesn't exist or is empty * make sure the data directory either doesn't exist or is empty
*
* Returns 0 if nonexistent, 1 if exists and empty, 2 if not empty,
* or -1 if trouble accessing directory
*/ */
static bool static int
check_data_dir(void) check_data_dir(void)
{ {
DIR *chkdir; DIR *chkdir;
struct dirent *file; struct dirent *file;
bool empty = true; int result = 1;
errno = 0;
chkdir = opendir(pg_data); chkdir = opendir(pg_data);
if (!chkdir) if (!chkdir)
return (errno == ENOENT); return (errno == ENOENT) ? 0 : -1;
while ((file = readdir(chkdir)) != NULL) while ((file = readdir(chkdir)) != NULL)
{ {
...@@ -729,14 +734,17 @@ check_data_dir(void) ...@@ -729,14 +734,17 @@ check_data_dir(void)
} }
else else
{ {
empty = false; result = 2; /* not empty */
break; break;
} }
} }
closedir(chkdir); closedir(chkdir);
return empty; if (errno != 0)
result = -1; /* some kind of I/O error? */
return result;
} }
/* /*
...@@ -2315,35 +2323,54 @@ main(int argc, char *argv[]) ...@@ -2315,35 +2323,54 @@ main(int argc, char *argv[])
pqsignal(SIGTERM, trapsig); pqsignal(SIGTERM, trapsig);
#endif #endif
/* clear this we'll use it in a few lines */ switch (check_data_dir())
errno = 0;
if (!check_data_dir())
{ {
fprintf(stderr, case 0:
"%s: directory \"%s\" exists but is not empty\n" /* PGDATA not there, must create it */
"If you want to create a new database system, either remove or empty\n" printf("creating directory %s ... ",
"the directory \"%s\" or run %s\n" pg_data);
"with an argument other than \"%s\".\n", fflush(stdout);
progname, pg_data, pg_data, progname, pg_data);
exit(1); if (!mkdatadir(NULL))
} exit_nicely();
else
check_ok();
/* made_new_pgdata = true;
* check_data_dir() called opendir - the errno should still be hanging break;
* around
*/
if (errno == ENOENT)
{
printf("creating directory %s ... ", pg_data);
fflush(stdout);
if (!mkdatadir(NULL)) case 1:
exit_nicely(); /* Present but empty, fix permissions and use it */
else printf("fixing permissions on existing directory %s ... ",
check_ok(); pg_data);
fflush(stdout);
made_new_pgdata = true; if (chmod(pg_data, 0700) != 0)
{
perror(pg_data);
/* don't exit_nicely(), it'll try to remove pg_data contents */
exit(1);
}
else
check_ok();
break;
case 2:
/* Present and not empty */
fprintf(stderr,
"%s: directory \"%s\" exists but is not empty\n"
"If you want to create a new database system, either remove or empty\n"
"the directory \"%s\" or run %s\n"
"with an argument other than \"%s\".\n",
progname, pg_data, pg_data, progname, pg_data);
/* don't exit_nicely(), it'll try to remove pg_data contents */
exit(1);
default:
/* Trouble accessing directory */
perror(pg_data);
/* don't exit_nicely(), it'll try to remove pg_data contents */
exit(1);
} }
/* Create required subdirectories */ /* Create required subdirectories */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment