2023-02-03 21:18:52 +00:00
|
|
|
#pragma once
|
|
|
|
#include <luna/SharedPtr.h>
|
|
|
|
|
|
|
|
namespace VFS
|
|
|
|
{
|
|
|
|
enum class InodeType
|
|
|
|
{
|
|
|
|
RegularFile,
|
|
|
|
Directory
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileSystem;
|
|
|
|
|
|
|
|
class Inode
|
|
|
|
{
|
|
|
|
public:
|
2023-02-25 18:22:05 +00:00
|
|
|
// Directory-specific methods
|
2023-02-25 18:06:50 +00:00
|
|
|
virtual Result<SharedPtr<Inode>> find(const char* name) const = 0;
|
2023-02-03 21:18:52 +00:00
|
|
|
|
2023-02-25 18:22:05 +00:00
|
|
|
virtual Result<SharedPtr<Inode>> create_file(const char* name) = 0;
|
|
|
|
|
2023-03-10 20:09:08 +00:00
|
|
|
virtual Result<SharedPtr<Inode>> create_subdirectory(const char* name) = 0;
|
|
|
|
|
2023-03-10 23:52:39 +00:00
|
|
|
// 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;
|
|
|
|
|
2023-02-25 18:22:05 +00:00
|
|
|
// Generic methods
|
2023-02-03 21:18:52 +00:00
|
|
|
virtual FileSystem& fs() const = 0;
|
|
|
|
|
|
|
|
virtual ~Inode() = default;
|
|
|
|
|
2023-02-25 16:54:01 +00:00
|
|
|
virtual InodeType type() const = 0;
|
2023-02-25 17:03:11 +00:00
|
|
|
|
|
|
|
virtual usize inode_number() const = 0;
|
2023-02-25 16:54:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FileInode : Inode
|
|
|
|
{
|
|
|
|
public:
|
2023-02-25 18:06:50 +00:00
|
|
|
Result<SharedPtr<Inode>> find(const char*) const override
|
2023-02-25 16:54:01 +00:00
|
|
|
{
|
|
|
|
return err(ENOTDIR);
|
|
|
|
}
|
|
|
|
|
2023-02-25 18:22:05 +00:00
|
|
|
Result<SharedPtr<Inode>> create_file(const char*) override
|
|
|
|
{
|
|
|
|
return err(ENOTDIR);
|
|
|
|
}
|
|
|
|
|
2023-03-10 20:09:08 +00:00
|
|
|
Result<SharedPtr<Inode>> create_subdirectory(const char*) override
|
|
|
|
{
|
|
|
|
return err(ENOTDIR);
|
|
|
|
}
|
|
|
|
|
2023-02-25 16:54:01 +00:00
|
|
|
InodeType type() const override
|
|
|
|
{
|
|
|
|
return InodeType::RegularFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~FileInode() = default;
|
2023-02-03 21:18:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FileSystem
|
|
|
|
{
|
|
|
|
public:
|
2023-03-10 21:18:48 +00:00
|
|
|
virtual SharedPtr<Inode> root_inode() const = 0;
|
2023-02-03 21:18:52 +00:00
|
|
|
|
2023-02-25 17:03:11 +00:00
|
|
|
virtual Result<SharedPtr<Inode>> create_file_inode() = 0;
|
|
|
|
|
2023-02-27 14:04:29 +00:00
|
|
|
virtual Result<SharedPtr<Inode>> create_dir_inode(SharedPtr<Inode> parent) = 0;
|
2023-02-25 18:08:55 +00:00
|
|
|
|
2023-02-03 21:18:52 +00:00
|
|
|
virtual ~FileSystem() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern SharedPtr<FileSystem> root_fs;
|
|
|
|
|
2023-03-10 21:18:48 +00:00
|
|
|
Result<SharedPtr<Inode>> resolve_path(const char* path);
|
2023-02-25 17:05:25 +00:00
|
|
|
|
2023-03-11 00:13:44 +00:00
|
|
|
Result<SharedPtr<Inode>> create_directory(const char* path);
|
2023-03-11 09:23:46 +00:00
|
|
|
Result<SharedPtr<Inode>> create_file(const char* path);
|
2023-03-11 00:13:44 +00:00
|
|
|
|
2023-02-25 17:05:25 +00:00
|
|
|
Inode& root_inode();
|
2023-02-03 21:18:52 +00:00
|
|
|
}
|