From 233d9913281add6a1fcc530de06f3025ab6e958c Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Mon, 29 May 2017 15:24:59 +0200 Subject: [PATCH] mufs: add mufs_read() to read data using a file tree --- source/fs/mufs/tree.c | 38 ++++++++++++++++++++++++++++++++++++++ source/fs/mufs/tree.h | 2 ++ 2 files changed, 40 insertions(+) diff --git a/source/fs/mufs/tree.c b/source/fs/mufs/tree.c index 8d47633..155089a 100644 --- a/source/fs/mufs/tree.c +++ b/source/fs/mufs/tree.c @@ -1,7 +1,10 @@ #include +#include +#include #include #include +#include #include @@ -157,3 +160,38 @@ int mufs_shrink_tree(struct mufs *fs, struct mufs_tree *tree, uint32_t max_size) return 0; } + +static size_t mufs_do_read(struct mufs *fs, struct mufs_tree *tree, + void *data, uint32_t va, size_t len) +{ + uint32_t page, offset; + + offset = va & ((UINT32_C(1) << fs->dev->log2_block_size) - 1); + va = align(va, fs->dev->log2_block_size); + + memset(data, 0, len); + len = min(len, (UINT32_C(1) << fs->dev->log2_block_size) - offset); + + if (mufs_lookup_page(fs, tree, &page, va) < 0) + return 0; + + return flash_read(fs->dev, page, data, len); +} + +size_t mufs_read(struct mufs *fs, struct mufs_tree *tree, void *data, + uint32_t va, size_t len) +{ + uint8_t *buf = data; + size_t ret, nbytes = 0; + + while (len) { + if ((ret = mufs_do_read(fs, tree, buf, va, len)) == 0) + return nbytes; + + buf += ret; + va += ret; + len -= ret; + } + + return nbytes; +} diff --git a/source/fs/mufs/tree.h b/source/fs/mufs/tree.h index 4d6a801..0604370 100644 --- a/source/fs/mufs/tree.h +++ b/source/fs/mufs/tree.h @@ -7,3 +7,5 @@ int mufs_alloc_page(struct mufs *fs, struct mufs_tree *tree, uint32_t *page, void mufs_free_page(struct mufs *fs, struct mufs_tree *tree, uint32_t va); int mufs_extend_tree(struct mufs *fs, struct mufs_tree *tree, uint8_t depth); int mufs_shrink_tree(struct mufs *fs, struct mufs_tree *tree, uint32_t max_size); +size_t mufs_read(struct mufs *fs, struct mufs_tree *tree, void *data, + uint32_t va, size_t len);