SysFSComponent.cpp 1.9 KB

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