From ebf2b16d09b087a183c3ba6ee40ee00d094fb258 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 28 Jun 2025 21:28:54 +0300 Subject: [PATCH] Kernel: Implement chown to ext2 and tmpfs --- kernel/include/kernel/FS/Ext2/Inode.h | 1 + kernel/include/kernel/FS/TmpFS/Inode.h | 1 + kernel/kernel/FS/Ext2/Inode.cpp | 20 ++++++++++++++++++++ kernel/kernel/FS/TmpFS/Inode.cpp | 7 +++++++ 4 files changed, 29 insertions(+) diff --git a/kernel/include/kernel/FS/Ext2/Inode.h b/kernel/include/kernel/FS/Ext2/Inode.h index d84851bd..bfa07c25 100644 --- a/kernel/include/kernel/FS/Ext2/Inode.h +++ b/kernel/include/kernel/FS/Ext2/Inode.h @@ -46,6 +46,7 @@ namespace Kernel virtual BAN::ErrorOr write_impl(off_t, BAN::ConstByteSpan) override; virtual BAN::ErrorOr truncate_impl(size_t) override; virtual BAN::ErrorOr chmod_impl(mode_t) override; + virtual BAN::ErrorOr chown_impl(uid_t, gid_t) override; virtual BAN::ErrorOr utimens_impl(const timespec[2]) override; virtual BAN::ErrorOr fsync_impl() override; diff --git a/kernel/include/kernel/FS/TmpFS/Inode.h b/kernel/include/kernel/FS/TmpFS/Inode.h index eaffad03..76d17c24 100644 --- a/kernel/include/kernel/FS/TmpFS/Inode.h +++ b/kernel/include/kernel/FS/TmpFS/Inode.h @@ -48,6 +48,7 @@ namespace Kernel TmpInode(TmpFileSystem&, ino_t, const TmpInodeInfo&); virtual BAN::ErrorOr chmod_impl(mode_t) override; + virtual BAN::ErrorOr chown_impl(uid_t, gid_t) override; virtual BAN::ErrorOr utimens_impl(const timespec[2]) override; virtual BAN::ErrorOr fsync_impl() override { return {}; } diff --git a/kernel/kernel/FS/Ext2/Inode.cpp b/kernel/kernel/FS/Ext2/Inode.cpp index c7560433..0dcdf75d 100644 --- a/kernel/kernel/FS/Ext2/Inode.cpp +++ b/kernel/kernel/FS/Ext2/Inode.cpp @@ -289,6 +289,26 @@ namespace Kernel return {}; } + BAN::ErrorOr Ext2Inode::chown_impl(uid_t uid, gid_t gid) + { + if (m_inode.uid == uid && m_inode.gid == gid) + return {}; + + const auto old_uid = m_inode.uid; + const auto old_gid = m_inode.gid; + + m_inode.uid = uid; + m_inode.gid = gid; + if (auto ret = sync(); ret.is_error()) + { + m_inode.uid = old_uid; + m_inode.gid = old_gid; + return ret.release_error(); + } + + return {}; + } + BAN::ErrorOr Ext2Inode::utimens_impl(const timespec times[2]) { const uint32_t old_times[2] { diff --git a/kernel/kernel/FS/TmpFS/Inode.cpp b/kernel/kernel/FS/TmpFS/Inode.cpp index 78e46cb8..c28ba0e1 100644 --- a/kernel/kernel/FS/TmpFS/Inode.cpp +++ b/kernel/kernel/FS/TmpFS/Inode.cpp @@ -100,6 +100,13 @@ namespace Kernel return {}; } + BAN::ErrorOr TmpInode::chown_impl(uid_t new_uid, gid_t new_gid) + { + m_inode_info.uid = new_uid; + m_inode_info.gid = new_gid; + return {}; + } + BAN::ErrorOr TmpInode::utimens_impl(const timespec times[2]) { if (times[0].tv_nsec != UTIME_OMIT)