rm-rf: optionally fsync() after removing directory tree
authorLennart Poettering <lennart@poettering.net>
Tue, 5 Oct 2021 08:32:56 +0000 (10:32 +0200)
committerLuca Boccassi <bluca@debian.org>
Sun, 18 Jun 2023 14:55:54 +0000 (15:55 +0100)
(cherry picked from commit bdfe7ada0d4d66e6d6e65f2822acbb1ec230f9c2)
(cherry picked from commit 2426beacca09d84091759be45b25c88116302184)
(cherry picked from commit 0e180f8e9c25c707b0465ad1b9447a4360f785f1)
(cherry picked from commit 9a9c2220cd3cb61c2de9c482f8ed7fa60807b14a)

Gbp-Pq: Name rm-rf-optionally-fsync-after-removing-directory-tree.patch

src/basic/rm-rf.c
src/basic/rm-rf.h

index f1b8445d7a2725553a11f947a216c408bd73ee37..cf671c25764e817ada63a188a6aee409a1572981 100644 (file)
@@ -249,6 +249,9 @@ int rm_rf_children(
                         ret = r;
         }
 
+        if (FLAGS_SET(flags, REMOVE_SYNCFS) && syncfs(dirfd(d)) < 0 && ret >= 0)
+                ret = -errno;
+
         return ret;
 }
 
index b0d5b6387adbc4145678772f7669fb363f8545cf..2619fc52f8928e80f7685b457680ce542beb7be9 100644 (file)
@@ -12,6 +12,7 @@ typedef enum RemoveFlags {
         REMOVE_SUBVOLUME        = 1 << 3, /* Drop btrfs subvolumes in the tree too */
         REMOVE_MISSING_OK       = 1 << 4, /* If the top-level directory is missing, ignore the ENOENT for it */
         REMOVE_CHMOD            = 1 << 5, /* chmod() for write access if we cannot delete something */
+        REMOVE_SYNCFS           = 1 << 6, /* syncfs() the root of the specified directory after removing everything in it */
 } RemoveFlags;
 
 int rm_rf_children(int fd, RemoveFlags flags, const struct stat *root_dev);