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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d96d9bc was d96d9bc, checked in by Jakub Jermar <jakub@…>, 8 years ago

Rename chdir() to vfs_cwd_set() and getcwd() to vfs_cwd_get()

  • Property mode set to 100644
File size: 7.5 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
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;
[445e7c0]101 memset(rm->nwd, 0, PATH_MAX);
[216d6fc]102
103 if (NULL == (rm->owd = (char *) malloc(PATH_MAX)))
104 return 0;
[445e7c0]105 memset(rm->owd, 0, PATH_MAX);
[216d6fc]106
107 if (NULL == (rm->cwd = (char *) malloc(PATH_MAX)))
108 return 0;
[445e7c0]109 memset(rm->cwd, 0, PATH_MAX);
[216d6fc]110
[d96d9bc]111 vfs_cwd_set(".");
[216d6fc]112
[d96d9bc]113 if (EOK != vfs_cwd_get(rm->owd, PATH_MAX))
[216d6fc]114 return 0;
115
116 return 1;
117}
118
[b510d52]119static void rm_end(rm_job_t *rm)
[216d6fc]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
[081d60f]131static unsigned int rm_single(const char *path)
132{
[79ea5af]133 if (vfs_unlink_path(path) != EOK) {
[081d60f]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
[b19e892]151 fd = vfs_lookup(path, WALK_REGULAR);
[6afc9d7]152 if (fd >= 0) {
[9c4cf0d]153 vfs_put(fd);
[081d60f]154 return RM_FILE;
155 }
156
157 return RM_BOGUS;
158}
159
[ed2c8ff]160static unsigned int rm_recursive_not_empty_dirs(const char *path)
161{
162 DIR *dirp;
163 struct dirent *dp;
164 char buff[PATH_MAX];
165 unsigned int scope;
166 unsigned int ret = 0;
167
168 dirp = opendir(path);
169 if (!dirp) {
170 /* May have been deleted between scoping it and opening it */
171 cli_error(CL_EFAIL, "Could not open %s", path);
172 return ret;
173 }
174
175 memset(buff, 0, sizeof(buff));
176 while ((dp = readdir(dirp))) {
177 snprintf(buff, PATH_MAX - 1, "%s/%s", path, dp->d_name);
178 scope = rm_scope(buff);
179 switch (scope) {
180 case RM_BOGUS:
181 break;
182 case RM_FILE:
183 ret += rm_single(buff);
184 break;
185 case RM_DIR:
186 ret += rm_recursive(buff);
187 break;
188 }
189 }
[713ea96a]190
191 closedir(dirp);
[ed2c8ff]192
193 return ret;
194}
195
[b510d52]196static unsigned int rm_recursive(const char *path)
[216d6fc]197{
198 int rc;
[ed2c8ff]199 unsigned int ret = 0;
[216d6fc]200
201 /* First see if it will just go away */
[79ea5af]202 rc = vfs_unlink_path(path);
203 if (rc == EOK)
[216d6fc]204 return 0;
205
206 /* Its not empty, recursively scan it */
[ed2c8ff]207 ret = rm_recursive_not_empty_dirs(path);
208
209 /* Delete directory */
[79ea5af]210 rc = vfs_unlink_path(path);
211 if (rc == EOK)
212 return EOK;
[ed2c8ff]213
214 cli_error(CL_ENOTSUP, "Can not remove %s", path);
215
216 return ret + 1;
[216d6fc]217}
218
219/* Dispays help for rm in various levels */
[809813d]220void help_cmd_rm(unsigned int level)
[216d6fc]221{
222 if (level == HELP_SHORT) {
223 printf("`%s' removes files and directories.\n", cmdname);
224 } else {
225 help_cmd_rm(HELP_SHORT);
226 printf(
227 "Usage: %s [options] <path>\n"
228 "Options:\n"
229 " -h, --help A short option summary\n"
230 " -v, --version Print version information and exit\n"
231 " -r, --recursive Recursively remove sub directories\n"
232 " -f, --force Do not prompt prior to removing files\n"
233 " -s, --safe Stop if directories change during removal\n\n"
234 "Currently, %s is under development, some options don't work.\n",
235 cmdname, cmdname);
236 }
[809813d]237 return;
[216d6fc]238}
239
240/* Main entry point for rm, accepts an array of arguments */
[809813d]241int cmd_rm(char **argv)
[216d6fc]242{
243 unsigned int argc;
244 unsigned int i, scope, ret = 0;
245 int c, opt_ind;
246 size_t len;
247 char *buff = NULL;
248
[43e02a6]249 argc = cli_count_args(argv);
250
[216d6fc]251 if (argc < 2) {
252 cli_error(CL_EFAIL,
253 "%s: insufficient arguments. Try %s --help", cmdname, cmdname);
254 return CMD_FAILURE;
255 }
256
257 if (!rm_start(&rm)) {
258 cli_error(CL_ENOMEM, "%s: could not initialize", cmdname);
259 rm_end(&rm);
260 return CMD_FAILURE;
261 }
262
[86cf96d]263 for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) {
[216d6fc]264 c = getopt_long(argc, argv, "hvrfs", long_options, &opt_ind);
265 switch (c) {
266 case 'h':
267 help_cmd_rm(HELP_LONG);
268 return CMD_SUCCESS;
269 case 'v':
270 printf("%s\n", RM_VERSION);
271 return CMD_SUCCESS;
272 case 'r':
273 rm.recursive = 1;
274 break;
275 case 'f':
276 rm.force = 1;
277 break;
278 case 's':
279 rm.safe = 1;
280 break;
281 }
282 }
283
[5b3cf90]284 if ((unsigned) optind == argc) {
[216d6fc]285 cli_error(CL_EFAIL,
286 "%s: insufficient arguments. Try %s --help", cmdname, cmdname);
287 rm_end(&rm);
288 return CMD_FAILURE;
289 }
290
291 i = optind;
292 while (NULL != argv[i]) {
[92fd52d7]293 len = str_size(argv[i]) + 2;
[216d6fc]294 buff = (char *) realloc(buff, len);
[b7be230]295 if (buff == NULL) {
296 printf("rm: out of memory\n");
297 ret = 1;
298 break;
299 }
[445e7c0]300 memset(buff, 0, len);
[afe1d1e]301 snprintf(buff, len, "%s", argv[i]);
[216d6fc]302
303 scope = rm_scope(buff);
304 switch (scope) {
305 case RM_BOGUS: /* FIXME */
306 case RM_FILE:
307 ret += rm_single(buff);
308 break;
309 case RM_DIR:
310 if (! rm.recursive) {
311 printf("%s is a directory, use -r to remove it.\n", buff);
312 ret ++;
313 } else {
314 ret += rm_recursive(buff);
315 }
316 break;
317 }
318 i++;
319 }
320
321 if (NULL != buff)
322 free(buff);
323
324 rm_end(&rm);
325
326 if (ret)
327 return CMD_FAILURE;
328 else
329 return CMD_SUCCESS;
330}
331
Note: See TracBrowser for help on using the repository browser.