Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * rmtree.c
4 : : *
5 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
6 : : * Portions Copyright (c) 1994, Regents of the University of California
7 : : *
8 : : * IDENTIFICATION
9 : : * src/common/rmtree.c
10 : : *
11 : : *-------------------------------------------------------------------------
12 : : */
13 : :
14 : : #ifndef FRONTEND
15 : : #include "postgres.h"
16 : : #else
17 : : #include "postgres_fe.h"
18 : : #endif
19 : :
20 : : #include <unistd.h>
21 : : #include <sys/stat.h>
22 : :
23 : : #include "common/file_utils.h"
24 : :
25 : : #ifndef FRONTEND
26 : : #include "storage/fd.h"
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
32 : : #include "common/logging.h"
33 : : #define LOG_LEVEL PG_LOG_WARNING
34 : : #define OPENDIR(x) opendir(x)
35 : : #define CLOSEDIR(x) closedir(x)
36 : : #endif
37 : :
38 : : /*
39 : : * rmtree
40 : : *
41 : : * Delete a directory tree recursively.
42 : : * Assumes path points to a valid directory.
43 : : * Deletes everything under path.
44 : : * If rmtopdir is true deletes the directory too.
45 : : * Returns true if successful, false if there was any problem.
46 : : * (The details of the problem are reported already, so caller
47 : : * doesn't really have to say anything more, but most do.)
48 : : */
49 : : bool
4695 peter_e@gmx.net 50 :CBC 3963 : rmtree(const char *path, bool rmtopdir)
51 : : {
52 : : char pathbuf[MAXPGPATH];
53 : : DIR *dir;
54 : : struct dirent *de;
1304 tmunro@postgresql.or 55 : 3963 : bool result = true;
56 : 3963 : size_t dirnames_size = 0;
57 : 3963 : size_t dirnames_capacity = 8;
58 : : char **dirnames;
59 : :
60 : 3963 : dir = OPENDIR(path);
61 [ + + ]: 3963 : if (dir == NULL)
62 : : {
63 [ + - ]: 1 : pg_log_warning("could not open directory \"%s\": %m", path);
4695 peter_e@gmx.net 64 : 1 : return false;
65 : : }
66 : :
261 michael@paquier.xyz 67 : 3962 : dirnames = palloc_array(char *, dirnames_capacity);
68 : :
1304 tmunro@postgresql.or 69 [ + + ]: 187857 : while (errno = 0, (de = readdir(dir)))
70 : : {
71 [ + + ]: 183895 : if (strcmp(de->d_name, ".") == 0 ||
72 [ + + ]: 179933 : strcmp(de->d_name, "..") == 0)
4695 peter_e@gmx.net 73 : 7924 : continue;
1304 tmunro@postgresql.or 74 : 175971 : snprintf(pathbuf, sizeof(pathbuf), "%s/%s", path, de->d_name);
75 [ - + + ]: 175971 : switch (get_dirent_type(pathbuf, de, false, LOG_LEVEL))
76 : : {
1304 tmunro@postgresql.or 77 :UBC 0 : case PGFILETYPE_ERROR:
78 : : /* already logged, press on */
79 : 0 : break;
1304 tmunro@postgresql.or 80 :CBC 3040 : case PGFILETYPE_DIR:
81 : :
82 : : /*
83 : : * Defer recursion until after we've closed this directory, to
84 : : * avoid using more than one file descriptor at a time.
85 : : */
86 [ + + ]: 3040 : if (dirnames_size == dirnames_capacity)
87 : : {
10 michael@paquier.xyz 88 :GNC 214 : dirnames = repalloc_array(dirnames, char *, dirnames_capacity * 2);
1304 tmunro@postgresql.or 89 :CBC 214 : dirnames_capacity *= 2;
90 : : }
91 : 3040 : dirnames[dirnames_size++] = pstrdup(pathbuf);
92 : 3040 : break;
93 : 172931 : default:
94 [ - + - - ]: 172931 : if (unlink(pathbuf) != 0 && errno != ENOENT)
95 : : {
1196 peter@eisentraut.org 96 [ # # ]:UBC 0 : pg_log_warning("could not remove file \"%s\": %m", pathbuf);
4695 peter_e@gmx.net 97 : 0 : result = false;
98 : : }
1304 tmunro@postgresql.or 99 :CBC 172931 : break;
100 : : }
101 : : }
102 : :
103 [ - + ]: 3962 : if (errno != 0)
104 : : {
1304 tmunro@postgresql.or 105 [ # # ]:UBC 0 : pg_log_warning("could not read directory \"%s\": %m", path);
106 : 0 : result = false;
107 : : }
108 : :
1304 tmunro@postgresql.or 109 :CBC 3962 : CLOSEDIR(dir);
110 : :
111 : : /* Now recurse into the subdirectories we found. */
112 [ + + ]: 7002 : for (size_t i = 0; i < dirnames_size; ++i)
113 : : {
114 [ - + ]: 3040 : if (!rmtree(dirnames[i], true))
1304 tmunro@postgresql.or 115 :UBC 0 : result = false;
1304 tmunro@postgresql.or 116 :CBC 3040 : pfree(dirnames[i]);
117 : : }
118 : :
4695 peter_e@gmx.net 119 [ + - ]: 3962 : if (rmtopdir)
120 : : {
121 [ - + ]: 3962 : if (rmdir(path) != 0)
122 : : {
1304 tmunro@postgresql.or 123 [ # # ]:UBC 0 : pg_log_warning("could not remove directory \"%s\": %m", path);
4695 peter_e@gmx.net 124 : 0 : result = false;
125 : : }
126 : : }
127 : :
1304 tmunro@postgresql.or 128 :CBC 3962 : pfree(dirnames);
129 : :
4695 peter_e@gmx.net 130 : 3962 : return result;
131 : : }
|