42
42
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
43
43
* Portions Copyright (c) 1994, Regents of the University of California
44
44
*
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 $
46
46
*
47
47
*-------------------------------------------------------------------------
48
48
*/
@@ -163,7 +163,7 @@ static char *get_id(void);
163
163
static char * get_encoding_id (char * );
164
164
static char * get_short_version (void );
165
165
static int mkdir_p (char * , mode_t );
166
- static bool check_data_dir (void );
166
+ static int check_data_dir (void );
167
167
static bool mkdatadir (char * );
168
168
static bool chklocale (const char * );
169
169
static void setlocales (void );
@@ -274,8 +274,8 @@ rmtree(char *path, bool rmtopdir)
274
274
char buf [MAXPGPATH + 64 ];
275
275
276
276
#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 ,
279
279
rmtopdir ? "" : "/*" );
280
280
#else
281
281
snprintf (buf , sizeof (buf ), "%s /s /q \"%s\"" ,
@@ -707,18 +707,23 @@ get_short_version(void)
707
707
708
708
/*
709
709
* 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
710
713
*/
711
- static bool
714
+ static int
712
715
check_data_dir (void )
713
716
{
714
717
DIR * chkdir ;
715
718
struct dirent * file ;
716
- bool empty = true;
719
+ int result = 1 ;
720
+
721
+ errno = 0 ;
717
722
718
723
chkdir = opendir (pg_data );
719
724
720
725
if (!chkdir )
721
- return (errno == ENOENT );
726
+ return (errno == ENOENT ) ? 0 : -1 ;
722
727
723
728
while ((file = readdir (chkdir )) != NULL )
724
729
{
@@ -729,14 +734,17 @@ check_data_dir(void)
729
734
}
730
735
else
731
736
{
732
- empty = false;
737
+ result = 2 ; /* not empty */
733
738
break ;
734
739
}
735
740
}
736
741
737
742
closedir (chkdir );
738
743
739
- return empty ;
744
+ if (errno != 0 )
745
+ result = -1 ; /* some kind of I/O error? */
746
+
747
+ return result ;
740
748
}
741
749
742
750
/*
@@ -2315,35 +2323,54 @@ main(int argc, char *argv[])
2315
2323
pqsignal (SIGTERM , trapsig );
2316
2324
#endif
2317
2325
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 ())
2322
2327
{
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 ();
2331
2338
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 ;
2340
2341
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 );
2345
2347
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 );
2347
2374
}
2348
2375
2349
2376
/* Create required subdirectories */
0 commit comments