VirtualFileSystem.h 3.5 KB

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