archive.h 10 KB

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