From f2d4d60c018ad264a2285a30a10b83434b043166 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Wed, 14 Jun 2017 06:20:11 +0200 Subject: [PATCH] mufs: add find_dirent_size() to determine how many bytes are used within a directory bin --- source/fs/mufs/dir.c | 33 +++++++++++++++++++++++++++++++++ source/fs/mufs/dir.h | 2 ++ source/fs/mufs/path.c | 2 ++ 3 files changed, 37 insertions(+) diff --git a/source/fs/mufs/dir.c b/source/fs/mufs/dir.c index d4a1edb..6eb0d8e 100644 --- a/source/fs/mufs/dir.c +++ b/source/fs/mufs/dir.c @@ -182,6 +182,39 @@ void mufs_closedir(struct mufs_dir *dir) free(dir); } +size_t find_dirent_size(struct mufs *fs, struct mufs_tree *tree, + uint32_t va) +{ + struct flash_dev *dev = fs->dev; + size_t block_size = 1 << dev->log2_block_size; + char data[block_size]; + struct mufs_dentry *entry; + uint32_t offset, len; + + va = align(va, dev->log2_block_size); + + if (mufs_tree_read(tree, data, va, 1 << dev->log2_block_size) == 0) + return 0; + + for (offset = 0; offset < block_size; offset += len) { + entry = (struct mufs_dentry *)(data + offset); + len = sizeof *entry; + + if (block_size - offset < len) + break; + + if (!entry->type) + break; + + len += entry->path_len; + + if (block_size - offset < len) + break; + } + + return offset; +} + size_t read_dirent(struct mufs *fs, struct mufs_tree *tree, struct mufs_dirent *dirent, uint32_t va) { diff --git a/source/fs/mufs/dir.h b/source/fs/mufs/dir.h index bb50def..a9f09d0 100644 --- a/source/fs/mufs/dir.h +++ b/source/fs/mufs/dir.h @@ -20,6 +20,8 @@ struct mufs_dentry { struct mufs_tree *resolve_path(struct mufs *fs, const char *path, struct mufs_stat *stat); +size_t find_dirent_size(struct mufs *fs, struct mufs_tree *tree, + uint32_t va); 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/path.c b/source/fs/mufs/path.c index 4210b69..6a1b4ff 100644 --- a/source/fs/mufs/path.c +++ b/source/fs/mufs/path.c @@ -53,6 +53,8 @@ int mufs_mkpath(struct mufs *fs, const char *path, unsigned type) dirent.tree.root = 0; dirent.tree.depth = 0; + tree->file_size = find_dirent_size(fs, tree, tree->file_size); + if (!is_aligned(tree->file_size, fs->dev->log2_block_size) && write_dirent(tree, tree->file_size, &dirent) > 0) return 0;