Whamcloud - gitweb
b=21670 use int instead of char to hold getopt rc in accordance with its prototype
[fs/lustre-release.git] / lustre / tests / iopentest1.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <limits.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <libgen.h>
46 #include <errno.h>
47
48 const char *progname;
49 const char usage_fmt[] = "Usage: %s <file> <mountpoint>\n";
50 #define INAME_LEN (PATH_MAX + 1)
51
52 #define CHECK_IT(exp, pstr) \
53 if (!(exp)) { \
54     fprintf(stderr, "%s: at %s:%d: ", progname, __FILE__, __LINE__); \
55     perror((pstr)); \
56     exit(1); \
57 }
58
59 #define CHECK_SNPRINTF(rc, len) \
60     CHECK_IT((rc) > 0 && (rc) <= (len), "snprintf() failed")
61
62 static char *get_iname(char *fname, const char *mtpt)
63 {
64         char *iname;
65         int fd, rc;
66         struct stat buf;
67
68         iname = malloc(INAME_LEN);
69         CHECK_IT(iname, "malloc() failed");
70
71         fd = open(fname, O_CREAT, 0644);
72         if (fd < 0 && errno != EISDIR) {
73                 fprintf(stderr, "%s:%d: open(%s) failed: %s\n", __FILE__,
74                         __LINE__, fname, strerror(errno));
75                 exit(1);
76         }
77
78         if (fd >= 0)
79                 close(fd);
80
81         rc = stat(fname, &buf);
82         if (rc != 0) {
83                 fprintf(stderr, "%s:%d: stat(%s) failed: %s\n", __FILE__,
84                         __LINE__, fname, strerror(errno));
85                 exit(1);
86         }
87
88         rc = snprintf(iname, INAME_LEN,
89                       "%s/__iopen__/%lu", mtpt, (unsigned long)buf.st_ino);
90         CHECK_SNPRINTF(rc, INAME_LEN);
91
92         return iname;
93 }
94
95 int main(int argc, char *argv[])
96 {
97         char *fname, *mtpt, *iname, *pname;
98         struct stat buf;
99         int rc;
100         int i;
101
102         pname = strdup(argv[0]);
103         progname = basename(pname);
104         
105         if (argc != 3) {
106                 fprintf(stderr, usage_fmt, progname);
107                 return 1;
108         }
109
110         fname = argv[1];
111         mtpt  = argv[2];
112
113         iname = get_iname(fname, mtpt);
114         printf("%s:started...\n", argv[0]);
115         for (i = 0; i < 10000; i++) {
116                 rc = stat(fname, &buf);
117                 if (rc != 0) {
118                         fprintf(stderr, "%s:%d: stat(%s) failed: %s\n",
119                                 __FILE__, __LINE__, fname, strerror(errno));
120                         exit(1);
121                 }
122
123                 rc = stat(iname, &buf);
124                 if (rc != 0) {
125                         fprintf(stderr, "%s:%d: stat(%s) failed: %s\n",
126                                 __FILE__, __LINE__, iname, strerror(errno));
127                         exit(1);
128                 }
129         }
130         printf("%s:finished...\n", argv[0]);
131
132         return 0;
133 }