Whamcloud - gitweb
Don't pass garbage mode bits to the filesystem with O_CREAT.
[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
5 #if 0
6 #define DEBUG
7 #endif
8
9 /* for O_DIRECTORY and O_DIRECT */
10 #define _GNU_SOURCE
11
12 #include <stdio.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20
21 typedef struct flag_mapping {
22        const char *string;
23        const int  flag;
24 } FLAG_MAPPING;
25
26 FLAG_MAPPING flag_table[] = {
27        {"O_RDONLY", O_RDONLY},
28        {"O_WRONLY", O_WRONLY},
29        {"O_RDWR", O_RDWR},
30        {"O_CREAT", O_CREAT},
31        {"O_EXCL", O_EXCL},
32        {"O_NOCTTY", O_NOCTTY},
33        {"O_TRUNC", O_TRUNC},
34        {"O_APPEND", O_APPEND},
35        {"O_NONBLOCK", O_NONBLOCK},
36        {"O_NDELAY", O_NDELAY},
37        {"O_SYNC", O_SYNC},
38        {"O_DIRECT", O_DIRECT},
39        {"O_LARGEFILE", O_LARGEFILE},
40        {"O_DIRECTORY", O_DIRECTORY},
41        {"O_NOFOLLOW", O_NOFOLLOW},
42        {"", -1}
43 };
44
45 void Usage_and_abort(void)
46 {
47        fprintf(stderr, "Usage: openfile -f flags [ -m mode ] filename \n");
48        fprintf(stderr, "e.g. openfile -f O_RDWR:O_CREAT -m 0755 /etc/passwd\n");
49        exit(-1);
50 }
51
52 int main(int argc, char** argv)
53 {
54         int    fd;
55         int    flags=0;
56         mode_t mode=0644;
57         char*  fname=NULL;
58         int    mode_set=0;
59         int    flag_set=0;
60         int    file_set=0;
61         char   c;
62         char*  cloned_flags;
63
64         if (argc == 1)
65                 Usage_and_abort();
66
67         while ((c = getopt (argc, argv, "f:m:")) != -1) {
68                 switch (c) {
69                 case 'f': {
70                         char *tmp;
71
72                         cloned_flags = (char *)malloc(strlen(optarg)+1);
73                         if (cloned_flags == NULL) {
74                                 fprintf(stderr, "Insufficient memory.\n");
75                                 exit(-1);
76                         }
77
78                         strncpy(cloned_flags, optarg, strlen(optarg)+1);
79                         for (tmp = strtok(optarg, ":|"); tmp;
80                              tmp = strtok(NULL, ":|")) {
81                                 int i = 0;
82 #ifdef DEBUG
83                                 printf("flags = %s\n",tmp);
84 #endif
85                                 flag_set = 1;
86                                 for (i = 0; flag_table[i].flag != -1; i++) {
87                                         if (!strcmp(tmp, flag_table[i].string)){
88                                                 flags |= flag_table[i].flag;
89                                                 break;
90                                         }
91                                 }
92
93                                 if (flag_table[i].flag == -1) {
94                                         fprintf(stderr, "No such flag: %s\n",
95                                                 tmp);
96                                         exit(-1);
97                                 }
98                         }
99 #ifdef DEBUG
100                         printf("flags = %x\n", flags);
101 #endif
102                         break;
103                 }
104                 case 'm':
105 #ifdef DEBUG
106                         printf("mode = %s\n", optarg);
107 #endif
108                         mode = strtol(optarg, NULL, 8);
109                         mode_set = 1;
110 #ifdef DEBUG
111                         printf("mode = %o\n", mode);
112 #endif
113                         break;
114                 default:
115                         fprintf(stderr, "Bad parameters.\n");
116                         Usage_and_abort();
117                 }
118         }
119
120         if (optind == argc) {
121                 fprintf(stderr, "Bad parameters.\n");
122                 Usage_and_abort();
123         }
124
125         fname = argv[optind];
126         file_set = 1;
127
128         if (!flag_set || !file_set) {
129                 fprintf(stderr, "Missing flag or file-name\n");
130                 exit(-1);
131         }
132
133
134         if (flags & O_CREAT)
135                 fd = open(fname, flags, mode);
136         else
137                 fd = open(fname, flags);
138
139         if (fd != -1) {
140                 printf("Succeed in opening file \"%s\"(flags=%s",
141                        fname, cloned_flags);
142
143                 if (mode_set)
144                         printf(", mode=%o", mode);
145                 printf(")\n");
146                 close(fd);
147                 return 0;
148         }
149
150         fprintf(stderr, "Error in opening file \"%s\"(flags=%s",
151                 fname, cloned_flags);
152         if (mode_set)
153                 fprintf(stderr, ", mode=%o", mode);
154         fprintf(stderr, ") %d: %s\n", errno, strerror(errno));
155
156         return errno;
157 }