Skip to content

Commit 21a4542

Browse files
committed
[msh] rm command supports recursive deletion of folders
1 parent aeff91b commit 21a4542

File tree

1 file changed

+113
-4
lines changed

1 file changed

+113
-4
lines changed

components/finsh/msh_file.c

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,22 +265,131 @@ int cmd_cat(int argc, char **argv)
265265
}
266266
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cat, __cmd_cat, Concatenate FILE(s));
267267

268+
static void directory_delete_for_msh(const char *pathname, char f, char v)
269+
{
270+
DIR *dir = NULL;
271+
struct dirent *dirent = NULL;
272+
char *full_path;
273+
274+
if (pathname == RT_NULL)
275+
return;
276+
277+
full_path = (char *)rt_malloc(DFS_PATH_MAX);
278+
if (full_path == RT_NULL)
279+
return;
280+
281+
dir = opendir(pathname);
282+
if (dir == RT_NULL)
283+
{
284+
if (f == 0)
285+
{
286+
rt_kprintf("cannot remove '%s'\n", pathname);
287+
}
288+
rt_free(full_path);
289+
return;
290+
}
291+
292+
while (1)
293+
{
294+
dirent = readdir(dir);
295+
if (dirent == RT_NULL)
296+
break;
297+
if (rt_strcmp(".", dirent->d_name) != 0 &&
298+
rt_strcmp("..", dirent->d_name) != 0)
299+
{
300+
rt_sprintf(full_path, "%s/%s", pathname, dirent->d_name);
301+
if (dirent->d_type == DT_REG)
302+
{
303+
if (unlink(full_path) != 0)
304+
{
305+
if (f == 0)
306+
rt_kprintf("cannot remove '%s'\n", full_path);
307+
}
308+
else if (v)
309+
{
310+
rt_kprintf("removed '%s'\n", full_path);
311+
}
312+
}
313+
else if (dirent->d_type == DT_DIR)
314+
{
315+
directory_delete_for_msh(full_path, f, v);
316+
}
317+
}
318+
}
319+
closedir(dir);
320+
rt_free(full_path);
321+
if (unlink(pathname) != 0)
322+
{
323+
if (f == 0)
324+
rt_kprintf("cannot remove '%s'\n", pathname);
325+
}
326+
else if (v)
327+
{
328+
rt_kprintf("removed directory '%s'\n", pathname);
329+
}
330+
}
331+
268332
int cmd_rm(int argc, char **argv)
269333
{
270-
int index;
334+
int index, n;
335+
char f = 0, r = 0, v = 0;
271336

272337
if (argc == 1)
273338
{
274-
rt_kprintf("Usage: rm FILE...\n");
339+
rt_kprintf("Usage: rm option(s) FILE...\n");
275340
rt_kprintf("Remove (unlink) the FILE(s).\n");
276341
return 0;
277342
}
278343

279-
for (index = 1; index < argc; index ++)
344+
if (argv[1][0] == '-')
280345
{
281-
unlink(argv[index]);
346+
for (n = 0; argv[1][n]; n++)
347+
{
348+
switch (argv[1][n])
349+
{
350+
case 'f': f = 1; break;
351+
case 'r': r = 1; break;
352+
case 'v': v = 1; break;
353+
case '-': break;
354+
default:
355+
rt_kprintf("Error: Bad option: %c\n", argv[1][n]);
356+
return 0;
357+
}
358+
}
359+
argc -= 1;
360+
argv = argv + 1;
282361
}
283362

363+
for (index = 1; index < argc; index ++)
364+
{
365+
struct stat s;
366+
if (stat (argv[index], &s) == 0)
367+
{
368+
if (s.st_mode & S_IFDIR)
369+
{
370+
if (r == 0)
371+
rt_kprintf("cannot remove '%s': Is a directory\n", argv[index]);
372+
else
373+
directory_delete_for_msh(argv[index], f, v);
374+
}
375+
else if (s.st_mode & S_IFREG)
376+
{
377+
if (unlink(argv[index]) != 0)
378+
{
379+
if (f == 0)
380+
rt_kprintf("cannot remove '%s'\n", argv[index]);
381+
}
382+
else if (v)
383+
{
384+
rt_kprintf("removed '%s'\n", argv[index]);
385+
}
386+
}
387+
}
388+
else if (f == 0)
389+
{
390+
rt_kprintf("cannot remove '%s': No such file or directory\n", argv[index]);
391+
}
392+
}
284393
return 0;
285394
}
286395
FINSH_FUNCTION_EXPORT_ALIAS(cmd_rm, __cmd_rm, Remove(unlink) the FILE(s).);

0 commit comments

Comments
 (0)