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
RevLine 
[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 <dirent.h>
33#include <getopt.h>
[3bf907a]34#include <mem.h>
[19f857a]35#include <str.h>
[79ea5af]36#include <vfs/vfs.h>
[216d6fc]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
[a000878c]45static const char *cmdname = "rm";
[216d6fc]46#define RM_VERSION "0.0.1"
47
[3771a6e]48static struct option const long_options[] = {
[216d6fc]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
[081d60f]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
[b510d52]91static unsigned int rm_start(rm_job_t *rm)
[216d6fc]92{
93 rm->recursive = 0;
94 rm->force = 0;
95 rm->safe = 0;
96
[7c3fb9b]97 /*
98 * Make sure we can allocate enough memory to store
99 * what is needed in the job structure
100 */
[216d6fc]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
[d96d9bc]113 vfs_cwd_set(".");
[216d6fc]114
[d96d9bc]115 if (EOK != vfs_cwd_get(rm->owd, PATH_MAX))
[216d6fc]116 return 0;
117
118 return 1;
119}
120
[b510d52]121static 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]133static 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
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
[f77c1c9]153 if (vfs_lookup(path, WALK_REGULAR, &fd) == EOK) {
[9c4cf0d]154 vfs_put(fd);
[081d60f]155 return RM_FILE;
156 }
157
158 return RM_BOGUS;
159}
160
[ed2c8ff]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))) {
[41bbab6]178 int len = snprintf(buff, PATH_MAX - 1, "%s/%s", path, dp->d_name);
[1433ecda]179 if (len > PATH_MAX - 1) {
[41bbab6]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 }
[ed2c8ff]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 }
[713ea96a]196
197 closedir(dirp);
[a35b458]198
[ed2c8ff]199 return ret;
200}
201
[b510d52]202static unsigned int rm_recursive(const char *path)
[216d6fc]203{
[b7fd2a0]204 errno_t rc;
[ed2c8ff]205 unsigned int ret = 0;
[216d6fc]206
207 /* First see if it will just go away */
[79ea5af]208 rc = vfs_unlink_path(path);
209 if (rc == EOK)
[216d6fc]210 return 0;
211
212 /* Its not empty, recursively scan it */
[ed2c8ff]213 ret = rm_recursive_not_empty_dirs(path);
214
215 /* Delete directory */
[79ea5af]216 rc = vfs_unlink_path(path);
217 if (rc == EOK)
[d5c1051]218 return 0;
[ed2c8ff]219
220 cli_error(CL_ENOTSUP, "Can not remove %s", path);
221
222 return ret + 1;
[216d6fc]223}
224
225/* Dispays help for rm in various levels */
[809813d]226void help_cmd_rm(unsigned int level)
[216d6fc]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(
[1433ecda]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);
[216d6fc]242 }
[809813d]243 return;
[216d6fc]244}
245
246/* Main entry point for rm, accepts an array of arguments */
[809813d]247int cmd_rm(char **argv)
[216d6fc]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
[43e02a6]255 argc = cli_count_args(argv);
256
[216d6fc]257 if (argc < 2) {
258 cli_error(CL_EFAIL,
[1433ecda]259 "%s: insufficient arguments. Try %s --help", cmdname, cmdname);
[216d6fc]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
[948222e4]269 c = 0;
270 optreset = 1;
271 optind = 0;
272 opt_ind = 0;
273
274 while (c != -1) {
[216d6fc]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
[5b3cf90]295 if ((unsigned) optind == argc) {
[216d6fc]296 cli_error(CL_EFAIL,
[1433ecda]297 "%s: insufficient arguments. Try %s --help", cmdname, cmdname);
[216d6fc]298 rm_end(&rm);
299 return CMD_FAILURE;
300 }
301
302 i = optind;
303 while (NULL != argv[i]) {
[92fd52d7]304 len = str_size(argv[i]) + 2;
[216d6fc]305 buff = (char *) realloc(buff, len);
[b7be230]306 if (buff == NULL) {
307 printf("rm: out of memory\n");
308 ret = 1;
309 break;
310 }
[445e7c0]311 memset(buff, 0, len);
[afe1d1e]312 snprintf(buff, len, "%s", argv[i]);
[216d6fc]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:
[1433ecda]321 if (!rm.recursive) {
[216d6fc]322 printf("%s is a directory, use -r to remove it.\n", buff);
[1433ecda]323 ret++;
[216d6fc]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.