chiark / gitweb /
Revert some uses of xsprintf
[elogind.git] / src / basic / rm-rf.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2015 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <dirent.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdbool.h>
24 #include <stddef.h>
25 #include <sys/stat.h>
26 #include <sys/statfs.h>
27 #include <unistd.h>
28
29 //#include "btrfs-util.h"
30 #include "fd-util.h"
31 #include "log.h"
32 #include "macro.h"
33 #include "mount-util.h"
34 #include "path-util.h"
35 #include "rm-rf.h"
36 #include "stat-util.h"
37 #include "string-util.h"
38
39 static bool is_physical_fs(const struct statfs *sfs) {
40         return !is_temporary_fs(sfs) && !is_cgroup_fs(sfs);
41 }
42
43 int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
44         _cleanup_closedir_ DIR *d = NULL;
45         int ret = 0, r;
46         struct statfs sfs;
47
48         assert(fd >= 0);
49
50         /* This returns the first error we run into, but nevertheless
51          * tries to go on. This closes the passed fd. */
52
53         if (!(flags & REMOVE_PHYSICAL)) {
54
55                 r = fstatfs(fd, &sfs);
56                 if (r < 0) {
57                         safe_close(fd);
58                         return -errno;
59                 }
60
61                 if (is_physical_fs(&sfs)) {
62                         /* We refuse to clean physical file systems
63                          * with this call, unless explicitly
64                          * requested. This is extra paranoia just to
65                          * be sure we never ever remove non-state
66                          * data */
67
68                         log_error("Attempted to remove disk file system, and we can't allow that.");
69                         safe_close(fd);
70                         return -EPERM;
71                 }
72         }
73
74         d = fdopendir(fd);
75         if (!d) {
76                 safe_close(fd);
77                 return errno == ENOENT ? 0 : -errno;
78         }
79
80         for (;;) {
81                 struct dirent *de;
82                 bool is_dir;
83                 struct stat st;
84
85                 errno = 0;
86                 de = readdir(d);
87                 if (!de) {
88                         if (errno > 0 && ret == 0)
89                                 ret = -errno;
90                         return ret;
91                 }
92
93                 if (streq(de->d_name, ".") || streq(de->d_name, ".."))
94                         continue;
95
96                 if (de->d_type == DT_UNKNOWN ||
97                     (de->d_type == DT_DIR && (root_dev || (flags & REMOVE_SUBVOLUME)))) {
98                         if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
99                                 if (ret == 0 && errno != ENOENT)
100                                         ret = -errno;
101                                 continue;
102                         }
103
104                         is_dir = S_ISDIR(st.st_mode);
105                 } else
106                         is_dir = de->d_type == DT_DIR;
107
108                 if (is_dir) {
109                         int subdir_fd;
110
111                         /* if root_dev is set, remove subdirectories only if device is same */
112                         if (root_dev && st.st_dev != root_dev->st_dev)
113                                 continue;
114
115                         subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
116                         if (subdir_fd < 0) {
117                                 if (ret == 0 && errno != ENOENT)
118                                         ret = -errno;
119                                 continue;
120                         }
121
122                         /* Stop at mount points */
123                         r = fd_is_mount_point(fd, de->d_name, 0);
124                         if (r < 0) {
125                                 if (ret == 0 && r != -ENOENT)
126                                         ret = r;
127
128                                 safe_close(subdir_fd);
129                                 continue;
130                         }
131                         if (r) {
132                                 safe_close(subdir_fd);
133                                 continue;
134                         }
135
136 #if 0 /// elogind does not support BTRFS this directly
137                         if ((flags & REMOVE_SUBVOLUME) && st.st_ino == 256) {
138
139                                 /* This could be a subvolume, try to remove it */
140
141                                 r = btrfs_subvol_remove_fd(fd, de->d_name, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
142                                 if (r < 0) {
143                                         if (r != -ENOTTY && r != -EINVAL) {
144                                                 if (ret == 0)
145                                                         ret = r;
146
147                                                 safe_close(subdir_fd);
148                                                 continue;
149                                         }
150
151                                         /* ENOTTY, then it wasn't a
152                                          * btrfs subvolume, continue
153                                          * below. */
154                                 } else {
155                                         /* It was a subvolume, continue. */
156                                         safe_close(subdir_fd);
157                                         continue;
158                                 }
159                         }
160 #endif // 0
161
162                         /* We pass REMOVE_PHYSICAL here, to avoid
163                          * doing the fstatfs() to check the file
164                          * system type again for each directory */
165                         r = rm_rf_children(subdir_fd, flags | REMOVE_PHYSICAL, root_dev);
166                         if (r < 0 && ret == 0)
167                                 ret = r;
168
169                         if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
170                                 if (ret == 0 && errno != ENOENT)
171                                         ret = -errno;
172                         }
173
174                 } else if (!(flags & REMOVE_ONLY_DIRECTORIES)) {
175
176                         if (unlinkat(fd, de->d_name, 0) < 0) {
177                                 if (ret == 0 && errno != ENOENT)
178                                         ret = -errno;
179                         }
180                 }
181         }
182 }
183
184 int rm_rf(const char *path, RemoveFlags flags) {
185         int fd, r;
186         struct statfs s;
187
188         assert(path);
189
190         /* We refuse to clean the root file system with this
191          * call. This is extra paranoia to never cause a really
192          * seriously broken system. */
193         if (path_equal(path, "/")) {
194                 log_error("Attempted to remove entire root file system, and we can't allow that.");
195                 return -EPERM;
196         }
197
198 #if 0 /// elogind does not support BTRFS this directly
199         if ((flags & (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) == (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) {
200                 /* Try to remove as subvolume first */
201                 r = btrfs_subvol_remove(path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
202                 if (r >= 0)
203                         return r;
204
205                 if (r != -ENOTTY && r != -EINVAL && r != -ENOTDIR)
206                         return r;
207
208                 /* Not btrfs or not a subvolume */
209         }
210 #endif // 0
211
212         fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
213         if (fd < 0) {
214
215                 if (errno != ENOTDIR && errno != ELOOP)
216                         return -errno;
217
218                 if (!(flags & REMOVE_PHYSICAL)) {
219                         if (statfs(path, &s) < 0)
220                                 return -errno;
221
222                         if (is_physical_fs(&s)) {
223                                 log_error("Attempted to remove disk file system, and we can't allow that.");
224                                 return -EPERM;
225                         }
226                 }
227
228                 if ((flags & REMOVE_ROOT) && !(flags & REMOVE_ONLY_DIRECTORIES))
229                         if (unlink(path) < 0 && errno != ENOENT)
230                                 return -errno;
231
232                 return 0;
233         }
234
235         r = rm_rf_children(fd, flags, NULL);
236
237         if (flags & REMOVE_ROOT) {
238                 if (rmdir(path) < 0) {
239                         if (r == 0 && errno != ENOENT)
240                                 r = -errno;
241                 }
242         }
243
244         return r;
245 }