summaryrefslogtreecommitdiff
path: root/Kernel/SystemExposed.cpp
blob: 12598dad7dff0858c2d1cafba3980c74aa12e344 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <Kernel/FileSystem/SysFS.h>
#include <Kernel/SystemExposed.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();
}

SystemExposedComponent::SystemExposedComponent(StringView name)
    : m_name(KString::try_create(name).release_nonnull())
    , m_component_index(allocate_inode_index())
{
}

KResult SystemExposedFolder::traverse_as_directory(unsigned fsid, Function<bool(const FS::DirectoryEntryView&)> callback) const
{
    Locker locker(SystemRegistrar::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<SystemExposedComponent> SystemExposedFolder::lookup(StringView name)
{
    for (auto& component : m_components) {
        if (component.name() == name) {
            return component;
        }
    }
    return {};
}

SystemExposedFolder::SystemExposedFolder(String name)
    : SystemExposedComponent(name)
{
}

SystemExposedFolder::SystemExposedFolder(String name, const SystemExposedFolder& parent_folder)
    : SystemExposedComponent(name)
    , m_parent_folder(parent_folder)
{
}

NonnullRefPtr<Inode> SystemExposedFolder::to_inode(const SysFS& sysfs_instance) const
{
    return SysFSDirectoryInode::create(sysfs_instance, *this);
}

NonnullRefPtr<Inode> SystemExposedComponent::to_inode(const SysFS& sysfs_instance) const
{
    return SysFSInode::create(sysfs_instance, *this);
}

}