VirtualFileSystem.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #include <AK/String.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_DIRECT 04000000
  26. #define O_NOFOLLOW_NOERROR 0x4000000
  27. class Custody;
  28. class Device;
  29. class FileDescription;
  30. class VFS {
  31. AK_MAKE_ETERNAL
  32. public:
  33. class Mount {
  34. public:
  35. Mount(RefPtr<Custody>&&, NonnullRefPtr<FS>&&);
  36. InodeIdentifier host() const;
  37. InodeIdentifier guest() const { return m_guest; }
  38. const FS& guest_fs() const { return *m_guest_fs; }
  39. String absolute_path() const;
  40. private:
  41. InodeIdentifier m_host;
  42. InodeIdentifier m_guest;
  43. NonnullRefPtr<FS> m_guest_fs;
  44. RefPtr<Custody> m_host_custody;
  45. };
  46. static VFS& the();
  47. VFS();
  48. ~VFS();
  49. bool mount_root(NonnullRefPtr<FS>&&);
  50. KResult mount(NonnullRefPtr<FS>&&, StringView path);
  51. KResult mount(NonnullRefPtr<FS>&&, Custody& mount_point);
  52. KResult unmount(InodeIdentifier guest_inode_id);
  53. KResultOr<NonnullRefPtr<FileDescription>> open(StringView path, int options, mode_t mode, Custody& base);
  54. KResultOr<NonnullRefPtr<FileDescription>> create(StringView path, int options, mode_t mode, Custody& parent_custody);
  55. KResult mkdir(StringView path, mode_t mode, Custody& base);
  56. KResult link(StringView old_path, StringView new_path, Custody& base);
  57. KResult unlink(StringView path, Custody& base);
  58. KResult symlink(StringView target, StringView linkpath, Custody& base);
  59. KResult rmdir(StringView path, Custody& base);
  60. KResult chmod(StringView path, mode_t, Custody& base);
  61. KResult chmod(Inode&, mode_t);
  62. KResult chown(StringView path, uid_t, gid_t, Custody& base);
  63. KResult chown(Inode&, uid_t, gid_t);
  64. KResult access(StringView path, int mode, Custody& base);
  65. KResultOr<InodeMetadata> lookup_metadata(StringView path, Custody& base, int options = 0);
  66. KResult utime(StringView path, Custody& base, time_t atime, time_t mtime);
  67. KResult rename(StringView oldpath, StringView newpath, Custody& base);
  68. KResult mknod(StringView path, mode_t, dev_t, Custody& base);
  69. KResultOr<NonnullRefPtr<Custody>> open_directory(StringView path, Custody& base);
  70. size_t mount_count() const { return m_mounts.size(); }
  71. void for_each_mount(Function<void(const Mount&)>) const;
  72. InodeIdentifier root_inode_id() const;
  73. void sync();
  74. Custody& root_custody();
  75. KResultOr<NonnullRefPtr<Custody>> resolve_path(StringView path, Custody& base, RefPtr<Custody>* parent = nullptr, int options = 0, int symlink_recursion_level = 0);
  76. private:
  77. friend class FileDescription;
  78. RefPtr<Inode> get_inode(InodeIdentifier);
  79. bool is_vfs_root(InodeIdentifier) const;
  80. void traverse_directory_inode(Inode&, Function<bool(const FS::DirectoryEntry&)>);
  81. Mount* find_mount_for_host(InodeIdentifier);
  82. Mount* find_mount_for_guest(InodeIdentifier);
  83. Lock m_lock { "VFSLock" };
  84. RefPtr<Inode> m_root_inode;
  85. NonnullOwnPtrVector<Mount> m_mounts;
  86. RefPtr<Custody> m_root_custody;
  87. };