archive.cpp 22 KB

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