1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-03 01:17:45 +03:00

Define FOREACH_DIRENT through FOREACH_DIRENT_ALL

As in the previous commit, 'de' is used as the iterator variable name.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2021-12-07 12:52:58 +01:00
parent c7f0d9e5ac
commit af3b864d76
31 changed files with 18 additions and 69 deletions

View File

@ -30,7 +30,6 @@ static int files_add(
_cleanup_closedir_ DIR *dir = NULL;
const char *dirpath;
struct dirent *de;
int r;
assert(h);

View File

@ -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)

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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");

View File

@ -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;
}

View File

@ -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;

View File

@ -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");

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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) {

View File

@ -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");

View File

@ -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;

View File

@ -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) {

View File

@ -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");

View File

@ -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"));

View File

@ -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");

View File

@ -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/");

View File

@ -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) {