Orion
Barry Importing existing Orion kernel d41a53c (2 years, 4 months ago)diff --git a/vfs/ext2fs/file.c b/vfs/ext2fs/file.c new file mode 100644 index 0000000..d0caa35 --- /dev/null +++ b/vfs/ext2fs/file.c @@ -0,0 +1,86 @@ +/* + * This file controls access to Ext2FS Files. It contains the functions called + * by the VFS for any operation on a Ext2FS File struct. + */ + +#include <stddef.h> +#include <string.h> +#include <dirent.h> +#include <errno.h> +#include "fs.h" +#include "../vfs.h" + +size_t ext2fs_read(File *file, char *buf, size_t size, off_t offset); +int ext2fs_readdir(File *file, DirEnt *dent, off_t index); +int ext2fs_open(File *file); + +FileOps ext2fsFileOps = { + .read = ext2fs_read, + .readdir = ext2fs_readdir, + .open = ext2fs_open, +}; + +/* Read a file */ +size_t +ext2fs_read(File *file, char *buf, size_t size, off_t offset) +{ + Ext2Inode inode; + uint16_t min; + size_t count = 0, i = offset / 4096; + uint32_t blk; + char ebuf[4096]; + ext2_read_inode(file->inode->super, file->inode->ino, &inode); + if (offset > inode.lsize) + return 0; + if (size + offset > inode.lsize) + size = inode.lsize - offset; + while (size) { + min = (size > 0x1000) ? 0x1000 : size; + blk = ext2_get_data_addr(file->inode->super, &inode, i); + ext2_read_block(file->inode->super, blk, ebuf); + memcpy(buf + count, ebuf + (offset % 4096), min); + size -= min; + count += min; + i++; + } + if (count >= 0x1000) + return count; +} + +/* Read a directory entry */ +int +ext2fs_readdir(File *file, DirEnt *dent, off_t index) +{ + char buf[4096]; + uint32_t block, blk = 0; + Ext2DirEntry *de; + Ext2Inode inode; + ext2_read_inode(file->inode->super, file->inode->ino, &inode); + for (blk = 0; blk < 0xFFFF; blk++) { + block = ext2_get_data_addr(file->inode->super, &inode, blk); + if (!block) + return -ENOENT; + ext2_read_block(file->inode->super, block, buf); + for (de = (Ext2DirEntry *) buf; + index && de < (Ext2DirEntry *) (buf + 4096); + de = (void *) ((char *) de + de->size), index--); + if (de >= (Ext2DirEntry *) (buf + 4096)) + return -ENOENT; + if (!index) + break; + } + if (!de->ino) + return -ENOENT; + dent->ino = de->ino; + dent->type = de->type; + dent->namelen = de->nameLen + 1; + strncpy(dent->name, de->name, de->size); + return 0; +} + +/* Open a file */ +int +ext2fs_open(File *file) +{ + return 0; +}