archive.h 9.8 KB

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