/* * Copyright (c) 2021, Liav A. * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include namespace Kernel { struct SysFSInodeData : public OpenFileDescriptionData { OwnPtr buffer; }; class SysFSDirectory; class SysFSComponent : public AtomicRefCounted { friend class SysFSDirectory; public: virtual StringView name() const = 0; virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const { return Error::from_errno(ENOTIMPL); } virtual ErrorOr traverse_as_directory(FileSystemID, Function(FileSystem::DirectoryEntryView const&)>) const { VERIFY_NOT_REACHED(); } virtual LockRefPtr lookup(StringView) { VERIFY_NOT_REACHED(); }; virtual mode_t permissions() const; virtual ErrorOr truncate(u64) { return EPERM; } virtual size_t size() const { return 0; } virtual ErrorOr write_bytes(off_t, size_t, UserOrKernelBuffer const&, OpenFileDescription*) { return EROFS; } virtual ErrorOr refresh_data(OpenFileDescription&) const { return {}; } virtual ErrorOr> to_inode(SysFS const&) const; InodeIndex component_index() const { return m_component_index; }; 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(); LockRefPtr m_parent_directory; IntrusiveListNode> m_list_node; private: InodeIndex m_component_index {}; }; class SysFSSymbolicLink : public SysFSComponent { public: virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override final; virtual ErrorOr> to_inode(SysFS const& sysfs_instance) const override final; protected: ErrorOr> try_generate_return_path_to_mount_point() const; ErrorOr> try_to_generate_buffer() const; explicit SysFSSymbolicLink(SysFSDirectory const& parent_directory, SysFSComponent const& pointed_component); LockRefPtr m_pointed_component; }; class SysFSDirectory : public SysFSComponent { public: virtual ErrorOr traverse_as_directory(FileSystemID, Function(FileSystem::DirectoryEntryView const&)>) const override final; virtual LockRefPtr lookup(StringView name) override final; virtual ErrorOr> to_inode(SysFS const& sysfs_instance) const override final; using ChildList = SpinlockProtected, LockRank::None>; protected: virtual bool is_root_directory() const { return false; } SysFSDirectory() {}; explicit SysFSDirectory(SysFSDirectory const& parent_directory); ChildList m_child_components {}; }; }