archive.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstddef>
  5. #include <memory>
  6. #include <system_error>
  7. #include <type_traits>
  8. #include <unordered_map>
  9. #include <utility>
  10. #include <boost/container/flat_map.hpp>
  11. #include "common/assert.h"
  12. #include "common/common_types.h"
  13. #include "common/file_util.h"
  14. #include "common/logging/log.h"
  15. #include "core/file_sys/archive_backend.h"
  16. #include "core/file_sys/archive_extsavedata.h"
  17. #include "core/file_sys/archive_ncch.h"
  18. #include "core/file_sys/archive_other_savedata.h"
  19. #include "core/file_sys/archive_savedata.h"
  20. #include "core/file_sys/archive_sdmc.h"
  21. #include "core/file_sys/archive_sdmcwriteonly.h"
  22. #include "core/file_sys/archive_systemsavedata.h"
  23. #include "core/file_sys/directory_backend.h"
  24. #include "core/file_sys/file_backend.h"
  25. #include "core/hle/hle.h"
  26. #include "core/hle/result.h"
  27. #include "core/hle/service/fs/archive.h"
  28. #include "core/hle/service/fs/fs_user.h"
  29. #include "core/hle/service/service.h"
  30. #include "core/memory.h"
  31. // Specializes std::hash for ArchiveIdCode, so that we can use it in std::unordered_map.
  32. // Workaroung for libstdc++ bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60970
  33. namespace std {
  34. template <>
  35. struct hash<Service::FS::ArchiveIdCode> {
  36. typedef Service::FS::ArchiveIdCode argument_type;
  37. typedef std::size_t result_type;
  38. result_type operator()(const argument_type& id_code) const {
  39. typedef std::underlying_type<argument_type>::type Type;
  40. return std::hash<Type>()(static_cast<Type>(id_code));
  41. }
  42. };
  43. }
  44. /// TODO(Subv): Confirm length of these strings
  45. const std::string SYSTEM_ID = "00000000000000000000000000000000";
  46. const std::string SDCARD_ID = "00000000000000000000000000000000";
  47. namespace Service {
  48. namespace FS {
  49. // TODO: Verify code
  50. /// Returned when a function is passed an invalid handle.
  51. const ResultCode ERR_INVALID_HANDLE(ErrorDescription::InvalidHandle, ErrorModule::FS,
  52. ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
  53. /// Returned when a function is passed an invalid archive handle.
  54. const ResultCode ERR_INVALID_ARCHIVE_HANDLE(ErrorDescription::FS_ArchiveNotMounted, ErrorModule::FS,
  55. ErrorSummary::NotFound,
  56. ErrorLevel::Status); // 0xC8804465
  57. // Command to access archive file
  58. enum class FileCommand : u32 {
  59. Dummy1 = 0x000100C6,
  60. Control = 0x040100C4,
  61. OpenSubFile = 0x08010100,
  62. Read = 0x080200C2,
  63. Write = 0x08030102,
  64. GetSize = 0x08040000,
  65. SetSize = 0x08050080,
  66. GetAttributes = 0x08060000,
  67. SetAttributes = 0x08070040,
  68. Close = 0x08080000,
  69. Flush = 0x08090000,
  70. SetPriority = 0x080A0040,
  71. GetPriority = 0x080B0000,
  72. OpenLinkFile = 0x080C0000,
  73. };
  74. // Command to access directory
  75. enum class DirectoryCommand : u32 {
  76. Dummy1 = 0x000100C6,
  77. Control = 0x040100C4,
  78. Read = 0x08010042,
  79. Close = 0x08020000,
  80. };
  81. File::File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path)
  82. : path(path), priority(0), backend(std::move(backend)) {}
  83. File::~File() {}
  84. ResultVal<bool> File::SyncRequest() {
  85. u32* cmd_buff = Kernel::GetCommandBuffer();
  86. FileCommand cmd = static_cast<FileCommand>(cmd_buff[0]);
  87. switch (cmd) {
  88. // Read from file...
  89. case FileCommand::Read: {
  90. u64 offset = cmd_buff[1] | ((u64)cmd_buff[2]) << 32;
  91. u32 length = cmd_buff[3];
  92. u32 address = cmd_buff[5];
  93. LOG_TRACE(Service_FS, "Read %s %s: offset=0x%llx length=%d address=0x%x",
  94. GetTypeName().c_str(), GetName().c_str(), offset, length, address);
  95. if (offset + length > backend->GetSize()) {
  96. LOG_ERROR(Service_FS,
  97. "Reading from out of bounds offset=0x%llX length=0x%08X file_size=0x%llX",
  98. offset, length, backend->GetSize());
  99. }
  100. std::vector<u8> data(length);
  101. ResultVal<size_t> read = backend->Read(offset, data.size(), data.data());
  102. if (read.Failed()) {
  103. cmd_buff[1] = read.Code().raw;
  104. return read.Code();
  105. }
  106. Memory::WriteBlock(address, data.data(), *read);
  107. cmd_buff[2] = static_cast<u32>(*read);
  108. break;
  109. }
  110. // Write to file...
  111. case FileCommand::Write: {
  112. u64 offset = cmd_buff[1] | ((u64)cmd_buff[2]) << 32;
  113. u32 length = cmd_buff[3];
  114. u32 flush = cmd_buff[4];
  115. u32 address = cmd_buff[6];
  116. LOG_TRACE(Service_FS, "Write %s %s: offset=0x%llx length=%d address=0x%x, flush=0x%x",
  117. GetTypeName().c_str(), GetName().c_str(), offset, length, address, flush);
  118. std::vector<u8> data(length);
  119. Memory::ReadBlock(address, data.data(), data.size());
  120. ResultVal<size_t> written = backend->Write(offset, data.size(), flush != 0, data.data());
  121. if (written.Failed()) {
  122. cmd_buff[1] = written.Code().raw;
  123. return written.Code();
  124. }
  125. cmd_buff[2] = static_cast<u32>(*written);
  126. break;
  127. }
  128. case FileCommand::GetSize: {
  129. LOG_TRACE(Service_FS, "GetSize %s %s", GetTypeName().c_str(), GetName().c_str());
  130. u64 size = backend->GetSize();
  131. cmd_buff[2] = (u32)size;
  132. cmd_buff[3] = size >> 32;
  133. break;
  134. }
  135. case FileCommand::SetSize: {
  136. u64 size = cmd_buff[1] | ((u64)cmd_buff[2] << 32);
  137. LOG_TRACE(Service_FS, "SetSize %s %s size=%llu", GetTypeName().c_str(), GetName().c_str(),
  138. size);
  139. backend->SetSize(size);
  140. break;
  141. }
  142. case FileCommand::Close: {
  143. LOG_TRACE(Service_FS, "Close %s %s", GetTypeName().c_str(), GetName().c_str());
  144. backend->Close();
  145. break;
  146. }
  147. case FileCommand::Flush: {
  148. LOG_TRACE(Service_FS, "Flush");
  149. backend->Flush();
  150. break;
  151. }
  152. case FileCommand::OpenLinkFile: {
  153. LOG_WARNING(Service_FS, "(STUBBED) File command OpenLinkFile %s", GetName().c_str());
  154. cmd_buff[3] = Kernel::g_handle_table.Create(this).ValueOr(INVALID_HANDLE);
  155. break;
  156. }
  157. case FileCommand::SetPriority: {
  158. priority = cmd_buff[1];
  159. LOG_TRACE(Service_FS, "SetPriority %u", priority);
  160. break;
  161. }
  162. case FileCommand::GetPriority: {
  163. cmd_buff[2] = priority;
  164. LOG_TRACE(Service_FS, "GetPriority");
  165. break;
  166. }
  167. // Unknown command...
  168. default:
  169. LOG_ERROR(Service_FS, "Unknown command=0x%08X!", cmd);
  170. ResultCode error = UnimplementedFunction(ErrorModule::FS);
  171. cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that.
  172. return error;
  173. }
  174. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  175. return MakeResult<bool>(false);
  176. }
  177. Directory::Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend,
  178. const FileSys::Path& path)
  179. : path(path), backend(std::move(backend)) {}
  180. Directory::~Directory() {}
  181. ResultVal<bool> Directory::SyncRequest() {
  182. u32* cmd_buff = Kernel::GetCommandBuffer();
  183. DirectoryCommand cmd = static_cast<DirectoryCommand>(cmd_buff[0]);
  184. switch (cmd) {
  185. // Read from directory...
  186. case DirectoryCommand::Read: {
  187. u32 count = cmd_buff[1];
  188. u32 address = cmd_buff[3];
  189. std::vector<FileSys::Entry> entries(count);
  190. LOG_TRACE(Service_FS, "Read %s %s: count=%d", GetTypeName().c_str(), GetName().c_str(),
  191. count);
  192. // Number of entries actually read
  193. u32 read = backend->Read(entries.size(), entries.data());
  194. cmd_buff[2] = read;
  195. Memory::WriteBlock(address, entries.data(), read * sizeof(FileSys::Entry));
  196. break;
  197. }
  198. case DirectoryCommand::Close: {
  199. LOG_TRACE(Service_FS, "Close %s %s", GetTypeName().c_str(), GetName().c_str());
  200. backend->Close();
  201. break;
  202. }
  203. // Unknown command...
  204. default:
  205. LOG_ERROR(Service_FS, "Unknown command=0x%08X!", cmd);
  206. ResultCode error = UnimplementedFunction(ErrorModule::FS);
  207. cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that.
  208. return MakeResult<bool>(false);
  209. }
  210. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  211. return MakeResult<bool>(false);
  212. }
  213. ////////////////////////////////////////////////////////////////////////////////////////////////////
  214. using FileSys::ArchiveBackend;
  215. using FileSys::ArchiveFactory;
  216. /**
  217. * Map of registered archives, identified by id code. Once an archive is registered here, it is
  218. * never removed until UnregisterArchiveTypes is called.
  219. */
  220. static boost::container::flat_map<ArchiveIdCode, std::unique_ptr<ArchiveFactory>> id_code_map;
  221. /**
  222. * Map of active archive handles. Values are pointers to the archives in `idcode_map`.
  223. */
  224. static std::unordered_map<ArchiveHandle, std::unique_ptr<ArchiveBackend>> handle_map;
  225. static ArchiveHandle next_handle;
  226. static ArchiveBackend* GetArchive(ArchiveHandle handle) {
  227. auto itr = handle_map.find(handle);
  228. return (itr == handle_map.end()) ? nullptr : itr->second.get();
  229. }
  230. ResultVal<ArchiveHandle> OpenArchive(ArchiveIdCode id_code, FileSys::Path& archive_path) {
  231. LOG_TRACE(Service_FS, "Opening archive with id code 0x%08X", id_code);
  232. auto itr = id_code_map.find(id_code);
  233. if (itr == id_code_map.end()) {
  234. // TODO: Verify error against hardware
  235. return ResultCode(ErrorDescription::NotFound, ErrorModule::FS, ErrorSummary::NotFound,
  236. ErrorLevel::Permanent);
  237. }
  238. CASCADE_RESULT(std::unique_ptr<ArchiveBackend> res, itr->second->Open(archive_path));
  239. // This should never even happen in the first place with 64-bit handles,
  240. while (handle_map.count(next_handle) != 0) {
  241. ++next_handle;
  242. }
  243. handle_map.emplace(next_handle, std::move(res));
  244. return MakeResult<ArchiveHandle>(next_handle++);
  245. }
  246. ResultCode CloseArchive(ArchiveHandle handle) {
  247. if (handle_map.erase(handle) == 0)
  248. return ERR_INVALID_ARCHIVE_HANDLE;
  249. else
  250. return RESULT_SUCCESS;
  251. }
  252. // TODO(yuriks): This might be what the fs:REG service is for. See the Register/Unregister calls in
  253. // http://3dbrew.org/wiki/Filesystem_services#ProgramRegistry_service_.22fs:REG.22
  254. ResultCode RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factory,
  255. ArchiveIdCode id_code) {
  256. auto result = id_code_map.emplace(id_code, std::move(factory));
  257. bool inserted = result.second;
  258. ASSERT_MSG(inserted, "Tried to register more than one archive with same id code");
  259. auto& archive = result.first->second;
  260. LOG_DEBUG(Service_FS, "Registered archive %s with id code 0x%08X", archive->GetName().c_str(),
  261. id_code);
  262. return RESULT_SUCCESS;
  263. }
  264. ResultVal<Kernel::SharedPtr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
  265. const FileSys::Path& path,
  266. const FileSys::Mode mode) {
  267. ArchiveBackend* archive = GetArchive(archive_handle);
  268. if (archive == nullptr)
  269. return ERR_INVALID_ARCHIVE_HANDLE;
  270. auto backend = archive->OpenFile(path, mode);
  271. if (backend.Failed())
  272. return backend.Code();
  273. auto file = Kernel::SharedPtr<File>(new File(backend.MoveFrom(), path));
  274. return MakeResult<Kernel::SharedPtr<File>>(std::move(file));
  275. }
  276. ResultCode DeleteFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
  277. ArchiveBackend* archive = GetArchive(archive_handle);
  278. if (archive == nullptr)
  279. return ERR_INVALID_ARCHIVE_HANDLE;
  280. return archive->DeleteFile(path);
  281. }
  282. ResultCode RenameFileBetweenArchives(ArchiveHandle src_archive_handle,
  283. const FileSys::Path& src_path,
  284. ArchiveHandle dest_archive_handle,
  285. const FileSys::Path& dest_path) {
  286. ArchiveBackend* src_archive = GetArchive(src_archive_handle);
  287. ArchiveBackend* dest_archive = GetArchive(dest_archive_handle);
  288. if (src_archive == nullptr || dest_archive == nullptr)
  289. return ERR_INVALID_ARCHIVE_HANDLE;
  290. if (src_archive == dest_archive) {
  291. return src_archive->RenameFile(src_path, dest_path);
  292. } else {
  293. // TODO: Implement renaming across archives
  294. return UnimplementedFunction(ErrorModule::FS);
  295. }
  296. }
  297. ResultCode DeleteDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
  298. ArchiveBackend* archive = GetArchive(archive_handle);
  299. if (archive == nullptr)
  300. return ERR_INVALID_ARCHIVE_HANDLE;
  301. return archive->DeleteDirectory(path);
  302. }
  303. ResultCode DeleteDirectoryRecursivelyFromArchive(ArchiveHandle archive_handle,
  304. const FileSys::Path& path) {
  305. ArchiveBackend* archive = GetArchive(archive_handle);
  306. if (archive == nullptr)
  307. return ERR_INVALID_ARCHIVE_HANDLE;
  308. return archive->DeleteDirectoryRecursively(path);
  309. }
  310. ResultCode CreateFileInArchive(ArchiveHandle archive_handle, const FileSys::Path& path,
  311. u64 file_size) {
  312. ArchiveBackend* archive = GetArchive(archive_handle);
  313. if (archive == nullptr)
  314. return ERR_INVALID_ARCHIVE_HANDLE;
  315. return archive->CreateFile(path, file_size);
  316. }
  317. ResultCode CreateDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
  318. ArchiveBackend* archive = GetArchive(archive_handle);
  319. if (archive == nullptr)
  320. return ERR_INVALID_ARCHIVE_HANDLE;
  321. return archive->CreateDirectory(path);
  322. }
  323. ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
  324. const FileSys::Path& src_path,
  325. ArchiveHandle dest_archive_handle,
  326. const FileSys::Path& dest_path) {
  327. ArchiveBackend* src_archive = GetArchive(src_archive_handle);
  328. ArchiveBackend* dest_archive = GetArchive(dest_archive_handle);
  329. if (src_archive == nullptr || dest_archive == nullptr)
  330. return ERR_INVALID_ARCHIVE_HANDLE;
  331. if (src_archive == dest_archive) {
  332. return src_archive->RenameDirectory(src_path, dest_path);
  333. } else {
  334. // TODO: Implement renaming across archives
  335. return UnimplementedFunction(ErrorModule::FS);
  336. }
  337. }
  338. ResultVal<Kernel::SharedPtr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
  339. const FileSys::Path& path) {
  340. ArchiveBackend* archive = GetArchive(archive_handle);
  341. if (archive == nullptr)
  342. return ERR_INVALID_ARCHIVE_HANDLE;
  343. auto backend = archive->OpenDirectory(path);
  344. if (backend.Failed())
  345. return backend.Code();
  346. auto directory = Kernel::SharedPtr<Directory>(new Directory(backend.MoveFrom(), path));
  347. return MakeResult<Kernel::SharedPtr<Directory>>(std::move(directory));
  348. }
  349. ResultVal<u64> GetFreeBytesInArchive(ArchiveHandle archive_handle) {
  350. ArchiveBackend* archive = GetArchive(archive_handle);
  351. if (archive == nullptr)
  352. return ERR_INVALID_ARCHIVE_HANDLE;
  353. return MakeResult<u64>(archive->GetFreeBytes());
  354. }
  355. ResultCode FormatArchive(ArchiveIdCode id_code, const FileSys::ArchiveFormatInfo& format_info,
  356. const FileSys::Path& path) {
  357. auto archive_itr = id_code_map.find(id_code);
  358. if (archive_itr == id_code_map.end()) {
  359. return UnimplementedFunction(ErrorModule::FS); // TODO(Subv): Find the right error
  360. }
  361. return archive_itr->second->Format(path, format_info);
  362. }
  363. ResultVal<FileSys::ArchiveFormatInfo> GetArchiveFormatInfo(ArchiveIdCode id_code,
  364. FileSys::Path& archive_path) {
  365. auto archive = id_code_map.find(id_code);
  366. if (archive == id_code_map.end()) {
  367. return UnimplementedFunction(ErrorModule::FS); // TODO(Subv): Find the right error
  368. }
  369. return archive->second->GetFormatInfo(archive_path);
  370. }
  371. ResultCode CreateExtSaveData(MediaType media_type, u32 high, u32 low, VAddr icon_buffer,
  372. u32 icon_size, const FileSys::ArchiveFormatInfo& format_info) {
  373. // Construct the binary path to the archive first
  374. FileSys::Path path =
  375. FileSys::ConstructExtDataBinaryPath(static_cast<u32>(media_type), high, low);
  376. auto archive = id_code_map.find(media_type == MediaType::NAND ? ArchiveIdCode::SharedExtSaveData
  377. : ArchiveIdCode::ExtSaveData);
  378. if (archive == id_code_map.end()) {
  379. return UnimplementedFunction(ErrorModule::FS); // TODO(Subv): Find the right error
  380. }
  381. auto ext_savedata = static_cast<FileSys::ArchiveFactory_ExtSaveData*>(archive->second.get());
  382. ResultCode result = ext_savedata->Format(path, format_info);
  383. if (result.IsError())
  384. return result;
  385. if (!Memory::IsValidVirtualAddress(icon_buffer))
  386. return ResultCode(-1); // TODO(Subv): Find the right error code
  387. std::vector<u8> smdh_icon(icon_size);
  388. Memory::ReadBlock(icon_buffer, smdh_icon.data(), smdh_icon.size());
  389. ext_savedata->WriteIcon(path, smdh_icon.data(), smdh_icon.size());
  390. return RESULT_SUCCESS;
  391. }
  392. ResultCode DeleteExtSaveData(MediaType media_type, u32 high, u32 low) {
  393. // Construct the binary path to the archive first
  394. FileSys::Path path =
  395. FileSys::ConstructExtDataBinaryPath(static_cast<u32>(media_type), high, low);
  396. std::string media_type_directory;
  397. if (media_type == MediaType::NAND) {
  398. media_type_directory = FileUtil::GetUserPath(D_NAND_IDX);
  399. } else if (media_type == MediaType::SDMC) {
  400. media_type_directory = FileUtil::GetUserPath(D_SDMC_IDX);
  401. } else {
  402. LOG_ERROR(Service_FS, "Unsupported media type %u", media_type);
  403. return ResultCode(-1); // TODO(Subv): Find the right error code
  404. }
  405. // Delete all directories (/user, /boss) and the icon file.
  406. std::string base_path =
  407. FileSys::GetExtDataContainerPath(media_type_directory, media_type == MediaType::NAND);
  408. std::string extsavedata_path = FileSys::GetExtSaveDataPath(base_path, path);
  409. if (FileUtil::Exists(extsavedata_path) && !FileUtil::DeleteDirRecursively(extsavedata_path))
  410. return ResultCode(-1); // TODO(Subv): Find the right error code
  411. return RESULT_SUCCESS;
  412. }
  413. ResultCode DeleteSystemSaveData(u32 high, u32 low) {
  414. // Construct the binary path to the archive first
  415. FileSys::Path path = FileSys::ConstructSystemSaveDataBinaryPath(high, low);
  416. std::string nand_directory = FileUtil::GetUserPath(D_NAND_IDX);
  417. std::string base_path = FileSys::GetSystemSaveDataContainerPath(nand_directory);
  418. std::string systemsavedata_path = FileSys::GetSystemSaveDataPath(base_path, path);
  419. if (!FileUtil::DeleteDirRecursively(systemsavedata_path))
  420. return ResultCode(-1); // TODO(Subv): Find the right error code
  421. return RESULT_SUCCESS;
  422. }
  423. ResultCode CreateSystemSaveData(u32 high, u32 low) {
  424. // Construct the binary path to the archive first
  425. FileSys::Path path = FileSys::ConstructSystemSaveDataBinaryPath(high, low);
  426. std::string nand_directory = FileUtil::GetUserPath(D_NAND_IDX);
  427. std::string base_path = FileSys::GetSystemSaveDataContainerPath(nand_directory);
  428. std::string systemsavedata_path = FileSys::GetSystemSaveDataPath(base_path, path);
  429. if (!FileUtil::CreateFullPath(systemsavedata_path))
  430. return ResultCode(-1); // TODO(Subv): Find the right error code
  431. return RESULT_SUCCESS;
  432. }
  433. void RegisterArchiveTypes() {
  434. // TODO(Subv): Add the other archive types (see here for the known types:
  435. // http://3dbrew.org/wiki/FS:OpenArchive#Archive_idcodes).
  436. std::string sdmc_directory = FileUtil::GetUserPath(D_SDMC_IDX);
  437. std::string nand_directory = FileUtil::GetUserPath(D_NAND_IDX);
  438. auto sdmc_factory = std::make_unique<FileSys::ArchiveFactory_SDMC>(sdmc_directory);
  439. if (sdmc_factory->Initialize())
  440. RegisterArchiveType(std::move(sdmc_factory), ArchiveIdCode::SDMC);
  441. else
  442. LOG_ERROR(Service_FS, "Can't instantiate SDMC archive with path %s",
  443. sdmc_directory.c_str());
  444. auto sdmcwo_factory = std::make_unique<FileSys::ArchiveFactory_SDMCWriteOnly>(sdmc_directory);
  445. if (sdmcwo_factory->Initialize())
  446. RegisterArchiveType(std::move(sdmcwo_factory), ArchiveIdCode::SDMCWriteOnly);
  447. else
  448. LOG_ERROR(Service_FS, "Can't instantiate SDMCWriteOnly archive with path %s",
  449. sdmc_directory.c_str());
  450. // Create the SaveData archive
  451. auto sd_savedata_source = std::make_shared<FileSys::ArchiveSource_SDSaveData>(sdmc_directory);
  452. auto savedata_factory = std::make_unique<FileSys::ArchiveFactory_SaveData>(sd_savedata_source);
  453. RegisterArchiveType(std::move(savedata_factory), ArchiveIdCode::SaveData);
  454. auto other_savedata_permitted_factory =
  455. std::make_unique<FileSys::ArchiveFactory_OtherSaveDataPermitted>(sd_savedata_source);
  456. RegisterArchiveType(std::move(other_savedata_permitted_factory),
  457. ArchiveIdCode::OtherSaveDataPermitted);
  458. auto other_savedata_general_factory =
  459. std::make_unique<FileSys::ArchiveFactory_OtherSaveDataGeneral>(sd_savedata_source);
  460. RegisterArchiveType(std::move(other_savedata_general_factory),
  461. ArchiveIdCode::OtherSaveDataGeneral);
  462. auto extsavedata_factory =
  463. std::make_unique<FileSys::ArchiveFactory_ExtSaveData>(sdmc_directory, false);
  464. if (extsavedata_factory->Initialize())
  465. RegisterArchiveType(std::move(extsavedata_factory), ArchiveIdCode::ExtSaveData);
  466. else
  467. LOG_ERROR(Service_FS, "Can't instantiate ExtSaveData archive with path %s",
  468. extsavedata_factory->GetMountPoint().c_str());
  469. auto sharedextsavedata_factory =
  470. std::make_unique<FileSys::ArchiveFactory_ExtSaveData>(nand_directory, true);
  471. if (sharedextsavedata_factory->Initialize())
  472. RegisterArchiveType(std::move(sharedextsavedata_factory), ArchiveIdCode::SharedExtSaveData);
  473. else
  474. LOG_ERROR(Service_FS, "Can't instantiate SharedExtSaveData archive with path %s",
  475. sharedextsavedata_factory->GetMountPoint().c_str());
  476. // Create the NCCH archive, basically a small variation of the RomFS archive
  477. auto savedatacheck_factory = std::make_unique<FileSys::ArchiveFactory_NCCH>(nand_directory);
  478. RegisterArchiveType(std::move(savedatacheck_factory), ArchiveIdCode::NCCH);
  479. auto systemsavedata_factory =
  480. std::make_unique<FileSys::ArchiveFactory_SystemSaveData>(nand_directory);
  481. RegisterArchiveType(std::move(systemsavedata_factory), ArchiveIdCode::SystemSaveData);
  482. }
  483. void UnregisterArchiveTypes() {
  484. id_code_map.clear();
  485. }
  486. /// Initialize archives
  487. void ArchiveInit() {
  488. next_handle = 1;
  489. AddService(new FS::Interface);
  490. RegisterArchiveTypes();
  491. }
  492. /// Shutdown archives
  493. void ArchiveShutdown() {
  494. handle_map.clear();
  495. UnregisterArchiveTypes();
  496. }
  497. } // namespace FS
  498. } // namespace Service