From 281c1abdfa76e14ca3aee42a17ed9798b6cd934a Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Mon, 29 May 2017 14:41:47 +0200 Subject: [PATCH] mufs: add mufs_extend_tree() to extend the depth of a file tree --- source/fs/mufs/tree.c | 21 +++++++++++++++++++++ source/fs/mufs/tree.h | 1 + 2 files changed, 22 insertions(+) diff --git a/source/fs/mufs/tree.c b/source/fs/mufs/tree.c index 01ad819..5f7c697 100644 --- a/source/fs/mufs/tree.c +++ b/source/fs/mufs/tree.c @@ -95,3 +95,24 @@ void mufs_free_page(struct mufs *fs, struct mufs_tree *tree, uint32_t va) { mufs_do_free_page(fs, tree->root, tree->depth, va); } + +int mufs_extend_tree(struct mufs *fs, struct mufs_tree *tree, uint8_t depth) +{ + uint32_t root; + + for (; tree->depth < depth; ++tree->depth) { + if (mufs_alloc_block(fs, &root) < 0) + return -1; + + if (!root) + continue; + + if (flash_write(fs->dev, root << fs->dev->log2_block_size, &tree->root, + sizeof tree->root) == 0) { + mufs_free_block(fs, root); + return -1; + } + } + + return 0; +} diff --git a/source/fs/mufs/tree.h b/source/fs/mufs/tree.h index ac7f60f..45650a4 100644 --- a/source/fs/mufs/tree.h +++ b/source/fs/mufs/tree.h @@ -5,3 +5,4 @@ int mufs_lookup_page(struct mufs *fs, struct mufs_tree *tree, uint32_t *page, int mufs_alloc_page(struct mufs *fs, struct mufs_tree *tree, uint32_t *page, uint32_t va); 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);