archive.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright 2014 Citra Emulator Project
  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 "common/common_types.h"
  8. #include "core/file_sys/archive_backend.h"
  9. #include "core/hle/kernel/session.h"
  10. #include "core/hle/result.h"
  11. namespace FileSys {
  12. class DirectoryBackend;
  13. class FileBackend;
  14. }
  15. /// The unique system identifier hash, also known as ID0
  16. extern const std::string SYSTEM_ID;
  17. /// The scrambled SD card CID, also known as ID1
  18. extern const std::string SDCARD_ID;
  19. namespace Service {
  20. namespace FS {
  21. /// Supported archive types
  22. enum class ArchiveIdCode : u32 {
  23. RomFS = 0x00000003,
  24. SaveData = 0x00000004,
  25. ExtSaveData = 0x00000006,
  26. SharedExtSaveData = 0x00000007,
  27. SystemSaveData = 0x00000008,
  28. SDMC = 0x00000009,
  29. SDMCWriteOnly = 0x0000000A,
  30. SaveDataCheck = 0x2345678A,
  31. };
  32. /// Media types for the archives
  33. enum class MediaType : u32 {
  34. NAND = 0,
  35. SDMC = 1
  36. };
  37. typedef u64 ArchiveHandle;
  38. class File : public Kernel::Session {
  39. public:
  40. File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path);
  41. ~File();
  42. std::string GetName() const override { return "Path: " + path.DebugStr(); }
  43. ResultVal<bool> SyncRequest() override;
  44. FileSys::Path path; ///< Path of the file
  45. u32 priority; ///< Priority of the file. TODO(Subv): Find out what this means
  46. std::unique_ptr<FileSys::FileBackend> backend; ///< File backend interface
  47. };
  48. class Directory : public Kernel::Session {
  49. public:
  50. Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend, const FileSys::Path& path);
  51. ~Directory();
  52. std::string GetName() const override { return "Directory: " + path.DebugStr(); }
  53. ResultVal<bool> SyncRequest() override;
  54. FileSys::Path path; ///< Path of the directory
  55. std::unique_ptr<FileSys::DirectoryBackend> backend; ///< File backend interface
  56. };
  57. /**
  58. * Opens an archive
  59. * @param id_code IdCode of the archive to open
  60. * @param archive_path Path to the archive, used with Binary paths
  61. * @return Handle to the opened archive
  62. */
  63. ResultVal<ArchiveHandle> OpenArchive(ArchiveIdCode id_code, FileSys::Path& archive_path);
  64. /**
  65. * Closes an archive
  66. * @param handle Handle to the archive to close
  67. */
  68. ResultCode CloseArchive(ArchiveHandle handle);
  69. /**
  70. * Registers an Archive type, instances of which can later be opened using its IdCode.
  71. * @param factory File system backend interface to the archive
  72. * @param id_code Id code used to access this type of archive
  73. */
  74. ResultCode RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factory, ArchiveIdCode id_code);
  75. /**
  76. * Open a File from an Archive
  77. * @param archive_handle Handle to an open Archive object
  78. * @param path Path to the File inside of the Archive
  79. * @param mode Mode under which to open the File
  80. * @return The opened File object as a Session
  81. */
  82. ResultVal<Kernel::SharedPtr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
  83. const FileSys::Path& path, const FileSys::Mode mode);
  84. /**
  85. * Delete a File from an Archive
  86. * @param archive_handle Handle to an open Archive object
  87. * @param path Path to the File inside of the Archive
  88. * @return Whether deletion succeeded
  89. */
  90. ResultCode DeleteFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path);
  91. /**
  92. * Rename a File between two Archives
  93. * @param src_archive_handle Handle to the source Archive object
  94. * @param src_path Path to the File inside of the source Archive
  95. * @param dest_archive_handle Handle to the destination Archive object
  96. * @param dest_path Path to the File inside of the destination Archive
  97. * @return Whether rename succeeded
  98. */
  99. ResultCode RenameFileBetweenArchives(ArchiveHandle src_archive_handle, const FileSys::Path& src_path,
  100. ArchiveHandle dest_archive_handle, const FileSys::Path& dest_path);
  101. /**
  102. * Delete a Directory from an Archive
  103. * @param archive_handle Handle to an open Archive object
  104. * @param path Path to the Directory inside of the Archive
  105. * @return Whether deletion succeeded
  106. */
  107. ResultCode DeleteDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path);
  108. /**
  109. * Create a File in an Archive
  110. * @param archive_handle Handle to an open Archive object
  111. * @param path Path to the File inside of the Archive
  112. * @param file_size The size of the new file, filled with zeroes
  113. * @return File creation result code
  114. */
  115. ResultCode CreateFileInArchive(ArchiveHandle archive_handle, const FileSys::Path& path, u64 file_size);
  116. /**
  117. * Create a Directory from an Archive
  118. * @param archive_handle Handle to an open Archive object
  119. * @param path Path to the Directory inside of the Archive
  120. * @return Whether creation of directory succeeded
  121. */
  122. ResultCode CreateDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path);
  123. /**
  124. * Rename a Directory between two Archives
  125. * @param src_archive_handle Handle to the source Archive object
  126. * @param src_path Path to the Directory inside of the source Archive
  127. * @param dest_archive_handle Handle to the destination Archive object
  128. * @param dest_path Path to the Directory inside of the destination Archive
  129. * @return Whether rename succeeded
  130. */
  131. ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle, const FileSys::Path& src_path,
  132. ArchiveHandle dest_archive_handle, const FileSys::Path& dest_path);
  133. /**
  134. * Open a Directory from an Archive
  135. * @param archive_handle Handle to an open Archive object
  136. * @param path Path to the Directory inside of the Archive
  137. * @return The opened Directory object as a Session
  138. */
  139. ResultVal<Kernel::SharedPtr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
  140. const FileSys::Path& path);
  141. /**
  142. * Get the free space in an Archive
  143. * @param archive_handle Handle to an open Archive object
  144. * @return The number of free bytes in the archive
  145. */
  146. ResultVal<u64> GetFreeBytesInArchive(ArchiveHandle archive_handle);
  147. /**
  148. * Erases the contents of the physical folder that contains the archive
  149. * identified by the specified id code and path
  150. * @param id_code The id of the archive to format
  151. * @param format_info Format information about the new archive
  152. * @param path The path to the archive, if relevant.
  153. * @return ResultCode 0 on success or the corresponding code on error
  154. */
  155. ResultCode FormatArchive(ArchiveIdCode id_code, const FileSys::ArchiveFormatInfo& format_info, const FileSys::Path& path = FileSys::Path());
  156. /**
  157. * Retrieves the format info about the archive of the specified type and path.
  158. * The format info is supplied by the client code when creating archives.
  159. * @param id_code The id of the archive
  160. * @param archive_path The path of the archive, if relevant
  161. * @return The format info of the archive, or the corresponding error code if failed.
  162. */
  163. ResultVal<FileSys::ArchiveFormatInfo> GetArchiveFormatInfo(ArchiveIdCode id_code, FileSys::Path& archive_path);
  164. /**
  165. * Creates a blank SharedExtSaveData archive for the specified extdata ID
  166. * @param media_type The media type of the archive to create (NAND / SDMC)
  167. * @param high The high word of the extdata id to create
  168. * @param low The low word of the extdata id to create
  169. * @param icon_buffer VAddr of the SMDH icon for this ExtSaveData
  170. * @param icon_size Size of the SMDH icon
  171. * @param format_info Format information about the new archive
  172. * @return ResultCode 0 on success or the corresponding code on error
  173. */
  174. ResultCode CreateExtSaveData(MediaType media_type, u32 high, u32 low, VAddr icon_buffer, u32 icon_size, const FileSys::ArchiveFormatInfo& format_info);
  175. /**
  176. * Deletes the SharedExtSaveData archive for the specified extdata ID
  177. * @param media_type The media type of the archive to delete (NAND / SDMC)
  178. * @param high The high word of the extdata id to delete
  179. * @param low The low word of the extdata id to delete
  180. * @return ResultCode 0 on success or the corresponding code on error
  181. */
  182. ResultCode DeleteExtSaveData(MediaType media_type, u32 high, u32 low);
  183. /**
  184. * Deletes the SystemSaveData archive folder for the specified save data id
  185. * @param high The high word of the SystemSaveData archive to delete
  186. * @param low The low word of the SystemSaveData archive to delete
  187. * @return ResultCode 0 on success or the corresponding code on error
  188. */
  189. ResultCode DeleteSystemSaveData(u32 high, u32 low);
  190. /**
  191. * Creates the SystemSaveData archive folder for the specified save data id
  192. * @param high The high word of the SystemSaveData archive to create
  193. * @param low The low word of the SystemSaveData archive to create
  194. * @return ResultCode 0 on success or the corresponding code on error
  195. */
  196. ResultCode CreateSystemSaveData(u32 high, u32 low);
  197. /// Initialize archives
  198. void ArchiveInit();
  199. /// Shutdown archives
  200. void ArchiveShutdown();
  201. /// Register all archive types
  202. void RegisterArchiveTypes();
  203. /// Unregister all archive types
  204. void UnregisterArchiveTypes();
  205. } // namespace FS
  206. } // namespace Service