From 26b0ccb4051d1f1f5de7a1509450be475041e0ec Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Mon, 12 Jun 2017 13:48:43 +0200 Subject: [PATCH] mufs: optionally record the type and file_size in resolve_path() --- include/fs/mufs.h | 5 +++++ source/fs/mufs/dir.c | 24 +++++++++++++++++++----- source/fs/mufs/dir.h | 3 ++- source/fs/mufs/file.c | 3 ++- source/fs/mufs/path.c | 5 +++-- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/include/fs/mufs.h b/include/fs/mufs.h index 3bd697a..0e990c5 100644 --- a/include/fs/mufs.h +++ b/include/fs/mufs.h @@ -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) diff --git a/source/fs/mufs/dir.c b/source/fs/mufs/dir.c index 2c9639c..c999fad 100644 --- a/source/fs/mufs/dir.c +++ b/source/fs/mufs/dir.c @@ -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; diff --git a/source/fs/mufs/dir.h b/source/fs/mufs/dir.h index 453105c..5596fcc 100644 --- a/source/fs/mufs/dir.h +++ b/source/fs/mufs/dir.h @@ -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, diff --git a/source/fs/mufs/file.c b/source/fs/mufs/file.c index 0097e60..9c146af 100644 --- a/source/fs/mufs/file.c +++ b/source/fs/mufs/file.c @@ -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; diff --git a/source/fs/mufs/path.c b/source/fs/mufs/path.c index 94c3529..d7869e0 100644 --- a/source/fs/mufs/path.c +++ b/source/fs/mufs/path.c @@ -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);