vfs_real.h 5.1 KB

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