mufs: add mufs_read() to read data using a file tree

tags/0.1.0
S.J.R. van Schaik 8 years ago
parent 9480a91ecc
commit 233d991328
  1. 38
      source/fs/mufs/tree.c
  2. 2
      source/fs/mufs/tree.h

@ -1,7 +1,10 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <bitops.h>
#include <flash.h>
#include <macros.h>
#include <fs/mufs.h>
@ -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;
}

@ -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);

Loading…
Cancel
Save