Skip to content

Commit 0104fc1

Browse files
committed
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...)
1 parent c38ff52 commit 0104fc1

File tree

1 file changed

+61
-34
lines changed

1 file changed

+61
-34
lines changed

src/bin/initdb/initdb.c

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
4343
* Portions Copyright (c) 1994, Regents of the University of California
4444
*
45-
* $Header: /cvsroot/pgsql/src/bin/initdb/initdb.c,v 1.7 2003/11/13 23:46:31 tgl Exp $
45+
* $Header: /cvsroot/pgsql/src/bin/initdb/initdb.c,v 1.8 2003/11/14 17:19:35 tgl Exp $
4646
*
4747
*-------------------------------------------------------------------------
4848
*/
@@ -163,7 +163,7 @@ static char *get_id(void);
163163
static char *get_encoding_id(char *);
164164
static char *get_short_version(void);
165165
static int mkdir_p(char *, mode_t);
166-
static bool check_data_dir(void);
166+
static int check_data_dir(void);
167167
static bool mkdatadir(char *);
168168
static bool chklocale(const char *);
169169
static void setlocales(void);
@@ -274,8 +274,8 @@ rmtree(char *path, bool rmtopdir)
274274
char buf[MAXPGPATH + 64];
275275

276276
#ifndef WIN32
277-
/* doesn't handle .* files */
278-
snprintf(buf, sizeof(buf), "rm -rf '%s%s'", path,
277+
/* doesn't handle .* files, but we don't make any... */
278+
snprintf(buf, sizeof(buf), "rm -rf \"%s\"%s", path,
279279
rmtopdir ? "" : "/*");
280280
#else
281281
snprintf(buf, sizeof(buf), "%s /s /q \"%s\"",
@@ -707,18 +707,23 @@ get_short_version(void)
707707

708708
/*
709709
* make sure the data directory either doesn't exist or is empty
710+
*
711+
* Returns 0 if nonexistent, 1 if exists and empty, 2 if not empty,
712+
* or -1 if trouble accessing directory
710713
*/
711-
static bool
714+
static int
712715
check_data_dir(void)
713716
{
714717
DIR *chkdir;
715718
struct dirent *file;
716-
bool empty = true;
719+
int result = 1;
720+
721+
errno = 0;
717722

718723
chkdir = opendir(pg_data);
719724

720725
if (!chkdir)
721-
return (errno == ENOENT);
726+
return (errno == ENOENT) ? 0 : -1;
722727

723728
while ((file = readdir(chkdir)) != NULL)
724729
{
@@ -729,14 +734,17 @@ check_data_dir(void)
729734
}
730735
else
731736
{
732-
empty = false;
737+
result = 2; /* not empty */
733738
break;
734739
}
735740
}
736741

737742
closedir(chkdir);
738743

739-
return empty;
744+
if (errno != 0)
745+
result = -1; /* some kind of I/O error? */
746+
747+
return result;
740748
}
741749

742750
/*
@@ -2315,35 +2323,54 @@ main(int argc, char *argv[])
23152323
pqsignal(SIGTERM, trapsig);
23162324
#endif
23172325

2318-
/* clear this we'll use it in a few lines */
2319-
errno = 0;
2320-
2321-
if (!check_data_dir())
2326+
switch (check_data_dir())
23222327
{
2323-
fprintf(stderr,
2324-
"%s: directory \"%s\" exists but is not empty\n"
2325-
"If you want to create a new database system, either remove or empty\n"
2326-
"the directory \"%s\" or run %s\n"
2327-
"with an argument other than \"%s\".\n",
2328-
progname, pg_data, pg_data, progname, pg_data);
2329-
exit(1);
2330-
}
2328+
case 0:
2329+
/* PGDATA not there, must create it */
2330+
printf("creating directory %s ... ",
2331+
pg_data);
2332+
fflush(stdout);
2333+
2334+
if (!mkdatadir(NULL))
2335+
exit_nicely();
2336+
else
2337+
check_ok();
23312338

2332-
/*
2333-
* check_data_dir() called opendir - the errno should still be hanging
2334-
* around
2335-
*/
2336-
if (errno == ENOENT)
2337-
{
2338-
printf("creating directory %s ... ", pg_data);
2339-
fflush(stdout);
2339+
made_new_pgdata = true;
2340+
break;
23402341

2341-
if (!mkdatadir(NULL))
2342-
exit_nicely();
2343-
else
2344-
check_ok();
2342+
case 1:
2343+
/* Present but empty, fix permissions and use it */
2344+
printf("fixing permissions on existing directory %s ... ",
2345+
pg_data);
2346+
fflush(stdout);
23452347

2346-
made_new_pgdata = true;
2348+
if (chmod(pg_data, 0700) != 0)
2349+
{
2350+
perror(pg_data);
2351+
/* don't exit_nicely(), it'll try to remove pg_data contents */
2352+
exit(1);
2353+
}
2354+
else
2355+
check_ok();
2356+
break;
2357+
2358+
case 2:
2359+
/* Present and not empty */
2360+
fprintf(stderr,
2361+
"%s: directory \"%s\" exists but is not empty\n"
2362+
"If you want to create a new database system, either remove or empty\n"
2363+
"the directory \"%s\" or run %s\n"
2364+
"with an argument other than \"%s\".\n",
2365+
progname, pg_data, pg_data, progname, pg_data);
2366+
/* don't exit_nicely(), it'll try to remove pg_data contents */
2367+
exit(1);
2368+
2369+
default:
2370+
/* Trouble accessing directory */
2371+
perror(pg_data);
2372+
/* don't exit_nicely(), it'll try to remove pg_data contents */
2373+
exit(1);
23472374
}
23482375

23492376
/* Create required subdirectories */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy