Whamcloud - gitweb
b=1792
[fs/lustre-release.git] / lustre / tests / iopentest1.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <limits.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <libgen.h>
10 #include <errno.h>
11
12 const char *progname;
13 const char usage_fmt[] = "Usage: %s <file> <mountpoint>\n";
14 #define INAME_LEN (PATH_MAX + 1)
15
16 #define CHECK_IT(exp, pstr) \
17 if (!(exp)) { \
18     fprintf(stderr, "%s: at %s:%d: ", progname, __FILE__, __LINE__); \
19     perror((pstr)); \
20     exit(1); \
21 }
22
23 #define CHECK_SNPRINTF(rc, len) \
24     CHECK_IT((rc) > 0 && (rc) <= (len), "snprintf() failed")
25
26 static char *get_iname(char *fname, const char *mtpt)
27 {
28         char *iname;
29         int fd, rc;
30         struct stat buf;
31
32         iname = malloc(INAME_LEN);
33         CHECK_IT(iname, "malloc() failed");
34
35         fd = open(fname, O_CREAT, 0644);
36         CHECK_IT(fd >= 0 || errno == EISDIR, "open(fname) failed");
37
38         if (fd >= 0)
39                 close(fd);
40
41         rc = stat(fname, &buf);
42         CHECK_IT(rc == 0, "stat(fname) failed");
43
44         rc = snprintf(iname, INAME_LEN,
45                       "%s/__iopen__/%lu", mtpt, buf.st_ino);
46         CHECK_SNPRINTF(rc, INAME_LEN);
47
48         return iname;
49 }
50
51 int main(int argc, char *argv[])
52 {
53         char *fname, *mtpt, *iname, *pname;
54         struct stat buf;
55         int rc;
56         int i;
57
58         pname = strdup(argv[0]);
59         progname = basename(pname);
60         
61         if (argc != 3) {
62                 fprintf(stderr, usage_fmt, progname);
63                 return 1;
64         }
65
66         fname = argv[1];
67         mtpt  = argv[2];
68
69         iname = get_iname(fname, mtpt);
70         i=10000;
71         printf("%s:started...\n",argv[0]);
72         do {
73                 rc = stat(fname, &buf);
74                 CHECK_IT(rc == 0, "stat(fname) failed");
75                 
76                 rc = stat(iname, &buf);
77                 CHECK_IT(rc == 0, "stat(iname) failed");
78                 i--;
79         } while (i>=1);
80
81         return 0;
82 }