util: add safe_closedir() similar to safe_fclose()

This commit is contained in:
Lennart Poettering 2015-09-17 01:21:19 +02:00
parent f6d9c616b6
commit ed0d40229b
5 changed files with 41 additions and 4 deletions

View File

@ -0,0 +1,27 @@
@@
expression p;
@@
- if (p) {
- closedir(p);
- p = NULL;
- }
+ p = safe_closedir(p);
@@
expression p;
@@
- if (p)
- closedir(p);
- p = NULL;
+ p = safe_closedir(p);
@@
expression p;
@@
- closedir(p);
- p = NULL;
+ p = safe_closedir(p);
@@
expression p;
@@
- if (p)
- closedir(p);
+ safe_closedir(p);

View File

@ -354,6 +354,17 @@ FILE* safe_fclose(FILE *f) {
return NULL; 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) { int unlink_noerrno(const char *path) {
PROTECT_ERRNO; PROTECT_ERRNO;
int r; int r;

View File

@ -151,6 +151,7 @@ void close_many(const int fds[], unsigned n_fd);
int fclose_nointr(FILE *f); int fclose_nointr(FILE *f);
FILE* safe_fclose(FILE *f); FILE* safe_fclose(FILE *f);
DIR* safe_closedir(DIR *f);
int parse_size(const char *t, uint64_t base, uint64_t *size); int parse_size(const char *t, uint64_t base, uint64_t *size);

View File

@ -1071,8 +1071,7 @@ static void manager_build_unit_path_cache(Manager *m) {
goto fail; goto fail;
} }
closedir(d); d = safe_closedir(d);
d = NULL;
} }
return; return;

View File

@ -806,8 +806,7 @@ static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_servic
if (!path) if (!path)
return -ENOMEM; return -ENOMEM;
if (d) safe_closedir(d);
closedir(d);
d = opendir(path); d = opendir(path);
if (!d) { if (!d) {