Component.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/Component.h>
  7. #include <Kernel/FileSystem/SysFS/DirectoryInode.h>
  8. #include <Kernel/FileSystem/SysFS/Inode.h>
  9. #include <Kernel/FileSystem/SysFS/LinkInode.h>
  10. #include <Kernel/FileSystem/SysFS/Registry.h>
  11. #include <Kernel/KLexicalPath.h>
  12. namespace Kernel {
  13. static Spinlock<LockRank::None> s_index_lock {};
  14. static InodeIndex s_next_inode_index { 0 };
  15. static size_t allocate_inode_index()
  16. {
  17. SpinlockLocker lock(s_index_lock);
  18. s_next_inode_index = s_next_inode_index.value() + 1;
  19. VERIFY(s_next_inode_index > 0);
  20. return s_next_inode_index.value();
  21. }
  22. SysFSComponent::SysFSComponent(SysFSDirectory const& parent_directory)
  23. : m_parent_directory(parent_directory)
  24. , m_component_index(allocate_inode_index())
  25. {
  26. }
  27. SysFSComponent::SysFSComponent()
  28. : m_component_index(allocate_inode_index())
  29. {
  30. }
  31. ErrorOr<NonnullOwnPtr<KString>> SysFSComponent::relative_path(NonnullOwnPtr<KString> name, size_t current_hop) const
  32. {
  33. if (current_hop >= 128)
  34. return Error::from_errno(ELOOP);
  35. if (!m_parent_directory)
  36. return name;
  37. auto joined_name = TRY(KLexicalPath::try_join(m_parent_directory->name(), name->view()));
  38. return m_parent_directory->relative_path(move(joined_name), current_hop + 1);
  39. }
  40. ErrorOr<size_t> SysFSComponent::relative_path_hops_count_from_mountpoint(size_t current_hop) const
  41. {
  42. if (current_hop >= 128)
  43. return Error::from_errno(ELOOP);
  44. if (!m_parent_directory)
  45. return current_hop;
  46. return m_parent_directory->relative_path_hops_count_from_mountpoint(current_hop + 1);
  47. }
  48. mode_t SysFSComponent::permissions() const
  49. {
  50. return S_IRUSR | S_IRGRP | S_IROTH;
  51. }
  52. ErrorOr<size_t> SysFSSymbolicLink::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
  53. {
  54. auto blob = TRY(try_to_generate_buffer());
  55. if ((size_t)offset >= blob->size())
  56. return 0;
  57. ssize_t nread = min(static_cast<off_t>(blob->size() - offset), static_cast<off_t>(count));
  58. TRY(buffer.write(blob->data() + offset, nread));
  59. return nread;
  60. }
  61. ErrorOr<NonnullOwnPtr<KBuffer>> SysFSSymbolicLink::try_to_generate_buffer() const
  62. {
  63. auto return_path_to_mount_point = TRY(try_generate_return_path_to_mount_point());
  64. if (!m_pointed_component)
  65. return Error::from_errno(EIO);
  66. auto pointed_component_base_name = MUST(KString::try_create(m_pointed_component->name()));
  67. auto pointed_component_relative_path = MUST(m_pointed_component->relative_path(move(pointed_component_base_name), 0));
  68. auto full_return_and_target_path = TRY(KString::formatted("{}{}", return_path_to_mount_point->view(), pointed_component_relative_path->view()));
  69. return KBuffer::try_create_with_bytes("SysFSSymbolicLink"sv, full_return_and_target_path->view().bytes());
  70. }
  71. static ErrorOr<NonnullOwnPtr<KString>> generate_return_path_to_mount_point(NonnullOwnPtr<KString> current_path, size_t remaining_hop)
  72. {
  73. if (remaining_hop == 0)
  74. return current_path;
  75. auto new_path = TRY(KString::formatted("../{}"sv, current_path->view()));
  76. remaining_hop--;
  77. return generate_return_path_to_mount_point(move(new_path), remaining_hop);
  78. }
  79. ErrorOr<NonnullOwnPtr<KString>> SysFSSymbolicLink::try_generate_return_path_to_mount_point() const
  80. {
  81. VERIFY(m_parent_directory);
  82. auto hops_from_mountpoint = TRY(m_parent_directory->relative_path_hops_count_from_mountpoint());
  83. if (hops_from_mountpoint == 0)
  84. return KString::try_create("./"sv);
  85. auto start_return_path = TRY(KString::try_create("./"sv));
  86. return generate_return_path_to_mount_point(move(start_return_path), hops_from_mountpoint);
  87. }
  88. SysFSSymbolicLink::SysFSSymbolicLink(SysFSDirectory const& parent_directory, SysFSComponent const& pointed_component)
  89. : SysFSComponent(parent_directory)
  90. , m_pointed_component(pointed_component)
  91. {
  92. }
  93. ErrorOr<void> SysFSDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
  94. {
  95. TRY(callback({ "."sv, { fsid, component_index() }, 0 }));
  96. if (is_root_directory()) {
  97. TRY(callback({ ".."sv, { fsid, component_index() }, 0 }));
  98. } else {
  99. VERIFY(m_parent_directory);
  100. TRY(callback({ ".."sv, { fsid, m_parent_directory->component_index() }, 0 }));
  101. }
  102. return m_child_components.with([&](auto& list) -> ErrorOr<void> {
  103. for (auto& child_component : list) {
  104. InodeIdentifier identifier = { fsid, child_component.component_index() };
  105. TRY(callback({ child_component.name(), identifier, 0 }));
  106. }
  107. return {};
  108. });
  109. }
  110. LockRefPtr<SysFSComponent> SysFSDirectory::lookup(StringView name)
  111. {
  112. return m_child_components.with([&](auto& list) -> LockRefPtr<SysFSComponent> {
  113. for (auto& child_component : list) {
  114. if (child_component.name() == name) {
  115. return child_component;
  116. }
  117. }
  118. return nullptr;
  119. });
  120. }
  121. SysFSDirectory::SysFSDirectory(SysFSDirectory const& parent_directory)
  122. : SysFSComponent(parent_directory)
  123. {
  124. }
  125. ErrorOr<NonnullLockRefPtr<SysFSInode>> SysFSDirectory::to_inode(SysFS const& sysfs_instance) const
  126. {
  127. return TRY(SysFSDirectoryInode::try_create(sysfs_instance, *this));
  128. }
  129. ErrorOr<NonnullLockRefPtr<SysFSInode>> SysFSSymbolicLink::to_inode(SysFS const& sysfs_instance) const
  130. {
  131. return TRY(SysFSLinkInode::try_create(sysfs_instance, *this));
  132. }
  133. ErrorOr<NonnullLockRefPtr<SysFSInode>> SysFSComponent::to_inode(SysFS const& sysfs_instance) const
  134. {
  135. return SysFSInode::try_create(sysfs_instance, *this);
  136. }
  137. }