Skip to content

Commit 9486d02

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 74d4009 commit 9486d02

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

src/bin/initdb/initdb.c

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ const char *subdirs[] = {
196196
"pg_snapshots",
197197
"pg_subtrans",
198198
"pg_twophase",
199+
"pg_multixact",
199200
"pg_multixact/members",
200201
"pg_multixact/offsets",
201202
"base",
@@ -229,7 +230,6 @@ static FILE *popen_check(const char *command, const char *mode);
229230
static void exit_nicely(void);
230231
static char *get_id(void);
231232
static char *get_encoding_id(char *encoding_name);
232-
static bool mkdatadir(const char *subdir);
233233
static void set_input(char **dest, char *filename);
234234
static void check_input(char *path);
235235
static void write_version_file(char *extrapath);
@@ -955,32 +955,6 @@ find_matching_ts_config(const char *lc_type)
955955
}
956956

957957

958-
/*
959-
* make the data directory (or one of its subdirectories if subdir is not NULL)
960-
*/
961-
static bool
962-
mkdatadir(const char *subdir)
963-
{
964-
char *path;
965-
966-
path = pg_malloc(strlen(pg_data) + 2 +
967-
(subdir == NULL ? 0 : strlen(subdir)));
968-
969-
if (subdir != NULL)
970-
sprintf(path, "%s/%s", pg_data, subdir);
971-
else
972-
strcpy(path, pg_data);
973-
974-
if (pg_mkdir_p(path, S_IRWXU) == 0)
975-
return true;
976-
977-
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
978-
progname, path, strerror(errno));
979-
980-
return false;
981-
}
982-
983-
984958
/*
985959
* set name of given input file variable under data directory
986960
*/
@@ -3220,8 +3194,12 @@ create_data_directory(void)
32203194
pg_data);
32213195
fflush(stdout);
32223196

3223-
if (!mkdatadir(NULL))
3197+
if (pg_mkdir_p(pg_data, S_IRWXU) != 0)
3198+
{
3199+
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
3200+
progname, pg_data, strerror(errno));
32243201
exit_nicely();
3202+
}
32253203
else
32263204
check_ok();
32273205

@@ -3403,10 +3381,25 @@ initialize_data_directory(void)
34033381
printf(_("creating subdirectories ... "));
34043382
fflush(stdout);
34053383

3406-
for (i = 0; i < (sizeof(subdirs) / sizeof(char *)); i++)
3384+
for (i = 0; i < lengthof(subdirs); i++)
34073385
{
3408-
if (!mkdatadir(subdirs[i]))
3386+
char *path;
3387+
3388+
path = pg_malloc(strlen(pg_data) + strlen(subdirs[i]) + 2);
3389+
sprintf(path, "%s/%s", pg_data, subdirs[i]);
3390+
3391+
/*
3392+
* The parent directory already exists, so we only need mkdir() not
3393+
* pg_mkdir_p() here, which avoids some failure modes; cf bug #13853.
3394+
*/
3395+
if (mkdir(path, S_IRWXU) < 0)
3396+
{
3397+
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
3398+
progname, path, strerror(errno));
34093399
exit_nicely();
3400+
}
3401+
3402+
free(path);
34103403
}
34113404

34123405
check_ok();

0 commit comments

Comments
 (0)