Component.cpp 5.4 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.h>
  7. #include <Kernel/FileSystem/SysFS/Component.h>
  8. #include <Kernel/FileSystem/SysFS/Registry.h>
  9. #include <Kernel/KLexicalPath.h>
  10. namespace Kernel {
  11. static Spinlock s_index_lock { LockRank::None };
  12. static InodeIndex s_next_inode_index { 0 };
  13. static size_t allocate_inode_index()
  14. {
  15. SpinlockLocker lock(s_index_lock);
  16. s_next_inode_index = s_next_inode_index.value() + 1;
  17. VERIFY(s_next_inode_index > 0);
  18. return s_next_inode_index.value();
  19. }
  20. SysFSComponent::SysFSComponent(SysFSDirectory const& parent_directory)
  21. : m_parent_directory(parent_directory)
  22. , m_component_index(allocate_inode_index())
  23. {
  24. }
  25. SysFSComponent::SysFSComponent()
  26. : m_component_index(allocate_inode_index())
  27. {
  28. }
  29. ErrorOr<NonnullOwnPtr<KString>> SysFSComponent::relative_path(NonnullOwnPtr<KString> name, size_t current_hop) const
  30. {
  31. if (current_hop >= 128)
  32. return Error::from_errno(ELOOP);
  33. if (!m_parent_directory)
  34. return name;
  35. auto joined_name = TRY(KLexicalPath::try_join(m_parent_directory->name(), name->view()));
  36. return m_parent_directory->relative_path(move(joined_name), current_hop + 1);
  37. }
  38. ErrorOr<size_t> SysFSComponent::relative_path_hops_count_from_mountpoint(size_t current_hop) const
  39. {
  40. if (current_hop >= 128)
  41. return Error::from_errno(ELOOP);
  42. if (!m_parent_directory)
  43. return current_hop;
  44. return m_parent_directory->relative_path_hops_count_from_mountpoint(current_hop + 1);
  45. }
  46. mode_t SysFSComponent::permissions() const
  47. {
  48. return S_IRUSR | S_IRGRP | S_IROTH;
  49. }
  50. ErrorOr<size_t> SysFSSymbolicLink::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
  51. {
  52. auto blob = TRY(try_to_generate_buffer());
  53. if ((size_t)offset >= blob->size())
  54. return 0;
  55. ssize_t nread = min(static_cast<off_t>(blob->size() - offset), static_cast<off_t>(count));
  56. TRY(buffer.write(blob->data() + offset, nread));
  57. return nread;
  58. }
  59. ErrorOr<NonnullOwnPtr<KBuffer>> SysFSSymbolicLink::try_to_generate_buffer() const
  60. {
  61. auto return_path_to_mount_point = TRY(try_generate_return_path_to_mount_point());
  62. if (!m_pointed_component)
  63. return Error::from_errno(EIO);
  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. }