From: Lennart Poettering Date: Fri, 15 Jan 2021 21:13:46 +0000 (+0100) Subject: fs-util/rm-rf: improve remove+free destructors to take and return NULL X-Git-Tag: v248-rc1~296^2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=63ec26a4bfdc6376119bba4378d9f2463858f376;p=thirdparty%2Fsystemd.git fs-util/rm-rf: improve remove+free destructors to take and return NULL Let#s make these helpers useful even without _cleanup_ logic, to destory arbitary fields: make them OK wiht a NULL pointer as input, and always return one as output. --- diff --git a/src/basic/fs-util.h b/src/basic/fs-util.h index 13b68729422..8bd8da17046 100644 --- a/src/basic/fs-util.h +++ b/src/basic/fs-util.h @@ -100,16 +100,25 @@ int chase_symlinks_and_opendir(const char *path, const char *root, unsigned chas int chase_symlinks_and_stat(const char *path, const char *root, unsigned chase_flags, char **ret_path, struct stat *ret_stat, int *ret_fd); /* Useful for usage with _cleanup_(), removes a directory and frees the pointer */ -static inline void rmdir_and_free(char *p) { +static inline char *rmdir_and_free(char *p) { PROTECT_ERRNO; + + if (!p) + return NULL; + (void) rmdir(p); free(p); + return NULL; } DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free); -static inline void unlink_and_free(char *p) { +static inline char* unlink_and_free(char *p) { + if (!p) + return NULL; + (void) unlink_noerrno(p); free(p); + return NULL; } DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free); diff --git a/src/basic/rm-rf.h b/src/basic/rm-rf.h index ec56232b5dc..6483b30d7e0 100644 --- a/src/basic/rm-rf.h +++ b/src/basic/rm-rf.h @@ -18,17 +18,27 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev); int rm_rf(const char *path, RemoveFlags flags); /* Useful for usage with _cleanup_(), destroys a directory and frees the pointer */ -static inline void rm_rf_physical_and_free(char *p) { +static inline char *rm_rf_physical_and_free(char *p) { PROTECT_ERRNO; + + if (!p) + return NULL; + (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL); free(p); + return NULL; } DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_physical_and_free); /* Similar as above, but also has magic btrfs subvolume powers */ -static inline void rm_rf_subvolume_and_free(char *p) { +static inline char *rm_rf_subvolume_and_free(char *p) { PROTECT_ERRNO; + + if (!p) + return NULL; + (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME); free(p); + return NULL; } DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_subvolume_and_free);