Custody.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. namespace Kernel {
  13. static Singleton<MutexProtected<Custody::AllCustodiesList>> s_all_instances;
  14. MutexProtected<Custody::AllCustodiesList>& Custody::all_instances()
  15. {
  16. return s_all_instances;
  17. }
  18. ErrorOr<NonnullRefPtr<Custody>> Custody::try_create(Custody* parent, StringView name, Inode& inode, int mount_flags)
  19. {
  20. return all_instances().with_exclusive([&](auto& all_custodies) -> ErrorOr<NonnullRefPtr<Custody>> {
  21. for (Custody& custody : all_custodies) {
  22. if (custody.parent() == parent
  23. && custody.name() == name
  24. && &custody.inode() == &inode
  25. && custody.mount_flags() == mount_flags) {
  26. return NonnullRefPtr { custody };
  27. }
  28. }
  29. auto name_kstring = TRY(KString::try_create(name));
  30. auto custody = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Custody(parent, move(name_kstring), inode, mount_flags)));
  31. all_custodies.prepend(*custody);
  32. return custody;
  33. });
  34. }
  35. Custody::Custody(Custody* parent, NonnullOwnPtr<KString> name, Inode& inode, int mount_flags)
  36. : m_parent(parent)
  37. , m_name(move(name))
  38. , m_inode(inode)
  39. , m_mount_flags(mount_flags)
  40. {
  41. }
  42. Custody::~Custody()
  43. {
  44. }
  45. ErrorOr<NonnullOwnPtr<KString>> Custody::try_serialize_absolute_path() const
  46. {
  47. if (!parent())
  48. return KString::try_create("/"sv);
  49. Vector<Custody const*, 32> custody_chain;
  50. size_t path_length = 0;
  51. for (auto const* custody = this; custody; custody = custody->parent()) {
  52. custody_chain.append(custody);
  53. path_length += custody->m_name->length() + 1;
  54. }
  55. VERIFY(path_length > 0);
  56. char* buffer;
  57. auto string = TRY(KString::try_create_uninitialized(path_length - 1, buffer));
  58. size_t string_index = 0;
  59. for (size_t custody_index = custody_chain.size() - 1; custody_index > 0; --custody_index) {
  60. buffer[string_index] = '/';
  61. ++string_index;
  62. auto const& custody_name = *custody_chain[custody_index - 1]->m_name;
  63. __builtin_memcpy(buffer + string_index, custody_name.characters(), custody_name.length());
  64. string_index += custody_name.length();
  65. }
  66. VERIFY(string->length() == string_index);
  67. buffer[string_index] = 0;
  68. return string;
  69. }
  70. String Custody::absolute_path() const
  71. {
  72. if (!parent())
  73. return "/";
  74. Vector<Custody const*, 32> custody_chain;
  75. for (auto const* custody = this; custody; custody = custody->parent())
  76. custody_chain.append(custody);
  77. StringBuilder builder;
  78. for (int i = custody_chain.size() - 2; i >= 0; --i) {
  79. builder.append('/');
  80. builder.append(custody_chain[i]->name());
  81. }
  82. return builder.to_string();
  83. }
  84. bool Custody::is_readonly() const
  85. {
  86. if (m_mount_flags & MS_RDONLY)
  87. return true;
  88. return m_inode->fs().is_readonly();
  89. }
  90. }