Custody.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/RefPtr.h>
  7. #include <AK/Singleton.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/StringView.h>
  10. #include <AK/Vector.h>
  11. #include <Kernel/FileSystem/Custody.h>
  12. #include <Kernel/FileSystem/Inode.h>
  13. namespace Kernel {
  14. static Singleton<SpinlockProtected<Custody::AllCustodiesList, LockRank::None>> s_all_instances;
  15. SpinlockProtected<Custody::AllCustodiesList, LockRank::None>& Custody::all_instances()
  16. {
  17. return s_all_instances;
  18. }
  19. ErrorOr<NonnullRefPtr<Custody>> Custody::try_create(Custody* parent, StringView name, Inode& inode, int mount_flags)
  20. {
  21. return all_instances().with([&](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. Custody::Custody(Custody* parent, NonnullOwnPtr<KString> name, Inode& inode, int mount_flags)
  37. : m_parent(parent)
  38. , m_name(move(name))
  39. , m_inode(inode)
  40. , m_mount_flags(mount_flags)
  41. {
  42. }
  43. Custody::~Custody() = default;
  44. ErrorOr<NonnullOwnPtr<KString>> Custody::try_serialize_absolute_path() const
  45. {
  46. if (!parent())
  47. return KString::try_create("/"sv);
  48. Vector<Custody const*, 32> custody_chain;
  49. size_t path_length = 0;
  50. for (auto const* custody = this; custody; custody = custody->parent()) {
  51. TRY(custody_chain.try_append(custody));
  52. path_length += custody->m_name->length() + 1;
  53. }
  54. VERIFY(path_length > 0);
  55. char* buffer;
  56. auto string = TRY(KString::try_create_uninitialized(path_length - 1, buffer));
  57. size_t string_index = 0;
  58. for (size_t custody_index = custody_chain.size() - 1; custody_index > 0; --custody_index) {
  59. buffer[string_index] = '/';
  60. ++string_index;
  61. auto const& custody_name = *custody_chain[custody_index - 1]->m_name;
  62. __builtin_memcpy(buffer + string_index, custody_name.characters(), custody_name.length());
  63. string_index += custody_name.length();
  64. }
  65. VERIFY(string->length() == string_index);
  66. buffer[string_index] = 0;
  67. return string;
  68. }
  69. bool Custody::is_readonly() const
  70. {
  71. if (m_mount_flags & MS_RDONLY)
  72. return true;
  73. return m_inode->fs().is_readonly();
  74. }
  75. }