diff options
author | Lennart Poettering <lennart@poettering.net> | 2016-12-01 23:19:31 +0100 |
---|---|---|
committer | Sven Eden <yamakuzure@gmx.net> | 2017-07-17 17:58:35 +0200 |
commit | c1748bb202ab6f6aa4e5c17409ef83cf3ffdaa37 (patch) | |
tree | 24ae7e9053c1cf5b05a3e70f991668789852e1d3 /src/basic/fs-util.h | |
parent | 4d207ba52cb963c2ebec17bfc4300d570aa43846 (diff) |
util-lib: add easy helpers for temporary directories that rmdir()ed via _cleanup_
This adds mkdtemp_malloc() that is a combination of mkdtemp() plus strdup(). It
initializes its return paremeter only if the temporary directory could be
created successfully, so that the parameter is exactly non-NULL when the
directory exists.
rmdir_and_free() and rmdir_and_freep() are also added, and the latter may be
used inside of _cleanup_ for such a directory string variable, to automatically
rmdir() the directory if it is non-NULL when the scope exits.
rmdir_and_free() is similar to the existing rm_rf_and_free() however, is only
removes a single directory and does not operate recursively.
Diffstat (limited to 'src/basic/fs-util.h')
-rw-r--r-- | src/basic/fs-util.h | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/basic/fs-util.h b/src/basic/fs-util.h index d925cc2d0..3f47537de 100644 --- a/src/basic/fs-util.h +++ b/src/basic/fs-util.h @@ -97,3 +97,10 @@ enum { }; int chase_symlinks(const char *path_with_prefix, const char *root, unsigned flags, char **ret); + +/* Useful for usage with _cleanup_(), removes a directory and frees the pointer */ +static inline void rmdir_and_free(char *p) { + (void) rmdir(p); + free(p); +} +DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free); |