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: public:
virtual ino_t ino() const override { return 0; } 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 nlink_t nlink() const override { return 0; }
virtual uid_t uid() const override { return 0; } virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; } virtual gid_t gid() const override { return 0; }

View File

@ -124,7 +124,7 @@ namespace Kernel
{ {
public: public:
virtual ino_t ino() const override { return m_index; }; 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 nlink_t nlink() const override { return m_inode.links_count; }
virtual uid_t uid() const override { return m_inode.uid; } virtual uid_t uid() const override { return m_inode.uid; }
virtual gid_t gid() const override { return m_inode.gid; } virtual gid_t gid() const override { return m_inode.gid; }

View File

@ -13,43 +13,48 @@ namespace Kernel
class Inode : public BAN::RefCounted<Inode> class Inode : public BAN::RefCounted<Inode>
{ {
public: public:
enum Mode : mode_t struct Mode
{ {
IXOTH = 0x0001, enum Mask : mode_t
IWOTH = 0x0002, {
IROTH = 0x0004, IXOTH = 0x0001,
IXGRP = 0x0008, IWOTH = 0x0002,
IWGRP = 0x0010, IROTH = 0x0004,
IRGRP = 0x0020, IXGRP = 0x0008,
IXUSR = 0x0040, IWGRP = 0x0010,
IWUSR = 0x0080, IRGRP = 0x0020,
IRUSR = 0x0100, IXUSR = 0x0040,
ISVTX = 0x0200, IWUSR = 0x0080,
ISGID = 0x0400, IRUSR = 0x0100,
ISUID = 0x0800, ISVTX = 0x0200,
IFIFO = 0x1000, ISGID = 0x0400,
IFCHR = 0x2000, ISUID = 0x0800,
IFDIR = 0x4000, IFIFO = 0x1000,
IFBLK = 0x6000, IFCHR = 0x2000,
IFREG = 0x8000, IFDIR = 0x4000,
IFLNK = 0xA000, IFBLK = 0x6000,
IFSOCK = 0xC000, IFREG = 0x8000,
TYPE_MASK = 0xF000, 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: public:
virtual ~Inode() {} 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(); } bool operator==(const Inode& other) const { return dev() == other.dev() && rdev() == other.rdev() && ino() == other.ino(); }
virtual ino_t ino() const = 0; virtual ino_t ino() const = 0;
virtual mode_t mode() const = 0; virtual Mode mode() const = 0;
virtual nlink_t nlink() const = 0; virtual nlink_t nlink() const = 0;
virtual uid_t uid() const = 0; virtual uid_t uid() const = 0;
virtual gid_t gid() const = 0; virtual gid_t gid() const = 0;
@ -64,11 +69,11 @@ namespace Kernel
virtual BAN::StringView name() const = 0; 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::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 (!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<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 (!ifdir()) return BAN::Error::from_errno(ENOTDIR); 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: public:
virtual ino_t ino() const override { return m_ino; } 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 nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return 0; } virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; } virtual gid_t gid() const override { return 0; }

View File

@ -43,7 +43,7 @@ namespace Kernel
public: public:
virtual ino_t ino() const override { return !!slave_bit; } 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 nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return 0; } virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; } virtual gid_t gid() const override { return 0; }
@ -92,7 +92,7 @@ namespace Kernel
public: public:
virtual ino_t ino() const override { return 0; } 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 nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return 0; } virtual uid_t uid() const override { return 0; }
virtual gid_t gid() const override { return 0; } virtual gid_t gid() const override { return 0; }

View File

@ -43,7 +43,7 @@ namespace Kernel
public: public:
virtual ino_t ino() const override { return m_index; } 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 nlink_t nlink() const override { return 1; }
virtual uid_t uid() const override { return 0; } virtual uid_t uid() const override { return 0; }
virtual gid_t gid() 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 // FIXME: update atime if needed
if (ifdir()) if (mode().ifdir())
return BAN::Error::from_errno(EISDIR); return BAN::Error::from_errno(EISDIR);
if (offset >= m_inode.size) if (offset >= m_inode.size)
@ -286,7 +286,7 @@ namespace Kernel
BAN::ErrorOr<BAN::Vector<BAN::String>> Ext2Inode::read_directory_entries(size_t index) BAN::ErrorOr<BAN::Vector<BAN::String>> Ext2Inode::read_directory_entries(size_t index)
{ {
if (!ifdir()) if (!mode().ifdir())
return BAN::Error::from_errno(ENOTDIR); return BAN::Error::from_errno(ENOTDIR);
uint32_t data_block_count = blocks(); uint32_t data_block_count = blocks();
@ -318,7 +318,7 @@ namespace Kernel
BAN::ErrorOr<void> Ext2Inode::create_file(BAN::StringView name, mode_t mode) BAN::ErrorOr<void> Ext2Inode::create_file(BAN::StringView name, mode_t mode)
{ {
if (!ifdir()) if (!this->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR); return BAN::Error::from_errno(ENOTDIR);
if (name.size() > 255) if (name.size() > 255)
@ -403,7 +403,7 @@ namespace Kernel
BAN::ErrorOr<BAN::RefPtr<Inode>> Ext2Inode::read_directory_inode(BAN::StringView file_name) BAN::ErrorOr<BAN::RefPtr<Inode>> Ext2Inode::read_directory_inode(BAN::StringView file_name)
{ {
if (!ifdir()) if (!mode().ifdir())
return BAN::Error::from_errno(ENOTDIR); return BAN::Error::from_errno(ENOTDIR);
uint32_t block_size = m_fs.block_size(); 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) BAN::ErrorOr<void> VirtualFileSystem::mount(FileSystem* file_system, BAN::StringView path)
{ {
auto file = TRY(file_from_absolute_path(path)); auto file = TRY(file_from_absolute_path(path));
if (!file.inode->ifdir()) if (!file.inode->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR); return BAN::Error::from_errno(ENOTDIR);
TRY(m_mount_points.push_back({ file, file_system })); TRY(m_mount_points.push_back({ file, file_system }));
return {}; return {};

View File

@ -106,7 +106,7 @@ namespace Kernel
out->st_dev = open_fd.inode->dev(); out->st_dev = open_fd.inode->dev();
out->st_ino = open_fd.inode->ino(); 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_nlink = open_fd.inode->nlink();
out->st_uid = open_fd.inode->uid(); out->st_uid = open_fd.inode->uid();
out->st_gid = open_fd.inode->gid(); out->st_gid = open_fd.inode->gid();
@ -153,7 +153,7 @@ namespace Kernel
BAN::String absolute_path = TRY(absolute_path_of(path)); BAN::String absolute_path = TRY(absolute_path_of(path));
auto file = TRY(VirtualFileSystem::get().file_from_absolute_path(absolute_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); return BAN::Error::from_errno(ENOTDIR);
m_working_directory = BAN::move(file.canonical_path); 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 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] {}; static char buffer[11] {};
buffer[0] = buffer[0] =
(mode & Inode::Mode::IFDIR) ? 'd' : mode.ifdir() ? 'd' :
(mode & Inode::Mode::IFCHR) ? 'c' : mode.ifblk() ? 'b' :
'-'; 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[1] = (mode.mode & Inode::Mode::IRUSR) ? 'r' : '-';
buffer[4] = (mode & Inode::Mode::IRGRP) ? 'r' : '-'; buffer[2] = (mode.mode & Inode::Mode::IWUSR) ? 'w' : '-';
buffer[5] = (mode & Inode::Mode::IWGRP) ? 'w' : '-'; buffer[3] = (mode.mode & Inode::Mode::IXUSR) ? 'x' : '-';
buffer[6] = (mode & Inode::Mode::IXGRP) ? 'x' : '-'; buffer[4] = (mode.mode & Inode::Mode::IRGRP) ? 'r' : '-';
buffer[7] = (mode & Inode::Mode::IROTH) ? 'r' : '-'; buffer[5] = (mode.mode & Inode::Mode::IWGRP) ? 'w' : '-';
buffer[8] = (mode & Inode::Mode::IWOTH) ? 'w' : '-'; buffer[6] = (mode.mode & Inode::Mode::IXGRP) ? 'x' : '-';
buffer[9] = (mode & Inode::Mode::IXOTH) ? '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; return (const char*)buffer;
}; };
@ -404,13 +406,15 @@ argument_done:
TRY(entry_path.append(entry)); TRY(entry_path.append(entry));
TRY(Process::current()->stat(entry_path, &st)); TRY(Process::current()->stat(entry_path, &st));
Inode::Mode mode { st.st_mode };
const char* color = const char* color =
(st.st_mode & Inode::Mode::IFDIR) ? "34" : mode.ifdir() ? "34" :
(st.st_mode & Inode::Mode::IFCHR) ? "33" : mode.ifchr() || mode.ifblk() ? "33" :
(st.st_mode & Inode::Mode::IXUSR) ? "32" : (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") else if (arguments.front() == "cat")
@ -439,11 +443,14 @@ argument_done:
stat st; stat st;
TRY(Process::current()->stat(arguments[1], &st)); TRY(Process::current()->stat(arguments[1], &st));
Inode::Mode mode { st.st_mode };
const char* type = const char* type =
(st.st_mode & Inode::Mode::IFREG) ? "regular file" : mode.ifreg() ? "regular file" :
(st.st_mode & Inode::Mode::IFDIR) ? "directory" : mode.ifdir() ? "directory" :
(st.st_mode & Inode::Mode::IFCHR) ? "character device" : mode.ifchr() ? "character device" :
"other"; mode.ifblk() ? "block device" :
"other";
TTY_PRINTLN(" File: {}", arguments[1]); TTY_PRINTLN(" File: {}", arguments[1]);
TTY_PRINTLN(" Size: {}\tBlocks: {}\tIO Block: {}\t {}", st.st_size, st.st_blocks, st.st_blksize, type); 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) if (st.st_rdev)
TTY_PRINT("\tDevice type: {},{}", st.st_rdev >> 8, st.st_rdev & 0xFF); TTY_PRINT("\tDevice type: {},{}", st.st_rdev >> 8, st.st_rdev & 0xFF);
TTY_PRINTLN(""); 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("Access: {}", BAN::from_unix_time(st.st_atime));
TTY_PRINTLN("Modify: {}", BAN::from_unix_time(st.st_mtime)); TTY_PRINTLN("Modify: {}", BAN::from_unix_time(st.st_mtime));
TTY_PRINTLN("Change: {}", BAN::from_unix_time(st.st_ctime)); TTY_PRINTLN("Change: {}", BAN::from_unix_time(st.st_ctime));