From 7b5187973ae9a5d12b8fe554bdde4e65f97a5b50 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Wed, 14 Jun 2017 13:29:16 +0200 Subject: [PATCH] shell: mufs: add proper mounting and unmounting --- include/fs/mufs.h | 3 ++- source/fs/mufs/super.c | 24 ++++++++++++++++++------ source/shell/mufs.c | 42 ++++++++++++++++++++++++++++++------------ 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/include/fs/mufs.h b/include/fs/mufs.h index 3c05bbe..378cc3b 100644 --- a/include/fs/mufs.h +++ b/include/fs/mufs.h @@ -38,7 +38,8 @@ struct mufs_stat { #define MUFS_FILE BIT(0) #define MUFS_DIR BIT(1) -int mufs_mount(struct mufs *fs, struct flash_dev *dev); +struct mufs *mufs_mount(struct flash_dev *dev); +void mufs_unmount(struct mufs *fs); int mufs_format(struct flash_dev *dev); char *mufs_abspath(const char *path); diff --git a/source/fs/mufs/super.c b/source/fs/mufs/super.c index 74bd798..e413cf6 100644 --- a/source/fs/mufs/super.c +++ b/source/fs/mufs/super.c @@ -17,18 +17,22 @@ struct mufs_super { struct mufs_dtree root; } __attribute__((packed)); -int mufs_mount(struct mufs *fs, struct flash_dev *dev) +struct mufs *mufs_mount(struct flash_dev *dev) { struct mufs_super super; + struct mufs *fs; - if (!fs || !dev) - return -1; + if (!dev) + return NULL; if (flash_read(dev, 0, &super, sizeof super) == 0) - return -1; + return NULL; if (memcmp(super.magic, "mufs", 4) != 0) - return -1; + return NULL; + + if (!(fs = malloc(sizeof *fs))) + return NULL; fs->dev = dev; fs->nblocks = super.nblocks; @@ -40,7 +44,15 @@ int mufs_mount(struct mufs *fs, struct flash_dev *dev) fs->root.root = super.root.root; fs->root.depth = super.root.depth; - return 0; + return fs; +} + +void mufs_unmount(struct mufs *fs) +{ + if (!fs) + return; + + free(fs); } int mufs_format(struct flash_dev *dev) diff --git a/source/shell/mufs.c b/source/shell/mufs.c index f032cf1..3cc313a 100644 --- a/source/shell/mufs.c +++ b/source/shell/mufs.c @@ -15,6 +15,7 @@ #include static void do_mufs_mount(const char *s); +static void do_mufs_unmount(const char *s); static void do_mufs_format(const char *s); static void do_mufs_mkdir(const char *s); static void do_mufs_rmdir(const char *s); @@ -40,7 +41,7 @@ static struct cmd mufs_cmds[] = { }; extern struct flash_dev *flash; -struct mufs mufs; +struct mufs *mufs; static void do_mufs_mount(const char *s) { @@ -51,13 +52,30 @@ static void do_mufs_mount(const char *s) return; } - if (mufs_mount(&mufs, flash) < 0) { + do_mufs_unmount(NULL); + + if (!(mufs = mufs_mount(flash))) { fprintf(stderr, "error: unable to mount the filesystem.\n"); return; } - printf("log2_nentries=%u\n", mufs.log2_nentries); - printf("nentries=%u\n", UINT32_C(1) << mufs.log2_nentries); + flash = NULL; +} + +static void do_mufs_unmount(const char *s) +{ + (void)s; + + if (!mufs) + return; + + if (flash) { + flash_release(flash); + flash = mufs->dev; + } + + mufs_unmount(mufs); + mufs = NULL; } static void do_mufs_format(const char *s) @@ -77,7 +95,7 @@ static void do_mufs_format(const char *s) static void do_mufs_mkdir(const char *s) { - if (mufs_mkdir(&mufs, s) < 0) { + if (mufs_mkdir(mufs, s) < 0) { fprintf(stderr, "error: unable to create the directory\n"); return; } @@ -85,7 +103,7 @@ static void do_mufs_mkdir(const char *s) static void do_mufs_rmdir(const char *s) { - if (mufs_rmdir(&mufs, s) < 0) { + if (mufs_rmdir(mufs, s) < 0) { fprintf(stderr, "error: unable to remove the directory\n"); return; } @@ -95,7 +113,7 @@ static void do_mufs_stat(const char *s) { struct mufs_stat stat; - if (mufs_stat(&mufs, s, &stat) < 0) { + if (mufs_stat(mufs, s, &stat) < 0) { fprintf(stderr, "error: unable to stat the file\n"); return; } @@ -114,7 +132,7 @@ static void do_mufs_cat(const char *s) char data[256]; struct mufs_file *file; - if (!(file = mufs_open(&mufs, s, MUFS_READ))) { + if (!(file = mufs_open(mufs, s, MUFS_READ))) { fprintf(stderr, "error: unable to open the file\n"); return; } @@ -143,7 +161,7 @@ static void do_mufs_append(const char *s) *line++ = '\0'; - if (!(file = mufs_open(&mufs, path, MUFS_WRITE | MUFS_APPEND))) { + if (!(file = mufs_open(mufs, path, MUFS_WRITE | MUFS_APPEND))) { fprintf(stderr, "error: unable to open the file\n"); goto err_free_path; } @@ -170,7 +188,7 @@ static void do_mufs_mv(const char *s) *new++ = '\0'; - if (mufs_rename(&mufs, old, new) < 0) { + if (mufs_rename(mufs, old, new) < 0) { fprintf(stderr, "error: unable to move the file\n"); goto err_free_old; } @@ -181,7 +199,7 @@ err_free_old: static void do_mufs_rm(const char *s) { - if (mufs_unlink(&mufs, s) < 0) { + if (mufs_unlink(mufs, s) < 0) { fprintf(stderr, "error: unable to remove the file\n"); return; } @@ -192,7 +210,7 @@ static void do_mufs_ls(const char *s) struct mufs_dirent ent; struct mufs_dir *dir; - if (!(dir = mufs_opendir(&mufs, s))) { + if (!(dir = mufs_opendir(mufs, s))) { fprintf(stderr, "error: unable to open the directory\n"); return; }