Custody.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <AK/StringView.h>
  8. #include <AK/Vector.h>
  9. #include <Kernel/FileSystem/Custody.h>
  10. #include <Kernel/FileSystem/Inode.h>
  11. namespace Kernel {
  12. KResultOr<NonnullRefPtr<Custody>> Custody::try_create(Custody* parent, StringView name, Inode& inode, int mount_flags)
  13. {
  14. auto name_kstring = KString::try_create(name);
  15. if (!name_kstring)
  16. return ENOMEM;
  17. auto custody = adopt_ref_if_nonnull(new (nothrow) Custody(parent, name_kstring.release_nonnull(), inode, mount_flags));
  18. if (!custody)
  19. return ENOMEM;
  20. return custody.release_nonnull();
  21. }
  22. Custody::Custody(Custody* parent, NonnullOwnPtr<KString> name, Inode& inode, int mount_flags)
  23. : m_parent(parent)
  24. , m_name(move(name))
  25. , m_inode(inode)
  26. , m_mount_flags(mount_flags)
  27. {
  28. }
  29. Custody::~Custody()
  30. {
  31. }
  32. OwnPtr<KString> Custody::try_create_absolute_path() const
  33. {
  34. if (!parent())
  35. return KString::try_create("/"sv);
  36. Vector<Custody const*, 32> custody_chain;
  37. size_t path_length = 0;
  38. for (auto* custody = this; custody; custody = custody->parent()) {
  39. custody_chain.append(custody);
  40. path_length += custody->m_name->length() + 1;
  41. }
  42. VERIFY(path_length > 0);
  43. char* buffer;
  44. auto string = KString::try_create_uninitialized(path_length - 1, buffer);
  45. if (!string)
  46. return string;
  47. size_t string_index = 0;
  48. for (size_t custody_index = custody_chain.size() - 1; custody_index > 0; --custody_index) {
  49. buffer[string_index] = '/';
  50. ++string_index;
  51. auto& custody_name = *custody_chain[custody_index - 1]->m_name;
  52. __builtin_memcpy(buffer + string_index, custody_name.characters(), custody_name.length());
  53. string_index += custody_name.length();
  54. }
  55. VERIFY(string->length() == string_index);
  56. buffer[string_index] = 0;
  57. return string;
  58. }
  59. String Custody::absolute_path() const
  60. {
  61. if (!parent())
  62. return "/";
  63. Vector<Custody const*, 32> custody_chain;
  64. for (auto* custody = this; custody; custody = custody->parent())
  65. custody_chain.append(custody);
  66. StringBuilder builder;
  67. for (int i = custody_chain.size() - 2; i >= 0; --i) {
  68. builder.append('/');
  69. builder.append(custody_chain[i]->name());
  70. }
  71. return builder.to_string();
  72. }
  73. bool Custody::is_readonly() const
  74. {
  75. if (m_mount_flags & MS_RDONLY)
  76. return true;
  77. return m_inode->fs().is_readonly();
  78. }
  79. }