Kernel: Inode can now return full mode value

Kernel::Inode::Mode is an union of bitmasked fields for every
possible bit
This commit is contained in:
Bananymous 2023-02-20 21:39:24 +02:00
parent fbc17eb6dd
commit 038379274e
4 changed files with 58 additions and 26 deletions

View File

@ -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<BAN::Vector<uint8_t>> read_all() override;

View File

@ -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<BAN::Vector<uint8_t>> read_all() = 0;

View File

@ -4,7 +4,7 @@
#include <kernel/kprint.h>
#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,
@ -138,16 +138,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<void> Ext2Inode::for_each_block(BAN::Function<BAN::ErrorOr<bool>(const BAN::Vector<uint8_t>&)>& func)
{
uint32_t data_blocks_left = m_inode.blocks / (2 << m_fs->superblock().log_block_size);
@ -237,7 +227,7 @@ namespace Kernel
BAN::ErrorOr<BAN::Vector<uint8_t>> Ext2Inode::read_all()
{
if (is_directory())
if (ifdir())
return BAN::Error::from_string("Inode is a directory");
BAN::Vector<uint8_t> data_buffer;
@ -265,7 +255,7 @@ namespace Kernel
BAN::ErrorOr<BAN::RefCounted<Inode>> Ext2Inode::directory_find(BAN::StringView file_name)
{
if (!is_directory())
if (!ifdir())
return BAN::Error::from_string("Inode is not a directory");
BAN::RefCounted<Inode> result;
@ -298,7 +288,7 @@ namespace Kernel
BAN::ErrorOr<BAN::Vector<BAN::RefCounted<Inode>>> Ext2Inode::directory_inodes()
{
if (!is_directory())
if (!ifdir())
return BAN::Error::from_string("Inode is not a directory");
BAN::Vector<BAN::RefCounted<Inode>> inodes;

View File

@ -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")
{