Custody.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2018-2022, 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<SpinlockProtected<Custody::AllCustodiesList>> s_all_instances;
  14. SpinlockProtected<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([&](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() = default;
  43. ErrorOr<NonnullOwnPtr<KString>> Custody::try_serialize_absolute_path() const
  44. {
  45. if (!parent())
  46. return KString::try_create("/"sv);
  47. Vector<Custody const*, 32> custody_chain;
  48. size_t path_length = 0;
  49. for (auto const* custody = this; custody; custody = custody->parent()) {
  50. TRY(custody_chain.try_append(custody));
  51. path_length += custody->m_name->length() + 1;
  52. }
  53. VERIFY(path_length > 0);
  54. char* buffer;
  55. auto string = TRY(KString::try_create_uninitialized(path_length - 1, buffer));
  56. size_t string_index = 0;
  57. for (size_t custody_index = custody_chain.size() - 1; custody_index > 0; --custody_index) {
  58. buffer[string_index] = '/';
  59. ++string_index;
  60. auto const& custody_name = *custody_chain[custody_index - 1]->m_name;
  61. __builtin_memcpy(buffer + string_index, custody_name.characters(), custody_name.length());
  62. string_index += custody_name.length();
  63. }
  64. VERIFY(string->length() == string_index);
  65. buffer[string_index] = 0;
  66. return string;
  67. }
  68. bool Custody::is_readonly() const
  69. {
  70. if (m_mount_flags & MS_RDONLY)
  71. return true;
  72. return m_inode->fs().is_readonly();
  73. }
  74. }