Skip to content

Commit 643a1a6

Browse files
committed
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.
1 parent 218ab68 commit 643a1a6

File tree

1 file changed

+20
-38
lines changed

1 file changed

+20
-38
lines changed

src/bin/pg_dump/pg_dumpall.c

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "catalog/pg_authid_d.h"
2323
#include "common/connect.h"
24+
#include "common/file_perm.h"
2425
#include "common/file_utils.h"
2526
#include "common/hashfn_unstable.h"
2627
#include "common/logging.h"
@@ -1954,49 +1955,30 @@ read_dumpall_filters(const char *filename, SimpleStringList *pattern)
19541955
static void
19551956
create_or_open_dir(const char *dirname)
19561957
{
1957-
struct stat st;
1958-
bool is_empty = false;
1958+
int ret;
19591959

1960-
/* we accept an empty existing directory */
1961-
if (stat(dirname, &st) == 0 && S_ISDIR(st.st_mode))
1960+
switch ((ret = pg_check_dir(dirname)))
19621961
{
1963-
DIR *dir = opendir(dirname);
1964-
1965-
if (dir)
1966-
{
1967-
struct dirent *d;
1968-
1969-
is_empty = true;
1970-
1971-
while (errno = 0, (d = readdir(dir)))
1972-
{
1973-
if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0)
1974-
{
1975-
is_empty = false;
1976-
break;
1977-
}
1978-
}
1979-
1980-
if (errno)
1981-
pg_fatal("could not read directory \"%s\": %m",
1982-
dirname);
1983-
1984-
if (closedir(dir))
1985-
pg_fatal("could not close directory \"%s\": %m",
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",
19861975
dirname);
1987-
}
19881976

1989-
if (!is_empty)
1990-
{
1991-
pg_log_error("directory \"%s\" exists but is not empty", dirname);
1992-
pg_log_error_hint("Either remove the directory "
1993-
"\"%s\" or its contents.",
1994-
dirname);
1995-
exit_nicely(1);
1996-
}
1977+
break;
1978+
default:
1979+
/* exists and is not empty */
1980+
pg_fatal("directory \"%s\" is not empty", dirname);
19971981
}
1998-
else if (mkdir(dirname, 0700) < 0)
1999-
pg_fatal("could not create directory \"%s\": %m", dirname);
20001982
}
20011983

20021984
/*

0 commit comments

Comments
 (0)