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 <errno.h>
|
---|
30 | #include <stdio.h>
|
---|
31 | #include <stdlib.h>
|
---|
32 | #include <unistd.h>
|
---|
33 | #include <dirent.h>
|
---|
34 | #include <getopt.h>
|
---|
35 | #include <mem.h>
|
---|
36 | #include <str.h>
|
---|
37 | #include <vfs/vfs.h>
|
---|
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 |
|
---|
46 | static const char *cmdname = "rm";
|
---|
47 | #define RM_VERSION "0.0.1"
|
---|
48 |
|
---|
49 | static struct option const long_options[] = {
|
---|
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 |
|
---|
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 |
|
---|
92 | static unsigned int rm_start(rm_job_t *rm)
|
---|
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;
|
---|
102 | memset(rm->nwd, 0, PATH_MAX);
|
---|
103 |
|
---|
104 | if (NULL == (rm->owd = (char *) malloc(PATH_MAX)))
|
---|
105 | return 0;
|
---|
106 | memset(rm->owd, 0, PATH_MAX);
|
---|
107 |
|
---|
108 | if (NULL == (rm->cwd = (char *) malloc(PATH_MAX)))
|
---|
109 | return 0;
|
---|
110 | memset(rm->cwd, 0, PATH_MAX);
|
---|
111 |
|
---|
112 | chdir(".");
|
---|
113 |
|
---|
114 | if (NULL == (getcwd(rm->owd, PATH_MAX)))
|
---|
115 | return 0;
|
---|
116 |
|
---|
117 | return 1;
|
---|
118 | }
|
---|
119 |
|
---|
120 | static void rm_end(rm_job_t *rm)
|
---|
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 |
|
---|
132 | static unsigned int rm_single(const char *path)
|
---|
133 | {
|
---|
134 | if (vfs_unlink_path(path) != EOK) {
|
---|
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 = vfs_lookup(path, WALK_REGULAR);
|
---|
153 | if (fd >= 0) {
|
---|
154 | close(fd);
|
---|
155 | return RM_FILE;
|
---|
156 | }
|
---|
157 |
|
---|
158 | return RM_BOGUS;
|
---|
159 | }
|
---|
160 |
|
---|
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 | }
|
---|
191 |
|
---|
192 | closedir(dirp);
|
---|
193 |
|
---|
194 | return ret;
|
---|
195 | }
|
---|
196 |
|
---|
197 | static unsigned int rm_recursive(const char *path)
|
---|
198 | {
|
---|
199 | int rc;
|
---|
200 | unsigned int ret = 0;
|
---|
201 |
|
---|
202 | /* First see if it will just go away */
|
---|
203 | rc = vfs_unlink_path(path);
|
---|
204 | if (rc == EOK)
|
---|
205 | return 0;
|
---|
206 |
|
---|
207 | /* Its not empty, recursively scan it */
|
---|
208 | ret = rm_recursive_not_empty_dirs(path);
|
---|
209 |
|
---|
210 | /* Delete directory */
|
---|
211 | rc = vfs_unlink_path(path);
|
---|
212 | if (rc == EOK)
|
---|
213 | return EOK;
|
---|
214 |
|
---|
215 | cli_error(CL_ENOTSUP, "Can not remove %s", path);
|
---|
216 |
|
---|
217 | return ret + 1;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /* Dispays help for rm in various levels */
|
---|
221 | void help_cmd_rm(unsigned int level)
|
---|
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 | }
|
---|
238 | return;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /* Main entry point for rm, accepts an array of arguments */
|
---|
242 | int cmd_rm(char **argv)
|
---|
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 |
|
---|
250 | argc = cli_count_args(argv);
|
---|
251 |
|
---|
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 |
|
---|
264 | for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) {
|
---|
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 |
|
---|
285 | if ((unsigned) optind == argc) {
|
---|
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]) {
|
---|
294 | len = str_size(argv[i]) + 2;
|
---|
295 | buff = (char *) realloc(buff, len);
|
---|
296 | if (buff == NULL) {
|
---|
297 | printf("rm: out of memory\n");
|
---|
298 | ret = 1;
|
---|
299 | break;
|
---|
300 | }
|
---|
301 | memset(buff, 0, len);
|
---|
302 | snprintf(buff, len, "%s", argv[i]);
|
---|
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 |
|
---|