40
40
*
41
41
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
42
42
* Portions Copyright (c) 1994, Regents of the University of California
43
- * Portions taken from FreeBSD.
44
43
*
45
44
* src/bin/initdb/initdb.c
46
45
*
@@ -152,11 +151,9 @@ static char **filter_lines_with_token(char **lines, const char *token);
152
151
static char * * readfile (const char * path );
153
152
static void writefile (char * path , char * * lines );
154
153
static FILE * popen_check (const char * command , const char * mode );
155
- static int mkdir_p (char * path , mode_t omode );
156
154
static void exit_nicely (void );
157
155
static char * get_id (void );
158
156
static char * get_encoding_id (char * encoding_name );
159
- static int check_data_dir (char * dir );
160
157
static bool mkdatadir (const char * subdir );
161
158
static void set_input (char * * dest , char * filename );
162
159
static void check_input (char * path );
@@ -470,110 +467,6 @@ popen_check(const char *command, const char *mode)
470
467
return cmdfd ;
471
468
}
472
469
473
- /* source stolen from FreeBSD /src/bin/mkdir/mkdir.c and adapted */
474
-
475
- /*
476
- * this tries to build all the elements of a path to a directory a la mkdir -p
477
- * we assume the path is in canonical form, i.e. uses / as the separator
478
- * we also assume it isn't null.
479
- *
480
- * note that on failure, the path arg has been modified to show the particular
481
- * directory level we had problems with.
482
- */
483
- static int
484
- mkdir_p (char * path , mode_t omode )
485
- {
486
- struct stat sb ;
487
- mode_t numask ,
488
- oumask ;
489
- int first ,
490
- last ,
491
- retval ;
492
- char * p ;
493
-
494
- p = path ;
495
- oumask = 0 ;
496
- retval = 0 ;
497
-
498
- #ifdef WIN32
499
- /* skip network and drive specifiers for win32 */
500
- if (strlen (p ) >= 2 )
501
- {
502
- if (p [0 ] == '/' && p [1 ] == '/' )
503
- {
504
- /* network drive */
505
- p = strstr (p + 2 , "/" );
506
- if (p == NULL )
507
- return 1 ;
508
- }
509
- else if (p [1 ] == ':' &&
510
- ((p [0 ] >= 'a' && p [0 ] <= 'z' ) ||
511
- (p [0 ] >= 'A' && p [0 ] <= 'Z' )))
512
- {
513
- /* local drive */
514
- p += 2 ;
515
- }
516
- }
517
- #endif
518
-
519
- if (p [0 ] == '/' ) /* Skip leading '/'. */
520
- ++ p ;
521
- for (first = 1 , last = 0 ; !last ; ++ p )
522
- {
523
- if (p [0 ] == '\0' )
524
- last = 1 ;
525
- else if (p [0 ] != '/' )
526
- continue ;
527
- * p = '\0' ;
528
- if (!last && p [1 ] == '\0' )
529
- last = 1 ;
530
- if (first )
531
- {
532
- /*
533
- * POSIX 1003.2: For each dir operand that does not name an
534
- * existing directory, effects equivalent to those caused by the
535
- * following command shall occcur:
536
- *
537
- * mkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode]
538
- * dir
539
- *
540
- * We change the user's umask and then restore it, instead of
541
- * doing chmod's.
542
- */
543
- oumask = umask (0 );
544
- numask = oumask & ~(S_IWUSR | S_IXUSR );
545
- (void ) umask (numask );
546
- first = 0 ;
547
- }
548
- if (last )
549
- (void ) umask (oumask );
550
-
551
- /* check for pre-existing directory; ok if it's a parent */
552
- if (stat (path , & sb ) == 0 )
553
- {
554
- if (!S_ISDIR (sb .st_mode ))
555
- {
556
- if (last )
557
- errno = EEXIST ;
558
- else
559
- errno = ENOTDIR ;
560
- retval = 1 ;
561
- break ;
562
- }
563
- }
564
- else if (mkdir (path , last ? omode : S_IRWXU | S_IRWXG | S_IRWXO ) < 0 )
565
- {
566
- retval = 1 ;
567
- break ;
568
- }
569
- if (!last )
570
- * p = '/' ;
571
- }
572
- if (!first && !last )
573
- (void ) umask (oumask );
574
- return retval ;
575
- }
576
-
577
470
/*
578
471
* clean up any files we created on failure
579
472
* if we created the data directory remove it too
@@ -801,59 +694,6 @@ find_matching_ts_config(const char *lc_type)
801
694
}
802
695
803
696
804
- /*
805
- * make sure the directory either doesn't exist or is empty
806
- *
807
- * Returns 0 if nonexistent, 1 if exists and empty, 2 if not empty,
808
- * or -1 if trouble accessing directory
809
- */
810
- static int
811
- check_data_dir (char * dir )
812
- {
813
- DIR * chkdir ;
814
- struct dirent * file ;
815
- int result = 1 ;
816
-
817
- errno = 0 ;
818
-
819
- chkdir = opendir (dir );
820
-
821
- if (!chkdir )
822
- return (errno == ENOENT ) ? 0 : -1 ;
823
-
824
- while ((file = readdir (chkdir )) != NULL )
825
- {
826
- if (strcmp ("." , file -> d_name ) == 0 ||
827
- strcmp (".." , file -> d_name ) == 0 )
828
- {
829
- /* skip this and parent directory */
830
- continue ;
831
- }
832
- else
833
- {
834
- result = 2 ; /* not empty */
835
- break ;
836
- }
837
- }
838
-
839
- #ifdef WIN32
840
-
841
- /*
842
- * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
843
- * released version
844
- */
845
- if (GetLastError () == ERROR_NO_MORE_FILES )
846
- errno = 0 ;
847
- #endif
848
-
849
- closedir (chkdir );
850
-
851
- if (errno != 0 )
852
- result = -1 ; /* some kind of I/O error? */
853
-
854
- return result ;
855
- }
856
-
857
697
/*
858
698
* make the data directory (or one of its subdirectories if subdir is not NULL)
859
699
*/
@@ -870,7 +710,7 @@ mkdatadir(const char *subdir)
870
710
else
871
711
strcpy (path , pg_data );
872
712
873
- if (mkdir_p (path , S_IRWXU ) == 0 )
713
+ if (pg_mkdir_p (path , S_IRWXU ) == 0 )
874
714
return true;
875
715
876
716
fprintf (stderr , _ ("%s: could not create directory \"%s\": %s\n" ),
@@ -2929,7 +2769,7 @@ main(int argc, char *argv[])
2929
2769
pqsignal (SIGPIPE , SIG_IGN );
2930
2770
#endif
2931
2771
2932
- switch (check_data_dir (pg_data ))
2772
+ switch (pg_check_dir (pg_data ))
2933
2773
{
2934
2774
case 0 :
2935
2775
/* PGDATA not there, must create it */
@@ -2995,16 +2835,16 @@ main(int argc, char *argv[])
2995
2835
exit_nicely ();
2996
2836
}
2997
2837
2998
- /* check if the specified xlog directory is empty */
2999
- switch (check_data_dir (xlog_dir ))
2838
+ /* check if the specified xlog directory exists/ is empty */
2839
+ switch (pg_check_dir (xlog_dir ))
3000
2840
{
3001
2841
case 0 :
3002
2842
/* xlog directory not there, must create it */
3003
2843
printf (_ ("creating directory %s ... " ),
3004
2844
xlog_dir );
3005
2845
fflush (stdout );
3006
2846
3007
- if (mkdir_p (xlog_dir , S_IRWXU ) != 0 )
2847
+ if (pg_mkdir_p (xlog_dir , S_IRWXU ) != 0 )
3008
2848
{
3009
2849
fprintf (stderr , _ ("%s: could not create directory \"%s\": %s\n" ),
3010
2850
progname , xlog_dir , strerror (errno ));
@@ -3015,6 +2855,7 @@ main(int argc, char *argv[])
3015
2855
3016
2856
made_new_xlogdir = true;
3017
2857
break ;
2858
+
3018
2859
case 1 :
3019
2860
/* Present but empty, fix permissions and use it */
3020
2861
printf (_ ("fixing permissions on existing directory %s ... " ),
@@ -3032,6 +2873,7 @@ main(int argc, char *argv[])
3032
2873
3033
2874
found_existing_xlogdir = true;
3034
2875
break ;
2876
+
3035
2877
case 2 :
3036
2878
/* Present and not empty */
3037
2879
fprintf (stderr ,
0 commit comments