1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2011 GraÅžvydas Ignotas
7 * This test checks if no duplicate d_off values are returned and
8 * that these offsets are seekable to entry with the right inode.
17 #include <sys/syscall.h>
19 struct linux_dirent64 {
22 unsigned short d_reclen;
28 #define HISTORY_LEN 1024
30 static uint64_t d_off_history[HISTORY_LEN];
31 static uint64_t d_ino_history[HISTORY_LEN];
35 fprintf(stderr, "usage: t_dir_offset2: <dir> [bufsize]\n");
39 int main(int argc, char *argv[])
43 int nread, bufsize = BUF_SIZE;
44 struct linux_dirent64 *d;
47 int retval = EXIT_SUCCESS;
50 bufsize = atoi(argv[2]);
53 if (bufsize > BUF_SIZE)
55 } else if (argc < 2) {
59 fd = open(argv[1], O_RDONLY | O_DIRECTORY);
67 nread = syscall(SYS_getdents64, fd, buf, bufsize);
76 for (bpos = 0; bpos < nread; total++) {
77 d = (struct linux_dirent64 *) (buf + bpos);
79 if (total >= HISTORY_LEN) {
80 fprintf(stderr, "too many files\n");
84 for (i = 0; i < total; i++)
86 if (d_off_history[i] == d->d_off) {
87 fprintf(stderr, "entries %d and %d have duplicate d_off %lld\n",
88 i, total, (long long int)d->d_off);
89 retval = EXIT_FAILURE;
92 d_off_history[total] = d->d_off;
93 d_ino_history[total] = d->d_ino;
98 /* check if seek works correctly */
99 d = (struct linux_dirent64 *)buf;
100 for (i = total - 1; i >= 0; i--)
102 lret = lseek(fd, i > 0 ? d_off_history[i - 1] : 0, SEEK_SET);
108 nread = syscall(SYS_getdents64, fd, buf, bufsize);
115 fprintf(stderr, "getdents returned 0 on entry %d\n", i);
116 retval = EXIT_FAILURE;
119 if (d->d_ino != d_ino_history[i]) {
120 fprintf(stderr, "entry %d has inode %lld, expected %lld\n",
121 i, (long long int)d->d_ino, (long long int)d_ino_history[i]);
122 retval = EXIT_FAILURE;