2023-02-03 21:18:52 +00:00
|
|
|
#pragma once
|
|
|
|
#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,
|
|
|
|
Device
|
2023-02-03 21:18:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FileSystem;
|
2023-03-28 19:15:26 +00:00
|
|
|
class Inode;
|
|
|
|
|
|
|
|
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-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;
|
|
|
|
|
|
|
|
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-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;
|
|
|
|
|
|
|
|
virtual u32 uid() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual u32 gid() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2023-03-12 15:55:46 +00:00
|
|
|
|
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-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-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-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
|
|
|
|
{
|
|
|
|
return InodeType::Device;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~DeviceInode() = 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-03-18 08:10:33 +00:00
|
|
|
virtual Result<SharedPtr<Inode>> create_device_inode(u32 major, u32 minor) = 0;
|
|
|
|
|
2023-02-03 21:18:52 +00:00
|
|
|
virtual ~FileSystem() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern SharedPtr<FileSystem> root_fs;
|
|
|
|
|
2023-04-11 20:14:57 +00:00
|
|
|
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth,
|
|
|
|
SharedPtr<VFS::Inode> working_directory = {});
|
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-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
|
|
|
}
|