vfs.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 <memory>
  6. #include <string>
  7. #include <string_view>
  8. #include <type_traits>
  9. #include <vector>
  10. #include <boost/optional.hpp>
  11. #include "common/common_types.h"
  12. namespace FileSys {
  13. class VfsDirectory;
  14. class VfsFile;
  15. class VfsFilesystem;
  16. enum class Mode : u32;
  17. // Convenience typedefs to use Vfs* interfaces
  18. using VirtualFilesystem = std::shared_ptr<VfsFilesystem>;
  19. using VirtualDir = std::shared_ptr<VfsDirectory>;
  20. using VirtualFile = std::shared_ptr<VfsFile>;
  21. // An enumeration representing what can be at the end of a path in a VfsFilesystem
  22. enum class VfsEntryType {
  23. None,
  24. File,
  25. Directory,
  26. };
  27. // A class representing an abstract filesystem. A default implementation given the root VirtualDir
  28. // is provided for convenience, but if the Vfs implementation has any additional state or
  29. // functionality, they will need to override.
  30. class VfsFilesystem : NonCopyable {
  31. public:
  32. explicit VfsFilesystem(VirtualDir root);
  33. virtual ~VfsFilesystem();
  34. // Gets the friendly name for the filesystem.
  35. virtual std::string GetName() const;
  36. // Return whether or not the user has read permissions on this filesystem.
  37. virtual bool IsReadable() const;
  38. // Return whether or not the user has write permission on this filesystem.
  39. virtual bool IsWritable() const;
  40. // Determine if the entry at path is non-existant, a file, or a directory.
  41. virtual VfsEntryType GetEntryType(std::string_view path) const;
  42. // Opens the file with path relative to root. If it doesn't exist, returns nullptr.
  43. virtual VirtualFile OpenFile(std::string_view path, Mode perms);
  44. // Creates a new, empty file at path
  45. virtual VirtualFile CreateFile(std::string_view path, Mode perms);
  46. // Copies the file from old_path to new_path, returning the new file on success and nullptr on
  47. // failure.
  48. virtual VirtualFile CopyFile(std::string_view old_path, std::string_view new_path);
  49. // Moves the file from old_path to new_path, returning the moved file on success and nullptr on
  50. // failure.
  51. virtual VirtualFile MoveFile(std::string_view old_path, std::string_view new_path);
  52. // Deletes the file with path relative to root, returing true on success.
  53. virtual bool DeleteFile(std::string_view path);
  54. // Opens the directory with path relative to root. If it doesn't exist, returns nullptr.
  55. virtual VirtualDir OpenDirectory(std::string_view path, Mode perms);
  56. // Creates a new, empty directory at path
  57. virtual VirtualDir CreateDirectory(std::string_view path, Mode perms);
  58. // Copies the directory from old_path to new_path, returning the new directory on success and
  59. // nullptr on failure.
  60. virtual VirtualDir CopyDirectory(std::string_view old_path, std::string_view new_path);
  61. // Moves the directory from old_path to new_path, returning the moved directory on success and
  62. // nullptr on failure.
  63. virtual VirtualDir MoveDirectory(std::string_view old_path, std::string_view new_path);
  64. // Deletes the directory with path relative to root, returing true on success.
  65. virtual bool DeleteDirectory(std::string_view path);
  66. protected:
  67. // Root directory in default implementation.
  68. VirtualDir root;
  69. };
  70. // A class representing a file in an abstract filesystem.
  71. class VfsFile : NonCopyable {
  72. public:
  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 size_t GetSize() const = 0;
  80. // Resizes the file to new_size. Returns whether or not the operation was successful.
  81. virtual bool Resize(size_t new_size) = 0;
  82. // Gets a pointer to the directory containing this file, returning nullptr if there is none.
  83. virtual std::shared_ptr<VfsDirectory> 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 size_t Read(u8* data, size_t length, 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 size_t Write(const u8* data, size_t length, size_t offset = 0) = 0;
  94. // Reads exactly one byte at the offset provided, returning boost::none on error.
  95. virtual boost::optional<u8> ReadByte(size_t offset = 0) const;
  96. // Reads size bytes starting at offset in file into a vector.
  97. virtual std::vector<u8> ReadBytes(size_t size, 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. size_t ReadArray(T* data, size_t number_elements, 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. size_t ReadBytes(T* data, size_t size, 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. size_t ReadObject(T* data, 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 retuns whether or not the byte was written
  123. // successfully.
  124. virtual bool WriteByte(u8 data, 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 size_t WriteBytes(const std::vector<u8>& data, 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. size_t WriteArray(const T* data, size_t number_elements, size_t offset = 0) {
  132. static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
  133. return Write(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. size_t WriteBytes(const T* data, size_t size, 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. size_t WriteObject(const T& data, size_t offset = 0) {
  146. static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
  147. return Write(&data, sizeof(T), offset);
  148. }
  149. // Renames the file to name. Returns whether or not the operation was successsful.
  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 : NonCopyable {
  156. public:
  157. virtual ~VfsDirectory();
  158. // Retrives the file located at path as if the current directory was root. Returns nullptr if
  159. // not found.
  160. virtual std::shared_ptr<VfsFile> GetFileRelative(std::string_view path) const;
  161. // Calls GetFileRelative(path) on the root of the current directory.
  162. virtual std::shared_ptr<VfsFile> GetFileAbsolute(std::string_view path) const;
  163. // Retrives the directory located at path as if the current directory was root. Returns nullptr
  164. // if not found.
  165. virtual std::shared_ptr<VfsDirectory> GetDirectoryRelative(std::string_view path) const;
  166. // Calls GetDirectoryRelative(path) on the root of the current directory.
  167. virtual std::shared_ptr<VfsDirectory> GetDirectoryAbsolute(std::string_view path) const;
  168. // Returns a vector containing all of the files in this directory.
  169. virtual std::vector<std::shared_ptr<VfsFile>> GetFiles() const = 0;
  170. // Returns the file with filename matching name. Returns nullptr if directory dosen't have a
  171. // file with name.
  172. virtual std::shared_ptr<VfsFile> GetFile(std::string_view name) const;
  173. // Returns a vector containing all of the subdirectories in this directory.
  174. virtual std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const = 0;
  175. // Returns the directory with name matching name. Returns nullptr if directory dosen't have a
  176. // directory with name.
  177. virtual std::shared_ptr<VfsDirectory> GetSubdirectory(std::string_view name) const;
  178. // Returns whether or not the directory can be written to.
  179. virtual bool IsWritable() const = 0;
  180. // Returns whether of not the directory can be read from.
  181. virtual bool IsReadable() const = 0;
  182. // Returns whether or not the directory is the root of the current file tree.
  183. virtual bool IsRoot() const;
  184. // Returns the name of the directory.
  185. virtual std::string GetName() const = 0;
  186. // Returns the total size of all files and subdirectories in this directory.
  187. virtual size_t GetSize() const;
  188. // Returns the parent directory of this directory. Returns nullptr if this directory is root or
  189. // has no parent.
  190. virtual std::shared_ptr<VfsDirectory> GetParentDirectory() const = 0;
  191. // Creates a new subdirectory with name name. Returns a pointer to the new directory or nullptr
  192. // if the operation failed.
  193. virtual std::shared_ptr<VfsDirectory> CreateSubdirectory(std::string_view name) = 0;
  194. // Creates a new file with name name. Returns a pointer to the new file or nullptr if the
  195. // operation failed.
  196. virtual std::shared_ptr<VfsFile> CreateFile(std::string_view name) = 0;
  197. // Creates a new file at the path relative to this directory. Also creates directories if
  198. // they do not exist and is supported by this implementation. Returns nullptr on any failure.
  199. virtual std::shared_ptr<VfsFile> CreateFileRelative(std::string_view path);
  200. // Creates a new file at the path relative to root of this directory. Also creates directories
  201. // if they do not exist and is supported by this implementation. Returns nullptr on any failure.
  202. virtual std::shared_ptr<VfsFile> CreateFileAbsolute(std::string_view path);
  203. // Creates a new directory at the path relative to this directory. Also creates directories if
  204. // they do not exist and is supported by this implementation. Returns nullptr on any failure.
  205. virtual std::shared_ptr<VfsDirectory> CreateDirectoryRelative(std::string_view path);
  206. // Creates a new directory at the path relative to root of this directory. Also creates
  207. // directories if they do not exist and is supported by this implementation. Returns nullptr on
  208. // any failure.
  209. virtual std::shared_ptr<VfsDirectory> CreateDirectoryAbsolute(std::string_view path);
  210. // Deletes the subdirectory with name and returns true on success.
  211. virtual bool DeleteSubdirectory(std::string_view name) = 0;
  212. // Deletes all subdirectories and files of subdirectory with name recirsively and then deletes
  213. // the subdirectory. Returns true on success.
  214. virtual bool DeleteSubdirectoryRecursive(std::string_view name);
  215. // Returnes whether or not the file with name name was deleted successfully.
  216. virtual bool DeleteFile(std::string_view name) = 0;
  217. // Returns whether or not this directory was renamed to name.
  218. virtual bool Rename(std::string_view name) = 0;
  219. // Returns whether or not the file with name src was successfully copied to a new file with name
  220. // dest.
  221. virtual bool Copy(std::string_view src, std::string_view dest);
  222. // Interprets the file with name file instead as a directory of type directory.
  223. // The directory must have a constructor that takes a single argument of type
  224. // std::shared_ptr<VfsFile>. Allows to reinterpret container files (i.e NCA, zip, XCI, etc) as a
  225. // subdirectory in one call.
  226. template <typename Directory>
  227. bool InterpretAsDirectory(std::string_view file) {
  228. auto file_p = GetFile(file);
  229. if (file_p == nullptr) {
  230. return false;
  231. }
  232. return ReplaceFileWithSubdirectory(file_p, std::make_shared<Directory>(file_p));
  233. }
  234. bool InterpretAsDirectory(const std::function<VirtualDir(VirtualFile)>& function,
  235. const std::string& file) {
  236. auto file_p = GetFile(file);
  237. if (file_p == nullptr)
  238. return false;
  239. return ReplaceFileWithSubdirectory(file_p, function(file_p));
  240. }
  241. // Returns the full path of this directory as a string, recursively
  242. virtual std::string GetFullPath() const;
  243. protected:
  244. // Backend for InterpretAsDirectory.
  245. // Removes all references to file and adds a reference to dir in the directory's implementation.
  246. virtual bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) = 0;
  247. };
  248. // A convenience partial-implementation of VfsDirectory that stubs out methods that should only work
  249. // if writable. This is to avoid redundant empty methods everywhere.
  250. class ReadOnlyVfsDirectory : public VfsDirectory {
  251. public:
  252. bool IsWritable() const override;
  253. bool IsReadable() const override;
  254. std::shared_ptr<VfsDirectory> CreateSubdirectory(std::string_view name) override;
  255. std::shared_ptr<VfsFile> CreateFile(std::string_view name) override;
  256. bool DeleteSubdirectory(std::string_view name) override;
  257. bool DeleteFile(std::string_view name) override;
  258. bool Rename(std::string_view name) override;
  259. };
  260. // Compare the two files, byte-for-byte, in increments specificed by block_size
  261. bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2, size_t block_size = 0x200);
  262. // A method that copies the raw data between two different implementations of VirtualFile. If you
  263. // are using the same implementation, it is probably better to use the Copy method in the parent
  264. // directory of src/dest.
  265. bool VfsRawCopy(VirtualFile src, VirtualFile dest);
  266. // Checks if the directory at path relative to rel exists. If it does, returns that. If it does not
  267. // it attempts to create it and returns the new dir or nullptr on failure.
  268. VirtualDir GetOrCreateDirectoryRelative(const VirtualDir& rel, std::string_view path);
  269. } // namespace FileSys