1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <ftw.h>
#include "fd-util.h"
#include "log.h"
#include "missing_magic.h"
#include "recurse-dir.h"
#include "strv.h"
#include "tests.h"
static char **list_nftw = NULL;
static int nftw_cb(
const char *fpath,
const struct stat *sb,
int typeflag,
struct FTW *ftwbuf) {
if (ftwbuf->level == 0) /* skip top-level */
return FTW_CONTINUE;
switch (typeflag) {
case FTW_F:
log_debug("ftw found %s", fpath);
assert_se(strv_extend(&list_nftw, fpath) >= 0);
break;
case FTW_SL:
log_debug("ftw found symlink %s, ignoring.", fpath);
break;
case FTW_D:
log_debug("ftw entering %s", fpath);
assert_se(strv_extendf(&list_nftw, "%s/", fpath) >= 0);
break;
case FTW_DNR:
log_debug("ftw open directory failed %s", fpath);
break;
case FTW_NS:
log_debug("ftw stat inode failed %s", fpath);
break;
case FTW_DP:
case FTW_SLN:
default:
assert_not_reached();
}
return FTW_CONTINUE;
}
static int recurse_dir_callback(
RecurseDirEvent event,
const char *path,
int dir_fd,
int inode_fd,
const struct dirent *de,
const struct statx *sx,
void *userdata) {
char ***l = userdata;
assert_se(path);
assert_se(de);
switch (event) {
case RECURSE_DIR_ENTRY:
assert_se(!IN_SET(de->d_type, DT_UNKNOWN, DT_DIR));
log_debug("found %s%s", path,
de->d_type == DT_LNK ? ", ignoring." : "");
if (de->d_type != DT_LNK)
assert_se(strv_extend(l, path) >= 0);
break;
case RECURSE_DIR_ENTER:
assert_se(de->d_type == DT_DIR);
log_debug("entering %s", path);
assert_se(strv_extendf(l, "%s/", path) >= 0);
break;
case RECURSE_DIR_LEAVE:
log_debug("leaving %s", path);
break;
case RECURSE_DIR_SKIP_MOUNT:
log_debug("skipping mount %s", path);
break;
case RECURSE_DIR_SKIP_DEPTH:
log_debug("skipping depth %s", path);
break;
case RECURSE_DIR_SKIP_OPEN_DIR_ERROR_BASE...RECURSE_DIR_SKIP_OPEN_DIR_ERROR_MAX:
log_debug_errno(event - RECURSE_DIR_SKIP_OPEN_DIR_ERROR_BASE, "failed to open dir %s: %m", path);
break;
case RECURSE_DIR_SKIP_OPEN_INODE_ERROR_BASE...RECURSE_DIR_SKIP_OPEN_INODE_ERROR_MAX:
log_debug_errno(event - RECURSE_DIR_SKIP_OPEN_INODE_ERROR_BASE, "failed to open inode %s: %m", path);
break;
case RECURSE_DIR_SKIP_STAT_INODE_ERROR_BASE...RECURSE_DIR_SKIP_STAT_INODE_ERROR_MAX:
log_debug_errno(event - RECURSE_DIR_SKIP_STAT_INODE_ERROR_BASE, "failed to stat inode %s: %m", path);
break;
default:
assert_not_reached();
}
return RECURSE_DIR_CONTINUE;
}
int main(int argc, char *argv[]) {
_cleanup_strv_free_ char **list_recurse_dir = NULL;
const char *p;
usec_t t1, t2, t3, t4;
_cleanup_close_ int fd = -EBADF;
log_show_color(true);
test_setup_logging(LOG_INFO);
if (argc > 1)
p = argv[1];
else
p = "/usr/share/man"; /* something hopefully reasonably stable while we run (and limited in size) */
fd = open(p, O_DIRECTORY|O_CLOEXEC);
if (fd < 0 && errno == ENOENT)
return log_tests_skipped_errno(errno, "Couldn't open directory %s", p);
assert_se(fd >= 0);
/* If the test directory is on an overlayfs then files and their directory may return different
* st_dev in stat results, which confuses nftw into thinking they're on different filesystems and
* won't return the result when the FTW_MOUNT flag is set. */
if (fd_is_fs_type(fd, OVERLAYFS_SUPER_MAGIC))
return log_tests_skipped("nftw mountpoint detection produces false-positives on overlayfs");
/* Enumerate the specified dirs in full, once via nftw(), and once via recurse_dir(), and ensure the
* results are identical. nftw() sometimes skips symlinks (see
* https://github.com/systemd/systemd/issues/29603), so ignore them to avoid bogus errors. */
t1 = now(CLOCK_MONOTONIC);
assert_se(recurse_dir(fd, p, 0, UINT_MAX, RECURSE_DIR_SORT|RECURSE_DIR_ENSURE_TYPE|RECURSE_DIR_SAME_MOUNT, recurse_dir_callback, &list_recurse_dir) >= 0);
t2 = now(CLOCK_MONOTONIC);
t3 = now(CLOCK_MONOTONIC);
assert_se(nftw(p, nftw_cb, 64, FTW_PHYS|FTW_MOUNT) >= 0);
t4 = now(CLOCK_MONOTONIC);
log_info("recurse_dir(): %s – nftw(): %s", FORMAT_TIMESPAN(t2 - t1, 1), FORMAT_TIMESPAN(t4 - t3, 1));
strv_sort(list_recurse_dir);
strv_sort(list_nftw);
for (size_t i = 0;; i++) {
const char *a = list_nftw ? list_nftw[i] : NULL,
*b = list_recurse_dir ? list_recurse_dir[i] : NULL;
if (!streq_ptr(a, b)) {
log_error("entry %zu different: %s vs %s", i, strna(a), strna(b));
assert_not_reached();
}
if (!a)
break;
}
list_nftw = strv_free(list_nftw);
return 0;
}
|