archive.h 9.7 KB

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