1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include <sys/statfs.h>
11 //#include "alloc-util.h"
12 //#include "btrfs-util.h"
13 #include "cgroup-util.h"
14 #include "dirent-util.h"
18 #include "mount-util.h"
19 #include "path-util.h"
21 #include "stat-util.h"
22 #include "string-util.h"
24 static bool is_physical_fs(const struct statfs *sfs) {
25 return !is_temporary_fs(sfs) && !is_cgroup_fs(sfs);
28 int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
29 _cleanup_closedir_ DIR *d = NULL;
36 /* This returns the first error we run into, but nevertheless
37 * tries to go on. This closes the passed fd. */
39 if (!(flags & REMOVE_PHYSICAL)) {
41 r = fstatfs(fd, &sfs);
47 if (is_physical_fs(&sfs)) {
48 /* We refuse to clean physical file systems with this call,
49 * unless explicitly requested. This is extra paranoia just
50 * to be sure we never ever remove non-state data. */
51 _cleanup_free_ char *path = NULL;
53 (void) fd_get_path(fd, &path);
54 log_error("Attempted to remove disk file system under \"%s\", and we can't allow that.",
65 return errno == ENOENT ? 0 : -errno;
68 FOREACH_DIRENT_ALL(de, d, return -errno) {
72 if (dot_or_dot_dot(de->d_name))
75 if (de->d_type == DT_UNKNOWN ||
76 (de->d_type == DT_DIR && (root_dev || (flags & REMOVE_SUBVOLUME)))) {
77 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
78 if (ret == 0 && errno != ENOENT)
83 is_dir = S_ISDIR(st.st_mode);
85 is_dir = de->d_type == DT_DIR;
90 /* if root_dev is set, remove subdirectories only if device is same */
91 if (root_dev && st.st_dev != root_dev->st_dev)
94 subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
96 if (ret == 0 && errno != ENOENT)
101 /* Stop at mount points */
102 r = fd_is_mount_point(fd, de->d_name, 0);
104 if (ret == 0 && r != -ENOENT)
107 safe_close(subdir_fd);
111 safe_close(subdir_fd);
115 #if 0 /// elogind does not support BTRFS this directly
116 if ((flags & REMOVE_SUBVOLUME) && st.st_ino == 256) {
118 /* This could be a subvolume, try to remove it */
120 r = btrfs_subvol_remove_fd(fd, de->d_name, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
122 if (!IN_SET(r, -ENOTTY, -EINVAL)) {
126 safe_close(subdir_fd);
130 /* ENOTTY, then it wasn't a
131 * btrfs subvolume, continue
134 /* It was a subvolume, continue. */
135 safe_close(subdir_fd);
141 /* We pass REMOVE_PHYSICAL here, to avoid
142 * doing the fstatfs() to check the file
143 * system type again for each directory */
144 r = rm_rf_children(subdir_fd, flags | REMOVE_PHYSICAL, root_dev);
145 if (r < 0 && ret == 0)
148 if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
149 if (ret == 0 && errno != ENOENT)
153 } else if (!(flags & REMOVE_ONLY_DIRECTORIES)) {
155 if (unlinkat(fd, de->d_name, 0) < 0) {
156 if (ret == 0 && errno != ENOENT)
164 int rm_rf(const char *path, RemoveFlags flags) {
170 /* We refuse to clean the root file system with this
171 * call. This is extra paranoia to never cause a really
172 * seriously broken system. */
173 if (path_equal_or_files_same(path, "/", AT_SYMLINK_NOFOLLOW)) {
174 log_error("Attempted to remove entire root file system (\"%s\"), and we can't allow that.", path);
178 #if 0 /// elogind does not support BTRFS this directly
179 if (FLAGS_SET(flags, REMOVE_SUBVOLUME | REMOVE_ROOT | REMOVE_PHYSICAL)) {
180 /* Try to remove as subvolume first */
181 r = btrfs_subvol_remove(path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
185 if (!IN_SET(r, -ENOTTY, -EINVAL, -ENOTDIR))
188 /* Not btrfs or not a subvolume */
192 fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
194 if (!IN_SET(errno, ENOTDIR, ELOOP))
197 if (!(flags & REMOVE_PHYSICAL)) {
198 if (statfs(path, &s) < 0)
201 if (is_physical_fs(&s)) {
202 log_error("Attempted to remove files from a disk file system under \"%s\", refusing.", path);
207 if ((flags & REMOVE_ROOT) && !(flags & REMOVE_ONLY_DIRECTORIES))
208 if (unlink(path) < 0 && errno != ENOENT)
214 r = rm_rf_children(fd, flags, NULL);
216 if (flags & REMOVE_ROOT) {
217 if (rmdir(path) < 0) {
218 if (r == 0 && errno != ENOENT)