From 2c1329d636f1a1902a3c6c2102eae244c9e64ca0 Mon Sep 17 00:00:00 2001 From: Amir Goldstein Date: Sun, 25 Apr 2021 10:14:42 +0300 Subject: [PATCH] src/t_dir_offset2: Add an option to find file by name Will be used to check for missing/stale entries. Signed-off-by: Amir Goldstein Reviewed-by: Eryu Guan Signed-off-by: Eryu Guan --- src/t_dir_offset2.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/t_dir_offset2.c b/src/t_dir_offset2.c index 7aeb990e..7af24519 100644 --- a/src/t_dir_offset2.c +++ b/src/t_dir_offset2.c @@ -8,11 +8,13 @@ * that these offsets are seekable to entry with the right inode. */ +#include #include #include #include #include #include +#include #include #include @@ -32,7 +34,7 @@ static uint64_t d_ino_history[HISTORY_LEN]; void usage() { - fprintf(stderr, "usage: t_dir_offset2: [bufsize]\n"); + fprintf(stderr, "usage: t_dir_offset2: [[bufsize] [-v]]\n"); exit(EXIT_FAILURE); } @@ -45,6 +47,9 @@ int main(int argc, char *argv[]) int bpos, total, i; off_t lret; int retval = EXIT_SUCCESS; + const char *filename = NULL; + int exists = 0, found = 0; + int verbose = 0; if (argc > 2) { bufsize = atoi(argv[2]); @@ -52,6 +57,12 @@ int main(int argc, char *argv[]) usage(); if (bufsize > BUF_SIZE) bufsize = BUF_SIZE; + + if (argc > 3) { + filename = argv[3]; + if (argc > 4 && !strcmp(argv[4], "-v")) + verbose = 1; + } } else if (argc < 2) { usage(); } @@ -62,6 +73,14 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } + if (filename) { + exists = !faccessat(fd, filename, F_OK, AT_SYMLINK_NOFOLLOW); + if (!exists && errno != ENOENT) { + perror("faccessat"); + exit(EXIT_FAILURE); + } + } + total = 0; for ( ; ; ) { nread = syscall(SYS_getdents64, fd, buf, bufsize); @@ -91,10 +110,28 @@ int main(int argc, char *argv[]) } d_off_history[total] = d->d_off; d_ino_history[total] = d->d_ino; + if (filename) { + if (verbose) + printf("entry #%d: %s (d_ino=%lld, d_off=%lld)\n", + i, d->d_name, (long long int)d->d_ino, + (long long int)d->d_off); + if (!strcmp(filename, d->d_name)) + found = 1; + } bpos += d->d_reclen; } } + if (filename) { + if (exists == found) { + printf("entry %s %sfound as expected\n", filename, found ? "" : "not "); + } else { + fprintf(stderr, "%s entry %s\n", + exists ? "missing" : "stale", filename); + exit(EXIT_FAILURE); + } + } + /* check if seek works correctly */ d = (struct linux_dirent64 *)buf; for (i = total - 1; i >= 0; i--) -- 2.30.2