Kernel: Inode::Mode is now a struct so we can have functions in it

This commit is contained in:
Bananymous 2023-03-30 14:41:15 +03:00
parent 0ce9fd8597
commit 5b3a00c64f
10 changed files with 80 additions and 68 deletions

View File

@ -74,7 +74,7 @@ namespace Kernel
public:
virtual ino_t ino() const override { return 0; }
virtual mode_t mode() const override { return Mode::IFDIR | Mode::IRUSR | Mode::IWUSR | Mode::IXUSR | Mode::IRGRP | Mode::IXGRP | Mode::IROTH | Mode::IXOTH; }
virtual Mode mode() const override { return { Mode::IFDIR | Mode::IRUSR | Mode::IWUSR | Mode::IXUSR | Mode::IRGRP | Mode::IXGRP | Mode::IROTH | Mode::IXOTH }; }
virtual nlink_t nlink() const override { return 0; }
virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; }

View File

@ -124,7 +124,7 @@ namespace Kernel
{
public:
virtual ino_t ino() const override { return m_index; };
virtual mode_t mode() const override { return m_inode.mode; }
virtual Mode mode() const override { return { m_inode.mode }; }
virtual nlink_t nlink() const override { return m_inode.links_count; }
virtual uid_t uid() const override { return m_inode.uid; }
virtual gid_t gid() const override { return m_inode.gid; }

View File

@ -13,43 +13,48 @@ namespace Kernel
class Inode : public BAN::RefCounted<Inode>
{
public:
enum Mode : mode_t
struct Mode
{
IXOTH = 0x0001,
IWOTH = 0x0002,
IROTH = 0x0004,
IXGRP = 0x0008,
IWGRP = 0x0010,
IRGRP = 0x0020,
IXUSR = 0x0040,
IWUSR = 0x0080,
IRUSR = 0x0100,
ISVTX = 0x0200,
ISGID = 0x0400,
ISUID = 0x0800,
IFIFO = 0x1000,
IFCHR = 0x2000,
IFDIR = 0x4000,
IFBLK = 0x6000,
IFREG = 0x8000,
IFLNK = 0xA000,
IFSOCK = 0xC000,
TYPE_MASK = 0xF000,
enum Mask : mode_t
{
IXOTH = 0x0001,
IWOTH = 0x0002,
IROTH = 0x0004,
IXGRP = 0x0008,
IWGRP = 0x0010,
IRGRP = 0x0020,
IXUSR = 0x0040,
IWUSR = 0x0080,
IRUSR = 0x0100,
ISVTX = 0x0200,
ISGID = 0x0400,
ISUID = 0x0800,
IFIFO = 0x1000,
IFCHR = 0x2000,
IFDIR = 0x4000,
IFBLK = 0x6000,
IFREG = 0x8000,
IFLNK = 0xA000,
IFSOCK = 0xC000,
TYPE_MASK = 0xF000,
};
bool ifchr() const { return (mode & Mask::TYPE_MASK) == Mask::IFCHR; }
bool ifdir() const { return (mode & Mask::TYPE_MASK) == Mask::IFDIR; }
bool ifblk() const { return (mode & Mask::TYPE_MASK) == Mask::IFBLK; }
bool ifreg() const { return (mode & Mask::TYPE_MASK) == Mask::IFREG; }
bool iflnk() const { return (mode & Mask::TYPE_MASK) == Mask::IFLNK; }
bool ifsock() const { return (mode & Mask::TYPE_MASK) == Mask::IFSOCK; }
mode_t mode;
};
public:
virtual ~Inode() {}
bool ifdir() const { return (mode() & Mode::TYPE_MASK) == Mode::IFDIR; }
bool ifblk() const { return (mode() & Mode::TYPE_MASK) == Mode::IFBLK; }
bool ifreg() const { return (mode() & Mode::TYPE_MASK) == Mode::IFREG; }
bool iflnk() const { return (mode() & Mode::TYPE_MASK) == Mode::IFLNK; }
bool ifsock() const { return (mode() & Mode::TYPE_MASK) == Mode::IFSOCK; }
bool operator==(const Inode& other) const { return dev() == other.dev() && rdev() == other.rdev() && ino() == other.ino(); }
virtual ino_t ino() const = 0;
virtual mode_t mode() const = 0;
virtual Mode mode() const = 0;
virtual nlink_t nlink() const = 0;
virtual uid_t uid() const = 0;
virtual gid_t gid() const = 0;
@ -64,11 +69,11 @@ namespace Kernel
virtual BAN::StringView name() const = 0;
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> read_directory_inode(BAN::StringView) { if (!ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); }
virtual BAN::ErrorOr<BAN::Vector<BAN::String>> read_directory_entries(size_t) { if (!ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); }
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> read_directory_inode(BAN::StringView) { if (!mode().ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); }
virtual BAN::ErrorOr<BAN::Vector<BAN::String>> read_directory_entries(size_t) { if (!mode().ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); }
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) { if ( ifdir()) return BAN::Error::from_errno(EISDIR); ASSERT_NOT_REACHED(); }
virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) { if (!ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); }
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) { if ( mode().ifdir()) return BAN::Error::from_errno(EISDIR); ASSERT_NOT_REACHED(); }
virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) { if (!mode().ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); }
};
}

