From 1b84efa4cd62ca17a885f58f30cfce6892dc6404 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 24 Mar 2025 20:54:12 +0900 Subject: [PATCH] rm-rf: introduce rm_rf_safe() and rm_rf_safep() --- src/shared/rm-rf.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/shared/rm-rf.h b/src/shared/rm-rf.h index 6e52bbb41d4..2e107da0e1a 100644 --- a/src/shared/rm-rf.h +++ b/src/shared/rm-rf.h @@ -34,8 +34,20 @@ static inline int rm_rf(const char *path, RemoveFlags flags) { return rm_rf_at(AT_FDCWD, path, flags); } -/* Useful for usage with _cleanup_(), destroys a directory and frees the pointer */ -static inline char *rm_rf_physical_and_free(char *p) { +/* Useful for using with _cleanup_(), destroys a directory on a temporary file system. */ +static inline const char* rm_rf_safe(const char *p) { + PROTECT_ERRNO; + + if (!p) + return NULL; + + (void) rm_rf(p, REMOVE_ROOT|REMOVE_MISSING_OK|REMOVE_CHMOD); + return NULL; +} +DEFINE_TRIVIAL_CLEANUP_FUNC(const char*, rm_rf_safe); + +/* Similar as above, but allow to destroy a directory on a physical file system, and also frees the pointer. */ +static inline char* rm_rf_physical_and_free(char *p) { PROTECT_ERRNO; if (!p) @@ -46,8 +58,8 @@ static inline char *rm_rf_physical_and_free(char *p) { } DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_physical_and_free); -/* Similar as above, but also has magic btrfs subvolume powers */ -static inline char *rm_rf_subvolume_and_free(char *p) { +/* Similar as above, but also has magic btrfs subvolume powers. */ +static inline char* rm_rf_subvolume_and_free(char *p) { PROTECT_ERRNO; if (!p) -- 2.47.3