vfs_real.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "common/file_util.h"
  8. #include "core/file_sys/mode.h"
  9. #include "core/file_sys/vfs.h"
  10. namespace FileSys {
  11. class RealVfsFilesystem : public VfsFilesystem {
  12. public:
  13. RealVfsFilesystem();
  14. std::string GetName() const override;
  15. bool IsReadable() const override;
  16. bool IsWritable() const override;
  17. VfsEntryType GetEntryType(std::string_view path) const override;
  18. VirtualFile OpenFile(std::string_view path, Mode perms = Mode::Read) override;
  19. VirtualFile CreateFile(std::string_view path, Mode perms = Mode::ReadWrite) override;
  20. VirtualFile CopyFile(std::string_view old_path, std::string_view new_path) override;
  21. VirtualFile MoveFile(std::string_view old_path, std::string_view new_path) override;
  22. bool DeleteFile(std::string_view path) override;
  23. VirtualDir OpenDirectory(std::string_view path, Mode perms = Mode::Read) override;
  24. VirtualDir CreateDirectory(std::string_view path, Mode perms = Mode::ReadWrite) override;
  25. VirtualDir CopyDirectory(std::string_view old_path, std::string_view new_path) override;
  26. VirtualDir MoveDirectory(std::string_view old_path, std::string_view new_path) override;
  27. bool DeleteDirectory(std::string_view path) override;
  28. private:
  29. boost::container::flat_map<std::string, std::weak_ptr<FileUtil::IOFile>> cache;
  30. };
  31. // An implmentation of VfsFile that represents a file on the user's computer.
  32. class RealVfsFile : public VfsFile {
  33. friend class RealVfsDirectory;
  34. friend class RealVfsFilesystem;
  35. RealVfsFile(RealVfsFilesystem& base, std::shared_ptr<FileUtil::IOFile> backing,
  36. const std::string& path, Mode perms = Mode::Read);
  37. public:
  38. std::string GetName() const override;
  39. size_t GetSize() const override;
  40. bool Resize(size_t new_size) override;
  41. std::shared_ptr<VfsDirectory> GetContainingDirectory() const override;
  42. bool IsWritable() const override;
  43. bool IsReadable() const override;
  44. size_t Read(u8* data, size_t length, size_t offset) const override;
  45. size_t Write(const u8* data, size_t length, size_t offset) override;
  46. bool Rename(std::string_view name) override;
  47. private:
  48. bool Close();
  49. RealVfsFilesystem& base;
  50. std::shared_ptr<FileUtil::IOFile> backing;
  51. std::string path;
  52. std::string parent_path;
  53. std::vector<std::string> path_components;
  54. std::vector<std::string> parent_components;
  55. Mode perms;
  56. };
  57. // An implementation of VfsDirectory that represents a directory on the user's computer.
  58. class RealVfsDirectory : public VfsDirectory {
  59. friend class RealVfsFilesystem;
  60. RealVfsDirectory(RealVfsFilesystem& base, const std::string& path, Mode perms = Mode::Read);
  61. public:
  62. std::shared_ptr<VfsFile> GetFileRelative(std::string_view path) const override;
  63. std::shared_ptr<VfsDirectory> GetDirectoryRelative(std::string_view path) const override;
  64. std::shared_ptr<VfsFile> GetFile(std::string_view name) const override;
  65. std::shared_ptr<VfsDirectory> GetSubdirectory(std::string_view name) const override;
  66. std::shared_ptr<VfsFile> CreateFileRelative(std::string_view path) override;
  67. std::shared_ptr<VfsDirectory> CreateDirectoryRelative(std::string_view path) override;
  68. bool DeleteSubdirectoryRecursive(std::string_view name) override;
  69. std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
  70. std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override;
  71. bool IsWritable() const override;
  72. bool IsReadable() const override;
  73. std::string GetName() const override;
  74. std::shared_ptr<VfsDirectory> GetParentDirectory() const override;
  75. std::shared_ptr<VfsDirectory> CreateSubdirectory(std::string_view name) override;
  76. std::shared_ptr<VfsFile> CreateFile(std::string_view name) override;
  77. bool DeleteSubdirectory(std::string_view name) override;
  78. bool DeleteFile(std::string_view name) override;
  79. bool Rename(std::string_view name) override;
  80. std::string GetFullPath() const override;
  81. protected:
  82. bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override;
  83. private:
  84. template <typename T, typename R>
  85. std::vector<std::shared_ptr<R>> IterateEntries() const;
  86. RealVfsFilesystem& base;
  87. std::string path;
  88. std::string parent_path;
  89. std::vector<std::string> path_components;
  90. std::vector<std::string> parent_components;
  91. Mode perms;
  92. };
  93. } // namespace FileSys