From af3b864d76cf916a026b7877099791811b348fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 7 Dec 2021 12:52:58 +0100 Subject: [PATCH] Define FOREACH_DIRENT through FOREACH_DIRENT_ALL As in the previous commit, 'de' is used as the iterator variable name. --- src/basic/conf-files.c | 1 - src/basic/dirent-util.h | 17 ++++++----------- src/basic/fd-util.c | 1 - src/basic/locale-util.c | 7 +++---- src/basic/os-util.c | 1 - src/basic/util.c | 1 - src/basic/virt.c | 7 +++---- src/boot/bootctl.c | 3 --- src/core/smack-setup.c | 3 --- src/coredump/coredump-vacuum.c | 1 - src/coredump/coredump.c | 9 ++++----- src/home/homed-manager.c | 2 -- src/journal/journald-stream.c | 1 - src/login/logind.c | 5 ----- src/machine/machined.c | 1 - src/mount/mount-tool.c | 1 - src/portable/portable.c | 3 --- src/pstore/pstore.c | 1 - src/shared/bootspec.c | 1 - src/shared/devnode-acl.c | 5 ++--- src/shared/efi-loader.c | 1 - src/shared/fdset.c | 1 - src/shared/install.c | 5 ----- src/shared/numa-util.c | 1 - src/shared/userdb.c | 1 - src/sysv-generator/sysv-generator.c | 2 -- src/test/test-cgroup-util.c | 1 - src/test/test-process-util.c | 1 - .../tty-ask-password-agent.c | 1 - src/userdb/userdbctl.c | 1 - .../xdg-autostart-generator.c | 1 - 31 files changed, 18 insertions(+), 69 deletions(-) diff --git a/src/basic/conf-files.c b/src/basic/conf-files.c index 9f0c97b691..287428b561 100644 --- a/src/basic/conf-files.c +++ b/src/basic/conf-files.c @@ -30,7 +30,6 @@ static int files_add( _cleanup_closedir_ DIR *dir = NULL; const char *dirpath; - struct dirent *de; int r; assert(h); diff --git a/src/basic/dirent-util.h b/src/basic/dirent-util.h index 2dc5dae1e1..5fde9043a3 100644 --- a/src/basic/dirent-util.h +++ b/src/basic/dirent-util.h @@ -14,17 +14,6 @@ bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pu struct dirent *readdir_ensure_type(DIR *d); struct dirent *readdir_no_dot(DIR *dirp); -#define FOREACH_DIRENT(de, d, on_error) \ - for (de = readdir_ensure_type(d);; de = readdir_ensure_type(d)) \ - if (!de) { \ - if (errno > 0) { \ - on_error; \ - } \ - break; \ - } else if (hidden_or_backup_file((de)->d_name)) \ - continue; \ - else - #define FOREACH_DIRENT_ALL(de, d, on_error) \ for (struct dirent *(de) = readdir_ensure_type(d);; (de) = readdir_ensure_type(d)) \ if (!de) { \ @@ -34,6 +23,12 @@ struct dirent *readdir_no_dot(DIR *dirp); break; \ } else +#define FOREACH_DIRENT(de, d, on_error) \ + FOREACH_DIRENT_ALL(de, d, on_error) \ + if (hidden_or_backup_file((de)->d_name)) \ + continue; \ + else + /* Maximum space one dirent structure might require at most */ #define DIRENT_SIZE_MAX CONST_MAX(sizeof(struct dirent), offsetof(struct dirent, d_name) + NAME_MAX + 1) diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index 2334a30e3d..3866e87675 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -300,7 +300,6 @@ int close_all_fds_without_malloc(const int except[], size_t n_except) { int close_all_fds(const int except[], size_t n_except) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; assert(n_except == 0 || except); diff --git a/src/basic/locale-util.c b/src/basic/locale-util.c index 9a7c7795e7..7f1a2f15f7 100644 --- a/src/basic/locale-util.c +++ b/src/basic/locale-util.c @@ -158,20 +158,19 @@ static int add_locales_from_archive(Set *locales) { static int add_locales_from_libdir (Set *locales) { _cleanup_closedir_ DIR *dir = NULL; - struct dirent *entry; int r; dir = opendir("/usr/lib/locale"); if (!dir) return errno == ENOENT ? 0 : -errno; - FOREACH_DIRENT(entry, dir, return -errno) { + FOREACH_DIRENT(de, dir, return -errno) { char *z; - if (entry->d_type != DT_DIR) + if (de->d_type != DT_DIR) continue; - z = normalize_locale(entry->d_name); + z = normalize_locale(de->d_name); if (!z) return -ENOMEM; diff --git a/src/basic/os-util.c b/src/basic/os-util.c index 69ea39345a..75c8500e51 100644 --- a/src/basic/os-util.c +++ b/src/basic/os-util.c @@ -89,7 +89,6 @@ int open_extension_release(const char *root, const char *extension, char **ret_p return log_debug_errno(r, "Cannot open %s/usr/lib/extension-release.d/, ignoring: %m", root); r = -ENOENT; - struct dirent *de; FOREACH_DIRENT(de, extension_release_dir, return -errno) { int k; diff --git a/src/basic/util.c b/src/basic/util.c index 43e74b773f..3aecb22fc4 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -118,7 +118,6 @@ void in_initrd_force(bool value) { int on_ac_power(void) { bool found_offline = false, found_online = false; _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r; d = opendir("/sys/class/power_supply"); diff --git a/src/basic/virt.c b/src/basic/virt.c index 9b7dbdc205..617439708d 100644 --- a/src/basic/virt.c +++ b/src/basic/virt.c @@ -98,7 +98,6 @@ static int detect_vm_device_tree(void) { r = read_one_line_file("/proc/device-tree/hypervisor/compatible", &hvtype); if (r == -ENOENT) { _cleanup_closedir_ DIR *dir = NULL; - struct dirent *dent; if (access("/proc/device-tree/ibm,partition-name", F_OK) == 0 && access("/proc/device-tree/hmc-managed?", F_OK) == 0 && @@ -114,9 +113,9 @@ static int detect_vm_device_tree(void) { return -errno; } - FOREACH_DIRENT(dent, dir, return -errno) - if (strstr(dent->d_name, "fw-cfg")) { - log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", dent->d_name); + FOREACH_DIRENT(de, dir, return -errno) + if (strstr(de->d_name, "fw-cfg")) { + log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", de->d_name); return VIRTUALIZATION_QEMU; } diff --git a/src/boot/bootctl.c b/src/boot/bootctl.c index 51d304ea04..1a940f9462 100644 --- a/src/boot/bootctl.c +++ b/src/boot/bootctl.c @@ -184,7 +184,6 @@ finish: static int enumerate_binaries(const char *esp_path, const char *path, const char *prefix) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; const char *p; int c = 0, r; @@ -658,7 +657,6 @@ static int copy_one_file(const char *esp_path, const char *name, bool force) { } static int install_binaries(const char *esp_path, bool force) { - struct dirent *de; _cleanup_closedir_ DIR *d = NULL; int r = 0; @@ -850,7 +848,6 @@ static int install_variables(const char *esp_path, static int remove_boot_efi(const char *esp_path) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; const char *p; int r, c = 0; diff --git a/src/core/smack-setup.c b/src/core/smack-setup.c index 5b2671ff40..f88cb80834 100644 --- a/src/core/smack-setup.c +++ b/src/core/smack-setup.c @@ -51,7 +51,6 @@ static int fdopen_unlocked_at(int dfd, const char *dir, const char *name, int *s static int write_access2_rules(const char *srcdir) { _cleanup_close_ int load2_fd = -1, change_fd = -1; _cleanup_closedir_ DIR *dir = NULL; - struct dirent *entry; int dfd = -1, r = 0; load2_fd = open("/sys/fs/smackfs/load2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); @@ -124,7 +123,6 @@ static int write_access2_rules(const char *srcdir) { static int write_cipso2_rules(const char *srcdir) { _cleanup_close_ int cipso2_fd = -1; _cleanup_closedir_ DIR *dir = NULL; - struct dirent *entry; int dfd = -1, r = 0; cipso2_fd = open("/sys/fs/smackfs/cipso2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); @@ -184,7 +182,6 @@ static int write_cipso2_rules(const char *srcdir) { static int write_netlabel_rules(const char *srcdir) { _cleanup_fclose_ FILE *dst = NULL; _cleanup_closedir_ DIR *dir = NULL; - struct dirent *entry; int dfd = -1, r = 0; dst = fopen("/sys/fs/smackfs/netlabel", "we"); diff --git a/src/coredump/coredump-vacuum.c b/src/coredump/coredump-vacuum.c index 0c08ab21dc..dcf9cc03cd 100644 --- a/src/coredump/coredump-vacuum.c +++ b/src/coredump/coredump-vacuum.c @@ -142,7 +142,6 @@ int coredump_vacuum(int exclude_fd, uint64_t keep_free, uint64_t max_use) { for (;;) { _cleanup_(vacuum_candidate_hashmap_freep) Hashmap *h = NULL; VacuumCandidate *worst = NULL; - struct dirent *de; uint64_t sum = 0; rewinddir(d); diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index cb74b34aff..9e0dafb1ce 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -580,7 +580,6 @@ static int compose_open_fds(pid_t pid, char **open_fds) { _cleanup_free_ char *buffer = NULL; _cleanup_fclose_ FILE *stream = NULL; const char *fddelim = "", *path; - struct dirent *dent = NULL; size_t size = 0; int r; @@ -600,20 +599,20 @@ static int compose_open_fds(pid_t pid, char **open_fds) { if (!stream) return -ENOMEM; - FOREACH_DIRENT(dent, proc_fd_dir, return -errno) { + FOREACH_DIRENT(de, proc_fd_dir, return -errno) { _cleanup_fclose_ FILE *fdinfo = NULL; _cleanup_free_ char *fdname = NULL; _cleanup_close_ int fd = -1; - r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname); + r = readlinkat_malloc(dirfd(proc_fd_dir), de->d_name, &fdname); if (r < 0) return r; - fprintf(stream, "%s%s:%s\n", fddelim, dent->d_name, fdname); + fprintf(stream, "%s%s:%s\n", fddelim, de->d_name, fdname); fddelim = "\n"; /* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */ - fd = openat(proc_fdinfo_fd, dent->d_name, O_NOFOLLOW|O_CLOEXEC|O_RDONLY); + fd = openat(proc_fdinfo_fd, de->d_name, O_NOFOLLOW|O_CLOEXEC|O_RDONLY); if (fd < 0) continue; diff --git a/src/home/homed-manager.c b/src/home/homed-manager.c index d49fcedb57..215b4567a9 100644 --- a/src/home/homed-manager.c +++ b/src/home/homed-manager.c @@ -447,7 +447,6 @@ unlink_this_file: static int manager_enumerate_records(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; assert(m); @@ -919,7 +918,6 @@ static int manager_assess_image( int manager_enumerate_images(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; assert(m); diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c index d94b1df02d..19f0799914 100644 --- a/src/journal/journald-stream.c +++ b/src/journal/journald-stream.c @@ -848,7 +848,6 @@ static int stdout_stream_restore(Server *s, const char *fname, int fd) { int server_restore_streams(Server *s, FDSet *fds) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; const char *path; int r; diff --git a/src/login/logind.c b/src/login/logind.c index 57a8604b7f..52b1d95034 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -244,7 +244,6 @@ static int manager_enumerate_buttons(Manager *m) { static int manager_enumerate_seats(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; assert(m); @@ -286,7 +285,6 @@ static int manager_enumerate_seats(Manager *m) { static int manager_enumerate_linger_users(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; assert(m); @@ -315,7 +313,6 @@ static int manager_enumerate_linger_users(Manager *m) { static int manager_enumerate_users(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r, k; assert(m); @@ -474,7 +471,6 @@ static int manager_attach_fds(Manager *m) { static int manager_enumerate_sessions(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0, k; assert(m); @@ -516,7 +512,6 @@ static int manager_enumerate_sessions(Manager *m) { static int manager_enumerate_inhibitors(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; assert(m); diff --git a/src/machine/machined.c b/src/machine/machined.c index 6ffa4191ec..a433afb1a6 100644 --- a/src/machine/machined.c +++ b/src/machine/machined.c @@ -136,7 +136,6 @@ static int manager_add_host_machine(Manager *m) { static int manager_enumerate_machines(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r; assert(m); diff --git a/src/mount/mount-tool.c b/src/mount/mount-tool.c index 412ef7813a..b0ea45debc 100644 --- a/src/mount/mount-tool.c +++ b/src/mount/mount-tool.c @@ -777,7 +777,6 @@ static int find_mount_points(const char *what, char ***list) { static int find_loop_device(const char *backing_file, char **loop_dev) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; _cleanup_free_ char *l = NULL; assert(backing_file); diff --git a/src/portable/portable.c b/src/portable/portable.c index 8b088f3e32..be311f94c4 100644 --- a/src/portable/portable.c +++ b/src/portable/portable.c @@ -244,7 +244,6 @@ static int extract_now( STRV_FOREACH(i, paths.search_path) { _cleanup_free_ char *resolved = NULL; _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; r = chase_symlinks_and_opendir(*i, where, 0, &resolved, &d); if (r < 0) { @@ -1492,7 +1491,6 @@ int portable_detach( _cleanup_set_free_ Set *unit_files = NULL, *markers = NULL; _cleanup_closedir_ DIR *d = NULL; const char *where, *item; - struct dirent *de; int ret = 0; int r; @@ -1662,7 +1660,6 @@ static int portable_get_state_internal( _cleanup_set_free_ Set *unit_files = NULL; _cleanup_closedir_ DIR *d = NULL; const char *where; - struct dirent *de; int r; assert(name_or_path); diff --git a/src/pstore/pstore.c b/src/pstore/pstore.c index 81538d9cb3..d820ceb9f6 100644 --- a/src/pstore/pstore.c +++ b/src/pstore/pstore.c @@ -323,7 +323,6 @@ static void process_dmesg_files(PStoreList *list) { static int list_files(PStoreList *list, const char *sourcepath) { _cleanup_(closedirp) DIR *dirp = NULL; - struct dirent *de; int r; dirp = opendir(sourcepath); diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index 298f3df3c7..0076092c2a 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -488,7 +488,6 @@ static int boot_entries_find_unified( size_t *n_entries) { _cleanup_(closedirp) DIR *d = NULL; - struct dirent *de; int r; assert(root); diff --git a/src/shared/devnode-acl.c b/src/shared/devnode-acl.c index 9ea89ee920..89ff566832 100644 --- a/src/shared/devnode-acl.c +++ b/src/shared/devnode-acl.c @@ -166,7 +166,6 @@ int devnode_acl_all(const char *seat, _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; _cleanup_set_free_free_ Set *nodes = NULL; _cleanup_closedir_ DIR *dir = NULL; - struct dirent *dent; sd_device *d; char *n; int r; @@ -218,11 +217,11 @@ int devnode_acl_all(const char *seat, * these devices are not known to the kernel at this moment */ dir = opendir("/run/udev/static_node-tags/uaccess"); if (dir) { - FOREACH_DIRENT(dent, dir, return -errno) { + FOREACH_DIRENT(de, dir, return -errno) { _cleanup_free_ char *unescaped_devname = NULL; ssize_t l; - l = cunescape(dent->d_name, UNESCAPE_RELAX, &unescaped_devname); + l = cunescape(de->d_name, UNESCAPE_RELAX, &unescaped_devname); if (l < 0) return l; diff --git a/src/shared/efi-loader.c b/src/shared/efi-loader.c index f5076fc02c..b14a2c32aa 100644 --- a/src/shared/efi-loader.c +++ b/src/shared/efi-loader.c @@ -485,7 +485,6 @@ static int cmp_uint16(const uint16_t *a, const uint16_t *b) { int efi_get_boot_options(uint16_t **options) { _cleanup_closedir_ DIR *dir = NULL; _cleanup_free_ uint16_t *list = NULL; - struct dirent *de; int count = 0; assert(options); diff --git a/src/shared/fdset.c b/src/shared/fdset.c index 679e4aad22..183fa239b6 100644 --- a/src/shared/fdset.c +++ b/src/shared/fdset.c @@ -112,7 +112,6 @@ int fdset_remove(FDSet *s, int fd) { int fdset_new_fill(FDSet **_s) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; FDSet *s; diff --git a/src/shared/install.c b/src/shared/install.c index 6e77a72bde..8f1af755fa 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -549,7 +549,6 @@ static int remove_marked_symlinks_fd( size_t *n_changes) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; assert(remove_symlinks_to); @@ -729,7 +728,6 @@ static int find_symlinks_in_directory( const char *config_path, bool *same_name_link) { - struct dirent *de; int r = 0; FOREACH_DIRENT(de, dir, return -errno) { @@ -814,7 +812,6 @@ static int find_symlinks( bool *same_name_link) { _cleanup_closedir_ DIR *config_dir = NULL; - struct dirent *de; int r = 0; assert(i); @@ -3369,7 +3366,6 @@ int unit_file_preset_all( STRV_FOREACH(i, paths.search_path) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; d = opendir(*i); if (!d) { @@ -3434,7 +3430,6 @@ int unit_file_get_list( STRV_FOREACH(dirname, paths.search_path) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; d = opendir(*dirname); if (!d) { diff --git a/src/shared/numa-util.c b/src/shared/numa-util.c index 644b0bde5b..4f757f0b55 100644 --- a/src/shared/numa-util.c +++ b/src/shared/numa-util.c @@ -128,7 +128,6 @@ int numa_to_cpu_set(const NUMAPolicy *policy, CPUSet *ret) { static int numa_max_node(void) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r, max_node = 0; d = opendir("/sys/devices/system/node"); diff --git a/src/shared/userdb.c b/src/shared/userdb.c index 5c2b3ddb8f..0eddd382e6 100644 --- a/src/shared/userdb.c +++ b/src/shared/userdb.c @@ -405,7 +405,6 @@ static int userdb_start_query( _cleanup_(strv_freep) char **except = NULL, **only = NULL; _cleanup_(closedirp) DIR *d = NULL; - struct dirent *de; const char *e; int r, ret = 0; diff --git a/src/sysv-generator/sysv-generator.c b/src/sysv-generator/sysv-generator.c index 1947664457..e9976540b5 100644 --- a/src/sysv-generator/sysv-generator.c +++ b/src/sysv-generator/sysv-generator.c @@ -718,7 +718,6 @@ static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) { STRV_FOREACH(path, sysvinit_path) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; d = opendir(*path); if (!d) { @@ -805,7 +804,6 @@ static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_servic for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i ++) { _cleanup_closedir_ DIR *d = NULL; _cleanup_free_ char *path = NULL; - struct dirent *de; path = path_join(*p, rcnd_table[i].path); if (!path) { diff --git a/src/test/test-cgroup-util.c b/src/test/test-cgroup-util.c index 621e68829e..93b114b3fd 100644 --- a/src/test/test-cgroup-util.c +++ b/src/test/test-cgroup-util.c @@ -167,7 +167,6 @@ TEST(get_paths, .sd_booted = true) { TEST(proc) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r; d = opendir("/proc"); diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index ab093a7457..06a640b1cc 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -135,7 +135,6 @@ static void test_get_process_cmdline_one(pid_t pid) { TEST(get_process_cmdline) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; assert_se(d = opendir("/proc")); diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c index 56110ba375..54a03af082 100644 --- a/src/tty-ask-password-agent/tty-ask-password-agent.c +++ b/src/tty-ask-password-agent/tty-ask-password-agent.c @@ -295,7 +295,6 @@ static int wall_tty_block(void) { static int process_password_files(void) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; d = opendir("/run/systemd/ask-password"); diff --git a/src/userdb/userdbctl.c b/src/userdb/userdbctl.c index 4dac6b0e44..c2acd85742 100644 --- a/src/userdb/userdbctl.c +++ b/src/userdb/userdbctl.c @@ -518,7 +518,6 @@ static int display_memberships(int argc, char *argv[], void *userdata) { static int display_services(int argc, char *argv[], void *userdata) { _cleanup_(table_unrefp) Table *t = NULL; _cleanup_(closedirp) DIR *d = NULL; - struct dirent *de; int r; d = opendir("/run/systemd/userdb/"); diff --git a/src/xdg-autostart-generator/xdg-autostart-generator.c b/src/xdg-autostart-generator/xdg-autostart-generator.c index 7b441a9a12..c5c6b54fdc 100644 --- a/src/xdg-autostart-generator/xdg-autostart-generator.c +++ b/src/xdg-autostart-generator/xdg-autostart-generator.c @@ -43,7 +43,6 @@ static int enumerate_xdg_autostart(Hashmap *all_services) { STRV_FOREACH(path, autostart_dirs) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; d = opendir(*path); if (!d) {