Custody.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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::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 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. String Custody::absolute_path() const
  33. {
  34. if (!parent())
  35. return "/";
  36. Vector<const Custody*, 32> custody_chain;
  37. for (auto* custody = this; custody; custody = custody->parent())
  38. custody_chain.append(custody);
  39. StringBuilder builder;
  40. for (int i = custody_chain.size() - 2; i >= 0; --i) {
  41. builder.append('/');
  42. builder.append(custody_chain[i]->name());
  43. }
  44. return builder.to_string();
  45. }
  46. bool Custody::is_readonly() const
  47. {
  48. if (m_mount_flags & MS_RDONLY)
  49. return true;
  50. return m_inode->fs().is_readonly();
  51. }
  52. }