Skip to content

Commit 882c592

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 c7aca3d commit 882c592

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
@@ -200,6 +200,7 @@ static const char *subdirs[] = {
200200
"pg_snapshots",
201201
"pg_subtrans",
202202
"pg_twophase",
203+
"pg_multixact",
203204
"pg_multixact/members",
204205
"pg_multixact/offsets",
205206
"base",
@@ -237,7 +238,6 @@ static FILE *popen_check(const char *command, const char *mode);
237238
static void exit_nicely(void);
238239
static char *get_id(void);
239240
static char *get_encoding_id(char *encoding_name);
240-
static bool mkdatadir(const char *subdir);
241241
static void set_input(char **dest, char *filename);
242242
static void check_input(char *path);
243243
static void write_version_file(char *extrapath);
@@ -932,29 +932,6 @@ find_matching_ts_config(const char *lc_type)
932932
}
933933

934934

935-
/*
936-
* make the data directory (or one of its subdirectories if subdir is not NULL)
937-
*/
938-
static bool
939-
mkdatadir(const char *subdir)
940-
{
941-
char *path;
942-
943-
if (subdir)
944-
path = psprintf("%s/%s", pg_data, subdir);
945-
else
946-
path = pg_strdup(pg_data);
947-
948-
if (pg_mkdir_p(path, S_IRWXU) == 0)
949-
return true;
950-
951-
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
952-
progname, path, strerror(errno));
953-
954-
return false;
955-
}
956-
957-
958935
/*
959936
* set name of given input file variable under data directory
960937
*/
@@ -3296,8 +3273,12 @@ create_data_directory(void)
32963273
pg_data);
32973274
fflush(stdout);
32983275

3299-
if (!mkdatadir(NULL))
3276+
if (pg_mkdir_p(pg_data, S_IRWXU) != 0)
3277+
{
3278+
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
3279+
progname, pg_data, strerror(errno));
33003280
exit_nicely();
3281+
}
33013282
else
33023283
check_ok();
33033284

@@ -3479,10 +3460,24 @@ initialize_data_directory(void)
34793460
printf(_("creating subdirectories ... "));
34803461
fflush(stdout);
34813462

3482-
for (i = 0; i < (sizeof(subdirs) / sizeof(char *)); i++)
3463+
for (i = 0; i < lengthof(subdirs); i++)
34833464
{
3484-
if (!mkdatadir(subdirs[i]))
3465+
char *path;
3466+
3467+
path = psprintf("%s/%s", pg_data, subdirs[i]);
3468+
3469+
/*
3470+
* The parent directory already exists, so we only need mkdir() not
3471+
* pg_mkdir_p() here, which avoids some failure modes; cf bug #13853.
3472+
*/
3473+
if (mkdir(path, S_IRWXU) < 0)
3474+
{
3475+
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
3476+
progname, path, strerror(errno));
34853477
exit_nicely();
3478+
}
3479+
3480+
free(path);
34863481
}
34873482

34883483
check_ok();

0 commit comments

Comments
 (0)