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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b7fd2a0 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • 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 for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) {
263 c = getopt_long(argc, argv, "hvrfs", long_options, &opt_ind);
264 switch (c) {
265 case 'h':
266 help_cmd_rm(HELP_LONG);
267 return CMD_SUCCESS;
268 case 'v':
269 printf("%s\n", RM_VERSION);
270 return CMD_SUCCESS;
271 case 'r':
272 rm.recursive = 1;
273 break;
274 case 'f':
275 rm.force = 1;
276 break;
277 case 's':
278 rm.safe = 1;
279 break;
280 }
281 }
282
283 if ((unsigned) optind == argc) {
284 cli_error(CL_EFAIL,
285 "%s: insufficient arguments. Try %s --help", cmdname, cmdname);
286 rm_end(&rm);
287 return CMD_FAILURE;
288 }
289
290 i = optind;
291 while (NULL != argv[i]) {
292 len = str_size(argv[i]) + 2;
293 buff = (char *) realloc(buff, len);
294 if (buff == NULL) {
295 printf("rm: out of memory\n");
296 ret = 1;
297 break;
298 }
299 memset(buff, 0, len);
300 snprintf(buff, len, "%s", argv[i]);
301
302 scope = rm_scope(buff);
303 switch (scope) {
304 case RM_BOGUS: /* FIXME */
305 case RM_FILE:
306 ret += rm_single(buff);
307 break;
308 case RM_DIR:
309 if (! rm.recursive) {
310 printf("%s is a directory, use -r to remove it.\n", buff);
311 ret ++;
312 } else {
313 ret += rm_recursive(buff);
314 }
315 break;
316 }
317 i++;
318 }
319
320 if (NULL != buff)
321 free(buff);
322
323 rm_end(&rm);
324
325 if (ret)
326 return CMD_FAILURE;
327 else
328 return CMD_SUCCESS;
329}
330
Note: See TracBrowser for help on using the repository browser.