]>
Commit | Line | Data |
---|---|---|
c6878637 LP |
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 | ||
11c3a366 TA |
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 | ||
d9e2daaf | 29 | #include "btrfs-util.h" |
3ffd4af2 | 30 | #include "fd-util.h" |
93cc7779 TA |
31 | #include "log.h" |
32 | #include "macro.h" | |
4349cd7c | 33 | #include "mount-util.h" |
07630cea | 34 | #include "path-util.h" |
3ffd4af2 | 35 | #include "rm-rf.h" |
8fcde012 | 36 | #include "stat-util.h" |
07630cea | 37 | #include "string-util.h" |
c6878637 LP |
38 | |
39 | int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) { | |
40 | _cleanup_closedir_ DIR *d = NULL; | |
41 | int ret = 0, r; | |
42 | ||
43 | assert(fd >= 0); | |
44 | ||
45 | /* This returns the first error we run into, but nevertheless | |
46 | * tries to go on. This closes the passed fd. */ | |
47 | ||
48 | if (!(flags & REMOVE_PHYSICAL)) { | |
49 | ||
50 | r = fd_is_temporary_fs(fd); | |
51 | if (r < 0) { | |
52 | safe_close(fd); | |
53 | return r; | |
54 | } | |
55 | ||
56 | if (!r) { | |
57 | /* We refuse to clean physical file systems | |
58 | * with this call, unless explicitly | |
59 | * requested. This is extra paranoia just to | |
60 | * be sure we never ever remove non-state | |
61 | * data */ | |
62 | ||
63 | log_error("Attempted to remove disk file system, and we can't allow that."); | |
64 | safe_close(fd); | |
65 | return -EPERM; | |
66 | } | |
67 | } | |
68 | ||
69 | d = fdopendir(fd); | |
70 | if (!d) { | |
71 | safe_close(fd); | |
72 | return errno == ENOENT ? 0 : -errno; | |
73 | } | |
74 | ||
75 | for (;;) { | |
76 | struct dirent *de; | |
77 | bool is_dir; | |
78 | struct stat st; | |
79 | ||
80 | errno = 0; | |
81 | de = readdir(d); | |
82 | if (!de) { | |
b3267152 | 83 | if (errno > 0 && ret == 0) |
c6878637 LP |
84 | ret = -errno; |
85 | return ret; | |
86 | } | |
87 | ||
88 | if (streq(de->d_name, ".") || streq(de->d_name, "..")) | |
89 | continue; | |
90 | ||
9e9b663a LP |
91 | if (de->d_type == DT_UNKNOWN || |
92 | (de->d_type == DT_DIR && (root_dev || (flags & REMOVE_SUBVOLUME)))) { | |
c6878637 LP |
93 | if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { |
94 | if (ret == 0 && errno != ENOENT) | |
95 | ret = -errno; | |
96 | continue; | |
97 | } | |
98 | ||
99 | is_dir = S_ISDIR(st.st_mode); | |
100 | } else | |
101 | is_dir = de->d_type == DT_DIR; | |
102 | ||
103 | if (is_dir) { | |
104 | int subdir_fd; | |
105 | ||
f25afeb6 | 106 | /* if root_dev is set, remove subdirectories only if device is same */ |
c6878637 LP |
107 | if (root_dev && st.st_dev != root_dev->st_dev) |
108 | continue; | |
109 | ||
110 | subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME); | |
111 | if (subdir_fd < 0) { | |
112 | if (ret == 0 && errno != ENOENT) | |
113 | ret = -errno; | |
114 | continue; | |
115 | } | |
116 | ||
f25afeb6 | 117 | /* Stop at mount points */ |
5d409034 | 118 | r = fd_is_mount_point(fd, de->d_name, 0); |
f25afeb6 LP |
119 | if (r < 0) { |
120 | if (ret == 0 && r != -ENOENT) | |
121 | ret = r; | |
122 | ||
123 | safe_close(subdir_fd); | |
124 | continue; | |
125 | } | |
126 | if (r) { | |
127 | safe_close(subdir_fd); | |
128 | continue; | |
129 | } | |
130 | ||
9e9b663a | 131 | if ((flags & REMOVE_SUBVOLUME) && st.st_ino == 256) { |
9e9b663a LP |
132 | |
133 | /* This could be a subvolume, try to remove it */ | |
134 | ||
5bcd08db | 135 | r = btrfs_subvol_remove_fd(fd, de->d_name, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA); |
d9e2daaf LP |
136 | if (r < 0) { |
137 | if (r != -ENOTTY && r != -EINVAL) { | |
9e9b663a | 138 | if (ret == 0) |
d9e2daaf | 139 | ret = r; |
9e9b663a LP |
140 | |
141 | safe_close(subdir_fd); | |
142 | continue; | |
143 | } | |
144 | ||
d9e2daaf LP |
145 | /* ENOTTY, then it wasn't a |
146 | * btrfs subvolume, continue | |
147 | * below. */ | |
9e9b663a LP |
148 | } else { |
149 | /* It was a subvolume, continue. */ | |
150 | safe_close(subdir_fd); | |
151 | continue; | |
152 | } | |
153 | } | |
154 | ||
c6878637 LP |
155 | /* We pass REMOVE_PHYSICAL here, to avoid |
156 | * doing the fstatfs() to check the file | |
157 | * system type again for each directory */ | |
158 | r = rm_rf_children(subdir_fd, flags | REMOVE_PHYSICAL, root_dev); | |
159 | if (r < 0 && ret == 0) | |
160 | ret = r; | |
161 | ||
162 | if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) { | |
163 | if (ret == 0 && errno != ENOENT) | |
164 | ret = -errno; | |
165 | } | |
166 | ||
167 | } else if (!(flags & REMOVE_ONLY_DIRECTORIES)) { | |
168 | ||
169 | if (unlinkat(fd, de->d_name, 0) < 0) { | |
170 | if (ret == 0 && errno != ENOENT) | |
171 | ret = -errno; | |
172 | } | |
173 | } | |
174 | } | |
175 | } | |
176 | ||
177 | int rm_rf(const char *path, RemoveFlags flags) { | |
178 | int fd, r; | |
179 | struct statfs s; | |
180 | ||
181 | assert(path); | |
182 | ||
183 | /* We refuse to clean the root file system with this | |
184 | * call. This is extra paranoia to never cause a really | |
185 | * seriously broken system. */ | |
186 | if (path_equal(path, "/")) { | |
187 | log_error("Attempted to remove entire root file system, and we can't allow that."); | |
188 | return -EPERM; | |
189 | } | |
190 | ||
d9e2daaf LP |
191 | if ((flags & (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) == (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) { |
192 | /* Try to remove as subvolume first */ | |
5bcd08db | 193 | r = btrfs_subvol_remove(path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA); |
d9e2daaf LP |
194 | if (r >= 0) |
195 | return r; | |
196 | ||
636aabc2 | 197 | if (r != -ENOTTY && r != -EINVAL && r != -ENOTDIR) |
d9e2daaf LP |
198 | return r; |
199 | ||
200 | /* Not btrfs or not a subvolume */ | |
201 | } | |
202 | ||
c6878637 LP |
203 | fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME); |
204 | if (fd < 0) { | |
205 | ||
206 | if (errno != ENOTDIR && errno != ELOOP) | |
207 | return -errno; | |
208 | ||
209 | if (!(flags & REMOVE_PHYSICAL)) { | |
210 | if (statfs(path, &s) < 0) | |
211 | return -errno; | |
212 | ||
213 | if (!is_temporary_fs(&s)) { | |
214 | log_error("Attempted to remove disk file system, and we can't allow that."); | |
215 | return -EPERM; | |
216 | } | |
217 | } | |
218 | ||
219 | if ((flags & REMOVE_ROOT) && !(flags & REMOVE_ONLY_DIRECTORIES)) | |
220 | if (unlink(path) < 0 && errno != ENOENT) | |
221 | return -errno; | |
222 | ||
223 | return 0; | |
224 | } | |
225 | ||
226 | r = rm_rf_children(fd, flags, NULL); | |
227 | ||
228 | if (flags & REMOVE_ROOT) { | |
d9e2daaf LP |
229 | if (rmdir(path) < 0) { |
230 | if (r == 0 && errno != ENOENT) | |
c6878637 LP |
231 | r = -errno; |
232 | } | |
233 | } | |
234 | ||
235 | return r; | |
236 | } |