From 1a1d36c2e421f496b52f5e607c1d5bcfa61df2e5 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Tue, 5 Oct 2021 10:32:56 +0200
Subject: [PATCH 5/9] rm-rf: optionally fsync() after removing directory tree

Conflict:modify files in basic instead of shared
Reference:https://github.com/systemd/systemd/commit/bdfe7ada0d4d66e6d6e65f2822acbb1ec230f9c2

(cherry picked from commit bdfe7ada0d4d66e6d6e65f2822acbb1ec230f9c2)
(cherry picked from commit 2426beacca09d84091759be45b25c88116302184)
(cherry picked from commit 0e180f8e9c25c707b0465ad1b9447a4360f785f1)
---
 src/basic/rm-rf.c | 3 +++
 src/basic/rm-rf.h | 1 +
 2 files changed, 4 insertions(+)

diff --git a/src/basic/rm-rf.c b/src/basic/rm-rf.c
index 8e3eb59..4e46654 100644
--- a/src/basic/rm-rf.c
+++ b/src/basic/rm-rf.c
@@ -251,6 +251,9 @@ int rm_rf_children(
                         ret = r;
         }
 
+        if (FLAGS_SET(flags, REMOVE_SYNCFS) && syncfs(dirfd(d)) < 0 && ret >= 0)
+                ret = -errno;
+
         return ret;
 }
 
diff --git a/src/basic/rm-rf.h b/src/basic/rm-rf.h
index 15f7a87..1ecd552 100644
--- a/src/basic/rm-rf.h
+++ b/src/basic/rm-rf.h
@@ -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);
-- 
2.23.0

