#pragma once #include #include namespace VFS { enum class InodeType { RegularFile, Directory, Device }; class FileSystem; class Inode { public: // Directory-specific methods virtual Result> find(const char* name) const = 0; virtual Result> create_file(const char* name) = 0; virtual Result> create_subdirectory(const char* name) = 0; virtual Result add_entry(SharedPtr inode, const char* name) = 0; // File-specific methods virtual Result read(u8* buf, usize offset, usize length) const = 0; virtual Result write(const u8* buf, usize offset, usize length) = 0; virtual Result truncate(usize size) = 0; // Metadata accessors virtual usize size() = 0; virtual mode_t mode() = 0; // Metadata changers virtual Result chmod(mode_t mode) = 0; // Generic VFS-related methods virtual FileSystem& fs() const = 0; virtual ~Inode() = default; virtual InodeType type() const = 0; virtual usize inode_number() const = 0; }; class FileInode : Inode { public: Result> find(const char*) const override { return err(ENOTDIR); } Result> create_file(const char*) override { return err(ENOTDIR); } Result> create_subdirectory(const char*) override { return err(ENOTDIR); } Result add_entry(SharedPtr, const char*) override { return err(ENOTDIR); } InodeType type() const override { return InodeType::RegularFile; } virtual ~FileInode() = default; }; class DeviceInode : Inode { public: Result> find(const char*) const override { return err(ENOTDIR); } Result> create_file(const char*) override { return err(ENOTDIR); } Result> create_subdirectory(const char*) override { return err(ENOTDIR); } Result add_entry(SharedPtr, const char*) override { return err(ENOTDIR); } InodeType type() const override { return InodeType::Device; } virtual ~DeviceInode() = default; }; class FileSystem { public: virtual SharedPtr root_inode() const = 0; virtual Result> create_file_inode() = 0; virtual Result> create_dir_inode(SharedPtr parent) = 0; virtual Result> create_device_inode(u32 major, u32 minor) = 0; virtual ~FileSystem() = default; }; extern SharedPtr root_fs; Result> resolve_path(const char* path); Result> create_directory(const char* path); Result> create_file(const char* path); Inode& root_inode(); }