|
21 | 21 |
|
22 | 22 | #include "catalog/pg_authid_d.h"
|
23 | 23 | #include "common/connect.h"
|
| 24 | +#include "common/file_perm.h" |
24 | 25 | #include "common/file_utils.h"
|
25 | 26 | #include "common/hashfn_unstable.h"
|
26 | 27 | #include "common/logging.h"
|
@@ -1954,49 +1955,30 @@ read_dumpall_filters(const char *filename, SimpleStringList *pattern)
|
1954 | 1955 | static void
|
1955 | 1956 | create_or_open_dir(const char *dirname)
|
1956 | 1957 | {
|
1957 |
| - struct stat st; |
1958 |
| - bool is_empty = false; |
| 1958 | + int ret; |
1959 | 1959 |
|
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))) |
1962 | 1961 | {
|
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", |
1986 | 1975 | dirname);
|
1987 |
| - } |
1988 | 1976 |
|
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); |
1997 | 1981 | }
|
1998 |
| - else if (mkdir(dirname, 0700) < 0) |
1999 |
| - pg_fatal("could not create directory \"%s\": %m", dirname); |
2000 | 1982 | }
|
2001 | 1983 |
|
2002 | 1984 | /*
|
|
0 commit comments