From ed0d40229bc536c424fad481e56afcaa1d0e38db Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 17 Sep 2015 01:21:19 +0200 Subject: util: add safe_closedir() similar to safe_fclose() --- src/basic/util.c | 11 +++++++++++ src/basic/util.h | 1 + 2 files changed, 12 insertions(+) (limited to 'src/basic') diff --git a/src/basic/util.c b/src/basic/util.c index e3b2af8e02..0c3ef2a68c 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -354,6 +354,17 @@ FILE* safe_fclose(FILE *f) { return NULL; } +DIR* safe_closedir(DIR *d) { + + if (d) { + PROTECT_ERRNO; + + assert_se(closedir(d) >= 0 || errno != EBADF); + } + + return NULL; +} + int unlink_noerrno(const char *path) { PROTECT_ERRNO; int r; diff --git a/src/basic/util.h b/src/basic/util.h index 8abaa740b2..db5dd74770 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -151,6 +151,7 @@ void close_many(const int fds[], unsigned n_fd); int fclose_nointr(FILE *f); FILE* safe_fclose(FILE *f); +DIR* safe_closedir(DIR *f); int parse_size(const char *t, uint64_t base, uint64_t *size); -- cgit v1.2.3-54-g00ecf