mufs: implement modes for read, write and append in mufs_open()

tags/0.1.0
S.J.R. van Schaik 8 years ago
parent ffc8669736
commit 2c1079d0f4
  1. 6
      include/fs/mufs.h
  2. 13
      source/fs/mufs/file.c

@ -26,6 +26,10 @@ struct mufs_dirent {
uint8_t type; uint8_t type;
}; };
#define MUFS_READ BIT(0)
#define MUFS_WRITE BIT(1)
#define MUFS_APPEND BIT(2)
int mufs_mount(struct mufs *fs, struct flash_dev *dev); int mufs_mount(struct mufs *fs, struct flash_dev *dev);
int mufs_format(struct flash_dev *dev); int mufs_format(struct flash_dev *dev);
@ -36,7 +40,7 @@ int mufs_readdir(struct mufs_dir *dir, struct mufs_dirent *dirent);
int mufs_mkdir(struct mufs *fs, const char *path); int mufs_mkdir(struct mufs *fs, const char *path);
int mufs_create(struct mufs *fs, const char *path); int mufs_create(struct mufs *fs, const char *path);
struct mufs_file *mufs_open(struct mufs *fs, const char *path); struct mufs_file *mufs_open(struct mufs *fs, const char *path, int mode);
void mufs_close(struct mufs_file *file); void mufs_close(struct mufs_file *file);
size_t mufs_read(struct mufs_file *file, void *data, size_t len); size_t mufs_read(struct mufs_file *file, void *data, size_t len);
size_t mufs_write(struct mufs_file *file, const void *data, size_t len); size_t mufs_write(struct mufs_file *file, const void *data, size_t len);

@ -15,6 +15,7 @@
struct mufs_file { struct mufs_file {
struct mufs *fs; struct mufs *fs;
struct mufs_tree *tree; struct mufs_tree *tree;
int mode;
uint32_t va; uint32_t va;
}; };
@ -23,7 +24,7 @@ int mufs_create(struct mufs *fs, const char *path)
return mufs_mkpath(fs, path, MUFS_FILE); return mufs_mkpath(fs, path, MUFS_FILE);
} }
struct mufs_file *mufs_open(struct mufs *fs, const char *path) struct mufs_file *mufs_open(struct mufs *fs, const char *path, int mode)
{ {
struct mufs_file *file; struct mufs_file *file;
@ -35,6 +36,7 @@ struct mufs_file *mufs_open(struct mufs *fs, const char *path)
if (!(file->tree = resolve_path(fs, path))) if (!(file->tree = resolve_path(fs, path)))
goto err_free_file; goto err_free_file;
file->mode = mode;
file->va = 0; file->va = 0;
return file; return file;
@ -59,6 +61,9 @@ size_t mufs_read(struct mufs_file *file, void *data, size_t len)
if (!file || !data || !len) if (!file || !data || !len)
return 0; return 0;
if (!(file->mode & MUFS_READ))
return 0;
if (!(ret = mufs_tree_read(file->tree, data, file->va, len))) if (!(ret = mufs_tree_read(file->tree, data, file->va, len)))
return 0; return 0;
@ -74,6 +79,12 @@ size_t mufs_write(struct mufs_file *file, const void *data, size_t len)
if (!file || !data || !len) if (!file || !data || !len)
return 0; return 0;
if (!(file->mode & MUFS_WRITE))
return 0;
if (file->mode & MUFS_APPEND)
file->va = file->tree->file_size;
if (!(ret = mufs_tree_write(file->tree, (void *)data, file->va, len))) if (!(ret = mufs_tree_write(file->tree, (void *)data, file->va, len)))
return 0; return 0;

Loading…
Cancel
Save