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