2023-02-03 21:18:52 +00:00
|
|
|
#pragma once
|
2023-05-23 18:45:24 +00:00
|
|
|
#include <bits/makedev.h>
|
2023-02-03 21:18:52 +00:00
|
|
|
#include <luna/SharedPtr.h>
|
2023-03-28 19:15:26 +00:00
|
|
|
#include <luna/StaticString.h>
|
2023-04-16 09:25:32 +00:00
|
|
|
#include <luna/StringView.h>
|
2023-03-12 15:55:46 +00:00
|
|
|
#include <sys/types.h>
|
2023-02-03 21:18:52 +00:00
|
|
|
|
2023-04-08 11:12:49 +00:00
|
|
|
struct Credentials;
|
|
|
|
|
2023-02-03 21:18:52 +00:00
|
|
|
namespace VFS
|
|
|
|
{
|
|
|
|
enum class InodeType
|
|
|
|
{
|
|
|
|
RegularFile,
|
2023-03-18 08:10:33 +00:00
|
|
|
Directory,
|
2023-05-26 18:27:47 +00:00
|
|
|
CharacterDevice,
|
|
|
|
BlockDevice,
|
2023-05-23 18:54:25 +00:00
|
|
|
Symlink,
|
|
|
|
FIFO,
|
2023-02-03 21:18:52 +00:00
|
|
|
};
|
|
|
|
|
2023-03-28 19:15:26 +00:00
|
|
|
class Inode;
|
|
|
|
|
2023-05-17 18:37:01 +00:00
|
|
|
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;
|
|
|
|
|
2023-05-20 19:46:31 +00:00
|
|
|
virtual Result<SharedPtr<Inode>> create_symlink_inode(StringView link) = 0;
|
|
|
|
|
2023-05-17 18:37:01 +00:00
|
|
|
virtual Result<void> set_mount_dir(SharedPtr<Inode> parent) = 0;
|
|
|
|
|
2023-06-22 17:57:12 +00:00
|
|
|
virtual Result<void> reset_mount_dir() = 0;
|
|
|
|
|
2023-06-21 19:32:28 +00:00
|
|
|
virtual bool is_readonly() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-17 18:37:01 +00:00
|
|
|
virtual u64 handles() const
|
|
|
|
{
|
|
|
|
return m_handles;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void add_handle()
|
|
|
|
{
|
|
|
|
m_handles++;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void remove_handle()
|
|
|
|
{
|
|
|
|
m_handles--;
|
|
|
|
}
|
|
|
|
|
2023-05-23 18:45:24 +00:00
|
|
|
virtual dev_t host_device_id() const = 0;
|
|
|
|
|
2023-05-17 18:37:01 +00:00
|
|
|
virtual ~FileSystem() = default;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
u64 m_handles { 0 };
|
|
|
|
};
|
|
|
|
|
2023-03-28 19:15:26 +00:00
|
|
|
struct DirectoryEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SharedPtr<Inode> inode;
|
|
|
|
StaticString<128> name;
|
|
|
|
};
|
2023-02-03 21:18:52 +00:00
|
|
|
|
|
|
|
class Inode
|
|
|
|
{
|
|
|
|
public:
|
2023-04-09 09:22:57 +00:00
|
|
|
virtual Result<u64> ioctl(int, void*)
|
|
|
|
{
|
|
|
|
return err(ENOTTY);
|
|
|
|
}
|
|
|
|
|
2023-07-11 10:05:09 +00:00
|
|
|
virtual Result<u64> isatty() const
|
|
|
|
{
|
|
|
|
return err(ENOTTY);
|
|
|
|
}
|
|
|
|
|
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-03-28 19:15:26 +00:00
|
|
|
virtual Option<DirectoryEntry> get(usize index) const = 0;
|
|
|
|
|
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-18 08:10:33 +00:00
|
|
|
virtual Result<void> add_entry(SharedPtr<Inode> inode, const char* name) = 0;
|
|
|
|
|
2023-04-12 16:11:36 +00:00
|
|
|
virtual Result<void> remove_entry(const char* name) = 0;
|
|
|
|
|
2023-05-17 17:40:37 +00:00
|
|
|
virtual Result<void> replace_entry(SharedPtr<Inode> inode, const char* name) = 0;
|
|
|
|
|
2023-04-12 16:11:36 +00:00
|
|
|
virtual usize entries() const = 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-03-12 13:43:58 +00:00
|
|
|
virtual Result<void> truncate(usize size) = 0;
|
|
|
|
|
2023-03-19 10:21:50 +00:00
|
|
|
virtual bool blocking() const = 0;
|
|
|
|
|
2023-05-20 19:46:31 +00:00
|
|
|
// Symlink-specific methods
|
|
|
|
virtual Result<StringView> readlink()
|
|
|
|
{
|
|
|
|
return StringView {};
|
|
|
|
}
|
|
|
|
|
2023-05-23 18:45:24 +00:00
|
|
|
virtual dev_t device_id() const
|
|
|
|
{
|
|
|
|
return luna_dev_makedev(0, 0);
|
|
|
|
}
|
|
|
|
|
2023-03-12 15:55:46 +00:00
|
|
|
// Metadata accessors
|
2023-04-08 11:12:49 +00:00
|
|
|
virtual usize size() const = 0;
|
2023-03-12 13:49:21 +00:00
|
|
|
|
2023-04-08 11:12:49 +00:00
|
|
|
virtual mode_t mode() const = 0;
|
|
|
|
|
2023-05-27 09:32:40 +00:00
|
|
|
virtual nlink_t nlinks() const
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-04-08 11:12:49 +00:00
|
|
|
virtual u32 uid() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual u32 gid() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2023-03-12 15:55:46 +00:00
|
|
|
|
2023-05-17 17:40:37 +00:00
|
|
|
virtual bool is_mountpoint() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-12 16:11:36 +00:00
|
|
|
virtual void did_link() = 0;
|
|
|
|
virtual void did_unlink() = 0;
|
|
|
|
|
2023-03-12 15:55:46 +00:00
|
|
|
// Metadata changers
|
|
|
|
virtual Result<void> chmod(mode_t mode) = 0;
|
|
|
|
|
2023-04-08 11:12:49 +00:00
|
|
|
virtual Result<void> chown(u32 uid, u32 gid) = 0;
|
|
|
|
|
2023-03-12 15:55:46 +00:00
|
|
|
// Generic VFS-related methods
|
2023-05-10 20:48:31 +00:00
|
|
|
virtual FileSystem* fs() const = 0;
|
2023-02-03 21:18:52 +00:00
|
|
|
|
|
|
|
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-05-17 18:37:01 +00:00
|
|
|
|
|
|
|
virtual void add_handle()
|
|
|
|
{
|
|
|
|
auto* f = fs();
|
|
|
|
if (f) f->add_handle();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void remove_handle()
|
|
|
|
{
|
|
|
|
auto* f = fs();
|
|
|
|
if (f) f->remove_handle();
|
|
|
|
}
|
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-03-28 19:15:26 +00:00
|
|
|
Option<DirectoryEntry> get(usize) const override
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
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-03-18 08:10:33 +00:00
|
|
|
Result<void> add_entry(SharedPtr<Inode>, const char*) override
|
|
|
|
{
|
|
|
|
return err(ENOTDIR);
|
|
|
|
}
|
|
|
|
|
2023-05-17 17:40:37 +00:00
|
|
|
Result<void> replace_entry(SharedPtr<Inode>, const char*) override
|
|
|
|
{
|
|
|
|
return err(ENOTDIR);
|
|
|
|
}
|
|
|
|
|
2023-04-12 16:11:36 +00:00
|
|
|
Result<void> remove_entry(const char*) override
|
|
|
|
{
|
|
|
|
return err(ENOTDIR);
|
|
|
|
}
|
|
|
|
|
|
|
|
usize entries() const override
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-19 10:21:50 +00:00
|
|
|
bool blocking() const override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2023-03-18 08:10:33 +00:00
|
|
|
class DeviceInode : Inode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Result<SharedPtr<Inode>> find(const char*) const override
|
|
|
|
{
|
|
|
|
return err(ENOTDIR);
|
|
|
|
}
|
|
|
|
|
2023-03-28 19:15:26 +00:00
|
|
|
Option<DirectoryEntry> get(usize) const override
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-03-18 08:10:33 +00:00
|
|
|
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-05-17 17:40:37 +00:00
|
|
|
Result<void> replace_entry(SharedPtr<Inode>, const char*) override
|
|
|
|
{
|
|
|
|
return err(ENOTDIR);
|
|
|
|
}
|
|
|
|
|
2023-04-12 16:11:36 +00:00
|
|
|
Result<void> remove_entry(const char*) override
|
|
|
|
{
|
|
|
|
return err(ENOTDIR);
|
|
|
|
}
|
|
|
|
|
|
|
|
usize entries() const override
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-18 08:10:33 +00:00
|
|
|
InodeType type() const override
|
|
|
|
{
|
2023-05-26 18:27:47 +00:00
|
|
|
return InodeType::CharacterDevice;
|
2023-03-18 08:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~DeviceInode() = default;
|
|
|
|
};
|
|
|
|
|
2023-04-11 20:14:57 +00:00
|
|
|
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth,
|
2023-05-20 19:46:31 +00:00
|
|
|
SharedPtr<VFS::Inode> working_directory = {},
|
|
|
|
bool follow_last_symlink = true);
|
2023-04-08 11:12:49 +00:00
|
|
|
|
2023-04-11 20:14:57 +00:00
|
|
|
Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth,
|
|
|
|
SharedPtr<VFS::Inode> working_directory = {});
|
|
|
|
|
|
|
|
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth,
|
|
|
|
SharedPtr<VFS::Inode> working_directory = {});
|
2023-02-25 17:05:25 +00:00
|
|
|
|
2023-04-16 09:25:32 +00:00
|
|
|
Result<void> validate_filename(StringView name);
|
|
|
|
|
2023-04-08 11:12:49 +00:00
|
|
|
bool can_execute(SharedPtr<Inode> inode, Credentials auth);
|
|
|
|
bool can_read(SharedPtr<Inode> inode, Credentials auth);
|
|
|
|
bool can_write(SharedPtr<Inode> inode, Credentials auth);
|
2023-04-08 14:32:56 +00:00
|
|
|
bool is_setuid(SharedPtr<Inode> inode);
|
|
|
|
bool is_setgid(SharedPtr<Inode> inode);
|
2023-06-03 09:55:10 +00:00
|
|
|
bool is_sticky(SharedPtr<Inode> inode);
|
2023-03-11 00:13:44 +00:00
|
|
|
|
2023-05-26 18:30:39 +00:00
|
|
|
bool is_seekable(SharedPtr<Inode> inode);
|
2023-05-26 18:27:47 +00:00
|
|
|
|
2023-02-25 17:05:25 +00:00
|
|
|
Inode& root_inode();
|
2023-05-17 17:40:37 +00:00
|
|
|
|
|
|
|
Result<void> mount_root(SharedPtr<VFS::FileSystem> fs);
|
2023-06-17 15:27:22 +00:00
|
|
|
Result<void> pivot_root(const char* new_root, const char* put_old, SharedPtr<VFS::Inode> working_directory);
|
2023-05-17 17:40:37 +00:00
|
|
|
Result<void> mount(const char* path, SharedPtr<VFS::FileSystem> fs, Credentials auth,
|
|
|
|
SharedPtr<Inode> working_directory = {});
|
2023-05-17 17:52:26 +00:00
|
|
|
|
|
|
|
Result<void> umount(const char* path, Credentials auth, SharedPtr<Inode> working_directory = {});
|
2023-02-03 21:18:52 +00:00
|
|
|
}
|