From 84a7325b22f1f90c0c719a2ec8ba131263e1208c Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 28 Sep 2025 14:58:47 +0200 Subject: feat: final changes before nekernel v0.0.6 Signed-off-by: Amlal El Mahrouss --- dev/kernel/FSKit/Ext2+IFS.h | 155 +++++++++++++++++++++++++++++++++++++++----- dev/kernel/FSKit/Ext2.h | 17 +++-- 2 files changed, 151 insertions(+), 21 deletions(-) (limited to 'dev/kernel/FSKit') diff --git a/dev/kernel/FSKit/Ext2+IFS.h b/dev/kernel/FSKit/Ext2+IFS.h index d73ae43c..2a28fe17 100644 --- a/dev/kernel/FSKit/Ext2+IFS.h +++ b/dev/kernel/FSKit/Ext2+IFS.h @@ -8,11 +8,12 @@ #include #include -namespace Kernel::Ext2 { +namespace Kernel { /// @brief Context for an EXT2 filesystem on a given drive -struct Ext2Context { - Kernel::DriveTrait* drive{nullptr}; - EXT2_SUPER_BLOCK* superblock{nullptr}; +class Ext2Context final { + public: + DriveTrait* drive{nullptr}; + EXT2_SUPER_BLOCK* superblock{nullptr}; /// @brief context with a drive Ext2Context(Kernel::DriveTrait* drv) : drive(drv) {} @@ -46,19 +47,19 @@ struct Ext2Context { return *this; } - Kernel::SizeT LeakBlockSize() const { + SizeT BlockSize() const { if (!superblock) return kExt2FSBlockSizeBase; return kExt2FSBlockSizeBase << superblock->fLogBlockSize; } - BOOL operator bool() { return superblock != nullptr; } + operator BOOL() { return superblock != nullptr; } }; /// ======================================================================= /// /// IFS FUNCTIONS /// ======================================================================= /// -inline bool ext2_read_block(Kernel::DriveTrait* drv, Kernel::UInt32 lba, void* buffer, +inline BOOL ext2_read_block(Kernel::DriveTrait* drv, Kernel::UInt32 lba, VoidPtr buffer, Kernel::UInt32 size) { if (!drv || !buffer) return false; @@ -71,12 +72,12 @@ inline bool ext2_read_block(Kernel::DriveTrait* drv, Kernel::UInt32 lba, void* b return pkt.fPacketGood; } -inline bool ext2_write_block(Kernel::DriveTrait* drv, Kernel::UInt32 lba, const void* buffer, +inline BOOL ext2_write_block(Kernel::DriveTrait* drv, Kernel::UInt32 lba, const VoidPtr buffer, Kernel::UInt32 size) { if (!drv || !buffer) return false; Kernel::DriveTrait::DrivePacket pkt{}; - pkt.fPacketContent = const_cast(buffer); + pkt.fPacketContent = const_cast(buffer); pkt.fPacketSize = size; pkt.fPacketLba = lba; drv->fOutput(pkt); @@ -119,7 +120,6 @@ inline Kernel::ErrorOr ext2_load_inode(Ext2Context* ctx, Kernel::UInt // Compute block group and index within group Kernel::UInt32 inodesPerGroup = ctx->superblock->fInodesPerGroup; Kernel::UInt32 group = (inodeNumber - 1) / inodesPerGroup; - Kernel::UInt32 index = (inodeNumber - 1) % inodesPerGroup; // dummy: just offset first inode Kernel::UInt32 inodeTableBlock = ctx->superblock->fFirstInode + group; @@ -133,8 +133,133 @@ inline Kernel::ErrorOr ext2_load_inode(Ext2Context* ctx, Kernel::UInt return Kernel::ErrorOr(ext2Node); } -inline Kernel::UInt32 inode_offset(const Ext2Context* ctx, Kernel::UInt32 inodeNumber) { - if (!ctx || !ctx->superblock) return 0; - return ((inodeNumber - 1) % ctx->superblock->fInodesPerGroup) * ctx->superblock->fInodeSize; -} -} // namespace Kernel::Ext2 +/* + * Ext2FileSystemParser Class + * + * Provides high-level interface for EXT2 filesystem operations + */ +class Ext2FileSystemParser { + private: + Ext2Context ctx; // Internal EXT2 context + + public: + /* + * Constructor + * Initializes the parser with a drive interface + * + * @param drive: Pointer to drive trait for disk I/O operations + */ + explicit Ext2FileSystemParser(DriveTrait* drive); + + /* + * Open a file or directory by path + * + * @param path: Full path to the file/directory (e.g., "/home/user/file.txt") + * @param restrict_type: Access mode restriction (e.g., "r", "w", "rw") + * @return: VoidPtr handle to the opened file/directory, or nullptr on failure + */ + VoidPtr Open(const char* path, const char* restrict_type); + + /* + * Read data from an open file node + * + * @param node: File node handle returned by Open() + * @param flags: Read operation flags + * @param size: Number of bytes to read + * @return: Pointer to allocated buffer containing read data, or nullptr on failure + * Caller is responsible for freeing the returned buffer + */ + VoidPtr Read(VoidPtr node, Int32 flags, SizeT size); + + /* + * Write data to an open file node + * + * @param node: File node handle returned by Open() + * @param data: Buffer containing data to write + * @param flags: Write operation flags + * @param size: Number of bytes to write + */ + Void Write(VoidPtr node, VoidPtr data, Int32 flags, SizeT size); + + /* + * Seek to a specific position in the file + * + * @param node: File node handle + * @param offset: Byte offset from beginning of file + * @return: true on success, false on failure + */ + BOOL Seek(VoidPtr node, SizeT offset); + + /* + * Get current position in the file + * + * @param node: File node handle + * @return: Current byte offset from beginning of file + */ + SizeT Tell(VoidPtr node); + + /* + * Reset file position to beginning + * + * @param node: File node handle + * @return: true on success, false on failure + */ + BOOL Rewind(VoidPtr node); + + /* + * Read data from a named file within a directory node + * + * @param name: Name of file within the directory + * @param node: Directory node handle + * @param flags: Read operation flags + * @param size: Number of bytes to read + * @return: Pointer to allocated buffer containing read data, or nullptr on failure + */ + VoidPtr Read(const char* name, VoidPtr node, Int32 flags, SizeT size); + + /* + * Write data to a named file within a directory node + * + * @param name: Name of file within the directory + * @param node: Directory node handle + * @param data: Buffer containing data to write + * @param flags: Write operation flags + * @param size: Number of bytes to write + */ + Void Write(const char* name, VoidPtr node, VoidPtr data, Int32 flags, SizeT size); + + /* + * Create a new regular file + * + * @param path: Full path for the new file + * @return: VoidPtr handle to the created file, or nullptr on failure + */ + VoidPtr Create(const char* path); + + /* + * Create a new directory + * + * @param path: Full path for the new directory + * @return: VoidPtr handle to the created directory, or nullptr on failure + */ + VoidPtr CreateDirectory(const char* path); + + /* + * Close and free a file/directory node + * Note: This method is not shown in the implementation but would typically be needed + * + * @param node: Node handle to close and free + */ + Void Close(VoidPtr node); + + /* + * Get file/directory information + * Note: This method is not shown in the implementation but would typically be needed + * + * @param node: Node handle + * @param info: Structure to fill with file information (size, type, permissions, etc.) + * @return: true on success, false on failure + */ + BOOL GetInfo(VoidPtr node, VoidPtr info); +}; +} // namespace Kernel diff --git a/dev/kernel/FSKit/Ext2.h b/dev/kernel/FSKit/Ext2.h index 06370a8c..be2e34a6 100644 --- a/dev/kernel/FSKit/Ext2.h +++ b/dev/kernel/FSKit/Ext2.h @@ -15,8 +15,6 @@ /// @file Ext2.h /// @brief EXT2 filesystem structures, constants, and base wrappers. -namespace Ext2 { - /// EXT2 Constants #define kExt2FSMagic (0xEF53) #define kExt2FSMaxFileNameLen (255U) @@ -40,6 +38,17 @@ enum { kExt2FileTypeSymbolicLink = 7 }; +typedef struct EXT2_GROUP_DESCRIPTOR final { + UInt32 fBlockBitmap; + UInt32 fInodeBitmap; + UInt32 fInodeTable; + UInt16 fFreeBlocksCount; + UInt16 fFreeInodesCount; + UInt16 fBgUsedDirsCount; + UInt16 fBgPad; + UInt32 fBgReserved[3]; +} EXT2_GROUP_DESCRIPTOR; + struct PACKED EXT2_SUPER_BLOCK final { Kernel::UInt32 fInodeCount; Kernel::UInt32 fBlockCount; @@ -137,7 +146,3 @@ struct Ext2Node { EXT2_INODE inode; Kernel::UInt32 cursor{0}; }; - -class Ext2FileSystemMgr; - -} // namespace Ext2 -- cgit v1.2.3