vfs_real.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <string_view>
  6. #include <boost/container/flat_map.hpp>
  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. class RealVfsFilesystem : public VfsFilesystem {
  14. public:
  15. RealVfsFilesystem();
  16. ~RealVfsFilesystem() override;
  17. std::string GetName() const override;
  18. bool IsReadable() const override;
  19. bool IsWritable() const override;
  20. VfsEntryType GetEntryType(std::string_view path) const override;
  21. VirtualFile OpenFile(std::string_view path, Mode perms = Mode::Read) override;
  22. VirtualFile CreateFile(std::string_view path, Mode perms = Mode::ReadWrite) override;
  23. VirtualFile CopyFile(std::string_view old_path, std::string_view new_path) override;
  24. VirtualFile MoveFile(std::string_view old_path, std::string_view new_path) override;
  25. bool DeleteFile(std::string_view path) override;
  26. VirtualDir OpenDirectory(std::string_view path, Mode perms = Mode::Read) override;
  27. VirtualDir CreateDirectory(std::string_view path, Mode perms = Mode::ReadWrite) override;
  28. VirtualDir CopyDirectory(std::string_view old_path, std::string_view new_path) override;
  29. VirtualDir MoveDirectory(std::string_view old_path, std::string_view new_path) override;
  30. bool DeleteDirectory(std::string_view path) override;
  31. private:
  32. boost::container::flat_map<std::string, std::weak_ptr<Common::FS::IOFile>> cache;
  33. };
  34. // An implmentation of VfsFile that represents a file on the user's computer.
  35. class RealVfsFile : public VfsFile {
  36. friend class RealVfsDirectory;
  37. friend class RealVfsFilesystem;
  38. public:
  39. ~RealVfsFile() override;
  40. std::string GetName() const override;
  41. std::size_t GetSize() const override;
  42. bool Resize(std::size_t new_size) override;
  43. VirtualDir GetContainingDirectory() const override;
  44. bool IsWritable() const override;
  45. bool IsReadable() const override;
  46. std::size_t Read(u8* data, std::size_t length, std::size_t offset) const override;
  47. std::size_t Write(const u8* data, std::size_t length, std::size_t offset) override;
  48. bool Rename(std::string_view name) override;
  49. private:
  50. RealVfsFile(RealVfsFilesystem& base, std::shared_ptr<Common::FS::IOFile> backing,
  51. const std::string& path, Mode perms = Mode::Read);
  52. void Close();
  53. RealVfsFilesystem& base;
  54. std::shared_ptr<Common::FS::IOFile> backing;
  55. std::string path;
  56. std::string parent_path;
  57. std::vector<std::string> path_components;
  58. Mode perms;
  59. };
  60. // An implementation of VfsDirectory that represents a directory on the user's computer.
  61. class RealVfsDirectory : public VfsDirectory {
  62. friend class RealVfsFilesystem;
  63. public:
  64. ~RealVfsDirectory() override;
  65. VirtualFile GetFileRelative(std::string_view relative_path) const override;
  66. VirtualDir GetDirectoryRelative(std::string_view relative_path) const override;
  67. VirtualFile GetFile(std::string_view name) const override;
  68. VirtualDir GetSubdirectory(std::string_view name) const override;
  69. VirtualFile CreateFileRelative(std::string_view relative_path) override;
  70. VirtualDir CreateDirectoryRelative(std::string_view relative_path) override;
  71. bool DeleteSubdirectoryRecursive(std::string_view name) override;
  72. std::vector<VirtualFile> GetFiles() const override;
  73. FileTimeStampRaw GetFileTimeStamp(std::string_view path) const override;
  74. std::vector<VirtualDir> GetSubdirectories() const override;
  75. bool IsWritable() const override;
  76. bool IsReadable() const override;
  77. std::string GetName() const override;
  78. VirtualDir GetParentDirectory() const override;
  79. VirtualDir CreateSubdirectory(std::string_view name) override;
  80. VirtualFile CreateFile(std::string_view name) override;
  81. bool DeleteSubdirectory(std::string_view name) override;
  82. bool DeleteFile(std::string_view name) override;
  83. bool Rename(std::string_view name) override;
  84. std::string GetFullPath() const override;
  85. std::map<std::string, VfsEntryType, std::less<>> GetEntries() const override;
  86. private:
  87. RealVfsDirectory(RealVfsFilesystem& base, const std::string& path, Mode perms = Mode::Read);
  88. template <typename T, typename R>
  89. std::vector<std::shared_ptr<R>> IterateEntries() const;
  90. RealVfsFilesystem& base;
  91. std::string path;
  92. std::string parent_path;
  93. std::vector<std::string> path_components;
  94. Mode perms;
  95. };
  96. } // namespace FileSys