Custody.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 String& 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 String& name, Inode& inode)
  27. {
  28. if (RefPtr<Custody> cached_custody = get_if_cached(parent, name)) {
  29. if (&cached_custody->inode() != &inode) {
  30. dbgprintf("WTF! cached custody for name '%s' has inode=%s, new inode=%s\n",
  31. name.characters(),
  32. cached_custody->inode().identifier().to_string().characters(),
  33. inode.identifier().to_string().characters());
  34. }
  35. ASSERT(&cached_custody->inode() == &inode);
  36. return *cached_custody;
  37. }
  38. return create(parent, name, inode);
  39. }
  40. Custody::Custody(Custody* parent, const String& name, Inode& inode)
  41. : m_parent(parent)
  42. , m_name(name)
  43. , m_inode(inode)
  44. {
  45. LOCKER(all_custodies().lock());
  46. all_custodies().resource().append(this);
  47. }
  48. Custody::~Custody()
  49. {
  50. LOCKER(all_custodies().lock());
  51. all_custodies().resource().remove(this);
  52. }
  53. String Custody::absolute_path() const
  54. {
  55. Vector<const Custody*, 32> custody_chain;
  56. for (auto* custody = this; custody; custody = custody->parent())
  57. custody_chain.append(custody);
  58. StringBuilder builder;
  59. for (int i = custody_chain.size() - 2; i >= 0; --i) {
  60. builder.append('/');
  61. builder.append(custody_chain[i]->name().characters());
  62. }
  63. return builder.to_string();
  64. }
  65. void Custody::did_delete(Badge<VFS>)
  66. {
  67. m_deleted = true;
  68. }
  69. void Custody::did_mount_on(Badge<VFS>)
  70. {
  71. m_mounted_on = true;
  72. }
  73. void Custody::did_rename(Badge<VFS>, const String& name)
  74. {
  75. m_name = name;
  76. }