diff options
author | Reverend Homer <mk.43.ecko@gmail.com> | 2016-12-09 12:04:30 +0300 |
---|---|---|
committer | Sven Eden <yamakuzure@gmx.net> | 2017-07-17 17:58:35 +0200 |
commit | 701d4cf38514d96ea0618807ac3b08a9e52c60cd (patch) | |
tree | 19e52e1fda295c23a617c0a536de41d6017fbbc9 /src/basic | |
parent | 277d0c5729bd21cd925596d4ca806c32aa7c8db5 (diff) |
tree-wide: replace all readdir cycles with FOREACH_DIRENT{,_ALL} (#4853)
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/fd-util.c | 5 | ||||
-rw-r--r-- | src/basic/fs-util.c | 19 | ||||
-rw-r--r-- | src/basic/rm-rf.c | 14 | ||||
-rw-r--r-- | src/basic/util.c | 17 |
4 files changed, 11 insertions, 44 deletions
diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index 3eb3324b5..13b025f44 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -234,12 +234,9 @@ int close_all_fds(const int except[], unsigned n_except) { return r; } - while ((de = readdir(d))) { + FOREACH_DIRENT(de, d, return -errno) { int fd = -1; - if (hidden_or_backup_file(de->d_name)) - continue; - if (safe_atoi(de->d_name, &fd) < 0) /* Let's better ignore this, just in case */ continue; diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index 471708bbe..05f10c86f 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -17,7 +17,6 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ -#include <dirent.h> #include <errno.h> #include <stddef.h> #include <stdio.h> @@ -450,6 +449,7 @@ int mkfifo_atomic(const char *path, mode_t mode) { int get_files_in_directory(const char *path, char ***list) { _cleanup_closedir_ DIR *d = NULL; + struct dirent *de; size_t bufsize = 0, n = 0; _cleanup_strv_free_ char **l = NULL; @@ -463,16 +463,7 @@ int get_files_in_directory(const char *path, char ***list) { if (!d) return -errno; - for (;;) { - struct dirent *de; - - errno = 0; - de = readdir(d); - if (!de && errno > 0) - return -errno; - if (!de) - break; - + FOREACH_DIRENT_ALL(de, d, return -errno) { dirent_ensure_type(d, de); if (!dirent_is_file(de)) @@ -813,10 +804,8 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags, return -ENOMEM; } - if (ret) { - *ret = done; - done = NULL; - } + *ret = done; + done = NULL; return exists; } diff --git a/src/basic/rm-rf.c b/src/basic/rm-rf.c index 78de895d3..a90359194 100644 --- a/src/basic/rm-rf.c +++ b/src/basic/rm-rf.c @@ -17,7 +17,6 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ -#include <dirent.h> #include <errno.h> #include <fcntl.h> #include <stdbool.h> @@ -43,6 +42,7 @@ static bool is_physical_fs(const struct statfs *sfs) { int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) { _cleanup_closedir_ DIR *d = NULL; + struct dirent *de; int ret = 0, r; struct statfs sfs; @@ -78,19 +78,10 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) { return errno == ENOENT ? 0 : -errno; } - for (;;) { - struct dirent *de; + FOREACH_DIRENT_ALL(de, d, return -errno) { bool is_dir; struct stat st; - errno = 0; - de = readdir(d); - if (!de) { - if (errno > 0 && ret == 0) - ret = -errno; - return ret; - } - if (streq(de->d_name, ".") || streq(de->d_name, "..")) continue; @@ -180,6 +171,7 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) { } } } + return ret; } int rm_rf(const char *path, RemoveFlags flags) { diff --git a/src/basic/util.c b/src/basic/util.c index f8ecc929b..9741f04aa 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -498,7 +498,7 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, u = nmemb; while (l < u) { idx = (l + u) / 2; - p = (const char *) base + idx * size; + p = (void *)(((const char *) base) + (idx * size)); comparison = compar(key, p, arg); if (comparison < 0) u = idx; @@ -513,28 +513,17 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, int on_ac_power(void) { bool found_offline = false, found_online = false; _cleanup_closedir_ DIR *d = NULL; + struct dirent *de; d = opendir("/sys/class/power_supply"); if (!d) return errno == ENOENT ? true : -errno; - for (;;) { - struct dirent *de; + FOREACH_DIRENT(de, d, return -errno) { _cleanup_close_ int fd = -1, device = -1; char contents[6]; ssize_t n; - errno = 0; - de = readdir(d); - if (!de && errno > 0) - return -errno; - - if (!de) - break; - - if (hidden_or_backup_file(de->d_name)) - continue; - device = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOCTTY); if (device < 0) { if (errno == ENOENT || errno == ENOTDIR) |