From 4170298b6ecff7ce697b81e13d9a81e3b825798c Mon Sep 17 00:00:00 2001 From: Andrew Dunstan Date: Thu, 10 Apr 2025 12:11:36 -0400 Subject: Further cleanup for directory creation on pg_dump/pg_dumpall Instead of two separate (and different) implementations, refactor to use a single common routine. Along the way, remove use of a hardcoded file permissions constant in favor of the common project setting for directory creation. Author: Mahendra Singh Thalor Discussion: https://postgr.es/m/CAKYtNApihL8X1h7XO-zOjznc8Ca66Aevgvhc9zOTh6DBh2iaeA@mail.gmail.com --- src/bin/pg_dump/dumputils.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/bin/pg_dump/dumputils.c') diff --git a/src/bin/pg_dump/dumputils.c b/src/bin/pg_dump/dumputils.c index ab0e9e6da3c..73ce34346b2 100644 --- a/src/bin/pg_dump/dumputils.c +++ b/src/bin/pg_dump/dumputils.c @@ -16,6 +16,8 @@ #include +#include "common/file_perm.h" +#include "common/logging.h" #include "dumputils.h" #include "fe_utils/string_utils.h" @@ -884,3 +886,37 @@ makeAlterConfigCommand(PGconn *conn, const char *configitem, pg_free(mine); } + +/* + * create_or_open_dir + * + * This will create a new directory with the given dirname. If there is + * already an empty directory with that name, then use it. + */ +void +create_or_open_dir(const char *dirname) +{ + int ret; + + switch ((ret = pg_check_dir(dirname))) + { + 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); + break; + default: + /* exists and is not empty */ + pg_fatal("directory \"%s\" is not empty", dirname); + } +} -- cgit v1.2.3