Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 33b054b

Browse files
committed
Use plain mkdir() not pg_mkdir_p() to create subdirectories of PGDATA.
When we're creating subdirectories of PGDATA during initdb, we know darn well that the parent directory exists (or should exist) and that the new subdirectory doesn't (or shouldn't). There is therefore no need to use anything more complicated than mkdir(). Using pg_mkdir_p() just opens us up to unexpected failure modes, such as the one exhibited in bug #13853 from Nuri Boardman. It's not very clear why pg_mkdir_p() went wrong there, but it is clear that we didn't need to be trying to create parent directories in the first place. We're not even saving any code, as proven by the fact that this patch nets out at minus five lines. Since this is a response to a field bug report, back-patch to all branches.
1 parent b1a9bad commit 33b054b

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

src/bin/initdb/initdb.c

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ static const char *const subdirs[] = {
203203
"pg_snapshots",
204204
"pg_subtrans",
205205
"pg_twophase",
206+
"pg_multixact",
206207
"pg_multixact/members",
207208
"pg_multixact/offsets",
208209
"base",
@@ -240,7 +241,6 @@ static FILE *popen_check(const char *command, const char *mode);
240241
static void exit_nicely(void);
241242
static char *get_id(void);
242243
static char *get_encoding_id(char *encoding_name);
243-
static bool mkdatadir(const char *subdir);
244244
static void set_input(char **dest, char *filename);
245245
static void check_input(char *path);
246246
static void write_version_file(char *extrapath);
@@ -928,29 +928,6 @@ find_matching_ts_config(const char *lc_type)
928928
}
929929

930930

931-
/*
932-
* make the data directory (or one of its subdirectories if subdir is not NULL)
933-
*/
934-
static bool
935-
mkdatadir(const char *subdir)
936-
{
937-
char *path;
938-
939-
if (subdir)
940-
path = psprintf("%s/%s", pg_data, subdir);
941-
else
942-
path = pg_strdup(pg_data);
943-
944-
if (pg_mkdir_p(path, S_IRWXU) == 0)
945-
return true;
946-
947-
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
948-
progname, path, strerror(errno));
949-
950-
return false;
951-
}
952-
953-
954931
/*
955932
* set name of given input file variable under data directory
956933
*/
@@ -2915,8 +2892,12 @@ create_data_directory(void)
29152892
pg_data);
29162893
fflush(stdout);
29172894

2918-
if (!mkdatadir(NULL))
2895+
if (pg_mkdir_p(pg_data, S_IRWXU) != 0)
2896+
{
2897+
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
2898+
progname, pg_data, strerror(errno));
29192899
exit_nicely();
2900+
}
29202901
else
29212902
check_ok();
29222903

@@ -3099,10 +3080,24 @@ initialize_data_directory(void)
30993080
printf(_("creating subdirectories ... "));
31003081
fflush(stdout);
31013082

3102-
for (i = 0; i < (sizeof(subdirs) / sizeof(char *)); i++)
3083+
for (i = 0; i < lengthof(subdirs); i++)
31033084
{
3104-
if (!mkdatadir(subdirs[i]))
3085+
char *path;
3086+
3087+
path = psprintf("%s/%s", pg_data, subdirs[i]);
3088+
3089+
/*
3090+
* The parent directory already exists, so we only need mkdir() not
3091+
* pg_mkdir_p() here, which avoids some failure modes; cf bug #13853.
3092+
*/
3093+
if (mkdir(path, S_IRWXU) < 0)
3094+
{
3095+
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
3096+
progname, path, strerror(errno));
31053097
exit_nicely();
3098+
}
3099+
3100+
free(path);
31063101
}
31073102

31083103
check_ok();

0 commit comments

Comments
 (0)