PostgreSQL Source Code: src/common/rmtree.c Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14#ifndef FRONTEND
16#else
18#endif
19
21#include <sys/stat.h>
22
24
25#ifndef FRONTEND
27#define pg_log_warning(...) elog(WARNING, __VA_ARGS__)
28#define LOG_LEVEL WARNING
29#define OPENDIR(x) AllocateDir(x)
30#define CLOSEDIR(x) FreeDir(x)
31#else
33#define LOG_LEVEL PG_LOG_WARNING
34#define OPENDIR(x) opendir(x)
35#define CLOSEDIR(x) closedir(x)
36#endif
37
38
39
40
41
42
43
44
45
46
47
48
49bool
50rmtree(const char *path, bool rmtopdir)
51{
55 bool result = true;
56 size_t dirnames_size = 0;
57 size_t dirnames_capacity = 8;
58 char **dirnames;
59
61 if (dir == NULL)
62 {
63 pg_log_warning("could not open directory \"%s\": %m", path);
64 return false;
65 }
66
67 dirnames = (char **) palloc(sizeof(char *) * dirnames_capacity);
68
69 while (errno = 0, (de = readdir(dir)))
70 {
71 if (strcmp(de->d_name, ".") == 0 ||
72 strcmp(de->d_name, "..") == 0)
73 continue;
74 snprintf(pathbuf, sizeof(pathbuf), "%s/%s", path, de->d_name);
76 {
78
79 break;
81
82
83
84
85
86 if (dirnames_size == dirnames_capacity)
87 {
89 sizeof(char *) * dirnames_capacity * 2);
90 dirnames_capacity *= 2;
91 }
92 dirnames[dirnames_size++] = pstrdup(pathbuf);
93 break;
94 default:
95 if (unlink(pathbuf) != 0 && errno != ENOENT)
96 {
97 pg_log_warning("could not remove file \"%s\": %m", pathbuf);
98 result = false;
99 }
100 break;
101 }
102 }
103
104 if (errno != 0)
105 {
106 pg_log_warning("could not read directory \"%s\": %m", path);
107 result = false;
108 }
109
111
112
113 for (size_t i = 0; i < dirnames_size; ++i)
114 {
115 if ((dirnames[i], true))
116 result = false;
118 }
119
120 if (rmtopdir)
121 {
122 if (rmdir(path) != 0)
123 {
124 pg_log_warning("could not remove directory \"%s\": %m", path);
125 result = false;
126 }
127 }
128
130
131 return result;
132}
struct dirent * readdir(DIR *)
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
char * pstrdup(const char *in)
void * repalloc(void *pointer, Size size)
void pfree(void *pointer)
bool rmtree(const char *path, bool rmtopdir)
#define pg_log_warning(...)