Whamcloud - gitweb
LU-12511 utils: Move utilies specific values out of Lustre UAPI headers
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  * Lustre is a trademark of Sun Microsystems, Inc.
29  */
30
31 /* for O_DIRECTORY and O_DIRECT */
32 #ifndef _GNU_SOURCE
33 #define _GNU_SOURCE
34 #endif
35
36 #include <stdio.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <linux/lustre/lustre_user.h>
45
46 typedef struct flag_mapping {
47         const char *string;
48         const int  flag;
49 } FLAG_MAPPING;
50
51 FLAG_MAPPING flag_table[] = {
52         {"O_RDONLY", O_RDONLY},
53         {"O_WRONLY", O_WRONLY},
54         {"O_RDWR", O_RDWR},
55         {"O_CREAT", O_CREAT},
56         {"O_EXCL", O_EXCL},
57         {"O_NOCTTY", O_NOCTTY},
58         {"O_TRUNC", O_TRUNC},
59         {"O_APPEND", O_APPEND},
60         {"O_NONBLOCK", O_NONBLOCK},
61         {"O_NDELAY", O_NDELAY},
62         {"O_SYNC", O_SYNC},
63 #ifdef O_DIRECT
64         {"O_DIRECT", O_DIRECT},
65 #endif
66         {"O_LARGEFILE", O_LARGEFILE},
67         {"O_DIRECTORY", O_DIRECTORY},
68         {"O_NOFOLLOW", O_NOFOLLOW},
69         {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
70         {"", -1}
71 };
72
73 void Usage_and_abort(void)
74 {
75         fprintf(stderr, "Usage: openfile -f flags [ -m mode ] filename\n");
76         fprintf(stderr,
77                 "e.g. openfile -f O_RDWR:O_CREAT -m 0755 /etc/passwd\n");
78         exit(-1);
79 }
80
81 int main(int argc, char **argv)
82 {
83         int fd;
84         int flags = 0;
85         mode_t mode = 0644;
86         char *fname = NULL;
87         int mode_set = 0;
88         int flag_set = 0;
89         int c;
90         int save_errno = 0;
91         int print_usage = 0;
92         char *cloned_flags = NULL;
93
94         if (argc == 1)
95                 Usage_and_abort();
96
97         while ((c = getopt(argc, argv, "f:m:")) != -1) {
98                 switch (c) {
99                 case 'f': {
100                         char *tmp;
101
102                         cloned_flags = strdup(optarg);
103                         if (!cloned_flags) {
104                                 fprintf(stderr, "Insufficient memory.\n");
105                                 save_errno = -1;
106                                 goto out;
107                         }
108
109                         flags = atoi(cloned_flags);
110                         if (flags > 0) {
111                                 flag_set = 1;
112 #ifdef DEBUG
113                                 printf("flags = %d\n", flags);
114 #endif
115                                 break;
116                         }
117
118                         flags = 0;
119
120                         for (tmp = strtok(cloned_flags, ":|"); tmp;
121                              tmp = strtok(NULL, ":|")) {
122                                 int i = 0;
123 #ifdef DEBUG
124                                 printf("flags = %s\n", tmp);
125 #endif
126                                 flag_set = 1;
127                                 for (i = 0; flag_table[i].flag != -1; i++) {
128                                         if (!strcmp(tmp,
129                                                     flag_table[i].string)) {
130                                                 flags |= flag_table[i].flag;
131                                                 break;
132                                         }
133                                 }
134
135                                 if (flag_table[i].flag == -1) {
136                                         fprintf(stderr, "No such flag: %s\n",
137                                                 tmp);
138                                         save_errno = -1;
139                                         goto out;
140                                 }
141                         }
142 #ifdef DEBUG
143                         printf("flags = %x\n", flags);
144 #endif
145                         break;
146                 }
147                 case 'm':
148 #ifdef DEBUG
149                         printf("mode = %s\n", optarg);
150 #endif
151                         mode = strtol(optarg, NULL, 8);
152                         mode_set = 1;
153 #ifdef DEBUG
154                         printf("mode = %o\n", mode);
155 #endif
156                         break;
157                 default:
158                         fprintf(stderr, "Bad parameters.\n");
159                         print_usage = 1;
160                         goto out;
161                 }
162         }
163
164         if (optind == argc) {
165                 fprintf(stderr, "Bad parameters.\n");
166                 print_usage = 1;
167                 goto out;
168         }
169
170         fname = argv[optind];
171
172         if (!flag_set) {
173                 fprintf(stderr, "Missing flag or file-name\n");
174                 save_errno = -1;
175                 goto out;
176         }
177
178         if (flags & O_CREAT)
179                 fd = open(fname, flags, mode);
180         else
181                 fd = open(fname, flags);
182
183         save_errno = errno;
184
185         if (fd != -1) {
186                 printf("Succeed in opening file \"%s\"(flags=%s",
187                        fname, cloned_flags);
188
189                 if (mode_set)
190                         printf(", mode=%o", mode);
191                 printf(")\n");
192                 close(fd);
193         } else {
194                 fprintf(stderr, "Error in opening file \"%s\"(flags=%s",
195                         fname, cloned_flags);
196                 if (mode_set)
197                         fprintf(stderr, ", mode=%o", mode);
198                 fprintf(stderr, ") %d: %s\n", save_errno, strerror(save_errno));
199         }
200 out:
201         if (cloned_flags)
202                 free(cloned_flags);
203         if (print_usage)
204                 Usage_and_abort();
205
206         return save_errno;
207 }