Whamcloud - gitweb
b=19123
[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 <libcfs/libcfs.h>
55 #include <lustre/lustre_user.h>
56
57 typedef struct flag_mapping {
58        const char *string;
59        const int  flag;
60 } FLAG_MAPPING;
61
62 FLAG_MAPPING flag_table[] = {
63        {"O_RDONLY", O_RDONLY},
64        {"O_WRONLY", O_WRONLY},
65        {"O_RDWR", O_RDWR},
66        {"O_CREAT", O_CREAT},
67        {"O_EXCL", O_EXCL},
68        {"O_NOCTTY", O_NOCTTY},
69        {"O_TRUNC", O_TRUNC},
70        {"O_APPEND", O_APPEND},
71        {"O_NONBLOCK", O_NONBLOCK},
72        {"O_NDELAY", O_NDELAY},
73        {"O_SYNC", O_SYNC},
74 #ifdef O_DIRECT
75        {"O_DIRECT", O_DIRECT},
76 #endif
77        {"O_LARGEFILE", O_LARGEFILE},
78        {"O_DIRECTORY", O_DIRECTORY},
79        {"O_NOFOLLOW", O_NOFOLLOW},
80        {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
81        {"", -1}
82 };
83
84 void Usage_and_abort(void)
85 {
86        fprintf(stderr, "Usage: openfile -f flags [ -m mode ] filename \n");
87        fprintf(stderr, "e.g. openfile -f O_RDWR:O_CREAT -m 0755 /etc/passwd\n");
88        exit(-1);
89 }
90
91 int main(int argc, char** argv)
92 {
93         int    fd;
94         int    flags = 0;
95         mode_t mode = 0644;
96         char  *fname = NULL;
97         int    mode_set = 0;
98         int    flag_set = 0;
99         int    c;
100         int    save_errno = 0;
101         int    print_usage = 0;
102         char  *cloned_flags = NULL;
103
104         if (argc == 1)
105                 Usage_and_abort();
106
107         while ((c = getopt (argc, argv, "f:m:")) != -1) {
108                 switch (c) {
109                 case 'f': {
110                         char *tmp;
111
112                         cloned_flags = (char *)malloc(strlen(optarg)+1);
113                         if (cloned_flags == NULL) {
114                                 fprintf(stderr, "Insufficient memory.\n");
115                                 save_errno = -1;
116                                 goto out;
117                         }
118
119                         strncpy(cloned_flags, optarg, strlen(optarg)+1);
120                         flags = atoi(cloned_flags);
121                         if (flags > 0) {
122                                 flag_set = 1;
123 #ifdef DEBUG
124                                 printf("flags = %d\n",flags);
125 #endif
126                                 break;
127                         } else 
128                                 flags = 0;
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 }