Component.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. auto pointed_component_base_name = MUST(KString::try_create(m_pointed_component->name()));
  65. auto pointed_component_relative_path = MUST(m_pointed_component->relative_path(move(pointed_component_base_name), 0));
  66. auto full_return_and_target_path = TRY(KString::formatted("{}{}", return_path_to_mount_point->view(), pointed_component_relative_path->view()));
  67. return KBuffer::try_create_with_bytes("SysFSSymbolicLink"sv, full_return_and_target_path->view().bytes());
  68. }
  69. static ErrorOr<NonnullOwnPtr<KString>> generate_return_path_to_mount_point(NonnullOwnPtr<KString> current_path, size_t remaining_hop)
  70. {
  71. if (remaining_hop == 0)
  72. return current_path;
  73. auto new_path = TRY(KString::formatted("../{}"sv, current_path->view()));
  74. remaining_hop--;
  75. return generate_return_path_to_mount_point(move(new_path), remaining_hop);
  76. }
  77. ErrorOr<NonnullOwnPtr<KString>> SysFSSymbolicLink::try_generate_return_path_to_mount_point() const
  78. {
  79. VERIFY(m_parent_directory);
  80. auto hops_from_mountpoint = TRY(m_parent_directory->relative_path_hops_count_from_mountpoint());
  81. if (hops_from_mountpoint == 0)
  82. return KString::try_create("./"sv);
  83. auto start_return_path = TRY(KString::try_create("./"sv));
  84. return generate_return_path_to_mount_point(move(start_return_path), hops_from_mountpoint);
  85. }
  86. SysFSSymbolicLink::SysFSSymbolicLink(SysFSDirectory const& parent_directory, SysFSComponent const& pointed_component)
  87. : SysFSComponent(parent_directory)
  88. , m_pointed_component(pointed_component)
  89. {
  90. }
  91. ErrorOr<void> SysFSDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
  92. {
  93. TRY(callback({ "."sv, { fsid, component_index() }, 0 }));
  94. if (is_root_directory()) {
  95. TRY(callback({ ".."sv, { fsid, component_index() }, 0 }));
  96. } else {
  97. VERIFY(m_parent_directory);
  98. TRY(callback({ ".."sv, { fsid, m_parent_directory->component_index() }, 0 }));
  99. }
  100. return m_child_components.with([&](auto& list) -> ErrorOr<void> {
  101. for (auto& child_component : list) {
  102. InodeIdentifier identifier = { fsid, child_component.component_index() };
  103. TRY(callback({ child_component.name(), identifier, 0 }));
  104. }
  105. return {};
  106. });
  107. }
  108. RefPtr<SysFSComponent> SysFSDirectory::lookup(StringView name)
  109. {
  110. return m_child_components.with([&](auto& list) -> RefPtr<SysFSComponent> {
  111. for (auto& child_component : list) {
  112. if (child_component.name() == name) {
  113. return child_component;
  114. }
  115. }
  116. return nullptr;
  117. });
  118. }
  119. SysFSDirectory::SysFSDirectory(SysFSDirectory const& parent_directory)
  120. : SysFSComponent(parent_directory)
  121. {
  122. }
  123. ErrorOr<NonnullRefPtr<SysFSInode>> SysFSDirectory::to_inode(SysFS const& sysfs_instance) const
  124. {
  125. return TRY(SysFSDirectoryInode::try_create(sysfs_instance, *this));
  126. }
  127. ErrorOr<NonnullRefPtr<SysFSInode>> SysFSSymbolicLink::to_inode(SysFS const& sysfs_instance) const
  128. {
  129. return TRY(SysFSLinkInode::try_create(sysfs_instance, *this));
  130. }
  131. ErrorOr<NonnullRefPtr<SysFSInode>> SysFSComponent::to_inode(SysFS const& sysfs_instance) const
  132. {
  133. return SysFSInode::try_create(sysfs_instance, *this);
  134. }
  135. }