Custody.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <AK/HashTable.h>
  2. #include <AK/StringBuilder.h>
  3. #include <Kernel/FileSystem/Custody.h>
  4. #include <Kernel/FileSystem/Inode.h>
  5. #include <Kernel/Lock.h>
  6. static Lockable<InlineLinkedList<Custody>>& all_custodies()
  7. {
  8. static Lockable<InlineLinkedList<Custody>>* list;
  9. if (!list)
  10. list = new Lockable<InlineLinkedList<Custody>>;
  11. return *list;
  12. }
  13. Custody* Custody::get_if_cached(Custody* parent, const StringView& name)
  14. {
  15. LOCKER(all_custodies().lock());
  16. for (auto& custody : all_custodies().resource()) {
  17. if (custody.is_deleted())
  18. continue;
  19. if (custody.is_mounted_on())
  20. continue;
  21. if (custody.parent() == parent && custody.name() == name)
  22. return &custody;
  23. }
  24. return nullptr;
  25. }
  26. NonnullRefPtr<Custody> Custody::get_or_create(Custody* parent, const StringView& name, Inode& inode)
  27. {
  28. if (RefPtr<Custody> cached_custody = get_if_cached(parent, name)) {
  29. if (&cached_custody->inode() != &inode) {
  30. dbg() << "WTF! Cached custody for name '" << name << "' has inode=" << cached_custody->inode().identifier() << ", new inode=" << inode.identifier();
  31. }
  32. ASSERT(&cached_custody->inode() == &inode);
  33. return *cached_custody;
  34. }
  35. return create(parent, name, inode);
  36. }
  37. Custody::Custody(Custody* parent, const StringView& name, Inode& inode)
  38. : m_parent(parent)
  39. , m_name(name)
  40. , m_inode(inode)
  41. {
  42. LOCKER(all_custodies().lock());
  43. all_custodies().resource().append(this);
  44. }
  45. Custody::~Custody()
  46. {
  47. LOCKER(all_custodies().lock());
  48. all_custodies().resource().remove(this);
  49. }
  50. String Custody::absolute_path() const
  51. {
  52. Vector<const Custody*, 32> custody_chain;
  53. for (auto* custody = this; custody; custody = custody->parent())
  54. custody_chain.append(custody);
  55. StringBuilder builder;
  56. for (int i = custody_chain.size() - 2; i >= 0; --i) {
  57. builder.append('/');
  58. builder.append(custody_chain[i]->name().characters());
  59. }
  60. return builder.to_string();
  61. }
  62. void Custody::did_delete(Badge<VFS>)
  63. {
  64. m_deleted = true;
  65. }
  66. void Custody::did_mount_on(Badge<VFS>)
  67. {
  68. m_mounted_on = true;
  69. }
  70. void Custody::did_rename(Badge<VFS>, const String& name)
  71. {
  72. m_name = name;
  73. }