1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Clean up checking for pg_dumpall output directory

Coverity objected to the original code, and in any case this is much
cleaner, using the existing routine pg_check_dir() instead of rolling
its own test.

Per suggestion from Tom Lane.
This commit is contained in:
Andrew Dunstan
2025-04-06 17:00:58 -04:00
parent 218ab68275
commit 643a1a6198

View File

@ -21,6 +21,7 @@
#include "catalog/pg_authid_d.h" #include "catalog/pg_authid_d.h"
#include "common/connect.h" #include "common/connect.h"
#include "common/file_perm.h"
#include "common/file_utils.h" #include "common/file_utils.h"
#include "common/hashfn_unstable.h" #include "common/hashfn_unstable.h"
#include "common/logging.h" #include "common/logging.h"
@ -1954,49 +1955,30 @@ read_dumpall_filters(const char *filename, SimpleStringList *pattern)
static void static void
create_or_open_dir(const char *dirname) create_or_open_dir(const char *dirname)
{ {
struct stat st; int ret;
bool is_empty = false;
/* we accept an empty existing directory */ switch ((ret = pg_check_dir(dirname)))
if (stat(dirname, &st) == 0 && S_ISDIR(st.st_mode))
{ {
DIR *dir = opendir(dirname); case -1:
/* opendir failed but not with ENOENT */
if (dir) pg_fatal("could not open directory \"%s\": %m", dirname);
{ break;
struct dirent *d; case 0:
/* directory does not exist */
is_empty = true; if (mkdir(dirname, pg_dir_create_mode) < 0)
pg_fatal("could not create directory \"%s\": %m", dirname);
while (errno = 0, (d = readdir(dir))) break;
{ case 1:
if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) /* exists and is empty, fix perms */
{ if (chmod(dirname, pg_dir_create_mode) != 0)
is_empty = false; pg_fatal("could not change permissions of directory \"%s\": %m",
break;
}
}
if (errno)
pg_fatal("could not read directory \"%s\": %m",
dirname); dirname);
if (closedir(dir)) break;
pg_fatal("could not close directory \"%s\": %m", default:
dirname); /* exists and is not empty */
} pg_fatal("directory \"%s\" is not empty", dirname);
if (!is_empty)
{
pg_log_error("directory \"%s\" exists but is not empty", dirname);
pg_log_error_hint("Either remove the directory "
"\"%s\" or its contents.",
dirname);
exit_nicely(1);
}
} }
else if (mkdir(dirname, 0700) < 0)
pg_fatal("could not create directory \"%s\": %m", dirname);
} }
/* /*