summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2022-09-26 22:31:52 +0200
committerDaan De Meyer <daan.j.demeyer@gmail.com>2022-11-09 11:14:10 +0100
commit56e2bceddc7383c4abe1ef0110192e491c729de4 (patch)
treec36f6aeaca7eb8cd53ff2e1a32afef88d83ca0c4
parent0ab5e2a4b4f5d435cb66b591ef5c700894663fd3 (diff)
downloadsystemd-56e2bceddc7383c4abe1ef0110192e491c729de4.tar.gz
rm-rf: Add rm_rf_physical_and_close()
In tests it's useful to be able to delete temporary directories via a file descriptor to them, so let's add rm_rf_physical_and_close() which gets the file descriptor path via /proc and tries to remove it that way.
-rw-r--r--src/shared/rm-rf.h17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/shared/rm-rf.h b/src/shared/rm-rf.h
index 24fd9a2aa2..86d3e57b0e 100644
--- a/src/shared/rm-rf.h
+++ b/src/shared/rm-rf.h
@@ -5,6 +5,7 @@
#include "alloc-util.h"
#include "errno-util.h"
+#include "fd-util.h"
typedef enum RemoveFlags {
REMOVE_ONLY_DIRECTORIES = 1 << 0, /* Only remove empty directories, no files */
@@ -51,3 +52,19 @@ static inline char *rm_rf_subvolume_and_free(char *p) {
return mfree(p);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_subvolume_and_free);
+
+static inline int rm_rf_physical_and_close(int fd) {
+ _cleanup_free_ char *p = NULL;
+
+ if (fd < 0)
+ return -1;
+
+ if (fd_get_path(fd, &p) < 0)
+ return safe_close(fd);
+
+ safe_close(fd);
+ (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_MISSING_OK|REMOVE_CHMOD);
+
+ return -1;
+}
+DEFINE_TRIVIAL_CLEANUP_FUNC(int, rm_rf_physical_and_close);