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