Skip to content

Commit 991c25d

Browse files
committed
[finsh] add mount/umount cmd
1 parent 45963a1 commit 991c25d

File tree

1 file changed

+93
-13
lines changed

1 file changed

+93
-13
lines changed

components/finsh/msh_file.c

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ int msh_exec_script(const char *cmd_line, int size)
101101
int length;
102102

103103
line_buf = (char *) rt_malloc(RT_CONSOLEBUF_SIZE);
104-
if (line_buf == RT_NULL)
104+
if (line_buf == RT_NULL)
105105
{
106106
close(fd);
107107
return -RT_ENOMEM;
@@ -295,7 +295,7 @@ static void directory_delete_for_msh(const char *pathname, char f, char v)
295295
if (dirent == RT_NULL)
296296
break;
297297
if (rt_strcmp(".", dirent->d_name) != 0 &&
298-
rt_strcmp("..", dirent->d_name) != 0)
298+
rt_strcmp("..", dirent->d_name) != 0)
299299
{
300300
rt_sprintf(full_path, "%s/%s", pathname, dirent->d_name);
301301
if (dirent->d_type == DT_REG)
@@ -347,13 +347,20 @@ int cmd_rm(int argc, char **argv)
347347
{
348348
switch (argv[1][n])
349349
{
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;
350+
case 'f':
351+
f = 1;
352+
break;
353+
case 'r':
354+
r = 1;
355+
break;
356+
case 'v':
357+
v = 1;
358+
break;
359+
case '-':
360+
break;
361+
default:
362+
rt_kprintf("Error: Bad option: %c\n", argv[1][n]);
363+
return 0;
357364
}
358365
}
359366
argc -= 1;
@@ -363,7 +370,7 @@ int cmd_rm(int argc, char **argv)
363370
for (index = 1; index < argc; index ++)
364371
{
365372
struct stat s;
366-
if (stat (argv[index], &s) == 0)
373+
if (stat(argv[index], &s) == 0)
367374
{
368375
if (s.st_mode & S_IFDIR)
369376
{
@@ -469,8 +476,81 @@ int cmd_mkfs(int argc, char **argv)
469476
}
470477
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkfs, __cmd_mkfs, format disk with file system);
471478

479+
extern struct dfs_filesystem filesystem_table[];
480+
int cmd_mount(int argc, char *argv[])
481+
{
482+
if (argc == 1)
483+
{
484+
struct dfs_filesystem *iter;
485+
486+
/* display the mount history */
487+
rt_kprintf("filesystem device mountpoint\n");
488+
rt_kprintf("---------- ------ ----------\n");
489+
for (iter = &filesystem_table[0];
490+
iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
491+
{
492+
if ((iter != NULL) && (iter->path != NULL))
493+
{
494+
rt_kprintf("%-10s %-6s %-s\n",
495+
iter->ops->name, iter->dev_id->parent.name, iter->path);
496+
}
497+
}
498+
return 0;
499+
}
500+
else if (argc == 4)
501+
{
502+
char *device = argv[1];
503+
char *path = argv[2];
504+
char *fstype = argv[3];
505+
506+
/* mount a filesystem to the specified directory */
507+
rt_kprintf("mount device %s(%s) onto %s ... ", device, fstype, path);
508+
if (dfs_mount(device, path, fstype, 0, 0) == 0)
509+
{
510+
rt_kprintf("succeed!\n");
511+
return 0;
512+
}
513+
else
514+
{
515+
rt_kprintf("failed!\n");
516+
return -1;
517+
}
518+
}
519+
else
520+
{
521+
rt_kprintf("Usage: mount <device> <mountpoint> <fstype>.\n");
522+
return -1;
523+
}
524+
}
525+
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mount, __cmd_mount, mount <device> <mountpoint> <fstype>);
526+
527+
/* unmount the filesystem from the specified mountpoint */
528+
int cmd_umount(int argc, char *argv[])
529+
{
530+
char *path = argv[1];
531+
532+
if (argc != 2)
533+
{
534+
rt_kprintf("Usage: unmount <mountpoint>.\n");
535+
return -1;
536+
}
537+
538+
rt_kprintf("unmount %s ... ", path);
539+
if (dfs_unmount(path) < 0)
540+
{
541+
rt_kprintf("failed!\n");
542+
return -1;
543+
}
544+
else
545+
{
546+
rt_kprintf("succeed!\n");
547+
return 0;
548+
}
549+
}
550+
FINSH_FUNCTION_EXPORT_ALIAS(cmd_umount, __cmd_umount, Unmount device from file system);
551+
472552
extern int df(const char *path);
473-
int cmd_df(int argc, char** argv)
553+
int cmd_df(int argc, char **argv)
474554
{
475555
if (argc != 2)
476556
{
@@ -492,7 +572,7 @@ int cmd_df(int argc, char** argv)
492572
}
493573
FINSH_FUNCTION_EXPORT_ALIAS(cmd_df, __cmd_df, disk free);
494574

495-
int cmd_echo(int argc, char** argv)
575+
int cmd_echo(int argc, char **argv)
496576
{
497577
if (argc == 2)
498578
{
@@ -505,7 +585,7 @@ int cmd_echo(int argc, char** argv)
505585
fd = open(argv[2], O_RDWR | O_APPEND | O_CREAT, 0);
506586
if (fd >= 0)
507587
{
508-
write (fd, argv[1], strlen(argv[1]));
588+
write(fd, argv[1], strlen(argv[1]));
509589
close(fd);
510590
}
511591
else

0 commit comments

Comments
 (0)