8000 Use plain mkdir() not pg_mkdir_p() to create subdirectories of PGDATA. · ccneo/postgres@d6d6400 · GitHub
[go: up one dir, main page]

Skip to content

Commit d6d6400

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 5c4cbd5 commit d6d6400

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
@@ -186,7 +186,6 @@ static FILE *popen_check(const char *command, const char *mode);
186186
static void exit_nicely(void);
187187
static char *get_id(void);
188188
static char *get_encoding_id(char *encoding_name);
189-
static bool mkdatadir(const char *subdir);
190189
static void set_input(char **dest, char *filename);
191190
static void check_input(char *path);
192191
static void write_version_file(char *extrapath);
@@ -746,32 +745,6 @@ find_matching_ts_config(const char *lc_type)
746745
}
747746

748747

749-
/*
750-
* make the data directory (or one of its subdirectories if subdir is not NULL)
751-
*/
752-
static bool
753-
mkdatadir(const char *subdir)
754-
{
755-
char *path;
756-
757-
path = pg_malloc(strlen(pg_data) + 2 +
758-
(subdir == NULL ? 0 : strlen(subdir)));
759-
760-
if (subdir != NULL)
761-
sprintf(path, "%s/%s", pg_data, subdir);
762-
else
763-
strcpy(path, pg_data);
764-
765-
if (pg_mkdir_p(path, S_IRWXU) == 0)
766-
return true;
767-
768-
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
769-
progname, path, strerror(errno));
770-
771-
return false;
772-
}
773-
774-
775748
/*
776749
* set name of given input file variable under data directory
777750
*/
@@ -2666,6 +2639,7 @@ main(int argc, char *argv[])
26662639
"pg_snapshots",
26672640
"pg_subtrans",
26682641
"pg_twophase",
2642+
"pg_multixact",
26692643
"pg_multixact/members",
26702644
"pg_multixact/offsets",
26712645
"base",
@@ -3144,8 +3118,12 @@ main(int argc, char *argv[])
31443118
pg_data);
31453119
fflush(stdout);
31463120

3147-
if (!mkdatadir(NULL))
3121+
if (pg_mkdir_p(pg_data, S_IRWXU) != 0)
3122+
{
3123+
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
3124+
progname, pg_data, strerror(errno));
31483125
exit_nicely();
3126+
}
31493127
else
31503128
check_ok();
31513129

@@ -3280,10 +3258,25 @@ main(int argc, char *argv[])
32803258
printf(_("creating subdirectories ... "));
32813259
fflush(stdout);
32823260

3283-
for (i = 0; i < (sizeof(subdirs) / sizeof(char *)); i++)
3261+
for (i = 0; i < lengthof(subdirs); i++)
32843262
{
3285-
if (!mkdatadir(subdirs[i]))
3263+
char *path;
3264+
3265+
path = pg_malloc(strlen(pg_data) + strlen(subdirs[i]) + 2);
3266+
sprintf(path, "%s/%s", pg_data, subdirs[i]);
3267+
3268+
/*
3269+
* The parent directory already exists, so we only need mkdir() not
3270+
* pg_mkdir_p() here, which avoids some failure modes; cf bug #13853.
3271+
*/
3272+
if (mkdir(path, S_IRWXU) < 0)
3273+
{
3274+
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
3275+
progname, path, strerror(errno));
32863276
exit_nicely();
3277+
}
3278+
3279+
free(path);
32873280
}
32883281

32893282
check_ok();

0 commit comments

Comments
 (0)
0