Luna/kernel/src/fs/VFS.h

166 lines
3.8 KiB
C
Raw Normal View History

#pragma once
#include <luna/SharedPtr.h>
#include <luna/StaticString.h>
2023-03-12 15:55:46 +00:00
#include <sys/types.h>
namespace VFS
{
enum class InodeType
{
RegularFile,
Directory,
Device
};
class FileSystem;
class Inode;
struct DirectoryEntry
{
public:
SharedPtr<Inode> inode;
StaticString<128> name;
};
class Inode
{
public:
// Directory-specific methods
2023-02-25 18:06:50 +00:00
virtual Result<SharedPtr<Inode>> find(const char* name) const = 0;
virtual Option<DirectoryEntry> get(usize index) 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;
2023-03-19 10:21:50 +00:00
virtual bool blocking() const = 0;
2023-03-12 15:55:46 +00:00
// Metadata accessors
virtual usize size() = 0;
2023-03-12 15:55:46 +00:00
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:
2023-02-25 18:06:50 +00:00
Result<SharedPtr<Inode>> find(const char*) const override
{
return err(ENOTDIR);
}
Option<DirectoryEntry> get(usize) const override
{
return {};
}
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);
}
2023-03-19 10:21:50 +00:00
bool blocking() const override
{
return false;
}
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);
}
Option<DirectoryEntry> get(usize) const override
{
return {};
}
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();
}