Skip to content

Commit

Permalink
sys/shell/vfs: add umount and rmount
Browse files Browse the repository at this point in the history
  • Loading branch information
fjmolinas committed Apr 15, 2022
1 parent 706e53e commit 33c02b4
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
15 changes: 15 additions & 0 deletions sys/include/vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,21 @@ int vfs_mount(vfs_mount_t *mountp);
*/
int vfs_mount_by_path(const char *path);

/**
* @brief unmount a file system with a pre-configured mount path
*
* @note This assumes mount points have been configured with @ref VFS_AUTO_MOUNT.
*
* @warning If the @ref pseudomodule_vfs_auto_format is used a format attempt will
* be made if the mount fails.
*
* @param[in] path Path of the pre-configured mount point
*
* @return 0 on success
* @return <0 on error
*/
int vfs_umount_by_path(const char *path);

/**
* @brief Rename a file
*
Expand Down
69 changes: 67 additions & 2 deletions sys/shell/commands/sc_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,26 @@ static void _vfs_usage(char **argv)
printf("%s ls <path>\n", argv[0]);
printf("%s cp <src> <dest>\n", argv[0]);
printf("%s mv <src> <dest>\n", argv[0]);
printf("%s md <path> \n", argv[0]);
printf("%s rm <file>\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 rmount [path]\n", argv[0]);
}
puts("r: Read [bytes] bytes at [offset] in file <path>");
puts("w: Write (<a>: append, <o> overwrite) <ascii> or <hex> string <data> in file <path>");
puts("ls: list files in <path>");
puts("ls: List files in <path>");
puts("mv: Move <src> file to <dest>");
puts("md: Create directory <path> ");
puts("cp: Copy <src> file to <dest>");
puts("rm: Unlink (delete) <file>");
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 */
Expand Down Expand Up @@ -172,6 +180,35 @@ static int _mount_handler(int argc, char **argv)
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;
}

int res = vfs_umount_by_path(argv[1]);
puts(strerror(res));
return res;
}

static int _rmount_handler(int argc, char **argv)
{
if (argc < 2) {
printf("usage: %s [path]\n", argv[0]);
puts("rmount pre-configured mount point");
return -1;
}

int res = vfs_umount_by_path(argv[1]);
if (res == 0) {
res = vfs_mount_by_path(argv[1]);
}
puts(strerror(res));
return res;
}

static int _read_handler(int argc, char **argv)
{
uint8_t buf[16];
Expand Down Expand Up @@ -528,6 +565,25 @@ static int _rm_handler(int argc, char **argv)
return 0;
}

static int _md_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("md ERR: %s\n", errbuf);
return 2;
}
return 0;
}

int _ls_handler(int argc, char **argv)
{
if (argc < 2) {
Expand Down Expand Up @@ -620,6 +676,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], "md") == 0) {
return _md_handler(argc - 1, &argv[1]);
}
else if (strcmp(argv[1], "rm") == 0) {
return _rm_handler(argc - 1, &argv[1]);
}
Expand All @@ -629,6 +688,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], "rmount") == 0) {
return _rmount_handler(argc - 1, &argv[1]);
}
else {
printf("vfs: unsupported sub-command \"%s\"\n", argv[1]);
return 1;
Expand Down
27 changes: 27 additions & 0 deletions sys/vfs/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,22 @@ static int _auto_mount(vfs_mount_t *mountp, unsigned i)
return res;
}

static int _auto_umount(vfs_mount_t *mountp, unsigned i)
{
(void) i;
DEBUG("vfs%u: unmounting as '%s'\n", i, mountp->mount_point);
int res = vfs_umount(mountp);
if (res == 0) {
return 0;
}

if (res) {
DEBUG("vfs%u umount: error %d\n", i, res);
}

return res;
}

void auto_init_vfs(void)
{
for (unsigned i = 0; i < MOUNTPOINTS_NUMOF; ++i) {
Expand All @@ -1151,4 +1167,15 @@ int vfs_mount_by_path(const char *path)
return -ENOENT;
}

int vfs_umount_by_path(const char *path)
{
for (unsigned i = 0; i < MOUNTPOINTS_NUMOF; ++i) {
if (strcmp(path, vfs_mountpoints_xfa[i].mount_point) == 0) {
return _auto_umount(&vfs_mountpoints_xfa[i], i);
}
}

return -ENOENT;
}

/** @} */

0 comments on commit 33c02b4

Please sign in to comment.