mufs: optionally record the type and file_size in resolve_path()

tags/0.1.0
S.J.R. van Schaik 7 years ago
parent d9e11a083b
commit 26b0ccb405
  1. 5
      include/fs/mufs.h
  2. 24
      source/fs/mufs/dir.c
  3. 3
      source/fs/mufs/dir.h
  4. 3
      source/fs/mufs/file.c
  5. 5
      source/fs/mufs/path.c

@ -26,6 +26,11 @@ struct mufs_dirent {
uint8_t type;
};
struct mufs_stat {
uint8_t type;
uint32_t file_size;
};
#define MUFS_READ BIT(0)
#define MUFS_WRITE BIT(1)
#define MUFS_APPEND BIT(2)

@ -93,7 +93,8 @@ err_free_dir:
return NULL;
}
struct mufs_tree *resolve_path(struct mufs *fs, const char *path)
struct mufs_tree *resolve_path(struct mufs *fs, const char *path,
struct mufs_stat *stat)
{
struct mufs_dir *dir = NULL;
struct mufs_dirent entry, *found = NULL;
@ -106,6 +107,12 @@ struct mufs_tree *resolve_path(struct mufs *fs, const char *path)
if (strcmp(s, "/") == 0) {
tree = &fs->root;
if (stat) {
stat->type = MUFS_DIR;
stat->file_size = tree->file_size;
}
goto err_free_s;
}
@ -130,9 +137,15 @@ struct mufs_tree *resolve_path(struct mufs *fs, const char *path)
goto err_closedir;
}
if (dir) {
tree = dir->tree;
dir->tree = NULL;
if (!dir)
goto err_free_s;
tree = dir->tree;
dir->tree = NULL;
if (stat) {
stat->type = entry.type;
stat->file_size = tree->file_size;
}
err_closedir:
@ -144,6 +157,7 @@ err_free_s:
struct mufs_dir *mufs_opendir(struct mufs *fs, const char *path)
{
struct mufs_stat stat;
struct mufs_dir *dir;
if (!(dir = malloc(sizeof(*dir))))
@ -151,7 +165,7 @@ struct mufs_dir *mufs_opendir(struct mufs *fs, const char *path)
dir->fs = fs;
if (!(dir->tree = resolve_path(fs, path)))
if (!(dir->tree = resolve_path(fs, path, &stat)) || stat.type != MUFS_DIR)
goto err_free_dir;
dir->va = 0;

@ -15,7 +15,8 @@ struct mufs_dentry {
#define MUFS_FILE BIT(0)
#define MUFS_DIR BIT(1)
struct mufs_tree *resolve_path(struct mufs *fs, const char *path);
struct mufs_tree *resolve_path(struct mufs *fs, const char *path,
struct mufs_stat *stat);
size_t read_dirent(struct mufs *fs, struct mufs_tree *tree,
struct mufs_dirent *dirent, uint32_t va);
size_t write_dirent(struct mufs_tree *tree,

@ -26,6 +26,7 @@ int mufs_create(struct mufs *fs, const char *path)
struct mufs_file *mufs_open(struct mufs *fs, const char *path, int mode)
{
struct mufs_stat stat;
struct mufs_file *file;
if (!(file = malloc(sizeof(*file))))
@ -33,7 +34,7 @@ struct mufs_file *mufs_open(struct mufs *fs, const char *path, int mode)
file->fs = fs;
if (!(file->tree = resolve_path(fs, path)))
if (!(file->tree = resolve_path(fs, path, &stat)) || stat.type != MUFS_FILE)
goto err_free_file;
file->mode = mode;

@ -15,6 +15,7 @@
int mufs_mkpath(struct mufs *fs, const char *path, unsigned type)
{
struct mufs_stat stat;
struct mufs_dirent dirent;
struct mufs_tree *tree;
size_t len;
@ -28,7 +29,7 @@ int mufs_mkpath(struct mufs *fs, const char *path, unsigned type)
return -1;
/* TODO: already exists. */
if (resolve_path(fs, s)) {
if (resolve_path(fs, s, NULL)) {
free(s);
return 0;
}
@ -42,7 +43,7 @@ int mufs_mkpath(struct mufs *fs, const char *path, unsigned type)
if (*name == '\0')
return -1;
if (!(tree = resolve_path(fs, s)))
if (!(tree = resolve_path(fs, s, &stat)) || stat.type != MUFS_DIR)
return -1;
len = min(strlen(name), sizeof(dirent.path) - 1);

Loading…
Cancel
Save