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