Whamcloud - gitweb
LU-1625 test: reduce test duration for nfs mode
[fs/lustre-release.git] / lustre / tests / openfile.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #if 0
36 #define DEBUG
37 #endif
38
39 /* for O_DIRECTORY and O_DIRECT */
40 #ifndef _GNU_SOURCE
41 #define _GNU_SOURCE
42 #endif
43
44 #include <stdio.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <string.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <libcfs/libcfs.h>
53 #include <lustre/lustre_user.h>
54
55 typedef struct flag_mapping {
56        const char *string;
57        const int  flag;
58 } FLAG_MAPPING;
59
60 FLAG_MAPPING flag_table[] = {
61        {"O_RDONLY", O_RDONLY},
62        {"O_WRONLY", O_WRONLY},
63        {"O_RDWR", O_RDWR},
64        {"O_CREAT", O_CREAT},
65        {"O_EXCL", O_EXCL},
66        {"O_NOCTTY", O_NOCTTY},
67        {"O_TRUNC", O_TRUNC},
68        {"O_APPEND", O_APPEND},
69        {"O_NONBLOCK", O_NONBLOCK},
70        {"O_NDELAY", O_NDELAY},
71        {"O_SYNC", O_SYNC},
72 #ifdef O_DIRECT
73        {"O_DIRECT", O_DIRECT},
74 #endif
75        {"O_LARGEFILE", O_LARGEFILE},
76        {"O_DIRECTORY", O_DIRECTORY},
77        {"O_NOFOLLOW", O_NOFOLLOW},
78        {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
79        {"", -1}
80 };
81
82 void Usage_and_abort(void)
83 {
84        fprintf(stderr, "Usage: openfile -f flags [ -m mode ] filename \n");
85        fprintf(stderr, "e.g. openfile -f O_RDWR:O_CREAT -m 0755 /etc/passwd\n");
86        exit(-1);
87 }
88
89 int main(int argc, char** argv)
90 {
91         int    fd;
92         int    flags = 0;
93         mode_t mode = 0644;
94         char  *fname = NULL;
95         int    mode_set = 0;
96         int    flag_set = 0;
97         int    c;
98         int    save_errno = 0;
99         int    print_usage = 0;
100         char  *cloned_flags = NULL;
101
102         if (argc == 1)
103                 Usage_and_abort();
104
105         while ((c = getopt (argc, argv, "f:m:")) != -1) {
106                 switch (c) {
107                 case 'f': {
108                         char *tmp;
109
110                         cloned_flags = (char *)malloc(strlen(optarg)+1);
111                         if (cloned_flags == NULL) {
112                                 fprintf(stderr, "Insufficient memory.\n");
113                                 save_errno = -1;
114                                 goto out;
115                         }
116
117                         strncpy(cloned_flags, optarg, strlen(optarg)+1);
118                         flags = atoi(cloned_flags);
119                         if (flags > 0) {
120                                 flag_set = 1;
121 #ifdef DEBUG
122                                 printf("flags = %d\n",flags);
123 #endif
124                                 break;
125                         } else 
126                                 flags = 0;
127
128                         for (tmp = strtok(cloned_flags, ":|"); tmp;
129                              tmp = strtok(NULL, ":|")) {
130                                 int i = 0;
131 #ifdef DEBUG
132                                 printf("flags = %s\n",tmp);
133 #endif
134                                 flag_set = 1;
135                                 for (i = 0; flag_table[i].flag != -1; i++) {
136                                         if (!strcmp(tmp, flag_table[i].string)){
137                                                 flags |= flag_table[i].flag;
138                                                 break;
139                                         }
140                                 }
141
142                                 if (flag_table[i].flag == -1) {
143                                         fprintf(stderr, "No such flag: %s\n",
144                                                 tmp);
145                                         save_errno = -1;
146                                         goto out;
147                                 }
148                         }
149 #ifdef DEBUG
150                         printf("flags = %x\n", flags);
151 #endif
152                         break;
153                 }
154                 case 'm':
155 #ifdef DEBUG
156                         printf("mode = %s\n", optarg);
157 #endif
158                         mode = strtol(optarg, NULL, 8);
159                         mode_set = 1;
160 #ifdef DEBUG
161                         printf("mode = %o\n", mode);
162 #endif
163                         break;
164                 default:
165                         fprintf(stderr, "Bad parameters.\n");
166                         print_usage = 1;
167                         goto out;
168                 }
169         }
170
171         if (optind == argc) {
172                 fprintf(stderr, "Bad parameters.\n");
173                 print_usage = 1;
174                 goto out;
175         }
176
177         fname = argv[optind];
178
179         if (!flag_set) {
180                 fprintf(stderr, "Missing flag or file-name\n");
181                 save_errno = -1;
182                 goto out;
183         }
184
185
186         if (flags & O_CREAT)
187                 fd = open(fname, flags, mode);
188         else
189                 fd = open(fname, flags);
190
191         save_errno = errno;
192
193         if (fd != -1) {
194                 printf("Succeed in opening file \"%s\"(flags=%s",
195                        fname, cloned_flags);
196
197                 if (mode_set)
198                         printf(", mode=%o", mode);
199                 printf(")\n");
200                 close(fd);
201         } else {
202                 fprintf(stderr, "Error in opening file \"%s\"(flags=%s",
203                         fname, cloned_flags);
204                 if (mode_set)
205                         fprintf(stderr, ", mode=%o", mode);
206                 fprintf(stderr, ") %d: %s\n", save_errno, strerror(save_errno));
207         }
208
209 out:
210         if (cloned_flags)
211                 free(cloned_flags);
212         if (print_usage)
213                 Usage_and_abort();
214
215         return save_errno;
216 }