From 7e88bbe5507f413f776d2a279b8e3f303b3b5c10 Mon Sep 17 00:00:00 2001 From: Liav A Date: Fri, 22 Apr 2022 20:04:38 +0300 Subject: [PATCH] Kernel/SysFS: Add two methods related to relative paths for components These methods will be used later on to introduce symbolic links support in the SysFS, so the kernel will be able to resolve relative paths of components in filesystem based on using the m_parent_directory pointer in each SysFSComponent object. --- Kernel/FileSystem/SysFS/Component.cpp | 29 +++++++++++++++++++++++++-- Kernel/FileSystem/SysFS/Component.h | 10 +++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Kernel/FileSystem/SysFS/Component.cpp b/Kernel/FileSystem/SysFS/Component.cpp index af366a5880e..38d5de68aba 100644 --- a/Kernel/FileSystem/SysFS/Component.cpp +++ b/Kernel/FileSystem/SysFS/Component.cpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace Kernel { @@ -21,11 +22,36 @@ static size_t allocate_inode_index() return s_next_inode_index.value(); } +SysFSComponent::SysFSComponent(SysFSDirectory const& parent_directory) + : m_parent_directory(parent_directory) + , m_component_index(allocate_inode_index()) +{ +} + SysFSComponent::SysFSComponent() : m_component_index(allocate_inode_index()) { } +ErrorOr> SysFSComponent::relative_path(NonnullOwnPtr name, size_t current_hop) const +{ + if (current_hop >= 128) + return Error::from_errno(ELOOP); + if (!m_parent_directory) + return name; + auto joined_name = TRY(KLexicalPath::try_join(m_parent_directory->name(), name->view())); + return m_parent_directory->relative_path(move(joined_name), current_hop + 1); +} + +ErrorOr SysFSComponent::relative_path_hops_count_from_mountpoint(size_t current_hop) const +{ + if (current_hop >= 128) + return Error::from_errno(ELOOP); + if (!m_parent_directory) + return current_hop; + return m_parent_directory->relative_path_hops_count_from_mountpoint(current_hop + 1); +} + mode_t SysFSComponent::permissions() const { return S_IRUSR | S_IRGRP | S_IROTH; @@ -56,8 +82,7 @@ RefPtr SysFSDirectory::lookup(StringView name) } SysFSDirectory::SysFSDirectory(SysFSDirectory const& parent_directory) - : SysFSComponent() - , m_parent_directory(parent_directory) + : SysFSComponent(parent_directory) { } diff --git a/Kernel/FileSystem/SysFS/Component.h b/Kernel/FileSystem/SysFS/Component.h index b7d1032e8c2..62162a29c9e 100644 --- a/Kernel/FileSystem/SysFS/Component.h +++ b/Kernel/FileSystem/SysFS/Component.h @@ -23,6 +23,7 @@ struct SysFSInodeData : public OpenFileDescriptionData { OwnPtr buffer; }; +class SysFSDirectory; class SysFSComponent : public RefCounted { public: virtual StringView name() const = 0; @@ -42,9 +43,15 @@ public: virtual ~SysFSComponent() = default; + ErrorOr> relative_path(NonnullOwnPtr, size_t current_hop = 0) const; + ErrorOr relative_path_hops_count_from_mountpoint(size_t current_hop = 0) const; + protected: + explicit SysFSComponent(SysFSDirectory const& parent_directory); SysFSComponent(); + RefPtr m_parent_directory; + private: InodeIndex m_component_index {}; }; @@ -57,10 +64,9 @@ public: virtual ErrorOr> to_inode(SysFS const& sysfs_instance) const override final; protected: - SysFSDirectory() = default; + SysFSDirectory() {}; explicit SysFSDirectory(SysFSDirectory const& parent_directory); NonnullRefPtrVector m_components; - RefPtr m_parent_directory; }; }