tree-wide: replace all readdir cycles with FOREACH_DIRENT{,_ALL} (#4853)
This commit is contained in:
parent
9258a1cae3
commit
8fb3f00997
2
TODO
2
TODO
|
@ -23,8 +23,6 @@ External:
|
|||
|
||||
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
|
||||
|
||||
Features:
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "dirent-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "macro.h"
|
||||
|
@ -234,12 +235,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;
|
||||
|
|
|
@ -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>
|
||||
|
@ -446,6 +445,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;
|
||||
|
||||
|
@ -459,16 +459,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))
|
||||
|
|
|
@ -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>
|
||||
|
@ -28,6 +27,7 @@
|
|||
|
||||
#include "btrfs-util.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "dirent-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "log.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) {
|
||||
_cleanup_closedir_ DIR *d = NULL;
|
||||
struct dirent *de;
|
||||
int ret = 0, r;
|
||||
struct statfs sfs;
|
||||
|
||||
|
@ -78,19 +79,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;
|
||||
|
||||
|
@ -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) {
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
***/
|
||||
|
||||
#include <alloca.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.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) {
|
||||
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)
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
#include "alloc-util.h"
|
||||
#include "def.h"
|
||||
#include "dirent-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "format-util.h"
|
||||
#include "killall.h"
|
||||
|
@ -172,7 +173,7 @@ static int killall(int sig, Set *pids, bool send_sighup) {
|
|||
if (!dir)
|
||||
return -errno;
|
||||
|
||||
while ((d = readdir(dir))) {
|
||||
FOREACH_DIRENT_ALL(d, dir, break) {
|
||||
pid_t pid;
|
||||
int r;
|
||||
|
||||
|
|
|
@ -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 <linux/kd.h>
|
||||
|
@ -233,6 +232,7 @@ static void manager_print_jobs_in_progress(Manager *m) {
|
|||
|
||||
static int have_ask_password(void) {
|
||||
_cleanup_closedir_ DIR *dir;
|
||||
struct dirent *de;
|
||||
|
||||
dir = opendir("/run/systemd/ask-password");
|
||||
if (!dir) {
|
||||
|
@ -242,19 +242,11 @@ static int have_ask_password(void) {
|
|||
return -errno;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
struct dirent *de;
|
||||
|
||||
errno = 0;
|
||||
de = readdir(dir);
|
||||
if (!de && errno > 0)
|
||||
return -errno;
|
||||
if (!de)
|
||||
return false;
|
||||
|
||||
FOREACH_DIRENT_ALL(de, dir, return -errno) {
|
||||
if (startswith(de->d_name, "ask."))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int manager_dispatch_ask_password_fd(sd_event_source *source,
|
||||
|
|
|
@ -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) {
|
||||
_cleanup_closedir_ DIR *d;
|
||||
struct dirent *de;
|
||||
|
||||
assert(top);
|
||||
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);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
struct dirent *de;
|
||||
FOREACH_DIRENT_ALL(de, d, return -errno) {
|
||||
int k;
|
||||
char *p;
|
||||
|
||||
errno = 0;
|
||||
de = readdir(d);
|
||||
if (!de)
|
||||
return -errno;
|
||||
|
||||
dirent_ensure_type(d, de);
|
||||
|
||||
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 0;
|
||||
}
|
||||
|
||||
static int should_skip_prefix(const char* p) {
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include "device-internal.h"
|
||||
#include "device-private.h"
|
||||
#include "device-util.h"
|
||||
#include "dirent-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "fs-util.h"
|
||||
|
@ -1627,7 +1628,7 @@ static int device_sysattrs_read_all(sd_device *device) {
|
|||
if (r < 0)
|
||||
return r;
|
||||
|
||||
for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
|
||||
FOREACH_DIRENT_ALL(dent, dir, return -errno) {
|
||||
char *path;
|
||||
struct stat statbuf;
|
||||
|
||||
|
|
|
@ -793,6 +793,7 @@ _public_ int sd_get_sessions(char ***sessions) {
|
|||
|
||||
_public_ int sd_get_uids(uid_t **users) {
|
||||
_cleanup_closedir_ DIR *d;
|
||||
struct dirent *de;
|
||||
int r = 0;
|
||||
unsigned n = 0;
|
||||
_cleanup_free_ uid_t *l = NULL;
|
||||
|
@ -801,19 +802,10 @@ _public_ int sd_get_uids(uid_t **users) {
|
|||
if (!d)
|
||||
return -errno;
|
||||
|
||||
for (;;) {
|
||||
struct dirent *de;
|
||||
FOREACH_DIRENT_ALL(de, d, return -errno) {
|
||||
int k;
|
||||
uid_t uid;
|
||||
|
||||
errno = 0;
|
||||
de = readdir(d);
|
||||
if (!de && errno > 0)
|
||||
return -errno;
|
||||
|
||||
if (!de)
|
||||
break;
|
||||
|
||||
dirent_ensure_type(d, de);
|
||||
|
||||
if (!dirent_is_file(de))
|
||||
|
|
|
@ -1286,8 +1286,7 @@ static int flush_devices(Manager *m) {
|
|||
} else {
|
||||
struct dirent *de;
|
||||
|
||||
while ((de = readdir(d))) {
|
||||
|
||||
FOREACH_DIRENT_ALL(de, d, break) {
|
||||
if (!dirent_is_file(de))
|
||||
continue;
|
||||
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
@ -25,6 +24,7 @@
|
|||
|
||||
#include "alloc-util.h"
|
||||
#include "conf-files.h"
|
||||
#include "dirent-util.h"
|
||||
#include "dropin.h"
|
||||
#include "escape.h"
|
||||
#include "fd-util.h"
|
||||
|
@ -124,6 +124,7 @@ static int iterate_dir(
|
|||
char ***strv) {
|
||||
|
||||
_cleanup_closedir_ DIR *d = NULL;
|
||||
struct dirent *de;
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
|
@ -148,21 +149,9 @@ static int iterate_dir(
|
|||
return log_error_errno(errno, "Failed to open directory %s: %m", path);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
struct dirent *de;
|
||||
FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read directory %s: %m", path)) {
|
||||
_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);
|
||||
if (!f)
|
||||
return log_oom();
|
||||
|
|
|
@ -18,13 +18,13 @@
|
|||
***/
|
||||
|
||||
#include <alloca.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "sd-daemon.h"
|
||||
|
||||
#include "dirent-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fdset.h"
|
||||
#include "log.h"
|
||||
|
@ -148,12 +148,9 @@ int fdset_new_fill(FDSet **_s) {
|
|||
goto finish;
|
||||
}
|
||||
|
||||
while ((de = readdir(d))) {
|
||||
FOREACH_DIRENT(de, d, return -errno) {
|
||||
int fd = -1;
|
||||
|
||||
if (hidden_or_backup_file(de->d_name))
|
||||
continue;
|
||||
|
||||
r = safe_atoi(de->d_name, &fd);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <fnmatch.h>
|
||||
|
@ -44,6 +43,7 @@
|
|||
#include "conf-files.h"
|
||||
#include "copy.h"
|
||||
#include "def.h"
|
||||
#include "dirent-util.h"
|
||||
#include "escape.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
|
@ -380,7 +380,7 @@ static int dir_cleanup(
|
|||
bool deleted = false;
|
||||
int r = 0;
|
||||
|
||||
while ((dent = readdir(d))) {
|
||||
FOREACH_DIRENT_ALL(dent, d, break) {
|
||||
struct stat s;
|
||||
usec_t age;
|
||||
_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) {
|
||||
_cleanup_closedir_ DIR *d;
|
||||
struct dirent *de;
|
||||
int r = 0;
|
||||
|
||||
assert(i);
|
||||
|
@ -1065,19 +1066,11 @@ static int item_do_children(Item *i, const char *path, action_t action) {
|
|||
if (!d)
|
||||
return errno == ENOENT || errno == ENOTDIR ? 0 : -errno;
|
||||
|
||||
for (;;) {
|
||||
FOREACH_DIRENT_ALL(de, d, r = -errno) {
|
||||
_cleanup_free_ char *p = NULL;
|
||||
struct dirent *de;
|
||||
int q;
|
||||
|
||||
errno = 0;
|
||||
de = readdir(d);
|
||||
if (!de) {
|
||||
if (errno > 0 && r == 0)
|
||||
r = -errno;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (STR_IN_SET(de->d_name, ".", ".."))
|
||||
continue;
|
||||
|
|
|
@ -100,6 +100,7 @@
|
|||
#include <unistd.h>
|
||||
#include <linux/pci_regs.h>
|
||||
|
||||
#include "dirent-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "stdio-util.h"
|
||||
|
@ -256,7 +257,7 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
|
|||
goto out;
|
||||
}
|
||||
|
||||
for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
|
||||
FOREACH_DIRENT_ALL(dent, dir, break) {
|
||||
int i;
|
||||
char *rest, *address, str[PATH_MAX];
|
||||
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <getopt.h>
|
||||
|
@ -31,6 +30,7 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "dirent-util.h"
|
||||
#include "string-util.h"
|
||||
#include "udev.h"
|
||||
|
||||
|
@ -405,7 +405,7 @@ static struct udev_device *handle_scsi_default(struct udev_device *parent, char
|
|||
parent = NULL;
|
||||
goto out;
|
||||
}
|
||||
for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
|
||||
FOREACH_DIRENT_ALL(dent, dir, break) {
|
||||
char *rest;
|
||||
int i;
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
|
@ -25,6 +24,7 @@
|
|||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "dirent-util.h"
|
||||
#include "format-util.h"
|
||||
#include "fs-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) {
|
||||
struct udev *udev = udev_device_get_udev(dev);
|
||||
DIR *dir;
|
||||
struct dirent *dent;
|
||||
int priority = 0;
|
||||
const char *target = NULL;
|
||||
|
||||
|
@ -141,12 +142,10 @@ static const char *link_find_prioritized(struct udev_device *dev, bool add, cons
|
|||
dir = opendir(stackdir);
|
||||
if (dir == NULL)
|
||||
return target;
|
||||
for (;;) {
|
||||
FOREACH_DIRENT_ALL(dent, dir, break) {
|
||||
struct udev_device *dev_db;
|
||||
struct dirent *dent;
|
||||
|
||||
dent = readdir(dir);
|
||||
if (dent == NULL || dent->d_name[0] == '\0')
|
||||
if (dent->d_name[0] == '\0')
|
||||
break;
|
||||
if (dent->d_name[0] == '.')
|
||||
continue;
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <fnmatch.h>
|
||||
|
@ -31,6 +30,7 @@
|
|||
|
||||
#include "alloc-util.h"
|
||||
#include "conf-files.h"
|
||||
#include "dirent-util.h"
|
||||
#include "escape.h"
|
||||
#include "fd-util.h"
|
||||
#include "fs-util.h"
|
||||
|
@ -703,7 +703,7 @@ static void attr_subst_subdir(char *attr, size_t len) {
|
|||
if (dir == NULL)
|
||||
return;
|
||||
|
||||
for (dent = readdir(dir); dent != NULL; dent = readdir(dir))
|
||||
FOREACH_DIRENT_ALL(dent, dir, break)
|
||||
if (dent->d_name[0] != '.') {
|
||||
char n[strlen(dent->d_name) + strlen(tail) + 1];
|
||||
|
||||
|
|
|
@ -17,13 +17,13 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "dirent-util.h"
|
||||
#include "stdio-util.h"
|
||||
#include "udev.h"
|
||||
|
||||
|
@ -57,7 +57,7 @@ void udev_watch_restore(struct udev *udev) {
|
|||
return;
|
||||
}
|
||||
|
||||
for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
|
||||
FOREACH_DIRENT_ALL(ent, dir, break) {
|
||||
char device[UTIL_PATH_SIZE];
|
||||
ssize_t len;
|
||||
struct udev_device *dev;
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <getopt.h>
|
||||
|
@ -26,6 +25,7 @@
|
|||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "dirent-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "string-util.h"
|
||||
#include "udev-util.h"
|
||||
|
@ -196,7 +196,7 @@ static void cleanup_dir(DIR *dir, mode_t mask, int depth) {
|
|||
if (depth <= 0)
|
||||
return;
|
||||
|
||||
for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
|
||||
FOREACH_DIRENT_ALL(dent, dir, break) {
|
||||
struct stat stats;
|
||||
|
||||
if (dent->d_name[0] == '.')
|
||||
|
|
Loading…
Reference in New Issue