Component.cpp 2.0 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/SysFS/Component.h>
  8. #include <Kernel/FileSystem/SysFS/Registry.h>
  9. namespace Kernel {
  10. static Spinlock s_index_lock;
  11. static InodeIndex s_next_inode_index { 0 };
  12. static size_t allocate_inode_index()
  13. {
  14. SpinlockLocker lock(s_index_lock);
  15. s_next_inode_index = s_next_inode_index.value() + 1;
  16. VERIFY(s_next_inode_index > 0);
  17. return s_next_inode_index.value();
  18. }
  19. SysFSComponent::SysFSComponent()
  20. : m_component_index(allocate_inode_index())
  21. {
  22. }
  23. mode_t SysFSComponent::permissions() const
  24. {
  25. return S_IRUSR | S_IRGRP | S_IROTH;
  26. }
  27. ErrorOr<void> SysFSDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
  28. {
  29. MutexLocker locker(SysFSComponentRegistry::the().get_lock());
  30. VERIFY(m_parent_directory);
  31. TRY(callback({ ".", { fsid, component_index() }, 0 }));
  32. TRY(callback({ "..", { fsid, m_parent_directory->component_index() }, 0 }));
  33. for (auto& component : m_components) {
  34. InodeIdentifier identifier = { fsid, component.component_index() };
  35. TRY(callback({ component.name(), identifier, 0 }));
  36. }
  37. return {};
  38. }
  39. RefPtr<SysFSComponent> SysFSDirectory::lookup(StringView name)
  40. {
  41. for (auto& component : m_components) {
  42. if (component.name() == name) {
  43. return component;
  44. }
  45. }
  46. return {};
  47. }
  48. SysFSDirectory::SysFSDirectory(SysFSDirectory const& parent_directory)
  49. : SysFSComponent()
  50. , m_parent_directory(parent_directory)
  51. {
  52. }
  53. ErrorOr<NonnullRefPtr<SysFSInode>> SysFSDirectory::to_inode(SysFS const& sysfs_instance) const
  54. {
  55. return TRY(SysFSDirectoryInode::try_create(sysfs_instance, *this));
  56. }
  57. ErrorOr<NonnullRefPtr<SysFSInode>> SysFSComponent::to_inode(SysFS const& sysfs_instance) const
  58. {
  59. return SysFSInode::try_create(sysfs_instance, *this);
  60. }
  61. }