[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 |
|
---|
[6afc9d7] | 29 | #include <errno.h>
|
---|
[216d6fc] | 30 | #include <stdio.h>
|
---|
| 31 | #include <stdlib.h>
|
---|
| 32 | #include <dirent.h>
|
---|
| 33 | #include <getopt.h>
|
---|
[3bf907a] | 34 | #include <mem.h>
|
---|
[19f857a] | 35 | #include <str.h>
|
---|
[79ea5af] | 36 | #include <vfs/vfs.h>
|
---|
[216d6fc] | 37 |
|
---|
| 38 | #include "config.h"
|
---|
| 39 | #include "errors.h"
|
---|
| 40 | #include "util.h"
|
---|
| 41 | #include "entry.h"
|
---|
| 42 | #include "rm.h"
|
---|
| 43 | #include "cmds.h"
|
---|
| 44 |
|
---|
[a000878c] | 45 | static const char *cmdname = "rm";
|
---|
[216d6fc] | 46 | #define RM_VERSION "0.0.1"
|
---|
| 47 |
|
---|
[3771a6e] | 48 | static struct option const long_options[] = {
|
---|
[216d6fc] | 49 | { "help", no_argument, 0, 'h' },
|
---|
| 50 | { "version", no_argument, 0, 'v' },
|
---|
| 51 | { "recursive", no_argument, 0, 'r' },
|
---|
| 52 | { "force", no_argument, 0, 'f' },
|
---|
| 53 | { "safe", no_argument, 0, 's' },
|
---|
| 54 | { 0, 0, 0, 0 }
|
---|
| 55 | };
|
---|
| 56 |
|
---|
[081d60f] | 57 | /* Return values for rm_scope() */
|
---|
| 58 | #define RM_BOGUS 0
|
---|
| 59 | #define RM_FILE 1
|
---|
| 60 | #define RM_DIR 2
|
---|
| 61 |
|
---|
| 62 | /* Flags for rm_update() */
|
---|
| 63 | #define _RM_ENTRY 0
|
---|
| 64 | #define _RM_ADVANCE 1
|
---|
| 65 | #define _RM_REWIND 2
|
---|
| 66 | #define _RM_EXIT 3
|
---|
| 67 |
|
---|
| 68 | /* A simple job structure */
|
---|
| 69 | typedef struct {
|
---|
| 70 | /* Options set at run time */
|
---|
| 71 | unsigned int force; /* -f option */
|
---|
| 72 | unsigned int recursive; /* -r option */
|
---|
| 73 | unsigned int safe; /* -s option */
|
---|
| 74 |
|
---|
| 75 | /* Keeps track of the job in progress */
|
---|
| 76 | int advance; /* How far deep we've gone since entering */
|
---|
| 77 | DIR *entry; /* Entry point to the tree being removed */
|
---|
| 78 | char *owd; /* Where we were when we invoked rm */
|
---|
| 79 | char *cwd; /* Current directory being transversed */
|
---|
| 80 | char *nwd; /* Next directory to be transversed */
|
---|
| 81 |
|
---|
| 82 | /* Counters */
|
---|
| 83 | int f_removed; /* Number of files unlinked */
|
---|
| 84 | int d_removed; /* Number of directories unlinked */
|
---|
| 85 | } rm_job_t;
|
---|
| 86 |
|
---|
| 87 | static rm_job_t rm;
|
---|
| 88 |
|
---|
| 89 | static unsigned int rm_recursive(const char *);
|
---|
| 90 |
|
---|
[b510d52] | 91 | static unsigned int rm_start(rm_job_t *rm)
|
---|
[216d6fc] | 92 | {
|
---|
| 93 | rm->recursive = 0;
|
---|
| 94 | rm->force = 0;
|
---|
| 95 | rm->safe = 0;
|
---|
| 96 |
|
---|
| 97 | /* Make sure we can allocate enough memory to store
|
---|
| 98 | * what is needed in the job structure */
|
---|
| 99 | if (NULL == (rm->nwd = (char *) malloc(PATH_MAX)))
|
---|
| 100 | return 0;
|
---|
[445e7c0] | 101 | memset(rm->nwd, 0, PATH_MAX);
|
---|
[216d6fc] | 102 |
|
---|
| 103 | if (NULL == (rm->owd = (char *) malloc(PATH_MAX)))
|
---|
| 104 | return 0;
|
---|
[445e7c0] | 105 | memset(rm->owd, 0, PATH_MAX);
|
---|
[216d6fc] | 106 |
|
---|
| 107 | if (NULL == (rm->cwd = (char *) malloc(PATH_MAX)))
|
---|
| 108 | return 0;
|
---|
[445e7c0] | 109 | memset(rm->cwd, 0, PATH_MAX);
|
---|
[216d6fc] | 110 |
|
---|
[d96d9bc] | 111 | vfs_cwd_set(".");
|
---|
[216d6fc] | 112 |
|
---|
[d96d9bc] | 113 | if (EOK != vfs_cwd_get(rm->owd, PATH_MAX))
|
---|
[216d6fc] | 114 | return 0;
|
---|
| 115 |
|
---|
| 116 | return 1;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[b510d52] | 119 | static void rm_end(rm_job_t *rm)
|
---|
[216d6fc] | 120 | {
|
---|
| 121 | if (NULL != rm->nwd)
|
---|
| 122 | free(rm->nwd);
|
---|
| 123 |
|
---|
| 124 | if (NULL != rm->owd)
|
---|
| 125 | free(rm->owd);
|
---|
| 126 |
|
---|
| 127 | if (NULL != rm->cwd)
|
---|
| 128 | free(rm->cwd);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[081d60f] | 131 | static unsigned int rm_single(const char *path)
|
---|
| 132 | {
|
---|
[79ea5af] | 133 | if (vfs_unlink_path(path) != EOK) {
|
---|
[081d60f] | 134 | cli_error(CL_EFAIL, "rm: could not remove file %s", path);
|
---|
| 135 | return 1;
|
---|
| 136 | }
|
---|
| 137 | return 0;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | static unsigned int rm_scope(const char *path)
|
---|
| 141 | {
|
---|
| 142 | int fd;
|
---|
| 143 | DIR *dirp;
|
---|
| 144 |
|
---|
| 145 | dirp = opendir(path);
|
---|
| 146 | if (dirp) {
|
---|
| 147 | closedir(dirp);
|
---|
| 148 | return RM_DIR;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[f77c1c9] | 151 | if (vfs_lookup(path, WALK_REGULAR, &fd) == EOK) {
|
---|
[9c4cf0d] | 152 | vfs_put(fd);
|
---|
[081d60f] | 153 | return RM_FILE;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | return RM_BOGUS;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[ed2c8ff] | 159 | static unsigned int rm_recursive_not_empty_dirs(const char *path)
|
---|
| 160 | {
|
---|
| 161 | DIR *dirp;
|
---|
| 162 | struct dirent *dp;
|
---|
| 163 | char buff[PATH_MAX];
|
---|
| 164 | unsigned int scope;
|
---|
| 165 | unsigned int ret = 0;
|
---|
| 166 |
|
---|
| 167 | dirp = opendir(path);
|
---|
| 168 | if (!dirp) {
|
---|
| 169 | /* May have been deleted between scoping it and opening it */
|
---|
| 170 | cli_error(CL_EFAIL, "Could not open %s", path);
|
---|
| 171 | return ret;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | memset(buff, 0, sizeof(buff));
|
---|
| 175 | while ((dp = readdir(dirp))) {
|
---|
| 176 | snprintf(buff, PATH_MAX - 1, "%s/%s", path, dp->d_name);
|
---|
| 177 | scope = rm_scope(buff);
|
---|
| 178 | switch (scope) {
|
---|
| 179 | case RM_BOGUS:
|
---|
| 180 | break;
|
---|
| 181 | case RM_FILE:
|
---|
| 182 | ret += rm_single(buff);
|
---|
| 183 | break;
|
---|
| 184 | case RM_DIR:
|
---|
| 185 | ret += rm_recursive(buff);
|
---|
| 186 | break;
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
[713ea96a] | 189 |
|
---|
| 190 | closedir(dirp);
|
---|
[ed2c8ff] | 191 |
|
---|
| 192 | return ret;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[b510d52] | 195 | static unsigned int rm_recursive(const char *path)
|
---|
[216d6fc] | 196 | {
|
---|
[b7fd2a0] | 197 | errno_t rc;
|
---|
[ed2c8ff] | 198 | unsigned int ret = 0;
|
---|
[216d6fc] | 199 |
|
---|
| 200 | /* First see if it will just go away */
|
---|
[79ea5af] | 201 | rc = vfs_unlink_path(path);
|
---|
| 202 | if (rc == EOK)
|
---|
[216d6fc] | 203 | return 0;
|
---|
| 204 |
|
---|
| 205 | /* Its not empty, recursively scan it */
|
---|
[ed2c8ff] | 206 | ret = rm_recursive_not_empty_dirs(path);
|
---|
| 207 |
|
---|
| 208 | /* Delete directory */
|
---|
[79ea5af] | 209 | rc = vfs_unlink_path(path);
|
---|
| 210 | if (rc == EOK)
|
---|
[d5c1051] | 211 | return 0;
|
---|
[ed2c8ff] | 212 |
|
---|
| 213 | cli_error(CL_ENOTSUP, "Can not remove %s", path);
|
---|
| 214 |
|
---|
| 215 | return ret + 1;
|
---|
[216d6fc] | 216 | }
|
---|
| 217 |
|
---|
| 218 | /* Dispays help for rm in various levels */
|
---|
[809813d] | 219 | void help_cmd_rm(unsigned int level)
|
---|
[216d6fc] | 220 | {
|
---|
| 221 | if (level == HELP_SHORT) {
|
---|
| 222 | printf("`%s' removes files and directories.\n", cmdname);
|
---|
| 223 | } else {
|
---|
| 224 | help_cmd_rm(HELP_SHORT);
|
---|
| 225 | printf(
|
---|
| 226 | "Usage: %s [options] <path>\n"
|
---|
| 227 | "Options:\n"
|
---|
| 228 | " -h, --help A short option summary\n"
|
---|
| 229 | " -v, --version Print version information and exit\n"
|
---|
| 230 | " -r, --recursive Recursively remove sub directories\n"
|
---|
| 231 | " -f, --force Do not prompt prior to removing files\n"
|
---|
| 232 | " -s, --safe Stop if directories change during removal\n\n"
|
---|
| 233 | "Currently, %s is under development, some options don't work.\n",
|
---|
| 234 | cmdname, cmdname);
|
---|
| 235 | }
|
---|
[809813d] | 236 | return;
|
---|
[216d6fc] | 237 | }
|
---|
| 238 |
|
---|
| 239 | /* Main entry point for rm, accepts an array of arguments */
|
---|
[809813d] | 240 | int cmd_rm(char **argv)
|
---|
[216d6fc] | 241 | {
|
---|
| 242 | unsigned int argc;
|
---|
| 243 | unsigned int i, scope, ret = 0;
|
---|
| 244 | int c, opt_ind;
|
---|
| 245 | size_t len;
|
---|
| 246 | char *buff = NULL;
|
---|
| 247 |
|
---|
[43e02a6] | 248 | argc = cli_count_args(argv);
|
---|
| 249 |
|
---|
[216d6fc] | 250 | if (argc < 2) {
|
---|
| 251 | cli_error(CL_EFAIL,
|
---|
| 252 | "%s: insufficient arguments. Try %s --help", cmdname, cmdname);
|
---|
| 253 | return CMD_FAILURE;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | if (!rm_start(&rm)) {
|
---|
| 257 | cli_error(CL_ENOMEM, "%s: could not initialize", cmdname);
|
---|
| 258 | rm_end(&rm);
|
---|
| 259 | return CMD_FAILURE;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[86cf96d] | 262 | for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) {
|
---|
[216d6fc] | 263 | c = getopt_long(argc, argv, "hvrfs", long_options, &opt_ind);
|
---|
| 264 | switch (c) {
|
---|
| 265 | case 'h':
|
---|
| 266 | help_cmd_rm(HELP_LONG);
|
---|
| 267 | return CMD_SUCCESS;
|
---|
| 268 | case 'v':
|
---|
| 269 | printf("%s\n", RM_VERSION);
|
---|
| 270 | return CMD_SUCCESS;
|
---|
| 271 | case 'r':
|
---|
| 272 | rm.recursive = 1;
|
---|
| 273 | break;
|
---|
| 274 | case 'f':
|
---|
| 275 | rm.force = 1;
|
---|
| 276 | break;
|
---|
| 277 | case 's':
|
---|
| 278 | rm.safe = 1;
|
---|
| 279 | break;
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[5b3cf90] | 283 | if ((unsigned) optind == argc) {
|
---|
[216d6fc] | 284 | cli_error(CL_EFAIL,
|
---|
| 285 | "%s: insufficient arguments. Try %s --help", cmdname, cmdname);
|
---|
| 286 | rm_end(&rm);
|
---|
| 287 | return CMD_FAILURE;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | i = optind;
|
---|
| 291 | while (NULL != argv[i]) {
|
---|
[92fd52d7] | 292 | len = str_size(argv[i]) + 2;
|
---|
[216d6fc] | 293 | buff = (char *) realloc(buff, len);
|
---|
[b7be230] | 294 | if (buff == NULL) {
|
---|
| 295 | printf("rm: out of memory\n");
|
---|
| 296 | ret = 1;
|
---|
| 297 | break;
|
---|
| 298 | }
|
---|
[445e7c0] | 299 | memset(buff, 0, len);
|
---|
[afe1d1e] | 300 | snprintf(buff, len, "%s", argv[i]);
|
---|
[216d6fc] | 301 |
|
---|
| 302 | scope = rm_scope(buff);
|
---|
| 303 | switch (scope) {
|
---|
| 304 | case RM_BOGUS: /* FIXME */
|
---|
| 305 | case RM_FILE:
|
---|
| 306 | ret += rm_single(buff);
|
---|
| 307 | break;
|
---|
| 308 | case RM_DIR:
|
---|
| 309 | if (! rm.recursive) {
|
---|
| 310 | printf("%s is a directory, use -r to remove it.\n", buff);
|
---|
| 311 | ret ++;
|
---|
| 312 | } else {
|
---|
| 313 | ret += rm_recursive(buff);
|
---|
| 314 | }
|
---|
| 315 | break;
|
---|
| 316 | }
|
---|
| 317 | i++;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | if (NULL != buff)
|
---|
| 321 | free(buff);
|
---|
| 322 |
|
---|
| 323 | rm_end(&rm);
|
---|
| 324 |
|
---|
| 325 | if (ret)
|
---|
| 326 | return CMD_FAILURE;
|
---|
| 327 | else
|
---|
| 328 | return CMD_SUCCESS;
|
---|
| 329 | }
|
---|
| 330 |
|
---|