Skip to content

Commit 4170298

Browse files
committed
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
1 parent 4909b38 commit 4170298

File tree

4 files changed

+40
-72
lines changed

4 files changed

+40
-72
lines changed

src/bin/pg_dump/dumputils.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include <ctype.h>
1818

19+
#include "common/file_perm.h"
20+
#include "common/logging.h"
1921
#include "dumputils.h"
2022
#include "fe_utils/string_utils.h"
2123

@@ -884,3 +886,37 @@ makeAlterConfigCommand(PGconn *conn, const char *configitem,
884886

885887
pg_free(mine);
886888
}
889+
890+
/*
891+
* create_or_open_dir
892+
*
893+
* This will create a new directory with the given dirname. If there is
894+
* already an empty directory with that name, then use it.
895+
*/
896+
void
897+
create_or_open_dir(const char *dirname)
898+
{
899+
int ret;
900+
901+
switch ((ret = pg_check_dir(dirname)))
902+
{
903+
case -1:
904+
/* opendir failed but not with ENOENT */
905+
pg_fatal("could not open directory \"%s\": %m", dirname);
906+
break;
907+
case 0:
908+
/* directory does not exist */
909+
if (mkdir(dirname, pg_dir_create_mode) < 0)
910+
pg_fatal("could not create directory \"%s\": %m", dirname);
911+
break;
912+
case 1:
913+
/* exists and is empty, fix perms */
914+
if (chmod(dirname, pg_dir_create_mode) != 0)
915+
pg_fatal("could not change permissions of directory \"%s\": %m",
916+
dirname);
917+
break;
918+
default:
919+
/* exists and is not empty */
920+
pg_fatal("directory \"%s\" is not empty", dirname);
921+
}
922+
}

src/bin/pg_dump/dumputils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ extern void makeAlterConfigCommand(PGconn *conn, const char *configitem,
6262
const char *type, const char *name,
6363
const char *type2, const char *name2,
6464
PQExpBuffer buf);
65+
extern void create_or_open_dir(const char *dirname);
6566

6667
#endif /* DUMPUTILS_H */

src/bin/pg_dump/pg_backup_directory.c

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
#include "common/file_utils.h"
4343
#include "compress_io.h"
44+
#include "dumputils.h"
4445
#include "parallel.h"
4546
#include "pg_backup_utils.h"
4647

@@ -156,41 +157,8 @@ InitArchiveFmt_Directory(ArchiveHandle *AH)
156157

157158
if (AH->mode == archModeWrite)
158159
{
159-
struct stat st;
160-
bool is_empty = false;
161-
162160
/* we accept an empty existing directory */
163-
if (stat(ctx->directory, &st) == 0 && S_ISDIR(st.st_mode))
164-
{
165-
DIR *dir = opendir(ctx->directory);
166-
167-
if (dir)
168-
{
169-
struct dirent *d;
170-
171-
is_empty = true;
172-
while (errno = 0, (d = readdir(dir)))
173-
{
174-
if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0)
175-
{
176-
is_empty = false;
177-
break;
178-
}
179-
}
180-
181-
if (errno)
182-
pg_fatal("could not read directory \"%s\": %m",
183-
ctx->directory);
184-
185-
if (closedir(dir))
186-
pg_fatal("could not close directory \"%s\": %m",
187-
ctx->directory);
188-
}
189-
}
190-
191-
if (!is_empty && mkdir(ctx->directory, 0700) < 0)
192-
pg_fatal("could not create directory \"%s\": %m",
193-
ctx->directory);
161+
create_or_open_dir(ctx->directory);
194162
}
195163
else
196164
{ /* Read Mode */

src/bin/pg_dump/pg_dumpall.c

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
#include "postgres_fe.h"
1717

18-
#include <sys/stat.h>
1918
#include <time.h>
2019
#include <unistd.h>
2120

@@ -78,7 +77,6 @@ static void executeCommand(PGconn *conn, const char *query);
7877
static void expand_dbname_patterns(PGconn *conn, SimpleStringList *patterns,
7978
SimpleStringList *names);
8079
static void read_dumpall_filters(const char *filename, SimpleStringList *pattern);
81-
static void create_or_open_dir(const char *dirname);
8280
static ArchiveFormat parseDumpFormat(const char *format);
8381

8482
static char pg_dump_bin[MAXPGPATH];
@@ -1655,7 +1653,7 @@ dumpDatabases(PGconn *conn, ArchiveFormat archDumpFormat)
16551653
snprintf(db_subdir, MAXPGPATH, "%s/databases", filename);
16561654

16571655
/* Create a subdirectory with 'databases' name under main directory. */
1658-
if (mkdir(db_subdir, 0755) != 0)
1656+
if (mkdir(db_subdir, pg_dir_create_mode) != 0)
16591657
pg_fatal("could not create subdirectory \"%s\": %m", db_subdir);
16601658

16611659
snprintf(map_file_path, MAXPGPATH, "%s/map.dat", filename);
@@ -1946,41 +1944,6 @@ read_dumpall_filters(const char *filename, SimpleStringList *pattern)
19461944
filter_free(&fstate);
19471945
}
19481946

1949-
/*
1950-
* create_or_open_dir
1951-
*
1952-
* This will create a new directory with the given dirname. If there is
1953-
* already an empty directory with that name, then use it.
1954-
*/
1955-
static void
1956-
create_or_open_dir(const char *dirname)
1957-
{
1958-
int ret;
1959-
1960-
switch ((ret = pg_check_dir(dirname)))
1961-
{
1962-
case -1:
1963-
/* opendir failed but not with ENOENT */
1964-
pg_fatal("could not open directory \"%s\": %m", dirname);
1965-
break;
1966-
case 0:
1967-
/* directory does not exist */
1968-
if (mkdir(dirname, pg_dir_create_mode) < 0)
1969-
pg_fatal("could not create directory \"%s\": %m", dirname);
1970-
break;
1971-
case 1:
1972-
/* exists and is empty, fix perms */
1973-
if (chmod(dirname, pg_dir_create_mode) != 0)
1974-
pg_fatal("could not change permissions of directory \"%s\": %m",
1975-
dirname);
1976-
1977-
break;
1978-
default:
1979-
/* exists and is not empty */
1980-
pg_fatal("directory \"%s\" is not empty", dirname);
1981-
}
1982-
}
1983-
19841947
/*
19851948
* parseDumpFormat
19861949
*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy