From 5b3a00c64f354058dfc337f5776efea674e974e7 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 30 Mar 2023 14:41:15 +0300 Subject: [PATCH] Kernel: Inode::Mode is now a struct so we can have functions in it --- kernel/include/kernel/Device.h | 2 +- kernel/include/kernel/FS/Ext2.h | 2 +- kernel/include/kernel/FS/Inode.h | 69 ++++++++++--------- kernel/include/kernel/Input/PS2Controller.h | 2 +- kernel/include/kernel/Storage/ATAController.h | 4 +- kernel/include/kernel/Storage/StorageDevice.h | 2 +- kernel/kernel/FS/Ext2.cpp | 8 +-- kernel/kernel/FS/VirtualFileSystem.cpp | 2 +- kernel/kernel/Process.cpp | 4 +- kernel/kernel/Shell.cpp | 53 +++++++------- 10 files changed, 80 insertions(+), 68 deletions(-) diff --git a/kernel/include/kernel/Device.h b/kernel/include/kernel/Device.h index de80dca4..15e804fc 100644 --- a/kernel/include/kernel/Device.h +++ b/kernel/include/kernel/Device.h @@ -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; } diff --git a/kernel/include/kernel/FS/Ext2.h b/kernel/include/kernel/FS/Ext2.h index 43e86ea1..89614482 100644 --- a/kernel/include/kernel/FS/Ext2.h +++ b/kernel/include/kernel/FS/Ext2.h @@ -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; } diff --git a/kernel/include/kernel/FS/Inode.h b/kernel/include/kernel/FS/Inode.h index 6e5bb0af..f32a2d55 100644 --- a/kernel/include/kernel/FS/Inode.h +++ b/kernel/include/kernel/FS/Inode.h @@ -13,43 +13,48 @@ namespace Kernel class Inode : public BAN::RefCounted { 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> read_directory_inode(BAN::StringView) { if (!ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); } - virtual BAN::ErrorOr> read_directory_entries(size_t) { if (!ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); } + virtual BAN::ErrorOr> read_directory_inode(BAN::StringView) { if (!mode().ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); } + virtual BAN::ErrorOr> read_directory_entries(size_t) { if (!mode().ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); } - virtual BAN::ErrorOr read(size_t, void*, size_t) { if ( ifdir()) return BAN::Error::from_errno(EISDIR); ASSERT_NOT_REACHED(); } - virtual BAN::ErrorOr create_file(BAN::StringView, mode_t) { if (!ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); } + virtual BAN::ErrorOr read(size_t, void*, size_t) { if ( mode().ifdir()) return BAN::Error::from_errno(EISDIR); ASSERT_NOT_REACHED(); } + virtual BAN::ErrorOr create_file(BAN::StringView, mode_t) { if (!mode().ifdir()) return BAN::Error::from_errno(ENOTDIR); ASSERT_NOT_REACHED(); } }; } \ No newline at end of file diff --git a/kernel/include/kernel/Input/PS2Controller.h b/kernel/include/kernel/Input/PS2Controller.h index 4128c42e..cb3849f6 100644 --- a/kernel/include/kernel/Input/PS2Controller.h +++ b/kernel/include/kernel/Input/PS2Controller.h @@ -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; } diff --git a/kernel/include/kernel/Storage/ATAController.h b/kernel/include/kernel/Storage/ATAController.h index 8ac6af75..9ced08cf 100644 --- a/kernel/include/kernel/Storage/ATAController.h +++ b/kernel/include/kernel/Storage/ATAController.h @@ -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; } diff --git a/kernel/include/kernel/Storage/StorageDevice.h b/kernel/include/kernel/Storage/StorageDevice.h index 6290e333..6547be12 100644 --- a/kernel/include/kernel/Storage/StorageDevice.h +++ b/kernel/include/kernel/Storage/StorageDevice.h @@ -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; } diff --git a/kernel/kernel/FS/Ext2.cpp b/kernel/kernel/FS/Ext2.cpp index cc59986c..24270726 100644 --- a/kernel/kernel/FS/Ext2.cpp +++ b/kernel/kernel/FS/Ext2.cpp @@ -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> 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 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> 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(); diff --git a/kernel/kernel/FS/VirtualFileSystem.cpp b/kernel/kernel/FS/VirtualFileSystem.cpp index 046f7077..6cf5ec9b 100644 --- a/kernel/kernel/FS/VirtualFileSystem.cpp +++ b/kernel/kernel/FS/VirtualFileSystem.cpp @@ -38,7 +38,7 @@ namespace Kernel BAN::ErrorOr 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 {}; diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 427dc2b4..01023071 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -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); diff --git a/kernel/kernel/Shell.cpp b/kernel/kernel/Shell.cpp index a0360f5f..be840156 100644 --- a/kernel/kernel/Shell.cpp +++ b/kernel/kernel/Shell.cpp @@ -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));