Whamcloud - gitweb
LU-14876 out: don't connect to busy MDS-MDS export
[fs/lustre-release.git] / lustre / tests / lseek_test.c
1 /* GPL HEADER START
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 only,
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License version 2 for more details (a copy is included
13  * in the LICENSE file that accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License
16  * version 2 along with this program; If not, see
17  * http://www.gnu.org/licenses/gpl-2.0.html
18  *
19  * GPL HEADER END
20  */
21
22 /*
23  * Copyright (c) 2020, Whamcloud.
24  * Author: Mikhail Pershin <mpershin@whamcloud.com>
25  */
26
27 /*
28  * Test does lseek with SEEK_DATA/SEEK_HOLE options on a file and prints result.
29  *
30  * Two input options are '-d|--data' for SEEK_DATA and '-l|--hole' for hole
31  */
32
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <getopt.h>
42
43 char usage[] =
44 "Usage: %s [option] <start> <filename>\n"
45 "       where options are:\n"
46 "       --hole|-l seek first hole offset after given offset\n"
47 "       --data|-d seek first data offset after given offset\n";
48
49 int main(int argc, char **argv)
50 {
51         int c;
52         struct option long_opts[] = {
53                 { .name = "hole", .has_arg = no_argument, .val = 'l' },
54                 { .name = "data", .has_arg = no_argument, .val = 'd' },
55                 { .name = NULL },
56         };
57         int opt = SEEK_HOLE;
58         int fd;
59         off_t cur_off;
60         off_t ret_off;
61
62         optind = 0;
63         while ((c = getopt_long(argc, argv, "ld", long_opts, NULL)) != -1) {
64                 switch (c) {
65                 case 'l':
66                         opt = SEEK_HOLE;
67                         break;
68                 case 'd':
69                         opt = SEEK_DATA;
70                         break;
71                 default:
72                         fprintf(stderr, "error: %s: unknown option '%s'\n",
73                                 argv[0], argv[optind - 1]);
74                 return -1;
75                 }
76         }
77
78         if (argc - optind < 2) {
79                 fprintf(stderr, usage, argv[0]);
80                 return -1;
81         }
82
83         cur_off = atoll(argv[optind]);
84
85         fd = open(argv[optind + 1], O_RDONLY);
86         if (fd < 0) {
87                 fprintf(stderr, "cannot open %s for reading, error %d",
88                         argv[optind + 1], errno);
89                 return -1;
90         }
91
92         ret_off = lseek(fd, cur_off, opt);
93         close(fd);
94
95         if (ret_off < 0) {
96                 fprintf(stderr, "lseek to %jd failed with %d\n",
97                         cur_off, errno);
98                 return ret_off;
99         }
100         printf("%jd\n", ret_off);
101         return 0;
102 }