archive.h 7.7 KB

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