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