Whamcloud - gitweb
LU-14876 out: don't connect to busy MDS-MDS export
[fs/lustre-release.git] / lustre / tests / mcreate.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) 2002, 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  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <sys/sysmacros.h>
33 #include <sys/stat.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <getopt.h>
39
40 void usage(const char *prog, int status)
41 {
42         fprintf(status == 0 ? stdout : stderr,
43                 "Usage: %s [OPTION]... FILE\n"
44                 "  -d, --device=DEV  use device number DEV\n"
45                 "  -h, --help        dispaly help\n"
46                 "  -m, --mode=MODE   use mode MODE\n"
47                 "  -M, --major=MAJOR use device major MAJOR\n"
48                 "  -N, --minor=MINOR use device minor MINOR\n",
49                 prog);
50
51         exit(status);
52 }
53
54 int main(int argc, char **argv)
55 {
56         struct option opts[] = {
57                 { .name = "device", .has_arg = required_argument, .val = 'd' },
58                 { .name = "help", .has_arg = no_argument, .val = 'h' },
59                 { .name = "mode", .has_arg = required_argument, .val = 'm' },
60                 { .name = "major", .has_arg = required_argument, .val = 'M' },
61                 { .name = "minor", .has_arg = required_argument, .val = 'N' },
62                 { .name = NULL }
63         };
64         const char *path;
65         mode_t mode = S_IFREG | 0644;
66         dev_t dev = 0;
67         int rc;
68         int c;
69
70         while ((c = getopt_long(argc, argv, "d:hm:M:N:", opts, NULL)) != -1) {
71                 switch (c) {
72                 case 'd':
73                         dev = strtoul(optarg, NULL, 0);
74                         break;
75                 case 'h':
76                         usage(argv[0], 0);
77                 case 'm':
78                         mode = strtoul(optarg, NULL, 0);
79                         break;
80                 case 'M':
81                         dev = makedev(strtoul(optarg, NULL, 0), minor(dev));
82                         break;
83                 case 'N':
84                         dev = makedev(major(dev), strtoul(optarg, NULL, 0));
85                         break;
86                 case '?':
87                         usage(argv[0], 1);
88                 }
89         }
90
91         if (argc - optind != 1)
92                 usage(argv[0], 1);
93
94         path = argv[optind];
95
96         if ((mode & S_IFMT) == S_IFDIR)
97                 rc = mkdir(path, mode & ~S_IFMT);
98         else if ((mode & S_IFMT) == S_IFLNK)
99                 rc = symlink("oldpath", path);
100         else
101                 rc = mknod(path, mode, dev);
102
103         if (rc) {
104                 rc = errno;
105                 fprintf(stderr, "%s: cannot create `%s' with mode %#o: %s\n",
106                         argv[0], path, mode, strerror(rc));
107         }
108
109         return rc;
110 }