From 8e90cbd6fd790c16acd86cba878a69160fa4f17b Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Mon, 24 May 2021 20:58:14 +0200 Subject: Finish first prototype of new filesystem --- src/filesystem.c | 61 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 15 deletions(-) (limited to 'src/filesystem.c') diff --git a/src/filesystem.c b/src/filesystem.c index 327b613..ee55dcf 100644 --- a/src/filesystem.c +++ b/src/filesystem.c @@ -117,7 +117,7 @@ fs_init(void) { void _fs_update_filesystem_header(void) { _fs_write(&filesystem, 0, offsetof(FileSystem, files)); -}; +} void _fs_update_file_index(u16 index) { @@ -238,7 +238,6 @@ _fs_free_blocks(u8 blk_id) { _fs_free_blocks(next_block); } -// Write to block as a new file. void _fs_write_to_block(u8 *src, u16 n_bytes, u16 blk_offset, u8 blk_id, u8 prev_blk, bool append) { @@ -295,7 +294,7 @@ fs_seek(File *file, int offset, SeekMode mode) { new_offset = MAX((int)file_size - 1 + offset, 0); } break; } - if (new_offset >= file_size) { + if (new_offset != 0 && new_offset >= file_size) { return -1; } file->offset = new_offset; @@ -365,22 +364,54 @@ fs_write(u8 *src, u16 n_bytes, File *file) { return n_bytes; } +void +_fs_read_from_block(u8 *dst, u16 n_bytes, u16 blk_offset, u8 blk_id) { + // Read initial block. + FileBlock block; + u16 block_pos = FILE_DATA_START + FILE_BLOCK_SIZE * blk_id; + _fs_read(&block, block_pos, sizeof(FileBlock)); + + u16 read_bytes = MIN(block.size - blk_offset, n_bytes); + _fs_read(dst, block_pos + blk_offset + sizeof(FileBlock), read_bytes); + + u16 remaining_bytes = n_bytes - read_bytes; + if (block.next_block != FS_NULL && remaining_bytes > 0) { + _fs_read_from_block(dst + read_bytes, remaining_bytes, 0, block.next_block); + } +} + u16 -fs_read(u8 *dst, u16 n_bytes, u16 offset, File *file) { - // File *file = &filesystem.files[file_index]; +fs_read(u8 *dst, u16 n_bytes, File *file) { + if ((file->mode & FS_OPEN_READ) == 0) { + return 0; + } + + // If there is an offset find the block index and relative offset. + u8 blk_id = filesystem.files[file->index].first_block; + u16 offset = file->offset; - // // Check if the offset is within limits. - // if (file->size == 0 || offset >= file->size - 1) { - // return 0; - // } + // Read as much as we can from the file after the offset. + u16 file_size = fs_file_size(file); + if (offset + n_bytes >= file_size) { + n_bytes = file_size - offset; + } - // // Read as much as we can. - // if (offset + n_bytes > file->size) { - // n_bytes = file->size - offset; - // } + if (offset >= FILE_BLOCK_CAPACITY) { + u16 n_blocks_offset = offset / FILE_BLOCK_CAPACITY; + for (size_t i = 0; i < n_blocks_offset; ++i) { + FileBlock block; + u16 block_pos = FILE_DATA_START + FILE_BLOCK_SIZE * blk_id; + _fs_read(&block, block_pos, sizeof(FileBlock)); + blk_id = block.next_block; + if (blk_id == FS_NULL) { + return 0; + } + } + offset = offset % FILE_BLOCK_CAPACITY; + } - // // Copy n_bytes to destination. - // _fs_read(dst, FILE_DATA_OFFSET + FILE_MAX_SIZE * file_index + offset, n_bytes); + // Copy n_bytes to destination. + _fs_read_from_block(dst, n_bytes, offset, blk_id); return n_bytes; } -- cgit v1.2.1