1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:57:35 +00:00

Kernel: Move SystemExposed.* => FileSystem/SysFSComponent.*

This commit is contained in:
Andreas Kling 2021-07-11 01:14:53 +02:00
parent 807aadbe6e
commit d40ea1a0a8
5 changed files with 4 additions and 4 deletions

View file

@ -8,7 +8,7 @@
#include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/FileSystem/Inode.h>
#include <Kernel/SystemExposed.h>
#include <Kernel/FileSystem/SysFSComponent.h>
namespace Kernel {

View file

@ -0,0 +1,74 @@
/*
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/FileSystem/SysFS.h>
#include <Kernel/FileSystem/SysFSComponent.h>
namespace Kernel {
static SpinLock<u8> s_index_lock;
static InodeIndex s_next_inode_index { 0 };
static size_t allocate_inode_index()
{
ScopedSpinLock lock(s_index_lock);
s_next_inode_index = s_next_inode_index.value() + 1;
VERIFY(s_next_inode_index > 0);
return s_next_inode_index.value();
}
SysFSComponent::SysFSComponent(StringView name)
: m_name(KString::try_create(name).release_nonnull())
, m_component_index(allocate_inode_index())
{
}
KResult SysFSDirectory::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
{
Locker locker(SysFSComponentRegistry::the().m_lock);
VERIFY(m_parent_folder);
callback({ ".", { fsid, component_index() }, 0 });
callback({ "..", { fsid, m_parent_folder->component_index() }, 0 });
for (auto& component : m_components) {
InodeIdentifier identifier = { fsid, component.component_index() };
callback({ component.name(), identifier, 0 });
}
return KSuccess;
}
RefPtr<SysFSComponent> SysFSDirectory::lookup(StringView name)
{
for (auto& component : m_components) {
if (component.name() == name) {
return component;
}
}
return {};
}
SysFSDirectory::SysFSDirectory(StringView name)
: SysFSComponent(name)
{
}
SysFSDirectory::SysFSDirectory(StringView name, SysFSDirectory const& parent_folder)
: SysFSComponent(name)
, m_parent_folder(parent_folder)
{
}
NonnullRefPtr<Inode> SysFSDirectory::to_inode(SysFS const& sysfs_instance) const
{
return SysFSDirectoryInode::create(sysfs_instance, *this);
}
NonnullRefPtr<Inode> SysFSComponent::to_inode(SysFS const& sysfs_instance) const
{
return SysFSInode::create(sysfs_instance, *this);
}
}

View file

@ -0,0 +1,60 @@
/*
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Function.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/StringView.h>
#include <AK/Types.h>
#include <Kernel/FileSystem/File.h>
#include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/FileSystem/Forward.h>
#include <Kernel/KResult.h>
namespace Kernel {
class SysFSComponent : public RefCounted<SysFSComponent> {
public:
virtual KResultOr<size_t> entries_count() const { VERIFY_NOT_REACHED(); };
virtual StringView name() const { return m_name->view(); }
virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer&, FileDescription*) const { VERIFY_NOT_REACHED(); }
virtual KResult traverse_as_directory(unsigned, Function<bool(FileSystem::DirectoryEntryView const&)>) const { VERIFY_NOT_REACHED(); }
virtual RefPtr<SysFSComponent> lookup(StringView) { VERIFY_NOT_REACHED(); };
virtual KResultOr<size_t> write_bytes(off_t, size_t, UserOrKernelBuffer const&, FileDescription*) { return -EROFS; }
virtual size_t size() const { return 0; }
virtual NonnullRefPtr<Inode> to_inode(SysFS const&) const;
InodeIndex component_index() const { return m_component_index; };
virtual ~SysFSComponent() = default;
protected:
explicit SysFSComponent(StringView name);
private:
NonnullOwnPtr<KString> m_name;
InodeIndex m_component_index {};
};
class SysFSDirectory : public SysFSComponent {
public:
virtual KResultOr<size_t> entries_count() const override { return m_components.size(); };
virtual KResult traverse_as_directory(unsigned, Function<bool(FileSystem::DirectoryEntryView const&)>) const override;
virtual RefPtr<SysFSComponent> lookup(StringView name) override;
virtual NonnullRefPtr<Inode> to_inode(SysFS const& sysfs_instance) const override final;
protected:
explicit SysFSDirectory(StringView name);
SysFSDirectory(StringView name, SysFSDirectory const& parent_folder);
NonnullRefPtrVector<SysFSComponent> m_components;
RefPtr<SysFSDirectory> m_parent_folder;
};
}