chiark / gitweb /
Link: port to new ethtool ETHTOOL_xLINKSETTINGS
[elogind.git] / src / basic / rm-rf.c
index d35d172958fb6ea3f3cc154f72e5d3f3cfff9a76..78de895d35d7188aebd699f632ceaa088d735fbb 100644 (file)
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
 /***
   This file is part of systemd.
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include "util.h"
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <sys/stat.h>
+#include <sys/statfs.h>
+#include <unistd.h>
+
+//#include "btrfs-util.h"
+#include "cgroup-util.h"
+#include "fd-util.h"
+#include "log.h"
+#include "macro.h"
+#include "mount-util.h"
 #include "path-util.h"
-// #include "btrfs-util.h"
 #include "rm-rf.h"
+#include "stat-util.h"
+#include "string-util.h"
+
+static bool is_physical_fs(const struct statfs *sfs) {
+        return !is_temporary_fs(sfs) && !is_cgroup_fs(sfs);
+}
 
 int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
         _cleanup_closedir_ DIR *d = NULL;
         int ret = 0, r;
+        struct statfs sfs;
 
         assert(fd >= 0);
 
@@ -35,13 +53,13 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
 
         if (!(flags & REMOVE_PHYSICAL)) {
 
-                r = fd_is_temporary_fs(fd);
+                r = fstatfs(fd, &sfs);
                 if (r < 0) {
                         safe_close(fd);
-                        return r;
+                        return -errno;
                 }
 
-                if (!r) {
+                if (is_physical_fs(&sfs)) {
                         /* We refuse to clean physical file systems
                          * with this call, unless explicitly
                          * requested. This is extra paranoia just to
@@ -68,7 +86,7 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
                 errno = 0;
                 de = readdir(d);
                 if (!de) {
-                        if (errno != 0 && ret == 0)
+                        if (errno > 0 && ret == 0)
                                 ret = -errno;
                         return ret;
                 }
@@ -116,13 +134,14 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
                                 continue;
                         }
 
-#if 0
+#if 0 /// elogind does not support BTRFS this directly
                         if ((flags & REMOVE_SUBVOLUME) && st.st_ino == 256) {
 
                                 /* This could be a subvolume, try to remove it */
-                                r = btrfs_subvol_remove_fd(fd, de->d_name, true);
+
+                                r = btrfs_subvol_remove_fd(fd, de->d_name, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
                                 if (r < 0) {
-                                       if (r != -ENOTTY && r != -EINVAL) {
+                                        if (r != -ENOTTY && r != -EINVAL) {
                                                 if (ret == 0)
                                                         ret = r;
 
@@ -138,7 +157,7 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
                                         safe_close(subdir_fd);
                                         continue;
                                 }
-                       }
+                        }
 #endif // 0
 
                         /* We pass REMOVE_PHYSICAL here, to avoid
@@ -177,18 +196,19 @@ int rm_rf(const char *path, RemoveFlags flags) {
                 return -EPERM;
         }
 
+#if 0 /// elogind does not support BTRFS this directly
         if ((flags & (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) == (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) {
                 /* Try to remove as subvolume first */
-#if 0
-               r = btrfs_subvol_remove(path, true);
+                r = btrfs_subvol_remove(path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
                 if (r >= 0)
                         return r;
 
                 if (r != -ENOTTY && r != -EINVAL && r != -ENOTDIR)
                         return r;
-#endif // 0
+
                 /* Not btrfs or not a subvolume */
         }
+#endif // 0
 
         fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
         if (fd < 0) {
@@ -200,7 +220,7 @@ int rm_rf(const char *path, RemoveFlags flags) {
                         if (statfs(path, &s) < 0)
                                 return -errno;
 
-                        if (!is_temporary_fs(&s)) {
+                        if (is_physical_fs(&s)) {
                                 log_error("Attempted to remove disk file system, and we can't allow that.");
                                 return -EPERM;
                         }