source: mainline/uspace/app/bdsh/cmds/modules/mkdir/mkdir.c@ 35b7d86e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 35b7d86e was 6afc9d7, checked in by Jiri Svoboda <jiri@…>, 10 years ago

UNIX-like I/O functions should use errno to return error code for many reasons.

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 * Copyright (c) 2008 Tim Post
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <dirent.h>
32#include <fcntl.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <getopt.h>
36#include <stdarg.h>
37#include <str.h>
38#include <errno.h>
39#include <str_error.h>
40#include <vfs/vfs.h>
41
42#include "config.h"
43#include "errors.h"
44#include "util.h"
45#include "entry.h"
46#include "mkdir.h"
47#include "cmds.h"
48
49#define MKDIR_VERSION "0.0.1"
50
51static const char *cmdname = "mkdir";
52
53static struct option const long_options[] = {
54 {"parents", no_argument, 0, 'p'},
55 {"verbose", no_argument, 0, 'v'},
56 {"mode", required_argument, 0, 'm'},
57 {"help", no_argument, 0, 'h'},
58 {"version", no_argument, 0, 'V'},
59 {"follow", no_argument, 0, 'f'},
60 {0, 0, 0, 0}
61};
62
63
64void help_cmd_mkdir(unsigned int level)
65{
66 if (level == HELP_SHORT) {
67 printf("`%s' creates a new directory\n", cmdname);
68 } else {
69 help_cmd_mkdir(HELP_SHORT);
70 printf(
71 "Usage: %s [options] <path>\n"
72 "Options:\n"
73 " -h, --help A short option summary\n"
74 " -V, --version Print version information and exit\n"
75 " -p, --parents Create needed parents for <path>\n"
76 " -m, --mode Set permissions to [mode] (UNUSED)\n"
77 " -v, --verbose Be extremely noisy about what is happening\n"
78 " -f, --follow Go to the new directory once created\n"
79 "Currently, %s is under development, some options don't work.\n",
80 cmdname, cmdname);
81 }
82
83 return;
84}
85
86/* This is kind of clunky, but effective for now */
87static unsigned int
88create_directory(const char *user_path, bool create_parents)
89{
90 /* Ensure we would always work with absolute and canonified path. */
91 char *path = vfs_absolutize(user_path, NULL);
92 if (path == NULL) {
93 cli_error(CL_ENOMEM, "%s: path too big?", cmdname);
94 return 1;
95 }
96
97 int ret = 0;
98
99 if (!create_parents) {
100 if (mkdir(path, 0) != 0) {
101 cli_error(CL_EFAIL, "%s: could not create %s (%s)",
102 cmdname, path, str_error(errno));
103 ret = 1;
104 }
105 } else {
106 /* Create the parent directories as well. */
107 size_t off = 0;
108 while (1) {
109 size_t prev_off = off;
110 wchar_t cur_char = str_decode(path, &off, STR_NO_LIMIT);
111 if ((cur_char == 0) || (cur_char == U_SPECIAL)) {
112 break;
113 }
114 if (cur_char != '/') {
115 continue;
116 }
117 if (prev_off == 0) {
118 continue;
119 }
120 /*
121 * If we are here, it means that:
122 * - we found /
123 * - it is not the first / (no need to create root
124 * directory)
125 *
126 * We would now overwrite the / with 0 to terminate the
127 * string (that shall be okay because we are
128 * overwriting at the beginning of UTF sequence).
129 * That would allow us to create the directories
130 * in correct nesting order.
131 *
132 * Notice that we ignore EEXIST errors as some of
133 * the parent directories may already exist.
134 */
135 char slash_char = path[prev_off];
136 path[prev_off] = 0;
137
138 if (mkdir(path, 0) != 0 && errno != EEXIST) {
139 cli_error(CL_EFAIL, "%s: could not create %s (%s)",
140 cmdname, path, str_error(errno));
141 ret = 1;
142 goto leave;
143 }
144
145 path[prev_off] = slash_char;
146 }
147 /* Create the final directory. */
148 if (mkdir(path, 0) != 0) {
149 cli_error(CL_EFAIL, "%s: could not create %s (%s)",
150 cmdname, path, str_error(errno));
151 ret = 1;
152 }
153 }
154
155leave:
156 free(path);
157 return ret;
158}
159
160int cmd_mkdir(char **argv)
161{
162 unsigned int argc, i, ret = 0;
163 bool create_parents = false, follow = false, verbose = false;
164 int c, opt_ind;
165
166 argc = cli_count_args(argv);
167
168 for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) {
169 c = getopt_long(argc, argv, "pvhVfm:", long_options, &opt_ind);
170 switch (c) {
171 case 'p':
172 create_parents = true;
173 break;
174 case 'v':
175 verbose = true;
176 break;
177 case 'h':
178 help_cmd_mkdir(HELP_LONG);
179 return CMD_SUCCESS;
180 case 'V':
181 printf("%s\n", MKDIR_VERSION);
182 return CMD_SUCCESS;
183 case 'f':
184 follow = true;
185 break;
186 case 'm':
187 printf("%s: [W] Ignoring mode %s\n", cmdname, optarg);
188 break;
189 }
190 }
191
192 argc -= optind;
193
194 if (argc < 1) {
195 printf("%s - incorrect number of arguments. Try `%s --help'\n",
196 cmdname, cmdname);
197 return CMD_FAILURE;
198 }
199
200 for (i = optind; argv[i] != NULL; i++) {
201 if (verbose)
202 printf("%s: creating %s%s\n",
203 cmdname, argv[i],
204 create_parents ? " (and all parents)" : "");
205 ret += create_directory(argv[i], create_parents);
206 }
207
208 if (follow && (argv[optind] != NULL)) {
209 if (chdir(argv[optind]) != 0)
210 printf("%s: Error switching to directory.", cmdname);
211 }
212
213 if (ret)
214 return CMD_FAILURE;
215 else
216 return CMD_SUCCESS;
217}
218
Note: See TracBrowser for help on using the repository browser.