tree-wide: replace all readdir cycles with FOREACH_DIRENT{,_ALL} (#4853)

This commit is contained in:
Reverend Homer 2016-12-09 12:04:30 +03:00 committed by Martin Pitt
parent 9258a1cae3
commit 8fb3f00997
20 changed files with 46 additions and 118 deletions

2
TODO
View File

@ -23,8 +23,6 @@ External:
Janitorial Clean-ups: Janitorial Clean-ups:
* replace manual readdir() loops with FOREACH_DIRENT or FOREACH_DIRENT_ALL
* Rearrange tests so that the various test-xyz.c match a specific src/basic/xyz.c again * Rearrange tests so that the various test-xyz.c match a specific src/basic/xyz.c again
Features: Features:

View File

@ -24,6 +24,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include "dirent-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "fs-util.h" #include "fs-util.h"
#include "macro.h" #include "macro.h"
@ -234,12 +235,9 @@ int close_all_fds(const int except[], unsigned n_except) {
return r; return r;
} }
while ((de = readdir(d))) { FOREACH_DIRENT(de, d, return -errno) {
int fd = -1; int fd = -1;
if (hidden_or_backup_file(de->d_name))
continue;
if (safe_atoi(de->d_name, &fd) < 0) if (safe_atoi(de->d_name, &fd) < 0)
/* Let's better ignore this, just in case */ /* Let's better ignore this, just in case */
continue; continue;

View File

@ -17,7 +17,6 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>. along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/ ***/
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
@ -446,6 +445,7 @@ int mkfifo_atomic(const char *path, mode_t mode) {
int get_files_in_directory(const char *path, char ***list) { int get_files_in_directory(const char *path, char ***list) {
_cleanup_closedir_ DIR *d = NULL; _cleanup_closedir_ DIR *d = NULL;
struct dirent *de;
size_t bufsize = 0, n = 0; size_t bufsize = 0, n = 0;
_cleanup_strv_free_ char **l = NULL; _cleanup_strv_free_ char **l = NULL;
@ -459,16 +459,7 @@ int get_files_in_directory(const char *path, char ***list) {
if (!d) if (!d)
return -errno; return -errno;
for (;;) { FOREACH_DIRENT_ALL(de, d, return -errno) {
struct dirent *de;
errno = 0;
de = readdir(d);
if (!de && errno > 0)
return -errno;
if (!de)
break;
dirent_ensure_type(d, de); dirent_ensure_type(d, de);
if (!dirent_is_file(de)) if (!dirent_is_file(de))

View File

@ -17,7 +17,6 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>. along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/ ***/
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h> #include <stdbool.h>
@ -28,6 +27,7 @@
#include "btrfs-util.h" #include "btrfs-util.h"
#include "cgroup-util.h" #include "cgroup-util.h"
#include "dirent-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "log.h" #include "log.h"
#include "macro.h" #include "macro.h"
@ -43,6 +43,7 @@ static bool is_physical_fs(const struct statfs *sfs) {
int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) { int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
_cleanup_closedir_ DIR *d = NULL; _cleanup_closedir_ DIR *d = NULL;
struct dirent *de;
int ret = 0, r; int ret = 0, r;
struct statfs sfs; struct statfs sfs;
@ -78,19 +79,10 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
return errno == ENOENT ? 0 : -errno; return errno == ENOENT ? 0 : -errno;
} }
for (;;) { FOREACH_DIRENT_ALL(de, d, return -errno) {
struct dirent *de;
bool is_dir; bool is_dir;
struct stat st; 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, "..")) if (streq(de->d_name, ".") || streq(de->d_name, ".."))
continue; continue;
@ -178,6 +170,7 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
} }
} }
} }
return ret;
} }
int rm_rf(const char *path, RemoveFlags flags) { int rm_rf(const char *path, RemoveFlags flags) {

View File

@ -18,7 +18,6 @@
***/ ***/
#include <alloca.h> #include <alloca.h>
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <sched.h> #include <sched.h>
@ -508,28 +507,17 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
int on_ac_power(void) { int on_ac_power(void) {
bool found_offline = false, found_online = false; bool found_offline = false, found_online = false;
_cleanup_closedir_ DIR *d = NULL; _cleanup_closedir_ DIR *d = NULL;
struct dirent *de;
d = opendir("/sys/class/power_supply"); d = opendir("/sys/class/power_supply");
if (!d) if (!d)
return errno == ENOENT ? true : -errno; return errno == ENOENT ? true : -errno;
for (;;) { FOREACH_DIRENT(de, d, return -errno) {
struct dirent *de;
_cleanup_close_ int fd = -1, device = -1; _cleanup_close_ int fd = -1, device = -1;
char contents[6]; char contents[6];
ssize_t n; 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); device = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (device < 0) { if (device < 0) {
if (errno == ENOENT || errno == ENOTDIR) if (errno == ENOENT || errno == ENOTDIR)

View File

@ -24,6 +24,7 @@
#include "alloc-util.h" #include "alloc-util.h"
#include "def.h" #include "def.h"
#include "dirent-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "format-util.h" #include "format-util.h"
#include "killall.h" #include "killall.h"
@ -172,7 +173,7 @@ static int killall(int sig, Set *pids, bool send_sighup) {
if (!dir) if (!dir)
return -errno; return -errno;
while ((d = readdir(dir))) { FOREACH_DIRENT_ALL(d, dir, break) {
pid_t pid; pid_t pid;
int r; int r;

View File

@ -17,7 +17,6 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>. along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/ ***/
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <linux/kd.h> #include <linux/kd.h>
@ -233,6 +232,7 @@ static void manager_print_jobs_in_progress(Manager *m) {
static int have_ask_password(void) { static int have_ask_password(void) {
_cleanup_closedir_ DIR *dir; _cleanup_closedir_ DIR *dir;
struct dirent *de;
dir = opendir("/run/systemd/ask-password"); dir = opendir("/run/systemd/ask-password");
if (!dir) { if (!dir) {
@ -242,19 +242,11 @@ static int have_ask_password(void) {
return -errno; return -errno;
} }
for (;;) { FOREACH_DIRENT_ALL(de, dir, return -errno) {
struct dirent *de;
errno = 0;
de = readdir(dir);
if (!de && errno > 0)
return -errno;
if (!de)
return false;
if (startswith(de->d_name, "ask.")) if (startswith(de->d_name, "ask."))
return true; return true;
} }
return false;
} }
static int manager_dispatch_ask_password_fd(sd_event_source *source, static int manager_dispatch_ask_password_fd(sd_event_source *source,

View File

@ -297,6 +297,7 @@ static int enumerate_dir_d(Hashmap *top, Hashmap *bottom, Hashmap *drops, const
static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const char *path, bool dropins) { static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const char *path, bool dropins) {
_cleanup_closedir_ DIR *d; _cleanup_closedir_ DIR *d;
struct dirent *de;
assert(top); assert(top);
assert(bottom); assert(bottom);
@ -313,16 +314,10 @@ static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const ch
return log_error_errno(errno, "Failed to open %s: %m", path); return log_error_errno(errno, "Failed to open %s: %m", path);
} }
for (;;) { FOREACH_DIRENT_ALL(de, d, return -errno) {
struct dirent *de;
int k; int k;
char *p; char *p;
errno = 0;
de = readdir(d);
if (!de)
return -errno;
dirent_ensure_type(d, de); dirent_ensure_type(d, de);
if (dropins && de->d_type == DT_DIR && endswith(de->d_name, ".d")) if (dropins && de->d_type == DT_DIR && endswith(de->d_name, ".d"))
@ -354,6 +349,7 @@ static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const ch
return k; return k;
} }
} }
return 0;
} }
static int should_skip_prefix(const char* p) { static int should_skip_prefix(const char* p) {

View File

@ -28,6 +28,7 @@
#include "device-internal.h" #include "device-internal.h"
#include "device-private.h" #include "device-private.h"
#include "device-util.h" #include "device-util.h"
#include "dirent-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "fs-util.h" #include "fs-util.h"
@ -1627,7 +1628,7 @@ static int device_sysattrs_read_all(sd_device *device) {
if (r < 0) if (r < 0)
return r; return r;
for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { FOREACH_DIRENT_ALL(dent, dir, return -errno) {
char *path; char *path;
struct stat statbuf; struct stat statbuf;

View File

@ -793,6 +793,7 @@ _public_ int sd_get_sessions(char ***sessions) {
_public_ int sd_get_uids(uid_t **users) { _public_ int sd_get_uids(uid_t **users) {
_cleanup_closedir_ DIR *d; _cleanup_closedir_ DIR *d;
struct dirent *de;
int r = 0; int r = 0;
unsigned n = 0; unsigned n = 0;
_cleanup_free_ uid_t *l = NULL; _cleanup_free_ uid_t *l = NULL;
@ -801,19 +802,10 @@ _public_ int sd_get_uids(uid_t **users) {
if (!d) if (!d)
return -errno; return -errno;
for (;;) { FOREACH_DIRENT_ALL(de, d, return -errno) {
struct dirent *de;
int k; int k;
uid_t uid; uid_t uid;
errno = 0;
de = readdir(d);
if (!de && errno > 0)
return -errno;
if (!de)
break;
dirent_ensure_type(d, de); dirent_ensure_type(d, de);
if (!dirent_is_file(de)) if (!dirent_is_file(de))

View File

@ -1286,8 +1286,7 @@ static int flush_devices(Manager *m) {
} else { } else {
struct dirent *de; struct dirent *de;
while ((de = readdir(d))) { FOREACH_DIRENT_ALL(de, d, break) {
if (!dirent_is_file(de)) if (!dirent_is_file(de))
continue; continue;

View File

@ -17,7 +17,6 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>. along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/ ***/
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
@ -25,6 +24,7 @@
#include "alloc-util.h" #include "alloc-util.h"
#include "conf-files.h" #include "conf-files.h"
#include "dirent-util.h"
#include "dropin.h" #include "dropin.h"
#include "escape.h" #include "escape.h"
#include "fd-util.h" #include "fd-util.h"
@ -124,6 +124,7 @@ static int iterate_dir(
char ***strv) { char ***strv) {
_cleanup_closedir_ DIR *d = NULL; _cleanup_closedir_ DIR *d = NULL;
struct dirent *de;
int r; int r;
assert(path); assert(path);
@ -148,21 +149,9 @@ static int iterate_dir(
return log_error_errno(errno, "Failed to open directory %s: %m", path); return log_error_errno(errno, "Failed to open directory %s: %m", path);
} }
for (;;) { FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read directory %s: %m", path)) {
struct dirent *de;
_cleanup_free_ char *f = NULL; _cleanup_free_ char *f = NULL;
errno = 0;
de = readdir(d);
if (!de && errno > 0)
return log_error_errno(errno, "Failed to read directory %s: %m", path);
if (!de)
break;
if (hidden_or_backup_file(de->d_name))
continue;
f = strjoin(path, "/", de->d_name); f = strjoin(path, "/", de->d_name);
if (!f) if (!f)
return log_oom(); return log_oom();

View File

@ -18,13 +18,13 @@
***/ ***/
#include <alloca.h> #include <alloca.h>
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stddef.h> #include <stddef.h>
#include "sd-daemon.h" #include "sd-daemon.h"
#include "dirent-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "fdset.h" #include "fdset.h"
#include "log.h" #include "log.h"
@ -148,12 +148,9 @@ int fdset_new_fill(FDSet **_s) {
goto finish; goto finish;
} }
while ((de = readdir(d))) { FOREACH_DIRENT(de, d, return -errno) {
int fd = -1; int fd = -1;
if (hidden_or_backup_file(de->d_name))
continue;
r = safe_atoi(de->d_name, &fd); r = safe_atoi(de->d_name, &fd);
if (r < 0) if (r < 0)
goto finish; goto finish;

View File

@ -18,7 +18,6 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>. along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/ ***/
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <fnmatch.h> #include <fnmatch.h>
@ -44,6 +43,7 @@
#include "conf-files.h" #include "conf-files.h"
#include "copy.h" #include "copy.h"
#include "def.h" #include "def.h"
#include "dirent-util.h"
#include "escape.h" #include "escape.h"
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
@ -380,7 +380,7 @@ static int dir_cleanup(
bool deleted = false; bool deleted = false;
int r = 0; int r = 0;
while ((dent = readdir(d))) { FOREACH_DIRENT_ALL(dent, d, break) {
struct stat s; struct stat s;
usec_t age; usec_t age;
_cleanup_free_ char *sub_path = NULL; _cleanup_free_ char *sub_path = NULL;
@ -1053,6 +1053,7 @@ typedef int (*action_t)(Item *, const char *);
static int item_do_children(Item *i, const char *path, action_t action) { static int item_do_children(Item *i, const char *path, action_t action) {
_cleanup_closedir_ DIR *d; _cleanup_closedir_ DIR *d;
struct dirent *de;
int r = 0; int r = 0;
assert(i); assert(i);
@ -1065,19 +1066,11 @@ static int item_do_children(Item *i, const char *path, action_t action) {
if (!d) if (!d)
return errno == ENOENT || errno == ENOTDIR ? 0 : -errno; return errno == ENOENT || errno == ENOTDIR ? 0 : -errno;
for (;;) { FOREACH_DIRENT_ALL(de, d, r = -errno) {
_cleanup_free_ char *p = NULL; _cleanup_free_ char *p = NULL;
struct dirent *de;
int q; int q;
errno = 0; errno = 0;
de = readdir(d);
if (!de) {
if (errno > 0 && r == 0)
r = -errno;
break;
}
if (STR_IN_SET(de->d_name, ".", "..")) if (STR_IN_SET(de->d_name, ".", ".."))
continue; continue;

View File

@ -100,6 +100,7 @@
#include <unistd.h> #include <unistd.h>
#include <linux/pci_regs.h> #include <linux/pci_regs.h>
#include "dirent-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "stdio-util.h" #include "stdio-util.h"
@ -256,7 +257,7 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
goto out; goto out;
} }
for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { FOREACH_DIRENT_ALL(dent, dir, break) {
int i; int i;
char *rest, *address, str[PATH_MAX]; char *rest, *address, str[PATH_MAX];

View File

@ -20,7 +20,6 @@
*/ */
#include <ctype.h> #include <ctype.h>
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <getopt.h> #include <getopt.h>
@ -31,6 +30,7 @@
#include <unistd.h> #include <unistd.h>
#include "alloc-util.h" #include "alloc-util.h"
#include "dirent-util.h"
#include "string-util.h" #include "string-util.h"
#include "udev.h" #include "udev.h"
@ -405,7 +405,7 @@ static struct udev_device *handle_scsi_default(struct udev_device *parent, char
parent = NULL; parent = NULL;
goto out; goto out;
} }
for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { FOREACH_DIRENT_ALL(dent, dir, break) {
char *rest; char *rest;
int i; int i;

View File

@ -15,7 +15,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h> #include <stdbool.h>
@ -25,6 +24,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include "dirent-util.h"
#include "format-util.h" #include "format-util.h"
#include "fs-util.h" #include "fs-util.h"
#include "selinux-util.h" #include "selinux-util.h"
@ -129,6 +129,7 @@ exit:
static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize) { static const char *link_find_prioritized(struct udev_device *dev, bool add, const char *stackdir, char *buf, size_t bufsize) {
struct udev *udev = udev_device_get_udev(dev); struct udev *udev = udev_device_get_udev(dev);
DIR *dir; DIR *dir;
struct dirent *dent;
int priority = 0; int priority = 0;
const char *target = NULL; const char *target = NULL;
@ -141,12 +142,10 @@ static const char *link_find_prioritized(struct udev_device *dev, bool add, cons
dir = opendir(stackdir); dir = opendir(stackdir);
if (dir == NULL) if (dir == NULL)
return target; return target;
for (;;) { FOREACH_DIRENT_ALL(dent, dir, break) {
struct udev_device *dev_db; struct udev_device *dev_db;
struct dirent *dent;
dent = readdir(dir); if (dent->d_name[0] == '\0')
if (dent == NULL || dent->d_name[0] == '\0')
break; break;
if (dent->d_name[0] == '.') if (dent->d_name[0] == '.')
continue; continue;

View File

@ -16,7 +16,6 @@
*/ */
#include <ctype.h> #include <ctype.h>
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <fnmatch.h> #include <fnmatch.h>
@ -31,6 +30,7 @@
#include "alloc-util.h" #include "alloc-util.h"
#include "conf-files.h" #include "conf-files.h"
#include "dirent-util.h"
#include "escape.h" #include "escape.h"
#include "fd-util.h" #include "fd-util.h"
#include "fs-util.h" #include "fs-util.h"
@ -703,7 +703,7 @@ static void attr_subst_subdir(char *attr, size_t len) {
if (dir == NULL) if (dir == NULL)
return; return;
for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) FOREACH_DIRENT_ALL(dent, dir, break)
if (dent->d_name[0] != '.') { if (dent->d_name[0] != '.') {
char n[strlen(dent->d_name) + strlen(tail) + 1]; char n[strlen(dent->d_name) + strlen(tail) + 1];

View File

@ -17,13 +17,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <sys/inotify.h> #include <sys/inotify.h>
#include <unistd.h> #include <unistd.h>
#include "dirent-util.h"
#include "stdio-util.h" #include "stdio-util.h"
#include "udev.h" #include "udev.h"
@ -57,7 +57,7 @@ void udev_watch_restore(struct udev *udev) {
return; return;
} }
for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) { FOREACH_DIRENT_ALL(ent, dir, break) {
char device[UTIL_PATH_SIZE]; char device[UTIL_PATH_SIZE];
ssize_t len; ssize_t len;
struct udev_device *dev; struct udev_device *dev;

View File

@ -16,7 +16,6 @@
*/ */
#include <ctype.h> #include <ctype.h>
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <getopt.h> #include <getopt.h>
@ -26,6 +25,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include "dirent-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "string-util.h" #include "string-util.h"
#include "udev-util.h" #include "udev-util.h"
@ -196,7 +196,7 @@ static void cleanup_dir(DIR *dir, mode_t mask, int depth) {
if (depth <= 0) if (depth <= 0)
return; return;
for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { FOREACH_DIRENT_ALL(dent, dir, break) {
struct stat stats; struct stat stats;
if (dent->d_name[0] == '.') if (dent->d_name[0] == '.')