vfs_real.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <map>
  5. #include <mutex>
  6. #include <optional>
  7. #include <string_view>
  8. #include "common/intrusive_list.h"
  9. #include "core/file_sys/fs_filesystem.h"
  10. #include "core/file_sys/vfs/vfs.h"
  11. namespace Common::FS {
  12. class IOFile;
  13. }
  14. namespace FileSys {
  15. struct FileReference : public Common::IntrusiveListBaseNode<FileReference> {
  16. std::shared_ptr<Common::FS::IOFile> file{};
  17. };
  18. class RealVfsFile;
  19. class RealVfsDirectory;
  20. class RealVfsFilesystem : public VfsFilesystem {
  21. public:
  22. RealVfsFilesystem();
  23. ~RealVfsFilesystem() override;
  24. std::string GetName() const override;
  25. bool IsReadable() const override;
  26. bool IsWritable() const override;
  27. VfsEntryType GetEntryType(std::string_view path) const override;
  28. VirtualFile OpenFile(std::string_view path, OpenMode perms = OpenMode::Read) override;
  29. VirtualFile CreateFile(std::string_view path, OpenMode perms = OpenMode::ReadWrite) override;
  30. VirtualFile CopyFile(std::string_view old_path, std::string_view new_path) override;
  31. VirtualFile MoveFile(std::string_view old_path, std::string_view new_path) override;
  32. bool DeleteFile(std::string_view path) override;
  33. VirtualDir OpenDirectory(std::string_view path, OpenMode perms = OpenMode::Read) override;
  34. VirtualDir CreateDirectory(std::string_view path,
  35. OpenMode perms = OpenMode::ReadWrite) override;
  36. VirtualDir CopyDirectory(std::string_view old_path, std::string_view new_path) override;
  37. VirtualDir MoveDirectory(std::string_view old_path, std::string_view new_path) override;
  38. bool DeleteDirectory(std::string_view path) override;
  39. private:
  40. using ReferenceListType = Common::IntrusiveListBaseTraits<FileReference>::ListType;
  41. std::map<std::string, std::weak_ptr<VfsFile>, std::less<>> cache;
  42. ReferenceListType open_references;
  43. ReferenceListType closed_references;
  44. std::mutex list_lock;
  45. size_t num_open_files{};
  46. private:
  47. friend class RealVfsFile;
  48. std::unique_lock<std::mutex> RefreshReference(const std::string& path, OpenMode perms,
  49. FileReference& reference);
  50. void DropReference(std::unique_ptr<FileReference>&& reference);
  51. private:
  52. friend class RealVfsDirectory;
  53. VirtualFile OpenFileFromEntry(std::string_view path, std::optional<u64> size,
  54. std::optional<std::string> parent_path,
  55. OpenMode perms = OpenMode::Read);
  56. private:
  57. void EvictSingleReferenceLocked();
  58. void InsertReferenceIntoListLocked(FileReference& reference);
  59. void RemoveReferenceFromListLocked(FileReference& reference);
  60. };
  61. // An implementation of VfsFile that represents a file on the user's computer.
  62. class RealVfsFile : public VfsFile {
  63. friend class RealVfsDirectory;
  64. friend class RealVfsFilesystem;
  65. public:
  66. ~RealVfsFile() override;
  67. std::string GetName() const override;
  68. std::size_t GetSize() const override;
  69. bool Resize(std::size_t new_size) override;
  70. VirtualDir GetContainingDirectory() const override;
  71. bool IsWritable() const override;
  72. bool IsReadable() const override;
  73. std::size_t Read(u8* data, std::size_t length, std::size_t offset) const override;
  74. std::size_t Write(const u8* data, std::size_t length, std::size_t offset) override;
  75. bool Rename(std::string_view name) override;
  76. private:
  77. RealVfsFile(RealVfsFilesystem& base, std::unique_ptr<FileReference> reference,
  78. const std::string& path, OpenMode perms = OpenMode::Read,
  79. std::optional<u64> size = {}, std::optional<std::string> parent_path = {});
  80. RealVfsFilesystem& base;
  81. std::unique_ptr<FileReference> reference;
  82. std::string path;
  83. std::string parent_path;
  84. std::vector<std::string> path_components;
  85. std::optional<u64> size;
  86. OpenMode perms;
  87. };
  88. // An implementation of VfsDirectory that represents a directory on the user's computer.
  89. class RealVfsDirectory : public VfsDirectory {
  90. friend class RealVfsFilesystem;
  91. public:
  92. ~RealVfsDirectory() override;
  93. VirtualFile GetFileRelative(std::string_view relative_path) const override;
  94. VirtualDir GetDirectoryRelative(std::string_view relative_path) const override;
  95. VirtualFile GetFile(std::string_view name) const override;
  96. VirtualDir GetSubdirectory(std::string_view name) const override;
  97. VirtualFile CreateFileRelative(std::string_view relative_path) override;
  98. VirtualDir CreateDirectoryRelative(std::string_view relative_path) override;
  99. bool DeleteSubdirectoryRecursive(std::string_view name) override;
  100. std::vector<VirtualFile> GetFiles() const override;
  101. FileTimeStampRaw GetFileTimeStamp(std::string_view path) const override;
  102. std::vector<VirtualDir> GetSubdirectories() const override;
  103. bool IsWritable() const override;
  104. bool IsReadable() const override;
  105. std::string GetName() const override;
  106. VirtualDir GetParentDirectory() const override;
  107. VirtualDir CreateSubdirectory(std::string_view name) override;
  108. VirtualFile CreateFile(std::string_view name) override;
  109. bool DeleteSubdirectory(std::string_view name) override;
  110. bool DeleteFile(std::string_view name) override;
  111. bool Rename(std::string_view name) override;
  112. std::string GetFullPath() const override;
  113. std::map<std::string, VfsEntryType, std::less<>> GetEntries() const override;
  114. private:
  115. RealVfsDirectory(RealVfsFilesystem& base, const std::string& path,
  116. OpenMode perms = OpenMode::Read);
  117. template <typename T, typename R>
  118. std::vector<std::shared_ptr<R>> IterateEntries() const;
  119. RealVfsFilesystem& base;
  120. std::string path;
  121. std::string parent_path;
  122. std::vector<std::string> path_components;
  123. OpenMode perms;
  124. };
  125. } // namespace FileSys