Custody.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. ErrorOr<NonnullRefPtr<Custody>> Custody::try_create(Custody* parent, StringView name, Inode& inode, int mount_flags)
  20. {
  21. return all_custodies().with_exclusive([&](auto& all_custodies) -> ErrorOr<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 NonnullRefPtr { custody };
  28. }
  29. }
  30. auto name_kstring = TRY(KString::try_create(name));
  31. auto custody = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Custody(parent, move(name_kstring), inode, mount_flags)));
  32. all_custodies.prepend(*custody);
  33. return custody;
  34. });
  35. }
  36. bool Custody::unref() const
  37. {
  38. bool should_destroy = all_custodies().with_exclusive([&](auto&) {
  39. if (deref_base())
  40. return false;
  41. m_all_custodies_list_node.remove();
  42. return true;
  43. });
  44. if (should_destroy)
  45. delete this;
  46. return should_destroy;
  47. }
  48. Custody::Custody(Custody* parent, NonnullOwnPtr<KString> name, Inode& inode, int mount_flags)
  49. : m_parent(parent)
  50. , m_name(move(name))
  51. , m_inode(inode)
  52. , m_mount_flags(mount_flags)
  53. {
  54. }
  55. Custody::~Custody()
  56. {
  57. }
  58. ErrorOr<NonnullOwnPtr<KString>> Custody::try_serialize_absolute_path() const
  59. {
  60. if (!parent())
  61. return KString::try_create("/"sv);
  62. Vector<Custody const*, 32> custody_chain;
  63. size_t path_length = 0;
  64. for (auto const* custody = this; custody; custody = custody->parent()) {
  65. custody_chain.append(custody);
  66. path_length += custody->m_name->length() + 1;
  67. }
  68. VERIFY(path_length > 0);
  69. char* buffer;
  70. auto string = TRY(KString::try_create_uninitialized(path_length - 1, buffer));
  71. size_t string_index = 0;
  72. for (size_t custody_index = custody_chain.size() - 1; custody_index > 0; --custody_index) {
  73. buffer[string_index] = '/';
  74. ++string_index;
  75. auto const& custody_name = *custody_chain[custody_index - 1]->m_name;
  76. __builtin_memcpy(buffer + string_index, custody_name.characters(), custody_name.length());
  77. string_index += custody_name.length();
  78. }
  79. VERIFY(string->length() == string_index);
  80. buffer[string_index] = 0;
  81. return string;
  82. }
  83. String Custody::absolute_path() const
  84. {
  85. if (!parent())
  86. return "/";
  87. Vector<Custody const*, 32> custody_chain;
  88. for (auto const* custody = this; custody; custody = custody->parent())
  89. custody_chain.append(custody);
  90. StringBuilder builder;
  91. for (int i = custody_chain.size() - 2; i >= 0; --i) {
  92. builder.append('/');
  93. builder.append(custody_chain[i]->name());
  94. }
  95. return builder.to_string();
  96. }
  97. bool Custody::is_readonly() const
  98. {
  99. if (m_mount_flags & MS_RDONLY)
  100. return true;
  101. return m_inode->fs().is_readonly();
  102. }
  103. }