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