View File

@ -14,7 +14,7 @@ namespace Kernel::Input
public:
virtual ino_t ino() const override { return m_ino; }
virtual mode_t mode() const override { return Mode::IFCHR | Mode::IRUSR | Mode::IRGRP; }
virtual Mode mode() const override { return { Mode::IFCHR | Mode::IRUSR | Mode::IRGRP }; }
virtual nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; }

View File

@ -43,7 +43,7 @@ namespace Kernel
public:
virtual ino_t ino() const override { return !!slave_bit; }
virtual mode_t mode() const override { return Mode::IFBLK; }
virtual Mode mode() const override { return { Mode::IFBLK }; }
virtual nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; }
@ -92,7 +92,7 @@ namespace Kernel
public:
virtual ino_t ino() const override { return 0; }
virtual mode_t mode() const override { return Mode::IFCHR; }
virtual Mode mode() const override { return { Mode::IFCHR }; }
virtual nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; }

View File

@ -43,7 +43,7 @@ namespace Kernel
public:
virtual ino_t ino() const override { return m_index; }
virtual mode_t mode() const override { return Mode::IFBLK | Mode::IRUSR | Mode::IRGRP; }
virtual Mode mode() const override { return { Mode::IFBLK | Mode::IRUSR | Mode::IRGRP }; }
virtual nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; }

View File

