From d74918409c9da9b9bb12654a8da76133c88f4d14 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Thu, 11 Jan 2024 18:29:05 -0800 Subject: [PATCH] [VictoriaFallS] Refactor Ext2Driver to share inode reading code. --- sys/victoriafalls/fs/ext2/ext2_driver.cpp | 50 ++++++++++------------- sys/victoriafalls/fs/ext2/ext2_driver.h | 2 + 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/sys/victoriafalls/fs/ext2/ext2_driver.cpp b/sys/victoriafalls/fs/ext2/ext2_driver.cpp index 9d90672..491ec80 100644 --- a/sys/victoriafalls/fs/ext2/ext2_driver.cpp +++ b/sys/victoriafalls/fs/ext2/ext2_driver.cpp @@ -56,39 +56,28 @@ glcr::ErrorOr> Ext2Driver::ReadDirectory( dbgln("Reading non directory."); return glcr::INVALID_ARGUMENT; } - - // This calculation is cursed. - uint64_t real_block_cnt = - (inode->blocks - 1) / (ext2_reader_->BlockSize() / 512) + 1; - - if (real_block_cnt > 12) { - dbgln("Cant handle indirect blocks yet"); - return glcr::FAILED_PRECONDITION; - } + ASSIGN_OR_RETURN(mmth::OwnedMemoryRegion dir, ReadInode(inode)); glcr::Vector directory; - for (uint64_t i = 0; i < real_block_cnt; i++) { - ASSIGN_OR_RETURN(mmth::OwnedMemoryRegion block, - ext2_reader_->ReadBlock(inode->block[i])); - uint64_t addr = block.vaddr(); - while (addr < block.vaddr() + ext2_reader_->BlockSize()) { - DirEntry* entry = reinterpret_cast(addr); - directory.PushBack(*entry); - glcr::StringView name(entry->name, entry->name_len); + + uint64_t addr = dir.vaddr(); + while (addr < dir.vaddr() + ext2_reader_->BlockSize()) { + DirEntry* entry = reinterpret_cast(addr); + directory.PushBack(*entry); + glcr::StringView name(entry->name, entry->name_len); #if EXT2_DEBUG - switch (entry->file_type) { - case kExt2FtFile: - dbgln("FILE (0x{x}): {}", entry->inode, name); - break; - case kExt2FtDirectory: - dbgln("DIR (0x{x}): {}", entry->inode, name); - break; - default: - dbgln("UNK (0x{x}): {}", entry->inode, name); - } -#endif - addr += entry->record_length; + switch (entry->file_type) { + case kExt2FtFile: + dbgln("FILE (0x{x}): {}", entry->inode, name); + break; + case kExt2FtDirectory: + dbgln("DIR (0x{x}): {}", entry->inode, name); + break; + default: + dbgln("UNK (0x{x}): {}", entry->inode, name); } +#endif + addr += entry->record_length; } return directory; } @@ -101,7 +90,10 @@ glcr::ErrorOr Ext2Driver::ReadFile( dbgln("Reading non file."); return glcr::INVALID_ARGUMENT; } + return ReadInode(inode); +} +glcr::ErrorOr Ext2Driver::ReadInode(Inode* inode) { // This calculation is cursed. uint64_t real_block_cnt = (inode->blocks - 1) / (ext2_reader_->BlockSize() / 512) + 1; diff --git a/sys/victoriafalls/fs/ext2/ext2_driver.h b/sys/victoriafalls/fs/ext2/ext2_driver.h index b1b4100..e70ab84 100644 --- a/sys/victoriafalls/fs/ext2/ext2_driver.h +++ b/sys/victoriafalls/fs/ext2/ext2_driver.h @@ -28,4 +28,6 @@ class Ext2Driver { Ext2Driver(const glcr::SharedPtr& reader, glcr::UniquePtr inode_table) : ext2_reader_(reader), inode_table_(glcr::Move(inode_table)) {} + + glcr::ErrorOr ReadInode(Inode* inode); };