Whamcloud - gitweb
5c1a9ad4bf2371742710d135e09fed530249e0ad
[fs/lustre-release.git] / lustre / tests / openclose.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <errno.h>
8 #include <string.h>
9
10 #ifndef O_DIRECT
11 #define O_DIRECT         040000 /* direct disk access hint */
12 #endif
13
14 int main(int argc, char *argv[])
15 {
16         char *filename;
17         unsigned long count, i;
18         int fd;
19
20         if (argc != 3) {
21                 fprintf(stderr, "usage: %s <filename> <iterations>\n", argv[0]);
22                 exit(1);
23         }
24
25         filename = argv[1];
26         count = strtoul(argv[2], NULL, 0);
27
28         fd = open(filename, O_RDWR|O_CREAT, 0644);
29         if (fd < 0) {
30                 fprintf(stderr, "open(%s, O_CREAT): %s\n", filename,
31                         strerror(errno));
32                 exit(1);
33         }
34         if (close(fd) < 0) {
35                 fprintf(stderr, "close(): %s\n", strerror(errno));
36                 exit(1);
37         }
38
39         for (i = 0; i < count; i++) {
40                 fd = open(filename, O_RDONLY|O_LARGEFILE|O_DIRECT);
41                 if (fd < 0) {
42                         fprintf(stderr, "open(%s, O_RDONLY): %s\n", filename,
43                                 strerror(errno));
44                         exit(1);
45                 }
46                 if (close(fd) < 0) {
47                         fprintf(stderr, "close(): %s\n", strerror(errno));
48                         exit(1);
49                 }
50         }
51         if (unlink(filename) < 0) {
52                 fprintf(stderr, "unlink(%s): %s\n", filename, strerror(errno));
53                 exit(1);
54         }
55         printf("Done.\n");
56         return 0;
57 }