Skip to content

Commit 32e7e7f

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 744d01c commit 32e7e7f

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
@@ -199,6 +199,7 @@ static const char *subdirs[] = {
199199
"pg_snapshots",
200200
"pg_subtrans",
201201
"pg_twophase",
202+
"pg_multixact",
202203
"pg_multixact/members",
203204
"pg_multixact/offsets",
204205
"base",
@@ -236,7 +237,6 @@ static FILE *popen_check(const char *command, const char *mode);
236237
static void exit_nicely(void);
237238
static char *get_id(void);
238239
static char *get_encoding_id(char *encoding_name);
239-
static bool mkdatadir(const char *subdir);
240240
static void set_input(char **dest, char *filename);
241241
static void check_input(char *path);
242242
static void write_version_file(char *extrapath);
@@ -924,29 +924,6 @@ find_matching_ts_config(const char *lc_type)
924924
}
925925

926926

927-
/*
928-
* make the data directory (or one of its subdirectories if subdir is not NULL)
929-
*/
930-
static bool
931-
mkdatadir(const char *subdir)
932-
{
933-
char *path;
934-
935-
if (subdir)
936-
path = psprintf("%s/%s", pg_data, subdir);
937-
else
938-
path = pg_strdup(pg_data);
939-
940-
if (pg_mkdir_p(path, S_IRWXU) == 0)
941-
return true;
942-
943-
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
944-
progname, path, strerror(errno));
945-
946-
return false;
947-
}
948-
949-
950927
/*
951928
* set name of given input file variable under data directory
952929
*/
@@ -3134,8 +3111,12 @@ create_data_directory(void)
31343111
pg_data);
31353112
fflush(stdout);
31363113

3137-
if (!mkdatadir(NULL))
3114+
if (pg_mkdir_p(pg_data, S_IRWXU) != 0)
3115+
{
3116+
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
3117+
progname, pg_data, strerror(errno));
31383118
exit_nicely();
3119+
}
31393120
else
31403121
check_ok();
31413122

@@ -3317,10 +3298,24 @@ initialize_data_directory(void)
33173298
printf(_("creating subdirectories ... "));
33183299
fflush(stdout);
33193300

3320-
for (i = 0; i < (sizeof(subdirs) / sizeof(char *)); i++)
3301+
for (i = 0; i < lengthof(subdirs); i++)
33213302
{
3322-
if (!mkdatadir(subdirs[i]))
3303+
char *path;
3304+
3305+
path = psprintf("%s/%s", pg_data, subdirs[i]);
3306+
3307+
/*
3308+
* The parent directory already exists, so we only need mkdir() not
3309+
* pg_mkdir_p() here, which avoids some failure modes; cf bug #13853.
3310+
*/
3311+
if (mkdir(path, S_IRWXU) < 0)
3312+
{
3313+
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
3314+
progname, path, strerror(errno));
33233315
exit_nicely();
3316+
}
3317+
3318+
free(path);
33243319
}
33253320

33263321
check_ok();

0 commit comments

Comments
 (0)