filesystem.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <utility>
  8. #include <vector>
  9. #include "common/bit_field.h"
  10. #include "common/common_types.h"
  11. #include "common/swap.h"
  12. #include "core/hle/result.h"
  13. namespace FileSys {
  14. class StorageBackend;
  15. class DirectoryBackend;
  16. // Path string type
  17. enum LowPathType : u32 {
  18. Invalid = 0,
  19. Empty = 1,
  20. Binary = 2,
  21. Char = 3,
  22. Wchar = 4,
  23. };
  24. enum EntryType : u8 {
  25. Directory = 0,
  26. File = 1,
  27. };
  28. enum class Mode : u32 {
  29. Read = 1,
  30. Write = 2,
  31. Append = 4,
  32. };
  33. class Path {
  34. public:
  35. Path() : type(Invalid) {}
  36. Path(const char* path) : type(Char), string(path) {}
  37. Path(std::vector<u8> binary_data) : type(Binary), binary(std::move(binary_data)) {}
  38. Path(LowPathType type, u32 size, u32 pointer);
  39. LowPathType GetType() const {
  40. return type;
  41. }
  42. /**
  43. * Gets the string representation of the path for debugging
  44. * @return String representation of the path for debugging
  45. */
  46. std::string DebugStr() const;
  47. std::string AsString() const;
  48. std::u16string AsU16Str() const;
  49. std::vector<u8> AsBinary() const;
  50. private:
  51. LowPathType type;
  52. std::vector<u8> binary;
  53. std::string string;
  54. std::u16string u16str;
  55. };
  56. /// Parameters of the archive, as specified in the Create or Format call.
  57. struct ArchiveFormatInfo {
  58. u32_le total_size; ///< The pre-defined size of the archive.
  59. u32_le number_directories; ///< The pre-defined number of directories in the archive.
  60. u32_le number_files; ///< The pre-defined number of files in the archive.
  61. u8 duplicate_data; ///< Whether the archive should duplicate the data.
  62. };
  63. static_assert(std::is_pod<ArchiveFormatInfo>::value, "ArchiveFormatInfo is not POD");
  64. class FileSystemBackend : NonCopyable {
  65. public:
  66. virtual ~FileSystemBackend() {}
  67. /**
  68. * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
  69. */
  70. virtual std::string GetName() const = 0;
  71. /**
  72. * Create a file specified by its path
  73. * @param path Path relative to the Archive
  74. * @param size The size of the new file, filled with zeroes
  75. * @return Result of the operation
  76. */
  77. virtual ResultCode CreateFile(const std::string& path, u64 size) const = 0;
  78. /**
  79. * Delete a file specified by its path
  80. * @param path Path relative to the archive
  81. * @return Result of the operation
  82. */
  83. virtual ResultCode DeleteFile(const std::string& path) const = 0;
  84. /**
  85. * Create a directory specified by its path
  86. * @param path Path relative to the archive
  87. * @return Result of the operation
  88. */
  89. virtual ResultCode CreateDirectory(const std::string& path) const = 0;
  90. /**
  91. * Delete a directory specified by its path
  92. * @param path Path relative to the archive
  93. * @return Result of the operation
  94. */
  95. virtual ResultCode DeleteDirectory(const Path& path) const = 0;
  96. /**
  97. * Delete a directory specified by its path and anything under it
  98. * @param path Path relative to the archive
  99. * @return Result of the operation
  100. */
  101. virtual ResultCode DeleteDirectoryRecursively(const Path& path) const = 0;
  102. /**
  103. * Rename a File specified by its path
  104. * @param src_path Source path relative to the archive
  105. * @param dest_path Destination path relative to the archive
  106. * @return Result of the operation
  107. */
  108. virtual ResultCode RenameFile(const std::string& src_path,
  109. const std::string& dest_path) const = 0;
  110. /**
  111. * Rename a Directory specified by its path
  112. * @param src_path Source path relative to the archive
  113. * @param dest_path Destination path relative to the archive
  114. * @return Result of the operation
  115. */
  116. virtual ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const = 0;
  117. /**
  118. * Open a file specified by its path, using the specified mode
  119. * @param path Path relative to the archive
  120. * @param mode Mode to open the file with
  121. * @return Opened file, or error code
  122. */
  123. virtual ResultVal<std::unique_ptr<StorageBackend>> OpenFile(const std::string& path,
  124. Mode mode) const = 0;
  125. /**
  126. * Open a directory specified by its path
  127. * @param path Path relative to the archive
  128. * @return Opened directory, or error code
  129. */
  130. virtual ResultVal<std::unique_ptr<DirectoryBackend>> OpenDirectory(
  131. const std::string& path) const = 0;
  132. /**
  133. * Get the free space
  134. * @return The number of free bytes in the archive
  135. */
  136. virtual u64 GetFreeSpaceSize() const = 0;
  137. /**
  138. * Get the type of the specified path
  139. * @return The type of the specified path or error code
  140. */
  141. virtual ResultVal<EntryType> GetEntryType(const std::string& path) const = 0;
  142. };
  143. class FileSystemFactory : NonCopyable {
  144. public:
  145. virtual ~FileSystemFactory() {}
  146. /**
  147. * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
  148. */
  149. virtual std::string GetName() const = 0;
  150. /**
  151. * Tries to open the archive of this type with the specified path
  152. * @param path Path to the archive
  153. * @return An ArchiveBackend corresponding operating specified archive path.
  154. */
  155. virtual ResultVal<std::unique_ptr<FileSystemBackend>> Open(const Path& path) = 0;
  156. /**
  157. * Deletes the archive contents and then re-creates the base folder
  158. * @param path Path to the archive
  159. * @return ResultCode of the operation, 0 on success
  160. */
  161. virtual ResultCode Format(const Path& path) = 0;
  162. /**
  163. * Retrieves the format info about the archive with the specified path
  164. * @param path Path to the archive
  165. * @return Format information about the archive or error code
  166. */
  167. virtual ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const = 0;
  168. };
  169. } // namespace FileSys