source: mainline/uspace/app/bdsh/cmds/modules/rm/rm.c

Last change on this file was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 7.7 KB
Line 
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 <dirent.h>
33#include <getopt.h>
34#include <mem.h>
35#include <str.h>
36#include <vfs/vfs.h>
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
45static const char *cmdname = "rm";
46#define RM_VERSION "0.0.1"
47
48static struct option const long_options[] = {
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
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 */
69typedef 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
87static rm_job_t rm;
88
89static unsigned int rm_recursive(const char *);
90
91static unsigned int rm_start(rm_job_t *rm)
92{
93 rm->recursive = 0;
94 rm->force = 0;
95 rm->safe = 0;
96
97 /*
98 * Make sure we can allocate enough memory to store
99 * what is needed in the job structure
100 */
101 if (NULL == (rm->nwd = (char *) malloc(PATH_MAX)))
102 return 0;
103 memset(rm->nwd, 0, PATH_MAX);
104
105 if (NULL == (rm->owd = (char *) malloc(PATH_MAX)))
106 return 0;
107 memset(rm->owd, 0, PATH_MAX);
108
109 if (NULL == (rm->cwd = (char *) malloc(PATH_MAX)))
110 return 0;
111 memset(rm->cwd, 0, PATH_MAX);
112
113 vfs_cwd_set(".");
114
115 if (EOK != vfs_cwd_get(rm->owd, PATH_MAX))
116 return 0;
117
118 return 1;
119}
120
121static void rm_end(rm_job_t *rm)
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
133static unsigned int rm_single(const char *path)
134{
135 if (vfs_unlink_path(path) != EOK) {
136 cli_error(CL_EFAIL, "rm: could not remove file %s", path);
137 return 1;
138 }
139 return 0;
140}
141
142static 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 if (vfs_lookup(path, WALK_REGULAR, &fd) == EOK) {
154 vfs_put(fd);
155 return RM_FILE;
156 }
157
158 return RM_BOGUS;
159}
160
161static 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 int len = snprintf(buff, PATH_MAX - 1, "%s/%s", path, dp->d_name);
179 if (len > PATH_MAX - 1) {
180 // TODO: Do not enforce arbitrary static limits.
181 cli_error(CL_EFAIL, "Path too long for %s/%s", path, dp->d_name);
182 continue;
183 }
184 scope = rm_scope(buff);
185 switch (scope) {
186 case RM_BOGUS:
187 break;
188 case RM_FILE:
189 ret += rm_single(buff);
190 break;
191 case RM_DIR:
192 ret += rm_recursive(buff);
193 break;
194 }
195 }
196
197 closedir(dirp);
198
199 return ret;
200}
201
202static unsigned int rm_recursive(const char *path)
203{
204 errno_t rc;
205 unsigned int ret = 0;
206
207 /* First see if it will just go away */
208 rc = vfs_unlink_path(path);
209 if (rc == EOK)
210 return 0;
211
212 /* Its not empty, recursively scan it */
213 ret = rm_recursive_not_empty_dirs(path);
214
215 /* Delete directory */
216 rc = vfs_unlink_path(path);
217 if (rc == EOK)
218 return 0;
219
220 cli_error(CL_ENOTSUP, "Can not remove %s", path);
221
222 return ret + 1;
223}
224
225/* Dispays help for rm in various levels */
226void help_cmd_rm(unsigned int level)
227{
228 if (level == HELP_SHORT) {
229 printf("`%s' removes files and directories.\n", cmdname);
230 } else {
231 help_cmd_rm(HELP_SHORT);
232 printf(
233 "Usage: %s [options] <path>\n"
234 "Options:\n"
235 " -h, --help A short option summary\n"
236 " -v, --version Print version information and exit\n"
237 " -r, --recursive Recursively remove sub directories\n"
238 " -f, --force Do not prompt prior to removing files\n"
239 " -s, --safe Stop if directories change during removal\n\n"
240 "Currently, %s is under development, some options don't work.\n",
241 cmdname, cmdname);
242 }
243 return;
244}
245
246/* Main entry point for rm, accepts an array of arguments */
247int cmd_rm(char **argv)
248{
249 unsigned int argc;
250 unsigned int i, scope, ret = 0;
251 int c, opt_ind;
252 size_t len;
253 char *buff = NULL;
254
255 argc = cli_count_args(argv);
256
257 if (argc < 2) {
258 cli_error(CL_EFAIL,
259 "%s: insufficient arguments. Try %s --help", cmdname, cmdname);
260 return CMD_FAILURE;
261 }
262
263 if (!rm_start(&rm)) {
264 cli_error(CL_ENOMEM, "%s: could not initialize", cmdname);
265 rm_end(&rm);
266 return CMD_FAILURE;
267 }
268
269 c = 0;
270 optreset = 1;
271 optind = 0;
272 opt_ind = 0;
273
274 while (c != -1) {
275 c = getopt_long(argc, argv, "hvrfs", long_options, &opt_ind);
276 switch (c) {
277 case 'h':
278 help_cmd_rm(HELP_LONG);
279 return CMD_SUCCESS;
280 case 'v':
281 printf("%s\n", RM_VERSION);
282 return CMD_SUCCESS;
283 case 'r':
284 rm.recursive = 1;
285 break;
286 case 'f':
287 rm.force = 1;
288 break;
289 case 's':
290 rm.safe = 1;
291 break;
292 }
293 }
294
295 if ((unsigned) optind == argc) {
296 cli_error(CL_EFAIL,
297 "%s: insufficient arguments. Try %s --help", cmdname, cmdname);
298 rm_end(&rm);
299 return CMD_FAILURE;
300 }
301
302 i = optind;
303 while (NULL != argv[i]) {
304 len = str_size(argv[i]) + 2;
305 buff = (char *) realloc(buff, len);
306 if (buff == NULL) {
307 printf("rm: out of memory\n");
308 ret = 1;
309 break;
310 }
311 memset(buff, 0, len);
312 snprintf(buff, len, "%s", argv[i]);
313
314 scope = rm_scope(buff);
315 switch (scope) {
316 case RM_BOGUS: /* FIXME */
317 case RM_FILE:
318 ret += rm_single(buff);
319 break;
320 case RM_DIR:
321 if (!rm.recursive) {
322 printf("%s is a directory, use -r to remove it.\n", buff);
323 ret++;
324 } else {
325 ret += rm_recursive(buff);
326 }
327 break;
328 }
329 i++;
330 }
331
332 if (NULL != buff)
333 free(buff);
334
335 rm_end(&rm);
336
337 if (ret)
338 return CMD_FAILURE;
339 else
340 return CMD_SUCCESS;
341}
Note: See TracBrowser for help on using the repository browser.