VirtualFileSystem.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #include <AK/AKString.h>
  3. #include <AK/Badge.h>
  4. #include <AK/Function.h>
  5. #include <AK/HashMap.h>
  6. #include <AK/NonnullOwnPtrVector.h>
  7. #include <AK/OwnPtr.h>
  8. #include <AK/RefPtr.h>
  9. #include <Kernel/FileSystem/FileSystem.h>
  10. #include <Kernel/FileSystem/InodeIdentifier.h>
  11. #include <Kernel/FileSystem/InodeMetadata.h>
  12. #include <Kernel/KResult.h>
  13. #define O_RDONLY 0
  14. #define O_WRONLY 1
  15. #define O_RDWR 2
  16. #define O_CREAT 0100
  17. #define O_EXCL 0200
  18. #define O_NOCTTY 0400
  19. #define O_TRUNC 01000
  20. #define O_APPEND 02000
  21. #define O_NONBLOCK 04000
  22. #define O_DIRECTORY 00200000
  23. #define O_NOFOLLOW 00400000
  24. #define O_CLOEXEC 02000000
  25. #define O_NOFOLLOW_NOERROR 0x4000000
  26. class Custody;
  27. class Device;
  28. class FileDescription;
  29. class VFS {
  30. AK_MAKE_ETERNAL
  31. public:
  32. class Mount {
  33. public:
  34. Mount(RefPtr<Custody>&&, NonnullRefPtr<FS>&&);
  35. InodeIdentifier host() const;
  36. InodeIdentifier guest() const { return m_guest; }
  37. const FS& guest_fs() const { return *m_guest_fs; }
  38. String absolute_path() const;
  39. private:
  40. InodeIdentifier m_host;
  41. InodeIdentifier m_guest;
  42. NonnullRefPtr<FS> m_guest_fs;
  43. RefPtr<Custody> m_host_custody;
  44. };
  45. static VFS& the();
  46. VFS();
  47. ~VFS();
  48. bool mount_root(NonnullRefPtr<FS>&&);
  49. KResult mount(NonnullRefPtr<FS>&&, StringView path);
  50. KResult mount(NonnullRefPtr<FS>&&, Custody& mount_point);
  51. KResult unmount(InodeIdentifier guest_inode_id);
  52. KResultOr<NonnullRefPtr<FileDescription>> open(StringView path, int options, mode_t mode, Custody& base);
  53. KResultOr<NonnullRefPtr<FileDescription>> create(StringView path, int options, mode_t mode, Custody& parent_custody);
  54. KResult mkdir(StringView path, mode_t mode, Custody& base);
  55. KResult link(StringView old_path, StringView new_path, Custody& base);
  56. KResult unlink(StringView path, Custody& base);
  57. KResult symlink(StringView target, StringView linkpath, Custody& base);
  58. KResult rmdir(StringView path, Custody& base);
  59. KResult chmod(StringView path, mode_t, Custody& base);
  60. KResult chmod(Inode&, mode_t);
  61. KResult chown(StringView path, uid_t, gid_t, Custody& base);
  62. KResult chown(Inode&, uid_t, gid_t);
  63. KResult access(StringView path, int mode, Custody& base);
  64. KResultOr<InodeMetadata> lookup_metadata(StringView path, Custody& base, int options = 0);
  65. KResult utime(StringView path, Custody& base, time_t atime, time_t mtime);
  66. KResult rename(StringView oldpath, StringView newpath, Custody& base);
  67. KResult mknod(StringView path, mode_t, dev_t, Custody& base);
  68. KResultOr<NonnullRefPtr<Custody>> open_directory(StringView path, Custody& base);
  69. size_t mount_count() const { return m_mounts.size(); }
  70. void for_each_mount(Function<void(const Mount&)>) const;
  71. InodeIdentifier root_inode_id() const;
  72. void sync();
  73. Custody& root_custody();
  74. KResultOr<NonnullRefPtr<Custody>> resolve_path(StringView path, Custody& base, RefPtr<Custody>* parent = nullptr, int options = 0);
  75. private:
  76. friend class FileDescription;
  77. RefPtr<Inode> get_inode(InodeIdentifier);
  78. bool is_vfs_root(InodeIdentifier) const;
  79. void traverse_directory_inode(Inode&, Function<bool(const FS::DirectoryEntry&)>);
  80. Mount* find_mount_for_host(InodeIdentifier);
  81. Mount* find_mount_for_guest(InodeIdentifier);
  82. Lock m_lock { "VFSLock" };
  83. RefPtr<Inode> m_root_inode;
  84. NonnullOwnPtrVector<Mount> m_mounts;
  85. RefPtr<Custody> m_root_custody;
  86. };