vfs.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <functional>
  5. #include <map>
  6. #include <memory>
  7. #include <optional>
  8. #include <string>
  9. #include <type_traits>
  10. #include <vector>
  11. #include "common/common_funcs.h"
  12. #include "common/common_types.h"
  13. #include "core/file_sys/fs_filesystem.h"
  14. #include "core/file_sys/vfs/vfs_types.h"
  15. namespace FileSys {
  16. // An enumeration representing what can be at the end of a path in a VfsFilesystem
  17. enum class VfsEntryType {
  18. None,
  19. File,
  20. Directory,
  21. };
  22. // A class representing an abstract filesystem. A default implementation given the root VirtualDir
  23. // is provided for convenience, but if the Vfs implementation has any additional state or
  24. // functionality, they will need to override.
  25. class VfsFilesystem {
  26. public:
  27. YUZU_NON_COPYABLE(VfsFilesystem);
  28. YUZU_NON_MOVEABLE(VfsFilesystem);
  29. explicit VfsFilesystem(VirtualDir root);
  30. virtual ~VfsFilesystem();
  31. // Gets the friendly name for the filesystem.
  32. virtual std::string GetName() const;
  33. // Return whether or not the user has read permissions on this filesystem.
  34. virtual bool IsReadable() const;
  35. // Return whether or not the user has write permission on this filesystem.
  36. virtual bool IsWritable() const;
  37. // Determine if the entry at path is non-existent, a file, or a directory.
  38. virtual VfsEntryType GetEntryType(std::string_view path) const;
  39. // Opens the file with path relative to root. If it doesn't exist, returns nullptr.
  40. virtual VirtualFile OpenFile(std::string_view path, OpenMode perms);
  41. // Creates a new, empty file at path
  42. virtual VirtualFile CreateFile(std::string_view path, OpenMode perms);
  43. // Copies the file from old_path to new_path, returning the new file on success and nullptr on
  44. // failure.
  45. virtual VirtualFile CopyFile(std::string_view old_path, std::string_view new_path);
  46. // Moves the file from old_path to new_path, returning the moved file on success and nullptr on
  47. // failure.
  48. virtual VirtualFile MoveFile(std::string_view old_path, std::string_view new_path);
  49. // Deletes the file with path relative to root, returning true on success.
  50. virtual bool DeleteFile(std::string_view path);
  51. // Opens the directory with path relative to root. If it doesn't exist, returns nullptr.
  52. virtual VirtualDir OpenDirectory(std::string_view path, OpenMode perms);
  53. // Creates a new, empty directory at path
  54. virtual VirtualDir CreateDirectory(std::string_view path, OpenMode perms);
  55. // Copies the directory from old_path to new_path, returning the new directory on success and
  56. // nullptr on failure.
  57. virtual VirtualDir CopyDirectory(std::string_view old_path, std::string_view new_path);
  58. // Moves the directory from old_path to new_path, returning the moved directory on success and
  59. // nullptr on failure.
  60. virtual VirtualDir MoveDirectory(std::string_view old_path, std::string_view new_path);
  61. // Deletes the directory with path relative to root, returning true on success.
  62. virtual bool DeleteDirectory(std::string_view path);
  63. protected:
  64. // Root directory in default implementation.
  65. VirtualDir root;
  66. };
  67. // A class representing a file in an abstract filesystem.
  68. class VfsFile {
  69. public:
  70. YUZU_NON_COPYABLE(VfsFile);
  71. YUZU_NON_MOVEABLE(VfsFile);
  72. VfsFile() = default;
  73. virtual ~VfsFile();
  74. // Retrieves the file name.
  75. virtual std::string GetName() const = 0;
  76. // Retrieves the extension of the file name.
  77. virtual std::string GetExtension() const;
  78. // Retrieves the size of the file.
  79. virtual std::size_t GetSize() const = 0;
  80. // Resizes the file to new_size. Returns whether or not the operation was successful.
  81. virtual bool Resize(std::size_t new_size) = 0;
  82. // Gets a pointer to the directory containing this file, returning nullptr if there is none.
  83. virtual VirtualDir GetContainingDirectory() const = 0;
  84. // Returns whether or not the file can be written to.
  85. virtual bool IsWritable() const = 0;
  86. // Returns whether or not the file can be read from.
  87. virtual bool IsReadable() const = 0;
  88. // The primary method of reading from the file. Reads length bytes into data starting at offset
  89. // into file. Returns number of bytes successfully read.
  90. virtual std::size_t Read(u8* data, std::size_t length, std::size_t offset = 0) const = 0;
  91. // The primary method of writing to the file. Writes length bytes from data starting at offset
  92. // into file. Returns number of bytes successfully written.
  93. virtual std::size_t Write(const u8* data, std::size_t length, std::size_t offset = 0) = 0;
  94. // Reads exactly one byte at the offset provided, returning std::nullopt on error.
  95. virtual std::optional<u8> ReadByte(std::size_t offset = 0) const;
  96. // Reads size bytes starting at offset in file into a vector.
  97. virtual std::vector<u8> ReadBytes(std::size_t size, std::size_t offset = 0) const;
  98. // Reads all the bytes from the file into a vector. Equivalent to 'file->Read(file->GetSize(),
  99. // 0)'
  100. virtual std::vector<u8> ReadAllBytes() const;
  101. // Reads an array of type T, size number_elements starting at offset.
  102. // Returns the number of bytes (sizeof(T)*number_elements) read successfully.
  103. template <typename T>
  104. std::size_t ReadArray(T* data, std::size_t number_elements, std::size_t offset = 0) const {
  105. static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
  106. return Read(reinterpret_cast<u8*>(data), number_elements * sizeof(T), offset);
  107. }
  108. // Reads size bytes into the memory starting at data starting at offset into the file.
  109. // Returns the number of bytes read successfully.
  110. template <typename T>
  111. std::size_t ReadBytes(T* data, std::size_t size, std::size_t offset = 0) const {
  112. static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
  113. return Read(reinterpret_cast<u8*>(data), size, offset);
  114. }
  115. // Reads one object of type T starting at offset in file.
  116. // Returns the number of bytes read successfully (sizeof(T)).
  117. template <typename T>
  118. std::size_t ReadObject(T* data, std::size_t offset = 0) const {
  119. static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
  120. return Read(reinterpret_cast<u8*>(data), sizeof(T), offset);
  121. }
  122. // Writes exactly one byte to offset in file and returns whether or not the byte was written
  123. // successfully.
  124. virtual bool WriteByte(u8 data, std::size_t offset = 0);
  125. // Writes a vector of bytes to offset in file and returns the number of bytes successfully
  126. // written.
  127. virtual std::size_t WriteBytes(const std::vector<u8>& data, std::size_t offset = 0);
  128. // Writes an array of type T, size number_elements to offset in file.
  129. // Returns the number of bytes (sizeof(T)*number_elements) written successfully.
  130. template <typename T>
  131. std::size_t WriteArray(const T* data, std::size_t number_elements, std::size_t offset = 0) {
  132. static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
  133. return Write(reinterpret_cast<const u8*>(data), number_elements * sizeof(T), offset);
  134. }
  135. // Writes size bytes starting at memory location data to offset in file.
  136. // Returns the number of bytes written successfully.
  137. template <typename T>
  138. std::size_t WriteBytes(const T* data, std::size_t size, std::size_t offset = 0) {
  139. static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
  140. return Write(reinterpret_cast<const u8*>(data), size, offset);
  141. }
  142. // Writes one object of type T to offset in file.
  143. // Returns the number of bytes written successfully (sizeof(T)).
  144. template <typename T>
  145. std::size_t WriteObject(const T& data, std::size_t offset = 0) {
  146. static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
  147. return Write(reinterpret_cast<const u8*>(&data), sizeof(T), offset);
  148. }
  149. // Renames the file to name. Returns whether or not the operation was successful.
  150. virtual bool Rename(std::string_view name) = 0;
  151. // Returns the full path of this file as a string, recursively
  152. virtual std::string GetFullPath() const;
  153. };
  154. // A class representing a directory in an abstract filesystem.
  155. class VfsDirectory {
  156. public:
  157. YUZU_NON_COPYABLE(VfsDirectory);
  158. YUZU_NON_MOVEABLE(VfsDirectory);
  159. VfsDirectory() = default;
  160. virtual ~VfsDirectory();
  161. // Retrieves the file located at path as if the current directory was root. Returns nullptr if
  162. // not found.
  163. virtual VirtualFile GetFileRelative(std::string_view path) const;
  164. // Calls GetFileRelative(path) on the root of the current directory.
  165. virtual VirtualFile GetFileAbsolute(std::string_view path) const;
  166. // Retrieves the directory located at path as if the current directory was root. Returns nullptr
  167. // if not found.
  168. virtual VirtualDir GetDirectoryRelative(std::string_view path) const;
  169. // Calls GetDirectoryRelative(path) on the root of the current directory.
  170. virtual VirtualDir GetDirectoryAbsolute(std::string_view path) const;
  171. // Returns a vector containing all of the files in this directory.
  172. virtual std::vector<VirtualFile> GetFiles() const = 0;
  173. // Returns the file with filename matching name. Returns nullptr if directory doesn't have a
  174. // file with name.
  175. virtual VirtualFile GetFile(std::string_view name) const;
  176. // Returns a struct containing the file's timestamp.
  177. virtual FileTimeStampRaw GetFileTimeStamp(std::string_view path) const;
  178. // Returns a vector containing all of the subdirectories in this directory.
  179. virtual std::vector<VirtualDir> GetSubdirectories() const = 0;
  180. // Returns the directory with name matching name. Returns nullptr if directory doesn't have a
  181. // directory with name.
  182. virtual VirtualDir GetSubdirectory(std::string_view name) const;
  183. // Returns whether or not the directory can be written to.
  184. virtual bool IsWritable() const = 0;
  185. // Returns whether of not the directory can be read from.
  186. virtual bool IsReadable() const = 0;
  187. // Returns whether or not the directory is the root of the current file tree.
  188. virtual bool IsRoot() const;
  189. // Returns the name of the directory.
  190. virtual std::string GetName() const = 0;
  191. // Returns the total size of all files and subdirectories in this directory.
  192. virtual std::size_t GetSize() const;
  193. // Returns the parent directory of this directory. Returns nullptr if this directory is root or
  194. // has no parent.
  195. virtual VirtualDir GetParentDirectory() const = 0;
  196. // Creates a new subdirectory with name name. Returns a pointer to the new directory or nullptr
  197. // if the operation failed.
  198. virtual VirtualDir CreateSubdirectory(std::string_view name) = 0;
  199. // Creates a new file with name name. Returns a pointer to the new file or nullptr if the
  200. // operation failed.
  201. virtual VirtualFile CreateFile(std::string_view name) = 0;
  202. // Creates a new file at the path relative to this directory. Also creates directories if
  203. // they do not exist and is supported by this implementation. Returns nullptr on any failure.
  204. virtual VirtualFile CreateFileRelative(std::string_view path);
  205. // Creates a new file at the path relative to root of this directory. Also creates directories
  206. // if they do not exist and is supported by this implementation. Returns nullptr on any failure.
  207. virtual VirtualFile CreateFileAbsolute(std::string_view path);
  208. // Creates a new directory at the path relative to this directory. Also creates directories if
  209. // they do not exist and is supported by this implementation. Returns nullptr on any failure.
  210. virtual VirtualDir CreateDirectoryRelative(std::string_view path);
  211. // Creates a new directory at the path relative to root of this directory. Also creates
  212. // directories if they do not exist and is supported by this implementation. Returns nullptr on
  213. // any failure.
  214. virtual VirtualDir CreateDirectoryAbsolute(std::string_view path);
  215. // Deletes the subdirectory with the given name and returns true on success.
  216. virtual bool DeleteSubdirectory(std::string_view name) = 0;
  217. // Deletes all subdirectories and files within the provided directory and then deletes
  218. // the directory itself. Returns true on success.
  219. virtual bool DeleteSubdirectoryRecursive(std::string_view name);
  220. // Deletes all subdirectories and files within the provided directory.
  221. // Unlike DeleteSubdirectoryRecursive, this does not delete the provided directory.
  222. virtual bool CleanSubdirectoryRecursive(std::string_view name);
  223. // Returns whether or not the file with name name was deleted successfully.
  224. virtual bool DeleteFile(std::string_view name) = 0;
  225. // Returns whether or not this directory was renamed to name.
  226. virtual bool Rename(std::string_view name) = 0;
  227. // Returns whether or not the file with name src was successfully copied to a new file with name
  228. // dest.
  229. virtual bool Copy(std::string_view src, std::string_view dest);
  230. // Gets all of the entries directly in the directory (files and dirs), returning a map between
  231. // item name -> type.
  232. virtual std::map<std::string, VfsEntryType, std::less<>> GetEntries() const;
  233. // Returns the full path of this directory as a string, recursively
  234. virtual std::string GetFullPath() const;
  235. };
  236. // A convenience partial-implementation of VfsDirectory that stubs out methods that should only work
  237. // if writable. This is to avoid redundant empty methods everywhere.
  238. class ReadOnlyVfsDirectory : public VfsDirectory {
  239. public:
  240. bool IsWritable() const override;
  241. bool IsReadable() const override;
  242. VirtualDir CreateSubdirectory(std::string_view name) override;
  243. VirtualFile CreateFile(std::string_view name) override;
  244. VirtualFile CreateFileAbsolute(std::string_view path) override;
  245. VirtualFile CreateFileRelative(std::string_view path) override;
  246. VirtualDir CreateDirectoryAbsolute(std::string_view path) override;
  247. VirtualDir CreateDirectoryRelative(std::string_view path) override;
  248. bool DeleteSubdirectory(std::string_view name) override;
  249. bool DeleteSubdirectoryRecursive(std::string_view name) override;
  250. bool CleanSubdirectoryRecursive(std::string_view name) override;
  251. bool DeleteFile(std::string_view name) override;
  252. bool Rename(std::string_view name) override;
  253. };
  254. // Compare the two files, byte-for-byte, in increments specified by block_size
  255. bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2,
  256. std::size_t block_size = 0x1000);
  257. // A method that copies the raw data between two different implementations of VirtualFile. If you
  258. // are using the same implementation, it is probably better to use the Copy method in the parent
  259. // directory of src/dest.
  260. bool VfsRawCopy(const VirtualFile& src, const VirtualFile& dest, std::size_t block_size = 0x1000);
  261. // A method that performs a similar function to VfsRawCopy above, but instead copies entire
  262. // directories. It suffers the same performance penalties as above and an implementation-specific
  263. // Copy should always be preferred.
  264. bool VfsRawCopyD(const VirtualDir& src, const VirtualDir& dest, std::size_t block_size = 0x1000);
  265. // Checks if the directory at path relative to rel exists. If it does, returns that. If it does not
  266. // it attempts to create it and returns the new dir or nullptr on failure.
  267. VirtualDir GetOrCreateDirectoryRelative(const VirtualDir& rel, std::string_view path);
  268. } // namespace FileSys