dentry.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #include <list>
  3. #include <string>
  4. #include <types/hash_map.hpp>
  5. #include <types/path.hpp>
  6. #include <kernel/vfs/inode.hpp>
  7. namespace fs {
  8. struct dentry {
  9. public:
  10. using name_type = std::string;
  11. private:
  12. std::list<dentry>* children = nullptr;
  13. types::hash_map<name_type, dentry*>* idx_children = nullptr;
  14. public:
  15. dentry* parent;
  16. inode* ind;
  17. struct {
  18. uint32_t dir : 1; // whether the dentry is a directory.
  19. // if dir is 1, whether children contains valid data.
  20. // otherwise, ignored
  21. uint32_t present : 1;
  22. } flags;
  23. name_type name;
  24. explicit dentry(dentry* parent, inode* ind, name_type name);
  25. dentry(const dentry& val) = delete;
  26. constexpr dentry(dentry&& val)
  27. : children(std::exchange(val.children, nullptr))
  28. , idx_children(std::exchange(val.idx_children, nullptr))
  29. , parent(std::exchange(val.parent, nullptr))
  30. , ind(std::exchange(val.ind, nullptr))
  31. , flags { val.flags }
  32. , name(std::move(val.name))
  33. {
  34. if (children) {
  35. for (auto& item : *children)
  36. item.parent = this;
  37. }
  38. }
  39. dentry& operator=(const dentry& val) = delete;
  40. dentry& operator=(dentry&& val) = delete;
  41. constexpr ~dentry()
  42. {
  43. if (children) {
  44. delete children;
  45. children = nullptr;
  46. }
  47. if (idx_children) {
  48. delete idx_children;
  49. idx_children = nullptr;
  50. }
  51. }
  52. int load();
  53. dentry* append(inode* ind, name_type name);
  54. dentry* find(const name_type& name);
  55. dentry* replace(dentry* val);
  56. void remove(const name_type& name);
  57. // out_dst SHOULD be empty
  58. void path(const dentry& root, types::path& out_dst) const;
  59. };
  60. } // namespace fs