Whamcloud - gitweb
Land b1_8_gate onto b1_8 (20081218_1708)
[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
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 #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
130                         for (tmp = strtok(cloned_flags, ":|"); tmp;
131                              tmp = strtok(NULL, ":|")) {
132                                 int i = 0;
133 #ifdef DEBUG
134                                 printf("flags = %s\n",tmp);
135 #endif
136                                 flag_set = 1;
137                                 for (i = 0; flag_table[i].flag != -1; i++) {
138                                         if (!strcmp(tmp, flag_table[i].string)){
139                                                 flags |= flag_table[i].flag;
140                                                 break;
141                                         }
142                                 }
143
144                                 if (flag_table[i].flag == -1) {
145                                         fprintf(stderr, "No such flag: %s\n",
146                                                 tmp);
147                                         save_errno = -1;
148                                         goto out;
149                                 }
150                         }
151 #ifdef DEBUG
152                         printf("flags = %x\n", flags);
153 #endif
154                         break;
155                 }
156                 case 'm':
157 #ifdef DEBUG
158                         printf("mode = %s\n", optarg);
159 #endif
160                         mode = strtol(optarg, NULL, 8);
161                         mode_set = 1;
162 #ifdef DEBUG
163                         printf("mode = %o\n", mode);
164 #endif
165                         break;
166                 default:
167                         fprintf(stderr, "Bad parameters.\n");
168                         print_usage = 1;
169                         goto out;
170                 }
171         }
172
173         if (optind == argc) {
174                 fprintf(stderr, "Bad parameters.\n");
175                 print_usage = 1;
176                 goto out;
177         }
178
179         fname = argv[optind];
180
181         if (!flag_set) {
182                 fprintf(stderr, "Missing flag or file-name\n");
183                 save_errno = -1;
184                 goto out;
185         }
186
187
188         if (flags & O_CREAT)
189                 fd = open(fname, flags, mode);
190         else
191                 fd = open(fname, flags);
192
193         save_errno = errno;
194
195         if (fd != -1) {
196                 printf("Succeed in opening file \"%s\"(flags=%s",
197                        fname, cloned_flags);
198
199                 if (mode_set)
200                         printf(", mode=%o", mode);
201                 printf(")\n");
202                 close(fd);
203         } else {
204                 fprintf(stderr, "Error in opening file \"%s\"(flags=%s",
205                         fname, cloned_flags);
206                 if (mode_set)
207                         fprintf(stderr, ", mode=%o", mode);
208                 fprintf(stderr, ") %d: %s\n", save_errno, strerror(save_errno));
209         }
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 }