From 81f172039abe16b6adeed3064eaefedd9bc8085e Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Mon, 11 Apr 2022 12:55:38 +0200 Subject: [PATCH] sys/shell/vfs: add umount, rmount and mkdir commands --- sys/shell/commands/sc_vfs.c | 73 +++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/sys/shell/commands/sc_vfs.c b/sys/shell/commands/sc_vfs.c index 98c15b00c23b..5ee0d269f7c8 100644 --- a/sys/shell/commands/sc_vfs.c +++ b/sys/shell/commands/sc_vfs.c @@ -57,18 +57,26 @@ static void _vfs_usage(char **argv) printf("%s ls \n", argv[0]); printf("%s cp \n", argv[0]); printf("%s mv \n", argv[0]); + printf("%s mkdir \n", argv[0]); printf("%s rm \n", argv[0]); printf("%s df [path]\n", argv[0]); if (MOUNTPOINTS_NUMOF > 0) { printf("%s mount [path]\n", argv[0]); } + if (MOUNTPOINTS_NUMOF > 0) { + printf("%s umount [path]\n", argv[0]); + } + if (MOUNTPOINTS_NUMOF > 0) { + printf("%s remount [path]\n", argv[0]); + } puts("r: Read [bytes] bytes at [offset] in file "); puts("w: Write (: append, overwrite) or string in file "); - puts("ls: list files in "); + puts("ls: List files in "); puts("mv: Move file to "); + puts("mkdir: Create directory "); puts("cp: Copy file to "); puts("rm: Unlink (delete) "); - puts("df: show file system space utilization stats"); + puts("df: Show file system space utilization stats"); } /* Macro used by _errno_string to expand errno labels to string and print it */ @@ -167,8 +175,39 @@ static int _mount_handler(int argc, char **argv) return -1; } + uint8_t buf[16]; + int res = vfs_mount_by_path(argv[1]); + _errno_string(res, (char *)buf, sizeof(buf)); + return res; +} + +static int _umount_handler(int argc, char **argv) +{ + if (argc < 2) { + printf("usage: %s [path]\n", argv[0]); + puts("umount pre-configured mount point"); + return -1; + } + + uint8_t buf[16]; + int res = vfs_unmount_by_path(argv[1]); + + _errno_string(res, (char *)buf, sizeof(buf)); + return res; +} + +static int _remount_handler(int argc, char **argv) +{ + if (argc < 2) { + printf("usage: %s [path]\n", argv[0]); + puts("remount pre-configured mount point"); + return -1; + } + + uint8_t buf[16]; + vfs_unmount_by_path(argv[1]); int res = vfs_mount_by_path(argv[1]); - puts(strerror(res)); + _errno_string(res, (char *)buf, sizeof(buf)); return res; } @@ -528,6 +567,25 @@ static int _rm_handler(int argc, char **argv) return 0; } +static int _mkdir_handler(int argc, char **argv) +{ + if (argc < 2) { + _vfs_usage(argv); + return 1; + } + char *dir_name = argv[1]; + printf("%s: mkdir: %s\n", argv[0], dir_name); + + int res = vfs_mkdir(dir_name, 0); + if (res < 0) { + char errbuf[16]; + _errno_string(res, (char *)errbuf, sizeof(errbuf)); + printf("mkdir ERR: %s\n", errbuf); + return 2; + } + return 0; +} + int _ls_handler(int argc, char **argv) { if (argc < 2) { @@ -620,6 +678,9 @@ int _vfs_handler(int argc, char **argv) else if (strcmp(argv[1], "mv") == 0) { return _mv_handler(argc - 1, &argv[1]); } + else if (strcmp(argv[1], "mkdir") == 0) { + return _mkdir_handler(argc - 1, &argv[1]); + } else if (strcmp(argv[1], "rm") == 0) { return _rm_handler(argc - 1, &argv[1]); } @@ -629,6 +690,12 @@ int _vfs_handler(int argc, char **argv) else if (MOUNTPOINTS_NUMOF > 0 && strcmp(argv[1], "mount") == 0) { return _mount_handler(argc - 1, &argv[1]); } + else if (MOUNTPOINTS_NUMOF > 0 && strcmp(argv[1], "umount") == 0) { + return _umount_handler(argc - 1, &argv[1]); + } + else if (MOUNTPOINTS_NUMOF > 0 && strcmp(argv[1], "remount") == 0) { + return _remount_handler(argc - 1, &argv[1]); + } else { printf("vfs: unsupported sub-command \"%s\"\n", argv[1]); return 1;