src/t_dir_offset2: Add an option to limit of buffer size
authorAmir Goldstein <amir73il@gmail.com>
Sun, 25 Apr 2021 07:14:41 +0000 (10:14 +0300)
committerEryu Guan <guaneryu@gmail.com>
Sun, 16 May 2021 14:37:06 +0000 (22:37 +0800)
Will be used to force readdir in several getdents calls.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Reviewed-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: Eryu Guan <guaneryu@gmail.com>
src/t_dir_offset2.c

index 5a6d719..7aeb990 100644 (file)
@@ -30,16 +30,32 @@ struct linux_dirent64 {
 static uint64_t d_off_history[HISTORY_LEN];
 static uint64_t d_ino_history[HISTORY_LEN];
 
-int
-main(int argc, char *argv[])
+void usage()
 {
-       int fd, nread;
+       fprintf(stderr, "usage: t_dir_offset2: <dir> [bufsize]\n");
+       exit(EXIT_FAILURE);
+}
+
+int main(int argc, char *argv[])
+{
+       int fd;
        char buf[BUF_SIZE];
+       int nread, bufsize = BUF_SIZE;
        struct linux_dirent64 *d;
        int bpos, total, i;
        off_t lret;
        int retval = EXIT_SUCCESS;
 
+       if (argc > 2) {
+               bufsize = atoi(argv[2]);
+               if (!bufsize)
+                       usage();
+               if (bufsize > BUF_SIZE)
+                       bufsize = BUF_SIZE;
+       } else if (argc < 2) {
+               usage();
+       }
+
        fd = open(argv[1], O_RDONLY | O_DIRECTORY);
        if (fd < 0) {
                perror("open");
@@ -48,7 +64,7 @@ main(int argc, char *argv[])
 
        total = 0;
        for ( ; ; ) {
-               nread = syscall(SYS_getdents64, fd, buf, BUF_SIZE);
+               nread = syscall(SYS_getdents64, fd, buf, bufsize);
                if (nread == -1) {
                        perror("getdents");
                        exit(EXIT_FAILURE);
@@ -89,7 +105,7 @@ main(int argc, char *argv[])
                        exit(EXIT_FAILURE);
                }
 
-               nread = syscall(SYS_getdents64, fd, buf, BUF_SIZE);
+               nread = syscall(SYS_getdents64, fd, buf, bufsize);
                if (nread == -1) {
                        perror("getdents");
                        exit(EXIT_FAILURE);