SysFSComponent.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/FileSystem/SysFS.h>
  7. #include <Kernel/FileSystem/SysFSComponent.h>
  8. namespace Kernel {
  9. static SpinLock<u8> s_index_lock;
  10. static InodeIndex s_next_inode_index { 0 };
  11. static size_t allocate_inode_index()
  12. {
  13. ScopedSpinLock lock(s_index_lock);
  14. s_next_inode_index = s_next_inode_index.value() + 1;
  15. VERIFY(s_next_inode_index > 0);
  16. return s_next_inode_index.value();
  17. }
  18. SysFSComponent::SysFSComponent(StringView name)
  19. : m_name(KString::try_create(name).release_nonnull())
  20. , m_component_index(allocate_inode_index())
  21. {
  22. }
  23. KResult SysFSDirectory::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
  24. {
  25. Locker locker(SysFSComponentRegistry::the().get_lock());
  26. VERIFY(m_parent_directory);
  27. callback({ ".", { fsid, component_index() }, 0 });
  28. callback({ "..", { fsid, m_parent_directory->component_index() }, 0 });
  29. for (auto& component : m_components) {
  30. InodeIdentifier identifier = { fsid, component.component_index() };
  31. callback({ component.name(), identifier, 0 });
  32. }
  33. return KSuccess;
  34. }
  35. RefPtr<SysFSComponent> SysFSDirectory::lookup(StringView name)
  36. {
  37. for (auto& component : m_components) {
  38. if (component.name() == name) {
  39. return component;
  40. }
  41. }
  42. return {};
  43. }
  44. SysFSDirectory::SysFSDirectory(StringView name)
  45. : SysFSComponent(name)
  46. {
  47. }
  48. SysFSDirectory::SysFSDirectory(StringView name, SysFSDirectory const& parent_directory)
  49. : SysFSComponent(name)
  50. , m_parent_directory(parent_directory)
  51. {
  52. }
  53. NonnullRefPtr<Inode> SysFSDirectory::to_inode(SysFS const& sysfs_instance) const
  54. {
  55. return SysFSDirectoryInode::create(sysfs_instance, *this);
  56. }
  57. NonnullRefPtr<Inode> SysFSComponent::to_inode(SysFS const& sysfs_instance) const
  58. {
  59. return SysFSInode::create(sysfs_instance, *this);
  60. }
  61. }