From: Yu Watanabe Date: Mon, 24 Mar 2025 11:54:12 +0000 (+0900) Subject: rm-rf: introduce rm_rf_safe() and rm_rf_safep() X-Git-Tag: v258-rc1~950^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1b84efa4cd62ca17a885f58f30cfce6892dc6404;p=thirdparty%2Fsystemd.git rm-rf: introduce rm_rf_safe() and rm_rf_safep() --- 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)