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
16 changed files with 20 additions and 42 deletions
+16 -4
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;
+4 -5
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; \
-2
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;
-2
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;
-1
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. */