Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / tests / openfile.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 [sun.com URL with a
20  * copy of GPLv2].
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 #if 0
38 #define DEBUG
39 #endif
40
41 /* for O_DIRECTORY and O_DIRECT */
42 #ifndef _GNU_SOURCE
43 #define _GNU_SOURCE
44 #endif
45
46 #include <stdio.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <fcntl.h>
50 #include <errno.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <lustre/lustre_user.h>
55
56 typedef struct flag_mapping {
57        const char *string;
58        const int  flag;
59 } FLAG_MAPPING;
60
61 FLAG_MAPPING flag_table[] = {
62        {"O_RDONLY", O_RDONLY},
63        {"O_WRONLY", O_WRONLY},
64        {"O_RDWR", O_RDWR},
65        {"O_CREAT", O_CREAT},
66        {"O_EXCL", O_EXCL},
67        {"O_NOCTTY", O_NOCTTY},
68        {"O_TRUNC", O_TRUNC},
69        {"O_APPEND", O_APPEND},
70        {"O_NONBLOCK", O_NONBLOCK},
71        {"O_NDELAY", O_NDELAY},
72        {"O_SYNC", O_SYNC},
73 #ifdef O_DIRECT
74        {"O_DIRECT", O_DIRECT},
75 #endif
76        {"O_LARGEFILE", O_LARGEFILE},
77        {"O_DIRECTORY", O_DIRECTORY},
78        {"O_NOFOLLOW", O_NOFOLLOW},
79        {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
80        {"", -1}
81 };
82
83 void Usage_and_abort(void)
84 {
85        fprintf(stderr, "Usage: openfile -f flags [ -m mode ] filename \n");
86        fprintf(stderr, "e.g. openfile -f O_RDWR:O_CREAT -m 0755 /etc/passwd\n");
87        exit(-1);
88 }
89
90 int main(int argc, char** argv)
91 {
92         int    fd;
93         int    flags = 0;
94         mode_t mode = 0644;
95         char*  fname = NULL;
96         int    mode_set = 0;
97         int    flag_set = 0;
98         int    c;
99         int    save_errno = 0;
100         int    print_usage = 0;
101         char*  cloned_flags = NULL;
102
103         if (argc == 1)
104                 Usage_and_abort();
105
106         while ((c = getopt (argc, argv, "f:m:")) != -1) {
107                 switch (c) {
108                 case 'f': {
109                         char *tmp;
110
111                         cloned_flags = (char *)malloc(strlen(optarg)+1);
112                         if (cloned_flags == NULL) {
113                                 fprintf(stderr, "Insufficient memory.\n");
114                                 save_errno = -1;
115                                 goto out;
116                         }
117
118                         strncpy(cloned_flags, optarg, strlen(optarg)+1);
119                         flags = atoi(cloned_flags);
120                         if (flags > 0) {
121                                 flag_set = 1;
122 #ifdef DEBUG
123                                 printf("flags = %d\n",flags);
124 #endif
125                                 break;
126                         } else 
127                                 flags = 0;
128                         
129                         for (tmp = strtok(cloned_flags, ":|"); tmp;
130                              tmp = strtok(NULL, ":|")) {
131                                 int i = 0;
132 #ifdef DEBUG
133                                 printf("flags = %s\n",tmp);
134 #endif
135                                 flag_set = 1;
136                                 for (i = 0; flag_table[i].flag != -1; i++) {
137                                         if (!strcmp(tmp, flag_table[i].string)){
138                                                 flags |= flag_table[i].flag;
139                                                 break;
140                                         }
141                                 }
142
143                                 if (flag_table[i].flag == -1) {
144                                         fprintf(stderr, "No such flag: %s\n",
145                                                 tmp);
146                                         save_errno = -1;
147                                         goto out;
148                                 }
149                         }
150 #ifdef DEBUG
151                         printf("flags = %x\n", flags);
152 #endif
153                         break;
154                 }
155                 case 'm':
156 #ifdef DEBUG
157                         printf("mode = %s\n", optarg);
158 #endif
159                         mode = strtol(optarg, NULL, 8);
160                         mode_set = 1;
161 #ifdef DEBUG
162                         printf("mode = %o\n", mode);
163 #endif
164                         break;
165                 default:
166                         fprintf(stderr, "Bad parameters.\n");
167                         print_usage = 1;
168                         goto out;
169                 }
170         }
171
172         if (optind == argc) {
173                 fprintf(stderr, "Bad parameters.\n");
174                 print_usage = 1;
175                 goto out;
176         }
177
178         fname = argv[optind];
179
180         if (!flag_set) {
181                 fprintf(stderr, "Missing flag or file-name\n");
182                 save_errno = -1;
183                 goto out;
184         }
185
186
187         if (flags & O_CREAT)
188                 fd = open(fname, flags, mode);
189         else
190                 fd = open(fname, flags);
191
192         save_errno = errno;
193
194         if (fd != -1) {
195                 printf("Succeed in opening file \"%s\"(flags=%s",
196                        fname, cloned_flags);
197
198                 if (mode_set)
199                         printf(", mode=%o", mode);
200                 printf(")\n");
201                 close(fd);
202         } else {
203                 fprintf(stderr, "Error in opening file \"%s\"(flags=%s",
204                         fname, cloned_flags);
205         }
206
207         if (mode_set)
208                 fprintf(stderr, ", mode=%o", mode);
209         fprintf(stderr, ") %d: %s\n", save_errno, strerror(save_errno));
210
211 out:
212         if (cloned_flags)
213                 free(cloned_flags);
214         if (print_usage)
215                 Usage_and_abort();
216
217         return save_errno;
218 }
219