1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-23 17:34:00 +03:00

Merge pull request #20004 from yuwata/readdir-ensure-type

dirent-util: introduce readdir_ensure_type()
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2021-06-24 15:11:06 +02:00 committed by GitHub
commit 280e4b368e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 20 additions and 42 deletions

View File

@ -7,7 +7,7 @@
#include "path-util.h"
#include "string-util.h"
int dirent_ensure_type(DIR *d, struct dirent *de) {
static int dirent_ensure_type(DIR *d, struct dirent *de) {
struct stat st;
assert(d);
@ -59,11 +59,23 @@ bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
return endswith(de->d_name, suffix);
}
struct dirent* readdir_no_dot(DIR *dirp) {
struct dirent* d;
struct dirent *readdir_ensure_type(DIR *d) {
struct dirent *de;
assert(d);
errno = 0;
de = readdir(d);
if (de)
(void) dirent_ensure_type(d, de);
return de;
}
struct dirent *readdir_no_dot(DIR *dirp) {
struct dirent *d;
for (;;) {
d = readdir(dirp);
d = readdir_ensure_type(dirp);
if (d && dot_or_dot_dot(d->d_name))
continue;
return d;

View File

@ -8,15 +8,14 @@
#include "macro.h"
#include "path-util.h"
int dirent_ensure_type(DIR *d, struct dirent *de);
bool dirent_is_file(const struct dirent *de) _pure_;
bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_;
struct dirent* readdir_no_dot(DIR *dirp);
struct dirent *readdir_ensure_type(DIR *d);
struct dirent *readdir_no_dot(DIR *dirp);
#define FOREACH_DIRENT(de, d, on_error) \
for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
for (de = readdir_ensure_type(d);; de = readdir_ensure_type(d)) \
if (!de) { \
if (errno > 0) { \
on_error; \
@ -27,7 +26,7 @@ struct dirent* readdir_no_dot(DIR *dirp);
else
#define FOREACH_DIRENT_ALL(de, d, on_error) \
for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
for (de = readdir_ensure_type(d);; de = readdir_ensure_type(d)) \
if (!de) { \
if (errno > 0) { \
on_error; \

View File

@ -580,8 +580,6 @@ int get_files_in_directory(const char *path, char ***list) {
return -errno;
FOREACH_DIRENT_ALL(de, d, return -errno) {
dirent_ensure_type(d, de);
if (!dirent_is_file(de))
continue;

View File

@ -169,8 +169,6 @@ static int add_locales_from_libdir (Set *locales) {
FOREACH_DIRENT(entry, dir, return -errno) {
char *z;
dirent_ensure_type(dir, entry);
if (entry->d_type != DT_DIR)
continue;

View File

@ -321,7 +321,6 @@ int unit_file_build_name_map(
if (hashmap_contains(ids, de->d_name))
continue;
dirent_ensure_type(d, de);
if (de->d_type == DT_LNK) {
/* We don't explicitly check for alias loops here. unit_ids_map_get() which
* limits the number of hops should be used to access the map. */

View File

@ -82,7 +82,6 @@ static int write_access2_rules(const char *srcdir) {
FOREACH_DIRENT(entry, dir, return 0) {
_cleanup_fclose_ FILE *policy = NULL;
dirent_ensure_type(dir, entry);
if (!dirent_is_file(entry))
continue;
@ -149,7 +148,6 @@ static int write_cipso2_rules(const char *srcdir) {
FOREACH_DIRENT(entry, dir, return 0) {
_cleanup_fclose_ FILE *policy = NULL;
dirent_ensure_type(dir, entry);
if (!dirent_is_file(entry))
continue;

View File

@ -311,8 +311,6 @@ static int enumerate_dir(
}
FOREACH_DIRENT_ALL(de, d, return -errno) {
dirent_ensure_type(d, de);
if (dropins && de->d_type == DT_DIR && endswith(de->d_name, ".d")) {
if (!GREEDY_REALLOC0(dirs, n_dirs + 2))
return -ENOMEM;

View File

@ -795,8 +795,6 @@ _public_ int sd_get_uids(uid_t **users) {
int k;
uid_t uid;
dirent_ensure_type(d, de);
if (!dirent_is_file(de))
continue;

View File

@ -1374,7 +1374,6 @@ static int flush_devices(Manager *m) {
struct dirent *de;
FOREACH_DIRENT_ALL(de, d, break) {
dirent_ensure_type(d, de);
if (!dirent_is_file(de))
continue;

View File

@ -299,7 +299,6 @@ static int manager_enumerate_linger_users(Manager *m) {
FOREACH_DIRENT(de, d, return -errno) {
int k;
dirent_ensure_type(d, de);
if (!dirent_is_file(de))
continue;

View File

@ -787,8 +787,6 @@ static int find_loop_device(const char *backing_file, char **loop_dev) {
_cleanup_free_ char *sys = NULL, *fname = NULL;
int r;
dirent_ensure_type(d, de);
if (de->d_type != DT_DIR)
continue;

View File

@ -312,7 +312,6 @@ static int extract_now(
if (hashmap_get(unit_files, de->d_name))
continue;
dirent_ensure_type(d, de);
if (!IN_SET(de->d_type, DT_LNK, DT_REG))
continue;
@ -1397,7 +1396,6 @@ int portable_detach(
if (set_contains(unit_files, de->d_name))
continue;
dirent_ensure_type(d, de);
if (!IN_SET(de->d_type, DT_LNK, DT_REG))
continue;
@ -1570,7 +1568,6 @@ static int portable_get_state_internal(
if (set_contains(unit_files, de->d_name))
continue;
dirent_ensure_type(d, de);
if (!IN_SET(de->d_type, DT_LNK, DT_REG))
continue;

View File

@ -482,7 +482,6 @@ static int boot_entries_find_unified(
_cleanup_free_ char *j = NULL, *osrelease = NULL, *cmdline = NULL;
_cleanup_close_ int fd = -1;
dirent_ensure_type(d, de);
if (!dirent_is_file(de))
continue;

View File

@ -568,8 +568,6 @@ static int remove_marked_symlinks_fd(
FOREACH_DIRENT(de, d, return -errno) {
dirent_ensure_type(d, de);
if (de->d_type == DT_DIR) {
_cleanup_free_ char *p = NULL;
int nfd, q;
@ -738,8 +736,6 @@ static int find_symlinks_in_directory(
bool found_path = false, found_dest, b = false;
int q;
dirent_ensure_type(dir, de);
if (de->d_type != DT_LNK)
continue;
@ -836,8 +832,6 @@ static int find_symlinks(
_cleanup_free_ const char *path = NULL;
_cleanup_closedir_ DIR *d = NULL;
dirent_ensure_type(config_dir, de);
if (de->d_type != DT_DIR)
continue;
@ -3379,8 +3373,6 @@ int unit_file_preset_all(
if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY))
continue;
dirent_ensure_type(d, de);
if (!IN_SET(de->d_type, DT_LNK, DT_REG))
continue;
@ -3457,8 +3449,6 @@ int unit_file_get_list(
if (hashmap_get(h, de->d_name))
continue;
dirent_ensure_type(d, de);
if (!IN_SET(de->d_type, DT_LNK, DT_REG))
continue;

View File

@ -139,8 +139,6 @@ static int numa_max_node(void) {
int node;
const char *n;
(void) dirent_ensure_type(d, de);
if (de->d_type != DT_DIR)
continue;

View File

@ -126,8 +126,6 @@ static void test_get_process_cmdline(void) {
FOREACH_DIRENT(de, d, return) {
pid_t pid;
dirent_ensure_type(d, de);
if (de->d_type != DT_DIR)
continue;