Custody.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. OwnPtr<KString> Custody::try_create_absolute_path() const
  63. {
  64. if (!parent())
  65. return KString::try_create("/"sv);
  66. Vector<Custody const*, 32> custody_chain;
  67. size_t path_length = 0;
  68. for (auto* custody = this; custody; custody = custody->parent()) {
  69. custody_chain.append(custody);
  70. path_length += custody->m_name->length() + 1;
  71. }
  72. VERIFY(path_length > 0);
  73. char* buffer;
  74. auto string = KString::try_create_uninitialized(path_length - 1, buffer);
  75. if (!string)
  76. return string;
  77. size_t string_index = 0;
  78. for (size_t custody_index = custody_chain.size() - 1; custody_index > 0; --custody_index) {
  79. buffer[string_index] = '/';
  80. ++string_index;
  81. auto& custody_name = *custody_chain[custody_index - 1]->m_name;
  82. __builtin_memcpy(buffer + string_index, custody_name.characters(), custody_name.length());
  83. string_index += custody_name.length();
  84. }
  85. VERIFY(string->length() == string_index);
  86. buffer[string_index] = 0;
  87. return string;
  88. }
  89. String Custody::absolute_path() const
  90. {
  91. if (!parent())
  92. return "/";
  93. Vector<Custody const*, 32> custody_chain;
  94. for (auto* custody = this; custody; custody = custody->parent())
  95. custody_chain.append(custody);
  96. StringBuilder builder;
  97. for (int i = custody_chain.size() - 2; i >= 0; --i) {
  98. builder.append('/');
  99. builder.append(custody_chain[i]->name());
  100. }
  101. return builder.to_string();
  102. }
  103. bool Custody::is_readonly() const
  104. {
  105. if (m_mount_flags & MS_RDONLY)
  106. return true;
  107. return m_inode->fs().is_readonly();
  108. }
  109. }