From: Andrew Dunstan Date: Sun, 6 Apr 2025 21:00:58 +0000 (-0400) Subject: Clean up checking for pg_dumpall output directory X-Git-Tag: REL_18_BETA1~233 X-Git-Url: http://git.postgresql.org/gitweb/?a=commitdiff_plain;h=643a1a61985bef25904965053020057200d4ae48;p=postgresql.git 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. --- diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index bbcac81a8fe..cdb8d84f064 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -21,6 +21,7 @@ #include "catalog/pg_authid_d.h" #include "common/connect.h" +#include "common/file_perm.h" #include "common/file_utils.h" #include "common/hashfn_unstable.h" #include "common/logging.h" @@ -1954,49 +1955,30 @@ read_dumpall_filters(const char *filename, SimpleStringList *pattern) static void create_or_open_dir(const char *dirname) { - struct stat st; - bool is_empty = false; + int ret; - /* we accept an empty existing directory */ - if (stat(dirname, &st) == 0 && S_ISDIR(st.st_mode)) + switch ((ret = pg_check_dir(dirname))) { - DIR *dir = opendir(dirname); - - if (dir) - { - struct dirent *d; - - is_empty = true; - - while (errno = 0, (d = readdir(dir))) - { - if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) - { - is_empty = false; - break; - } - } - - if (errno) - pg_fatal("could not read directory \"%s\": %m", - dirname); - - if (closedir(dir)) - pg_fatal("could not close directory \"%s\": %m", + case -1: + /* opendir failed but not with ENOENT */ + pg_fatal("could not open directory \"%s\": %m", dirname); + break; + case 0: + /* directory does not exist */ + if (mkdir(dirname, pg_dir_create_mode) < 0) + pg_fatal("could not create directory \"%s\": %m", dirname); + break; + case 1: + /* exists and is empty, fix perms */ + if (chmod(dirname, pg_dir_create_mode) != 0) + pg_fatal("could not change permissions of directory \"%s\": %m", 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); - } + break; + default: + /* exists and is not empty */ + pg_fatal("directory \"%s\" is not empty", dirname); } - else if (mkdir(dirname, 0700) < 0) - pg_fatal("could not create directory \"%s\": %m", dirname); } /*