From 038379274ead6368243c09a95f441556d1fcf833 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 20 Feb 2023 21:39:24 +0200 Subject: [PATCH] Kernel: Inode can now return full mode value Kernel::Inode::Mode is an union of bitmasked fields for every possible bit --- kernel/include/kernel/FS/Ext2.h | 5 ++--- kernel/include/kernel/FS/Inode.h | 31 +++++++++++++++++++++++++++++-- kernel/kernel/FS/Ext2.cpp | 20 +++++--------------- kernel/kernel/Shell.cpp | 28 ++++++++++++++++++++++------ 4 files changed, 58 insertions(+), 26 deletions(-) diff --git a/kernel/include/kernel/FS/Ext2.h b/kernel/include/kernel/FS/Ext2.h index 9d295995ea..cb19af8a1c 100644 --- a/kernel/include/kernel/FS/Ext2.h +++ b/kernel/include/kernel/FS/Ext2.h @@ -122,13 +122,12 @@ namespace Kernel class Ext2Inode : public Inode { public: - virtual bool is_directory() const override; - virtual bool is_regular_file() const override; - virtual uint16_t uid() const override { return m_inode.uid; } virtual uint16_t gid() const override { return m_inode.gid; } virtual uint32_t size() const override { return m_inode.size; } + virtual Mode mode() const override { return { .mode = m_inode.mode }; } + virtual BAN::StringView name() const override { return m_name; } virtual BAN::ErrorOr> read_all() override; diff --git a/kernel/include/kernel/FS/Inode.h b/kernel/include/kernel/FS/Inode.h index bb0af382b1..d51c569a60 100644 --- a/kernel/include/kernel/FS/Inode.h +++ b/kernel/include/kernel/FS/Inode.h @@ -11,13 +11,40 @@ namespace Kernel class Inode { public: - virtual bool is_directory() const = 0; - virtual bool is_regular_file() const = 0; + union Mode + { + struct + { + uint16_t IXOTH : 1; // 0x0001 + uint16_t IWOTH : 1; // 0x0002 + uint16_t IROTH : 1; // 0x0004 + uint16_t IXGRP : 1; // 0x0008 + uint16_t IWGRP : 1; // 0x0010 + uint16_t IRGRP : 1; // 0x0020 + uint16_t IXUSR : 1; // 0x0040 + uint16_t IWUSR : 1; // 0x0080 + uint16_t IRUSR : 1; // 0x0100 + uint16_t ISVTX : 1; // 0x0200 + uint16_t ISGID : 1; // 0x0400 + uint16_t ISUID : 1; // 0x0800 + uint16_t IFIFO : 1; // 0x1000 + uint16_t IFCHR : 1; // 0x2000 + uint16_t IFDIR : 1; // 0x4000 + uint16_t IFREG : 1; // 0x8000 + }; + uint16_t mode; + }; + + public: + bool ifdir() const { return mode().IFDIR; } + bool ifreg() const { return mode().IFREG; } virtual uint16_t uid() const = 0; virtual uint16_t gid() const = 0; virtual uint32_t size() const = 0; + virtual Mode mode() const = 0; + virtual BAN::StringView name() const = 0; virtual BAN::ErrorOr> read_all() = 0; diff --git a/kernel/kernel/FS/Ext2.cpp b/kernel/kernel/FS/Ext2.cpp index a7ab22493e..f7f1165405 100644 --- a/kernel/kernel/FS/Ext2.cpp +++ b/kernel/kernel/FS/Ext2.cpp @@ -4,7 +4,7 @@ #include -#define EXT2_DEBUG_PRINT 1 +#define EXT2_DEBUG_PRINT 0 namespace Kernel { @@ -88,7 +88,7 @@ namespace Kernel enum InodeMode { // -- file format -- - IFSOKC = 0xC000, + IFSOCK = 0xC000, IFLNK = 0xA000, IFREG = 0x8000, IFBLK = 0x6000, @@ -137,16 +137,6 @@ namespace Kernel }; } - - bool Ext2Inode::is_directory() const - { - return m_inode.mode & Ext2::Enum::IFDIR; - } - - bool Ext2Inode::is_regular_file() const - { - return m_inode.mode & Ext2::Enum::IFREG; - } BAN::ErrorOr Ext2Inode::for_each_block(BAN::Function(const BAN::Vector&)>& func) { @@ -237,7 +227,7 @@ namespace Kernel BAN::ErrorOr> Ext2Inode::read_all() { - if (is_directory()) + if (ifdir()) return BAN::Error::from_string("Inode is a directory"); BAN::Vector data_buffer; @@ -265,7 +255,7 @@ namespace Kernel BAN::ErrorOr> Ext2Inode::directory_find(BAN::StringView file_name) { - if (!is_directory()) + if (!ifdir()) return BAN::Error::from_string("Inode is not a directory"); BAN::RefCounted result; @@ -298,7 +288,7 @@ namespace Kernel BAN::ErrorOr>> Ext2Inode::directory_inodes() { - if (!is_directory()) + if (!ifdir()) return BAN::Error::from_string("Inode is not a directory"); BAN::Vector> inodes; diff --git a/kernel/kernel/Shell.cpp b/kernel/kernel/Shell.cpp index 652c59cb1b..47f8f3a66e 100644 --- a/kernel/kernel/Shell.cpp +++ b/kernel/kernel/Shell.cpp @@ -271,7 +271,7 @@ argument_done: path = path.substring(1); auto directory = VirtualFileSystem::get().root_inode(); - ASSERT(directory->is_directory()); + ASSERT(directory->ifdir()); if (arguments.size() == 2) { @@ -282,7 +282,7 @@ argument_done: if (inode_or_error.is_error()) return TTY_PRINTLN("{}", inode_or_error.get_error().get_message()); directory = inode_or_error.value(); - if (!directory->is_directory()) + if (!directory->ifdir()) return TTY_PRINTLN("expected argument to be path to directory"); } } @@ -292,13 +292,29 @@ argument_done: return TTY_PRINTLN("{}", inodes_or_error.get_error().get_message()); auto& inodes = inodes_or_error.value(); + auto mode_string = [](Inode::Mode mode) + { + static char buffer[11] {}; + buffer[0] = mode.IFDIR ? 'd' : '-'; + buffer[1] = mode.IRUSR ? 'r' : '-'; + buffer[2] = mode.IWUSR ? 'w' : '-'; + buffer[3] = mode.IXUSR ? 'x' : '-'; + buffer[4] = mode.IRGRP ? 'r' : '-'; + buffer[5] = mode.IWGRP ? 'w' : '-'; + buffer[6] = mode.IXGRP ? 'x' : '-'; + buffer[7] = mode.IROTH ? 'r' : '-'; + buffer[8] = mode.IWOTH ? 'w' : '-'; + buffer[9] = mode.IXOTH ? 'x' : '-'; + return (const char*)buffer; + }; + TTY_PRINTLN("/{}", path); for (auto& inode : inodes) - if (inode->is_directory()) - TTY_PRINTLN(" {7} \e[34m{}\e[m", inode->size(), inode->name()); + if (inode->ifdir()) + TTY_PRINTLN(" {} {7} \e[34m{}\e[m", mode_string(inode->mode()), inode->size(), inode->name()); for (auto& inode : inodes) - if (!inode->is_directory()) - TTY_PRINTLN(" {7} {}", inode->size(), inode->name()); + if (!inode->ifdir()) + TTY_PRINTLN(" {} {7} {}", mode_string(inode->mode()), inode->size(), inode->name()); } else if (arguments.front() == "cat") {