138 lines
3.2 KiB
C++
138 lines
3.2 KiB
C++
#pragma once
|
|
#include <luna/SharedPtr.h>
|
|
#include <sys/types.h>
|
|
|
|
namespace VFS
|
|
{
|
|
enum class InodeType
|
|
{
|
|
RegularFile,
|
|
Directory,
|
|
Device
|
|
};
|
|
|
|
class FileSystem;
|
|
|
|
class Inode
|
|
{
|
|
public:
|
|
// Directory-specific methods
|
|
virtual Result<SharedPtr<Inode>> find(const char* name) const = 0;
|
|
|
|
virtual Result<SharedPtr<Inode>> create_file(const char* name) = 0;
|
|
|
|
virtual Result<SharedPtr<Inode>> create_subdirectory(const char* name) = 0;
|
|
|
|
virtual Result<void> add_entry(SharedPtr<Inode> inode, const char* name) = 0;
|
|
|
|
// File-specific methods
|
|
virtual Result<usize> read(u8* buf, usize offset, usize length) const = 0;
|
|
|
|
virtual Result<usize> write(const u8* buf, usize offset, usize length) = 0;
|
|
|
|
virtual Result<void> truncate(usize size) = 0;
|
|
|
|
// Metadata accessors
|
|
virtual usize size() = 0;
|
|
|
|
virtual mode_t mode() = 0;
|
|
|
|
// Metadata changers
|
|
virtual Result<void> 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<SharedPtr<Inode>> find(const char*) const override
|
|
{
|
|
return err(ENOTDIR);
|
|
}
|
|
|
|
Result<SharedPtr<Inode>> create_file(const char*) override
|
|
{
|
|
return err(ENOTDIR);
|
|
}
|
|
|
|
Result<SharedPtr<Inode>> create_subdirectory(const char*) override
|
|
{
|
|
return err(ENOTDIR);
|
|
}
|
|
|
|
Result<void> add_entry(SharedPtr<Inode>, const char*) override
|
|
{
|
|
return err(ENOTDIR);
|
|
}
|
|
|
|
InodeType type() const override
|
|
{
|
|
return InodeType::RegularFile;
|
|
}
|
|
|
|
virtual ~FileInode() = default;
|
|
};
|
|
|
|
class DeviceInode : Inode
|
|
{
|
|
public:
|
|
Result<SharedPtr<Inode>> find(const char*) const override
|
|
{
|
|
return err(ENOTDIR);
|
|
}
|
|
|
|
Result<SharedPtr<Inode>> create_file(const char*) override
|
|
{
|
|
return err(ENOTDIR);
|
|
}
|
|
|
|
Result<SharedPtr<Inode>> create_subdirectory(const char*) override
|
|
{
|
|
return err(ENOTDIR);
|
|
}
|
|
|
|
Result<void> add_entry(SharedPtr<Inode>, const char*) override
|
|
{
|
|
return err(ENOTDIR);
|
|
}
|
|
|
|
InodeType type() const override
|
|
{
|
|
return InodeType::Device;
|
|
}
|
|
|
|
virtual ~DeviceInode() = default;
|
|
};
|
|
|
|
class FileSystem
|
|
{
|
|
public:
|
|
virtual SharedPtr<Inode> root_inode() const = 0;
|
|
|
|
virtual Result<SharedPtr<Inode>> create_file_inode() = 0;
|
|
|
|
virtual Result<SharedPtr<Inode>> create_dir_inode(SharedPtr<Inode> parent) = 0;
|
|
|
|
virtual Result<SharedPtr<Inode>> create_device_inode(u32 major, u32 minor) = 0;
|
|
|
|
virtual ~FileSystem() = default;
|
|
};
|
|
|
|
extern SharedPtr<FileSystem> root_fs;
|
|
|
|
Result<SharedPtr<Inode>> resolve_path(const char* path);
|
|
|
|
Result<SharedPtr<Inode>> create_directory(const char* path);
|
|
Result<SharedPtr<Inode>> create_file(const char* path);
|
|
|
|
Inode& root_inode();
|
|
}
|