@ -252,7 +252,7 @@ namespace Kernel
{
// FIXME: update atime if needed
if (ifdir())
if (mode().ifdir())
return BAN::Error::from_errno(EISDIR);
if (offset >= m_inode.size)
@ -286,7 +286,7 @@ namespace Kernel
BAN::ErrorOr<BAN::Vector<BAN::String>> Ext2Inode::read_directory_entries(size_t index)
{
if (!ifdir())
if (!mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
uint32_t data_block_count = blocks();
@ -318,7 +318,7 @@ namespace Kernel
BAN::ErrorOr<void> Ext2Inode::create_file(BAN::StringView name, mode_t mode)
{
if (!ifdir())
if (!this->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
if (name.size() > 255)
@ -403,7 +403,7 @@ namespace Kernel
BAN::ErrorOr<BAN::RefPtr<Inode>> Ext2Inode::read_directory_inode(BAN::StringView file_name)
{
if (!ifdir())
if (!mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
uint32_t block_size = m_fs.block_size();

View File

@ -38,7 +38,7 @@ namespace Kernel
BAN::ErrorOr<void> VirtualFileSystem::mount(FileSystem* file_system, BAN::StringView path)
{
auto file = TRY(file_from_absolute_path(path));
if (!file.inode->ifdir())
if (!file.inode->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
TRY(m_mount_points.push_back({ file, file_system }));
return {};

View File

@ -106,7 +106,7 @@ namespace Kernel
out->st_dev = open_fd.inode->dev();
out->st_ino = open_fd.inode->ino();
out->st_mode = open_fd.inode->mode();
out->st_mode = open_fd.inode->mode().mode;
out->st_nlink = open_fd.inode->nlink();
out->st_uid = open_fd.inode->uid();
out->st_gid = open_fd.inode->gid();
@ -153,7 +153,7 @@ namespace Kernel
BAN::String absolute_path = TRY(absolute_path_of(path));
auto file = TRY(VirtualFileSystem::get().file_from_absolute_path(absolute_path));
if (!file.inode->ifdir())
if (!file.inode->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
m_working_directory = BAN::move(file.canonical_path);

View File

@ -22,22 +22,24 @@ namespace Kernel
static auto s_default_prompt = "\\[\e[32m\\]user\\[\e[m\\]:\\[\e[34m\\]\\w\\[\e[m\\]# "sv;
static const char* mode_string(mode_t mode)
static const char* mode_string(Inode::Mode mode)
{
static char buffer[11] {};
buffer[0] =
(mode & Inode::Mode::IFDIR) ? 'd' :
(mode & Inode::Mode::IFCHR) ? 'c' :
'-';
buffer[1] = (mode & Inode::Mode::IRUSR) ? 'r' : '-';
buffer[2] = (mode & Inode::Mode::IWUSR) ? 'w' : '-';
buffer[3] = (mode & Inode::Mode::IXUSR) ? 'x' : '-';
buffer[4] = (mode & Inode::Mode::IRGRP) ? 'r' : '-';
buffer[5] = (mode & Inode::Mode::IWGRP) ? 'w' : '-';
buffer[6] = (mode & Inode::Mode::IXGRP) ? 'x' : '-';
buffer[7] = (mode & Inode::Mode::IROTH) ? 'r' : '-';
buffer[8] = (mode & Inode::Mode::IWOTH) ? 'w' : '-';
buffer[9] = (mode & Inode::Mode::IXOTH) ? 'x' : '-';
mode.ifdir() ? 'd' :
mode.ifblk() ? 'b' :
mode.ifchr() ? 'c' :
'-';
buffer[1] = (mode.mode & Inode::Mode::IRUSR) ? 'r' : '-';
buffer[2] = (mode.mode & Inode::Mode::IWUSR) ? 'w' : '-';
buffer[3] = (mode.mode & Inode::Mode::IXUSR) ? 'x' : '-';
buffer[4] = (mode.mode & Inode::Mode::IRGRP) ? 'r' : '-';
buffer[5] = (mode.mode & Inode::Mode::IWGRP) ? 'w' : '-';
buffer[6] = (mode.mode & Inode::Mode::IXGRP) ? 'x' : '-';
buffer[7] = (mode.mode & Inode::Mode::IROTH) ? 'r' : '-';
buffer[8] = (mode.mode & Inode::Mode::IWOTH) ? 'w' : '-';
buffer[9] = (mode.mode & Inode::Mode::IXOTH) ? 'x' : '-';
return (const char*)buffer;
};
@ -404,13 +406,15 @@ argument_done:
TRY(entry_path.append(entry));
TRY(Process::current()->stat(entry_path, &st));
Inode::Mode mode { st.st_mode };
const char* color =
(st.st_mode & Inode::Mode::IFDIR) ? "34" :
(st.st_mode & Inode::Mode::IFCHR) ? "33" :
(st.st_mode & Inode::Mode::IXUSR) ? "32" :
"";
mode.ifdir() ? "34" :
mode.ifchr() || mode.ifblk() ? "33" :
(mode.mode & Inode::Mode::IXUSR) ? "32" :
"";
TTY_PRINTLN(" {} {7} \e[{}m{}\e[m", mode_string(st.st_mode), st.st_size, color, entry);
TTY_PRINTLN(" {} {7} \e[{}m{}\e[m", mode_string(mode), st.st_size, color, entry);
}
}
else if (arguments.front() == "cat")
@ -439,11 +443,14 @@ argument_done:
stat st;
TRY(Process::current()->stat(arguments[1], &st));
Inode::Mode mode { st.st_mode };
const char* type =
(st.st_mode & Inode::Mode::IFREG) ? "regular file" :
(st.st_mode & Inode::Mode::IFDIR) ? "directory" :
(st.st_mode & Inode::Mode::IFCHR) ? "character device" :
"other";
mode.ifreg() ? "regular file" :
mode.ifdir() ? "directory" :
mode.ifchr() ? "character device" :
mode.ifblk() ? "block device" :
"other";
TTY_PRINTLN(" File: {}", arguments[1]);
TTY_PRINTLN(" Size: {}\tBlocks: {}\tIO Block: {}\t {}", st.st_size, st.st_blocks, st.st_blksize, type);
@ -451,7 +458,7 @@ argument_done:
if (st.st_rdev)
TTY_PRINT("\tDevice type: {},{}", st.st_rdev >> 8, st.st_rdev & 0xFF);
TTY_PRINTLN("");
TTY_PRINTLN("Access: ({4O}/{})\tUid: {}\tGid: {}", st.st_mode & 0777, mode_string(st.st_mode), st.st_uid, st.st_gid);
TTY_PRINTLN("Access: ({4O}/{})\tUid: {}\tGid: {}", mode.mode & 0777, mode_string(mode), st.st_uid, st.st_gid);
TTY_PRINTLN("Access: {}", BAN::from_unix_time(st.st_atime));
TTY_PRINTLN("Modify: {}", BAN::from_unix_time(st.st_mtime));
TTY_PRINTLN("Change: {}", BAN::from_unix_time(st.st_ctime));