diff options
author | Andrew Dunstan <andrew@dunslane.net> | 2025-04-10 12:11:36 -0400 |
---|---|---|
committer | Andrew Dunstan <andrew@dunslane.net> | 2025-04-10 12:11:36 -0400 |
commit | 4170298b6ecff7ce697b81e13d9a81e3b825798c (patch) | |
tree | 547611e21e494dbebefc70ff96b9becfe1598916 /src/bin/pg_dump/dumputils.c | |
parent | 4909b38af034fa4d2c67c5c71fd8509f870c1695 (diff) | |
download | postgresql-4170298b6ecff7ce697b81e13d9a81e3b825798c.tar.gz postgresql-4170298b6ecff7ce697b81e13d9a81e3b825798c.zip |
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 <mahi6run@gmail.com>
Discussion: https://postgr.es/m/CAKYtNApihL8X1h7XO-zOjznc8Ca66Aevgvhc9zOTh6DBh2iaeA@mail.gmail.com
Diffstat (limited to 'src/bin/pg_dump/dumputils.c')
-rw-r--r-- | src/bin/pg_dump/dumputils.c | 36 |
1 files changed, 36 insertions, 0 deletions
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 <ctype.h> +#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); + } +} |