archive.cpp 23 KB

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