Custody.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Singleton.h>
  7. #include <AK/StringBuilder.h>
  8. #include <AK/StringView.h>
  9. #include <AK/Vector.h>
  10. #include <Kernel/FileSystem/Custody.h>
  11. #include <Kernel/FileSystem/Inode.h>
  12. #include <Kernel/Locking/MutexProtected.h>
  13. namespace Kernel {
  14. static Singleton<MutexProtected<Custody::AllCustodiesList>> s_all_custodies;
  15. static MutexProtected<Custody::AllCustodiesList>& all_custodies()
  16. {
  17. return s_all_custodies;
  18. }
  19. KResultOr<NonnullRefPtr<Custody>> Custody::try_create(Custody* parent, StringView name, Inode& inode, int mount_flags)
  20. {
  21. return all_custodies().with_exclusive([&](auto& all_custodies) -> KResultOr<NonnullRefPtr<Custody>> {
  22. for (Custody& custody : all_custodies) {
  23. if (custody.parent() == parent
  24. && custody.name() == name
  25. && &custody.inode() == &inode
  26. && custody.mount_flags() == mount_flags) {
  27. return custody;
  28. }
  29. }
  30. auto name_kstring = KString::try_create(name);
  31. if (!name_kstring)
  32. return ENOMEM;
  33. auto custody = adopt_ref_if_nonnull(new (nothrow) Custody(parent, name_kstring.release_nonnull(), inode, mount_flags));
  34. if (!custody)
  35. return ENOMEM;
  36. all_custodies.prepend(*custody);
  37. return custody.release_nonnull();
  38. });
  39. }
  40. bool Custody::unref() const
  41. {
  42. bool should_destroy = all_custodies().with_exclusive([&](auto&) {
  43. if (deref_base())
  44. return false;
  45. m_all_custodies_list_node.remove();
  46. return true;
  47. });
  48. if (should_destroy)
  49. delete this;
  50. return should_destroy;
  51. }
  52. Custody::Custody(Custody* parent, NonnullOwnPtr<KString> name, Inode& inode, int mount_flags)
  53. : m_parent(parent)
  54. , m_name(move(name))
  55. , m_inode(inode)
  56. , m_mount_flags(mount_flags)
  57. {
  58. }
  59. Custody::~Custody()
  60. {
  61. }
  62. KResultOr<NonnullOwnPtr<KString>> Custody::try_serialize_absolute_path() const
  63. {
  64. if (!parent()) {
  65. auto string = KString::try_create("/"sv);
  66. if (!string)
  67. return ENOMEM;
  68. return string.release_nonnull();
  69. }
  70. Vector<Custody const*, 32> custody_chain;
  71. size_t path_length = 0;
  72. for (auto* custody = this; custody; custody = custody->parent()) {
  73. custody_chain.append(custody);
  74. path_length += custody->m_name->length() + 1;
  75. }
  76. VERIFY(path_length > 0);
  77. char* buffer;
  78. auto string = KString::try_create_uninitialized(path_length - 1, buffer);
  79. if (!string)
  80. return ENOMEM;
  81. size_t string_index = 0;
  82. for (size_t custody_index = custody_chain.size() - 1; custody_index > 0; --custody_index) {
  83. buffer[string_index] = '/';
  84. ++string_index;
  85. auto& custody_name = *custody_chain[custody_index - 1]->m_name;
  86. __builtin_memcpy(buffer + string_index, custody_name.characters(), custody_name.length());
  87. string_index += custody_name.length();
  88. }
  89. VERIFY(string->length() == string_index);
  90. buffer[string_index] = 0;
  91. return string.release_nonnull();
  92. }
  93. String Custody::absolute_path() const
  94. {
  95. if (!parent())
  96. return "/";
  97. Vector<Custody const*, 32> custody_chain;
  98. for (auto* custody = this; custody; custody = custody->parent())
  99. custody_chain.append(custody);
  100. StringBuilder builder;
  101. for (int i = custody_chain.size() - 2; i >= 0; --i) {
  102. builder.append('/');
  103. builder.append(custody_chain[i]->name());
  104. }
  105. return builder.to_string();
  106. }
  107. bool Custody::is_readonly() const
  108. {
  109. if (m_mount_flags & MS_RDONLY)
  110. return true;
  111. return m_inode->fs().is_readonly();
  112. }
  113. }