diff --git a/source/fs/mufs/tree.c b/source/fs/mufs/tree.c index 155089a..0c1a0bb 100644 --- a/source/fs/mufs/tree.c +++ b/source/fs/mufs/tree.c @@ -195,3 +195,38 @@ size_t mufs_read(struct mufs *fs, struct mufs_tree *tree, void *data, return nbytes; } + +static size_t mufs_do_write(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_alloc_page(fs, tree, &page, va) < 0) + return 0; + + return flash_write(fs->dev, page, data, len); +} + +size_t mufs_write(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_write(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 0604370..0f381da 100644 --- a/source/fs/mufs/tree.h +++ b/source/fs/mufs/tree.h @@ -9,3 +9,5 @@ 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); +size_t mufs_write(struct mufs *fs, struct mufs_tree *tree, void *data, + uint32_t va, size_t len);