[36ab7c7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Tim Post
|
---|
[216d6fc] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
[36ab7c7] | 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
[216d6fc] | 8 | *
|
---|
[36ab7c7] | 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.
|
---|
[216d6fc] | 16 | *
|
---|
[36ab7c7] | 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.
|
---|
[216d6fc] | 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <stdio.h>
|
---|
| 30 | #include <stdlib.h>
|
---|
[8d2dd7f2] | 31 | #include <stdint.h>
|
---|
[216d6fc] | 32 | #include <dirent.h>
|
---|
[be8f92d] | 33 | #include <getopt.h>
|
---|
| 34 | #include <stdarg.h>
|
---|
[19f857a] | 35 | #include <str.h>
|
---|
[738b549] | 36 | #include <errno.h>
|
---|
| 37 | #include <str_error.h>
|
---|
| 38 | #include <vfs/vfs.h>
|
---|
[216d6fc] | 39 |
|
---|
| 40 | #include "config.h"
|
---|
| 41 | #include "errors.h"
|
---|
[be8f92d] | 42 | #include "util.h"
|
---|
[216d6fc] | 43 | #include "entry.h"
|
---|
| 44 | #include "mkdir.h"
|
---|
| 45 | #include "cmds.h"
|
---|
| 46 |
|
---|
[be8f92d] | 47 | #define MKDIR_VERSION "0.0.1"
|
---|
| 48 |
|
---|
[a000878c] | 49 | static const char *cmdname = "mkdir";
|
---|
[216d6fc] | 50 |
|
---|
[3771a6e] | 51 | static struct option const long_options[] = {
|
---|
[be8f92d] | 52 | {"parents", no_argument, 0, 'p'},
|
---|
| 53 | {"verbose", no_argument, 0, 'v'},
|
---|
| 54 | {"mode", required_argument, 0, 'm'},
|
---|
| 55 | {"help", no_argument, 0, 'h'},
|
---|
| 56 | {"version", no_argument, 0, 'V'},
|
---|
| 57 | {"follow", no_argument, 0, 'f'},
|
---|
| 58 | {0, 0, 0, 0}
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 |
|
---|
[809813d] | 62 | void help_cmd_mkdir(unsigned int level)
|
---|
[216d6fc] | 63 | {
|
---|
| 64 | if (level == HELP_SHORT) {
|
---|
| 65 | printf("`%s' creates a new directory\n", cmdname);
|
---|
| 66 | } else {
|
---|
| 67 | help_cmd_mkdir(HELP_SHORT);
|
---|
[be8f92d] | 68 | printf(
|
---|
| 69 | "Usage: %s [options] <path>\n"
|
---|
| 70 | "Options:\n"
|
---|
| 71 | " -h, --help A short option summary\n"
|
---|
| 72 | " -V, --version Print version information and exit\n"
|
---|
| 73 | " -p, --parents Create needed parents for <path>\n"
|
---|
| 74 | " -m, --mode Set permissions to [mode] (UNUSED)\n"
|
---|
| 75 | " -v, --verbose Be extremely noisy about what is happening\n"
|
---|
| 76 | " -f, --follow Go to the new directory once created\n"
|
---|
| 77 | "Currently, %s is under development, some options don't work.\n",
|
---|
| 78 | cmdname, cmdname);
|
---|
[216d6fc] | 79 | }
|
---|
| 80 |
|
---|
[809813d] | 81 | return;
|
---|
[216d6fc] | 82 | }
|
---|
| 83 |
|
---|
[be8f92d] | 84 | /* This is kind of clunky, but effective for now */
|
---|
[b510d52] | 85 | static unsigned int
|
---|
[738b549] | 86 | create_directory(const char *user_path, bool create_parents)
|
---|
[216d6fc] | 87 | {
|
---|
[738b549] | 88 | /* Ensure we would always work with absolute and canonified path. */
|
---|
[6afc9d7] | 89 | char *path = vfs_absolutize(user_path, NULL);
|
---|
[738b549] | 90 | if (path == NULL) {
|
---|
[be8f92d] | 91 | cli_error(CL_ENOMEM, "%s: path too big?", cmdname);
|
---|
| 92 | return 1;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[738b549] | 95 | int ret = 0;
|
---|
[b7fd2a0] | 96 | errno_t rc;
|
---|
[be8f92d] | 97 |
|
---|
[b00255a] | 98 | if (!create_parents) {
|
---|
[d5c1051] | 99 | rc = vfs_link_path(path, KIND_DIRECTORY, NULL);
|
---|
| 100 | if (rc != EOK) {
|
---|
[738b549] | 101 | cli_error(CL_EFAIL, "%s: could not create %s (%s)",
|
---|
[d5c1051] | 102 | cmdname, path, str_error(rc));
|
---|
[738b549] | 103 | ret = 1;
|
---|
[be8f92d] | 104 | }
|
---|
| 105 | } else {
|
---|
[738b549] | 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;
|
---|
[be8f92d] | 113 | }
|
---|
[738b549] | 114 | if (cur_char != '/') {
|
---|
| 115 | continue;
|
---|
[be8f92d] | 116 | }
|
---|
[738b549] | 117 | if (prev_off == 0) {
|
---|
| 118 | continue;
|
---|
[be8f92d] | 119 | }
|
---|
[738b549] | 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 |
|
---|
[d5c1051] | 138 | rc = vfs_link_path(path, KIND_DIRECTORY, NULL);
|
---|
| 139 | if (rc != EOK && rc != EEXIST) {
|
---|
[738b549] | 140 | cli_error(CL_EFAIL, "%s: could not create %s (%s)",
|
---|
[d5c1051] | 141 | cmdname, path, str_error(rc));
|
---|
[738b549] | 142 | ret = 1;
|
---|
| 143 | goto leave;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | path[prev_off] = slash_char;
|
---|
| 147 | }
|
---|
| 148 | /* Create the final directory. */
|
---|
[d5c1051] | 149 | rc = vfs_link_path(path, KIND_DIRECTORY, NULL);
|
---|
| 150 | if (rc != EOK) {
|
---|
[738b549] | 151 | cli_error(CL_EFAIL, "%s: could not create %s (%s)",
|
---|
[d5c1051] | 152 | cmdname, path, str_error(rc));
|
---|
[738b549] | 153 | ret = 1;
|
---|
[be8f92d] | 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[738b549] | 157 | leave:
|
---|
| 158 | free(path);
|
---|
[be8f92d] | 159 | return ret;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[809813d] | 162 | int cmd_mkdir(char **argv)
|
---|
[be8f92d] | 163 | {
|
---|
[b00255a] | 164 | unsigned int argc, i, ret = 0;
|
---|
| 165 | bool create_parents = false, follow = false, verbose = false;
|
---|
[be8f92d] | 166 | int c, opt_ind;
|
---|
[216d6fc] | 167 |
|
---|
[43e02a6] | 168 | argc = cli_count_args(argv);
|
---|
[216d6fc] | 169 |
|
---|
[948222e4] | 170 | c = 0;
|
---|
| 171 | optreset = 1;
|
---|
| 172 | optind = 0;
|
---|
| 173 | opt_ind = 0;
|
---|
| 174 |
|
---|
| 175 | while (c != -1) {
|
---|
[be8f92d] | 176 | c = getopt_long(argc, argv, "pvhVfm:", long_options, &opt_ind);
|
---|
| 177 | switch (c) {
|
---|
| 178 | case 'p':
|
---|
[b00255a] | 179 | create_parents = true;
|
---|
[be8f92d] | 180 | break;
|
---|
| 181 | case 'v':
|
---|
[b00255a] | 182 | verbose = true;
|
---|
[be8f92d] | 183 | break;
|
---|
| 184 | case 'h':
|
---|
| 185 | help_cmd_mkdir(HELP_LONG);
|
---|
| 186 | return CMD_SUCCESS;
|
---|
| 187 | case 'V':
|
---|
| 188 | printf("%s\n", MKDIR_VERSION);
|
---|
| 189 | return CMD_SUCCESS;
|
---|
| 190 | case 'f':
|
---|
[b00255a] | 191 | follow = true;
|
---|
[be8f92d] | 192 | break;
|
---|
| 193 | case 'm':
|
---|
| 194 | printf("%s: [W] Ignoring mode %s\n", cmdname, optarg);
|
---|
| 195 | break;
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | argc -= optind;
|
---|
| 200 |
|
---|
| 201 | if (argc < 1) {
|
---|
| 202 | printf("%s - incorrect number of arguments. Try `%s --help'\n",
|
---|
[216d6fc] | 203 | cmdname, cmdname);
|
---|
| 204 | return CMD_FAILURE;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[be8f92d] | 207 | for (i = optind; argv[i] != NULL; i++) {
|
---|
[b00255a] | 208 | if (verbose)
|
---|
[be8f92d] | 209 | printf("%s: creating %s%s\n",
|
---|
| 210 | cmdname, argv[i],
|
---|
| 211 | create_parents ? " (and all parents)" : "");
|
---|
| 212 | ret += create_directory(argv[i], create_parents);
|
---|
[216d6fc] | 213 | }
|
---|
| 214 |
|
---|
[738b549] | 215 | if (follow && (argv[optind] != NULL)) {
|
---|
[d96d9bc] | 216 | if (vfs_cwd_set(argv[optind]) != EOK)
|
---|
[6afc9d7] | 217 | printf("%s: Error switching to directory.", cmdname);
|
---|
[738b549] | 218 | }
|
---|
[be8f92d] | 219 |
|
---|
| 220 | if (ret)
|
---|
| 221 | return CMD_FAILURE;
|
---|
| 222 | else
|
---|
| 223 | return CMD_SUCCESS;
|
---|
[216d6fc] | 224 | }
|
---|
| 225 |
|
---|