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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 948222e4 was 948222e4, checked in by Jiri Svoboda <jiri@…>, 8 years ago

A few more cases of for loops without iteration expression (which isn't really a for loop).

  • Property mode set to 100644
File size: 7.5 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 /* 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;
101 memset(rm->nwd, 0, PATH_MAX);
102
103 if (NULL == (rm->owd = (char *) malloc(PATH_MAX)))
104 return 0;
105 memset(rm->owd, 0, PATH_MAX);
106
107 if (NULL == (rm->cwd = (char *) malloc(PATH_MAX)))
108 return 0;
109 memset(rm->cwd, 0, PATH_MAX);
110
111 vfs_cwd_set(".");
112
113 if (EOK != vfs_cwd_get(rm->owd, PATH_MAX))
114 return 0;
115
116 return 1;
117}
118
119static void rm_end(rm_job_t *rm)
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
131static unsigned int rm_single(const char *path)
132{
133 if (vfs_unlink_path(path) != EOK) {
134 cli_error(CL_EFAIL, "rm: could not remove file %s", path);
135 return 1;
136 }
137 return 0;
138}
139
140static 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
151 if (vfs_lookup(path, WALK_REGULAR, &fd) == EOK) {
152 vfs_put(fd);
153 return RM_FILE;
154 }
155
156 return RM_BOGUS;
157}
158
159static 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 }
189
190 closedir(dirp);
191
192 return ret;
193}
194
195static unsigned int rm_recursive(const char *path)
196{
197 errno_t rc;
198 unsigned int ret = 0;
199
200 /* First see if it will just go away */
201 rc = vfs_unlink_path(path);
202 if (rc == EOK)
203 return 0;
204
205 /* Its not empty, recursively scan it */
206 ret = rm_recursive_not_empty_dirs(path);
207
208 /* Delete directory */
209 rc = vfs_unlink_path(path);
210 if (rc == EOK)
211 return 0;
212
213 cli_error(CL_ENOTSUP, "Can not remove %s", path);
214
215 return ret + 1;
216}
217
218/* Dispays help for rm in various levels */
219void help_cmd_rm(unsigned int level)
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 }
236 return;
237}
238
239/* Main entry point for rm, accepts an array of arguments */
240int cmd_rm(char **argv)
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
248 argc = cli_count_args(argv);
249
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
262 c = 0;
263 optreset = 1;
264 optind = 0;
265 opt_ind = 0;
266
267 while (c != -1) {
268 c = getopt_long(argc, argv, "hvrfs", long_options, &opt_ind);
269 switch (c) {
270 case 'h':
271 help_cmd_rm(HELP_LONG);
272 return CMD_SUCCESS;
273 case 'v':
274 printf("%s\n", RM_VERSION);
275 return CMD_SUCCESS;
276 case 'r':
277 rm.recursive = 1;
278 break;
279 case 'f':
280 rm.force = 1;
281 break;
282 case 's':
283 rm.safe = 1;
284 break;
285 }
286 }
287
288 if ((unsigned) optind == argc) {
289 cli_error(CL_EFAIL,
290 "%s: insufficient arguments. Try %s --help", cmdname, cmdname);
291 rm_end(&rm);
292 return CMD_FAILURE;
293 }
294
295 i = optind;
296 while (NULL != argv[i]) {
297 len = str_size(argv[i]) + 2;
298 buff = (char *) realloc(buff, len);
299 if (buff == NULL) {
300 printf("rm: out of memory\n");
301 ret = 1;
302 break;
303 }
304 memset(buff, 0, len);
305 snprintf(buff, len, "%s", argv[i]);
306
307 scope = rm_scope(buff);
308 switch (scope) {
309 case RM_BOGUS: /* FIXME */
310 case RM_FILE:
311 ret += rm_single(buff);
312 break;
313 case RM_DIR:
314 if (! rm.recursive) {
315 printf("%s is a directory, use -r to remove it.\n", buff);
316 ret ++;
317 } else {
318 ret += rm_recursive(buff);
319 }
320 break;
321 }
322 i++;
323 }
324
325 if (NULL != buff)
326 free(buff);
327
328 rm_end(&rm);
329
330 if (ret)
331 return CMD_FAILURE;
332 else
333 return CMD_SUCCESS;
334}
335
Note: See TracBrowser for help on using the repository browser.