mufs: add mufs_read() to read data using a file tree
This commit is contained in:
parent
9480a91ecc
commit
233d991328
2 changed files with 40 additions and 0 deletions
|
@ -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…
Add table
Reference in a new issue