archive.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/server_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. static constexpr char SYSTEM_ID[]{"00000000000000000000000000000000"};
  17. /// The scrambled SD card CID, also known as ID1
  18. static constexpr char SDCARD_ID[]{"00000000000000000000000000000000"};
  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. NCCH = 0x2345678A,
  31. OtherSaveDataGeneral = 0x567890B2,
  32. OtherSaveDataPermitted = 0x567890B4,
  33. };
  34. /// Media types for the archives
  35. enum class MediaType : u32 { NAND = 0, SDMC = 1, GameCard = 2 };
  36. typedef u64 ArchiveHandle;
  37. class File final : public SessionRequestHandler, public std::enable_shared_from_this<File> {
  38. public:
  39. File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path);
  40. ~File();
  41. std::string GetName() const {
  42. return "Path: " + path.DebugStr();
  43. }
  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. protected:
  48. void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
  49. };
  50. class Directory final : public SessionRequestHandler {
  51. public:
  52. Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend, const FileSys::Path& path);
  53. ~Directory();
  54. std::string GetName() const {
  55. return "Directory: " + path.DebugStr();
  56. }
  57. FileSys::Path path; ///< Path of the directory
  58. std::unique_ptr<FileSys::DirectoryBackend> backend; ///< File backend interface
  59. protected:
  60. void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
  61. };
  62. /**
  63. * Opens an archive
  64. * @param id_code IdCode of the archive to open
  65. * @param archive_path Path to the archive, used with Binary paths
  66. * @return Handle to the opened archive
  67. */
  68. ResultVal<ArchiveHandle> OpenArchive(ArchiveIdCode id_code, FileSys::Path& archive_path);
  69. /**
  70. * Closes an archive
  71. * @param handle Handle to the archive to close
  72. */
  73. ResultCode CloseArchive(ArchiveHandle handle);
  74. /**
  75. * Registers an Archive type, instances of which can later be opened using its IdCode.
  76. * @param factory File system backend interface to the archive
  77. * @param id_code Id code used to access this type of archive
  78. */
  79. ResultCode RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factory,
  80. ArchiveIdCode id_code);
  81. /**
  82. * Open a File from an Archive
  83. * @param archive_handle Handle to an open Archive object
  84. * @param path Path to the File inside of the Archive
  85. * @param mode Mode under which to open the File
  86. * @return The opened File object
  87. */
  88. ResultVal<std::shared_ptr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
  89. const FileSys::Path& path,
  90. const FileSys::Mode mode);
  91. /**
  92. * Delete a File from an Archive
  93. * @param archive_handle Handle to an open Archive object
  94. * @param path Path to the File inside of the Archive
  95. * @return Whether deletion succeeded
  96. */
  97. ResultCode DeleteFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path);
  98. /**
  99. * Rename a File between two Archives
  100. * @param src_archive_handle Handle to the source Archive object
  101. * @param src_path Path to the File inside of the source Archive
  102. * @param dest_archive_handle Handle to the destination Archive object
  103. * @param dest_path Path to the File inside of the destination Archive
  104. * @return Whether rename succeeded
  105. */
  106. ResultCode RenameFileBetweenArchives(ArchiveHandle src_archive_handle,
  107. const FileSys::Path& src_path,
  108. ArchiveHandle dest_archive_handle,
  109. const FileSys::Path& dest_path);
  110. /**
  111. * Delete a Directory from an Archive
  112. * @param archive_handle Handle to an open Archive object
  113. * @param path Path to the Directory inside of the Archive
  114. * @return Whether deletion succeeded
  115. */
  116. ResultCode DeleteDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path);
  117. /**
  118. * Delete a Directory and anything under it from an Archive
  119. * @param archive_handle Handle to an open Archive object
  120. * @param path Path to the Directory inside of the Archive
  121. * @return Whether deletion succeeded
  122. */
  123. ResultCode DeleteDirectoryRecursivelyFromArchive(ArchiveHandle archive_handle,
  124. const FileSys::Path& path);
  125. /**
  126. * Create a File in an Archive
  127. * @param archive_handle Handle to an open Archive object
  128. * @param path Path to the File inside of the Archive
  129. * @param file_size The size of the new file, filled with zeroes
  130. * @return File creation result code
  131. */
  132. ResultCode CreateFileInArchive(ArchiveHandle archive_handle, const FileSys::Path& path,
  133. u64 file_size);
  134. /**
  135. * Create a Directory from an Archive
  136. * @param archive_handle Handle to an open Archive object
  137. * @param path Path to the Directory inside of the Archive
  138. * @return Whether creation of directory succeeded
  139. */
  140. ResultCode CreateDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path);
  141. /**
  142. * Rename a Directory between two Archives
  143. * @param src_archive_handle Handle to the source Archive object
  144. * @param src_path Path to the Directory inside of the source Archive
  145. * @param dest_archive_handle Handle to the destination Archive object
  146. * @param dest_path Path to the Directory inside of the destination Archive
  147. * @return Whether rename succeeded
  148. */
  149. ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
  150. const FileSys::Path& src_path,
  151. ArchiveHandle dest_archive_handle,
  152. const FileSys::Path& dest_path);
  153. /**
  154. * Open a Directory from an Archive
  155. * @param archive_handle Handle to an open Archive object
  156. * @param path Path to the Directory inside of the Archive
  157. * @return The opened Directory object
  158. */
  159. ResultVal<std::shared_ptr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
  160. const FileSys::Path& path);
  161. /**
  162. * Get the free space in an Archive
  163. * @param archive_handle Handle to an open Archive object
  164. * @return The number of free bytes in the archive
  165. */
  166. ResultVal<u64> GetFreeBytesInArchive(ArchiveHandle archive_handle);
  167. /**
  168. * Erases the contents of the physical folder that contains the archive
  169. * identified by the specified id code and path
  170. * @param id_code The id of the archive to format
  171. * @param format_info Format information about the new archive
  172. * @param path The path to the archive, if relevant.
  173. * @return ResultCode 0 on success or the corresponding code on error
  174. */
  175. ResultCode FormatArchive(ArchiveIdCode id_code, const FileSys::ArchiveFormatInfo& format_info,
  176. const FileSys::Path& path = FileSys::Path());
  177. /**
  178. * Retrieves the format info about the archive of the specified type and path.
  179. * The format info is supplied by the client code when creating archives.
  180. * @param id_code The id of the archive
  181. * @param archive_path The path of the archive, if relevant
  182. * @return The format info of the archive, or the corresponding error code if failed.
  183. */
  184. ResultVal<FileSys::ArchiveFormatInfo> GetArchiveFormatInfo(ArchiveIdCode id_code,
  185. FileSys::Path& archive_path);
  186. /**
  187. * Creates a blank SharedExtSaveData archive for the specified extdata ID
  188. * @param media_type The media type of the archive to create (NAND / SDMC)
  189. * @param high The high word of the extdata id to create
  190. * @param low The low word of the extdata id to create
  191. * @param icon_buffer VAddr of the SMDH icon for this ExtSaveData
  192. * @param icon_size Size of the SMDH icon
  193. * @param format_info Format information about the new archive
  194. * @return ResultCode 0 on success or the corresponding code on error
  195. */
  196. ResultCode CreateExtSaveData(MediaType media_type, u32 high, u32 low, VAddr icon_buffer,
  197. u32 icon_size, const FileSys::ArchiveFormatInfo& format_info);
  198. /**
  199. * Deletes the SharedExtSaveData archive for the specified extdata ID
  200. * @param media_type The media type of the archive to delete (NAND / SDMC)
  201. * @param high The high word of the extdata id to delete
  202. * @param low The low word of the extdata id to delete
  203. * @return ResultCode 0 on success or the corresponding code on error
  204. */
  205. ResultCode DeleteExtSaveData(MediaType media_type, u32 high, u32 low);
  206. /**
  207. * Deletes the SystemSaveData archive folder for the specified save data id
  208. * @param high The high word of the SystemSaveData archive to delete
  209. * @param low The low word of the SystemSaveData archive to delete
  210. * @return ResultCode 0 on success or the corresponding code on error
  211. */
  212. ResultCode DeleteSystemSaveData(u32 high, u32 low);
  213. /**
  214. * Creates the SystemSaveData archive folder for the specified save data id
  215. * @param high The high word of the SystemSaveData archive to create
  216. * @param low The low word of the SystemSaveData archive to create
  217. * @return ResultCode 0 on success or the corresponding code on error
  218. */
  219. ResultCode CreateSystemSaveData(u32 high, u32 low);
  220. /// Initialize archives
  221. void ArchiveInit();
  222. /// Shutdown archives
  223. void ArchiveShutdown();
  224. /// Register all archive types
  225. void RegisterArchiveTypes();
  226. /// Unregister all archive types
  227. void UnregisterArchiveTypes();
  228. } // namespace FS
  229. } // namespace Service