From 4348270bd811048581d901012930befdf888cbb2 Mon Sep 17 00:00:00 2001 From: Mahendra Singh Thalor Date: Tue, 8 Apr 2025 15:57:15 +0530 Subject: [PATCH] move duplicate code of file_exists_in_directory and _fileExistsInDirectory in dumputils.c file we have file_exists_in_directory function in pg_restore.c and same code we are using in _fileExistsInDirectory function in pg_backup_archiver.c also. Move these common function into dumputils.c file. --- src/bin/pg_dump/dumputils.c | 17 +++++++++++++++++ src/bin/pg_dump/dumputils.h | 1 + src/bin/pg_dump/pg_backup_archiver.c | 20 ++++---------------- src/bin/pg_dump/pg_restore.c | 19 +------------------ 4 files changed, 23 insertions(+), 34 deletions(-) diff --git a/src/bin/pg_dump/dumputils.c b/src/bin/pg_dump/dumputils.c index 1d8ffe363e7..19a02c9ff59 100644 --- a/src/bin/pg_dump/dumputils.c +++ b/src/bin/pg_dump/dumputils.c @@ -920,3 +920,20 @@ create_or_open_dir(const char *dirname) pg_fatal("directory \"%s\" is not empty", dirname); } } + +/* + * file_exists_in_directory + * + * Returns true if the file exists in the given directory. + */ +bool +file_exists_in_directory(const char *dir, const char *filename) +{ + struct stat st; + char buf[MAXPGPATH]; + + if (snprintf(buf, MAXPGPATH, "%s/%s", dir, filename) >= MAXPGPATH) + pg_fatal("directory name too long: \"%s\"", dir); + + return (stat(buf, &st) == 0 && S_ISREG(st.st_mode)); +} diff --git a/src/bin/pg_dump/dumputils.h b/src/bin/pg_dump/dumputils.h index 91c6e612e28..7da049403fd 100644 --- a/src/bin/pg_dump/dumputils.h +++ b/src/bin/pg_dump/dumputils.h @@ -63,5 +63,6 @@ extern void makeAlterConfigCommand(PGconn *conn, const char *configitem, const char *type2, const char *name2, PQExpBuffer buf); extern void create_or_open_dir(const char *dirname); +extern bool file_exists_in_directory(const char *dir, const char *filename); #endif /* DUMPUTILS_H */ diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index f961162f365..51ff060448a 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -2212,18 +2212,6 @@ ReadStr(ArchiveHandle *AH) return buf; } -static bool -_fileExistsInDirectory(const char *dir, const char *filename) -{ - struct stat st; - char buf[MAXPGPATH]; - - if (snprintf(buf, MAXPGPATH, "%s/%s", dir, filename) >= MAXPGPATH) - pg_fatal("directory name too long: \"%s\"", dir); - - return (stat(buf, &st) == 0 && S_ISREG(st.st_mode)); -} - static int _discoverArchiveFormat(ArchiveHandle *AH) { @@ -2255,18 +2243,18 @@ _discoverArchiveFormat(ArchiveHandle *AH) if (stat(AH->fSpec, &st) == 0 && S_ISDIR(st.st_mode)) { AH->format = archDirectory; - if (_fileExistsInDirectory(AH->fSpec, "toc.dat")) + if (file_exists_in_directory(AH->fSpec, "toc.dat")) return AH->format; #ifdef HAVE_LIBZ - if (_fileExistsInDirectory(AH->fSpec, "toc.dat.gz")) + if (file_exists_in_directory(AH->fSpec, "toc.dat.gz")) return AH->format; #endif #ifdef USE_LZ4 - if (_fileExistsInDirectory(AH->fSpec, "toc.dat.lz4")) + if (file_exists_in_directory(AH->fSpec, "toc.dat.lz4")) return AH->format; #endif #ifdef USE_ZSTD - if (_fileExistsInDirectory(AH->fSpec, "toc.dat.zst")) + if (file_exists_in_directory(AH->fSpec, "toc.dat.zst")) return AH->format; #endif pg_fatal("directory \"%s\" does not appear to be a valid archive (\"toc.dat\" does not exist)", diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 06c28ab3149..24e18d38fdb 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -48,6 +48,7 @@ #include "common/string.h" #include "connectdb.h" +#include "dumputils.h" #include "fe_utils/option_utils.h" #include "fe_utils/string_utils.h" #include "filter.h" @@ -57,7 +58,6 @@ static void usage(const char *progname); static void read_restore_filters(const char *filename, RestoreOptions *opts); -static bool file_exists_in_directory(const char *dir, const char *filename); static int restore_one_database(const char *inputFileSpec, RestoreOptions *opts, int numWorkers, bool append_data, int num); static int read_one_statement(StringInfo inBuf, FILE *pfile); @@ -823,23 +823,6 @@ read_restore_filters(const char *filename, RestoreOptions *opts) filter_free(&fstate); } -/* - * file_exists_in_directory - * - * Returns true if the file exists in the given directory. - */ -static bool -file_exists_in_directory(const char *dir, const char *filename) -{ - struct stat st; - char buf[MAXPGPATH]; - - if (snprintf(buf, MAXPGPATH, "%s/%s", dir, filename) >= MAXPGPATH) - pg_fatal("directory name too long: \"%s\"", dir); - - return (stat(buf, &st) == 0 && S_ISREG(st.st_mode)); -} - /* * read_one_statement * -- 2.39.3