shell: mufs: add proper mounting and unmounting

tags/0.1.0
S.J.R. van Schaik 7 years ago
parent 92fc0e0ff1
commit 7b5187973a
  1. 3
      include/fs/mufs.h
  2. 24
      source/fs/mufs/super.c
  3. 42
      source/shell/mufs.c

@ -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);

@ -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)

@ -15,6 +15,7 @@
#include <fs/mufs.h>
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;
}

Loading…
Cancel
Save