filesystem.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <utility>
  4. #include "common/assert.h"
  5. #include "common/fs/path_util.h"
  6. #include "common/settings.h"
  7. #include "core/core.h"
  8. #include "core/file_sys/bis_factory.h"
  9. #include "core/file_sys/card_image.h"
  10. #include "core/file_sys/control_metadata.h"
  11. #include "core/file_sys/errors.h"
  12. #include "core/file_sys/mode.h"
  13. #include "core/file_sys/patch_manager.h"
  14. #include "core/file_sys/registered_cache.h"
  15. #include "core/file_sys/romfs_factory.h"
  16. #include "core/file_sys/savedata_factory.h"
  17. #include "core/file_sys/sdmc_factory.h"
  18. #include "core/file_sys/vfs.h"
  19. #include "core/file_sys/vfs_offset.h"
  20. #include "core/hle/service/filesystem/filesystem.h"
  21. #include "core/hle/service/filesystem/fsp_ldr.h"
  22. #include "core/hle/service/filesystem/fsp_pr.h"
  23. #include "core/hle/service/filesystem/fsp_srv.h"
  24. #include "core/hle/service/server_manager.h"
  25. #include "core/loader/loader.h"
  26. namespace Service::FileSystem {
  27. // A default size for normal/journal save data size if application control metadata cannot be found.
  28. // This should be large enough to satisfy even the most extreme requirements (~4.2GB)
  29. constexpr u64 SUFFICIENT_SAVE_DATA_SIZE = 0xF0000000;
  30. static FileSys::VirtualDir GetDirectoryRelativeWrapped(FileSys::VirtualDir base,
  31. std::string_view dir_name_) {
  32. std::string dir_name(Common::FS::SanitizePath(dir_name_));
  33. if (dir_name.empty() || dir_name == "." || dir_name == "/" || dir_name == "\\")
  34. return base;
  35. return base->GetDirectoryRelative(dir_name);
  36. }
  37. VfsDirectoryServiceWrapper::VfsDirectoryServiceWrapper(FileSys::VirtualDir backing_)
  38. : backing(std::move(backing_)) {}
  39. VfsDirectoryServiceWrapper::~VfsDirectoryServiceWrapper() = default;
  40. std::string VfsDirectoryServiceWrapper::GetName() const {
  41. return backing->GetName();
  42. }
  43. Result VfsDirectoryServiceWrapper::CreateFile(const std::string& path_, u64 size) const {
  44. std::string path(Common::FS::SanitizePath(path_));
  45. auto dir = GetDirectoryRelativeWrapped(backing, Common::FS::GetParentPath(path));
  46. if (dir == nullptr) {
  47. return FileSys::ERROR_PATH_NOT_FOUND;
  48. }
  49. FileSys::EntryType entry_type{};
  50. if (GetEntryType(&entry_type, path) == ResultSuccess) {
  51. return FileSys::ERROR_PATH_ALREADY_EXISTS;
  52. }
  53. auto file = dir->CreateFile(Common::FS::GetFilename(path));
  54. if (file == nullptr) {
  55. // TODO(DarkLordZach): Find a better error code for this
  56. return ResultUnknown;
  57. }
  58. if (!file->Resize(size)) {
  59. // TODO(DarkLordZach): Find a better error code for this
  60. return ResultUnknown;
  61. }
  62. return ResultSuccess;
  63. }
  64. Result VfsDirectoryServiceWrapper::DeleteFile(const std::string& path_) const {
  65. std::string path(Common::FS::SanitizePath(path_));
  66. if (path.empty()) {
  67. // TODO(DarkLordZach): Why do games call this and what should it do? Works as is but...
  68. return ResultSuccess;
  69. }
  70. auto dir = GetDirectoryRelativeWrapped(backing, Common::FS::GetParentPath(path));
  71. if (dir == nullptr || dir->GetFile(Common::FS::GetFilename(path)) == nullptr) {
  72. return FileSys::ERROR_PATH_NOT_FOUND;
  73. }
  74. if (!dir->DeleteFile(Common::FS::GetFilename(path))) {
  75. // TODO(DarkLordZach): Find a better error code for this
  76. return ResultUnknown;
  77. }
  78. return ResultSuccess;
  79. }
  80. Result VfsDirectoryServiceWrapper::CreateDirectory(const std::string& path_) const {
  81. std::string path(Common::FS::SanitizePath(path_));
  82. // NOTE: This is inaccurate behavior. CreateDirectory is not recursive.
  83. // CreateDirectory should return PathNotFound if the parent directory does not exist.
  84. // This is here temporarily in order to have UMM "work" in the meantime.
  85. // TODO (Morph): Remove this when a hardware test verifies the correct behavior.
  86. const auto components = Common::FS::SplitPathComponents(path);
  87. std::string relative_path;
  88. for (const auto& component : components) {
  89. // Skip empty path components
  90. if (component.empty()) {
  91. continue;
  92. }
  93. relative_path = Common::FS::SanitizePath(relative_path + '/' + component);
  94. auto new_dir = backing->CreateSubdirectory(relative_path);
  95. if (new_dir == nullptr) {
  96. // TODO(DarkLordZach): Find a better error code for this
  97. return ResultUnknown;
  98. }
  99. }
  100. return ResultSuccess;
  101. }
  102. Result VfsDirectoryServiceWrapper::DeleteDirectory(const std::string& path_) const {
  103. std::string path(Common::FS::SanitizePath(path_));
  104. auto dir = GetDirectoryRelativeWrapped(backing, Common::FS::GetParentPath(path));
  105. if (!dir->DeleteSubdirectory(Common::FS::GetFilename(path))) {
  106. // TODO(DarkLordZach): Find a better error code for this
  107. return ResultUnknown;
  108. }
  109. return ResultSuccess;
  110. }
  111. Result VfsDirectoryServiceWrapper::DeleteDirectoryRecursively(const std::string& path_) const {
  112. std::string path(Common::FS::SanitizePath(path_));
  113. auto dir = GetDirectoryRelativeWrapped(backing, Common::FS::GetParentPath(path));
  114. if (!dir->DeleteSubdirectoryRecursive(Common::FS::GetFilename(path))) {
  115. // TODO(DarkLordZach): Find a better error code for this
  116. return ResultUnknown;
  117. }
  118. return ResultSuccess;
  119. }
  120. Result VfsDirectoryServiceWrapper::CleanDirectoryRecursively(const std::string& path) const {
  121. const std::string sanitized_path(Common::FS::SanitizePath(path));
  122. auto dir = GetDirectoryRelativeWrapped(backing, Common::FS::GetParentPath(sanitized_path));
  123. if (!dir->CleanSubdirectoryRecursive(Common::FS::GetFilename(sanitized_path))) {
  124. // TODO(DarkLordZach): Find a better error code for this
  125. return ResultUnknown;
  126. }
  127. return ResultSuccess;
  128. }
  129. Result VfsDirectoryServiceWrapper::RenameFile(const std::string& src_path_,
  130. const std::string& dest_path_) const {
  131. std::string src_path(Common::FS::SanitizePath(src_path_));
  132. std::string dest_path(Common::FS::SanitizePath(dest_path_));
  133. auto src = backing->GetFileRelative(src_path);
  134. if (Common::FS::GetParentPath(src_path) == Common::FS::GetParentPath(dest_path)) {
  135. // Use more-optimized vfs implementation rename.
  136. if (src == nullptr)
  137. return FileSys::ERROR_PATH_NOT_FOUND;
  138. if (!src->Rename(Common::FS::GetFilename(dest_path))) {
  139. // TODO(DarkLordZach): Find a better error code for this
  140. return ResultUnknown;
  141. }
  142. return ResultSuccess;
  143. }
  144. // Move by hand -- TODO(DarkLordZach): Optimize
  145. auto c_res = CreateFile(dest_path, src->GetSize());
  146. if (c_res != ResultSuccess)
  147. return c_res;
  148. auto dest = backing->GetFileRelative(dest_path);
  149. ASSERT_MSG(dest != nullptr, "Newly created file with success cannot be found.");
  150. ASSERT_MSG(dest->WriteBytes(src->ReadAllBytes()) == src->GetSize(),
  151. "Could not write all of the bytes but everything else has succeeded.");
  152. if (!src->GetContainingDirectory()->DeleteFile(Common::FS::GetFilename(src_path))) {
  153. // TODO(DarkLordZach): Find a better error code for this
  154. return ResultUnknown;
  155. }
  156. return ResultSuccess;
  157. }
  158. Result VfsDirectoryServiceWrapper::RenameDirectory(const std::string& src_path_,
  159. const std::string& dest_path_) const {
  160. std::string src_path(Common::FS::SanitizePath(src_path_));
  161. std::string dest_path(Common::FS::SanitizePath(dest_path_));
  162. auto src = GetDirectoryRelativeWrapped(backing, src_path);
  163. if (Common::FS::GetParentPath(src_path) == Common::FS::GetParentPath(dest_path)) {
  164. // Use more-optimized vfs implementation rename.
  165. if (src == nullptr)
  166. return FileSys::ERROR_PATH_NOT_FOUND;
  167. if (!src->Rename(Common::FS::GetFilename(dest_path))) {
  168. // TODO(DarkLordZach): Find a better error code for this
  169. return ResultUnknown;
  170. }
  171. return ResultSuccess;
  172. }
  173. // TODO(DarkLordZach): Implement renaming across the tree (move).
  174. ASSERT_MSG(false,
  175. "Could not rename directory with path \"{}\" to new path \"{}\" because parent dirs "
  176. "don't match -- UNIMPLEMENTED",
  177. src_path, dest_path);
  178. // TODO(DarkLordZach): Find a better error code for this
  179. return ResultUnknown;
  180. }
  181. Result VfsDirectoryServiceWrapper::OpenFile(FileSys::VirtualFile* out_file,
  182. const std::string& path_, FileSys::Mode mode) const {
  183. const std::string path(Common::FS::SanitizePath(path_));
  184. std::string_view npath = path;
  185. while (!npath.empty() && (npath[0] == '/' || npath[0] == '\\')) {
  186. npath.remove_prefix(1);
  187. }
  188. auto file = backing->GetFileRelative(npath);
  189. if (file == nullptr) {
  190. return FileSys::ERROR_PATH_NOT_FOUND;
  191. }
  192. if (mode == FileSys::Mode::Append) {
  193. *out_file = std::make_shared<FileSys::OffsetVfsFile>(file, 0, file->GetSize());
  194. } else {
  195. *out_file = file;
  196. }
  197. return ResultSuccess;
  198. }
  199. Result VfsDirectoryServiceWrapper::OpenDirectory(FileSys::VirtualDir* out_directory,
  200. const std::string& path_) {
  201. std::string path(Common::FS::SanitizePath(path_));
  202. auto dir = GetDirectoryRelativeWrapped(backing, path);
  203. if (dir == nullptr) {
  204. // TODO(DarkLordZach): Find a better error code for this
  205. return FileSys::ERROR_PATH_NOT_FOUND;
  206. }
  207. *out_directory = dir;
  208. return ResultSuccess;
  209. }
  210. Result VfsDirectoryServiceWrapper::GetEntryType(FileSys::EntryType* out_entry_type,
  211. const std::string& path_) const {
  212. std::string path(Common::FS::SanitizePath(path_));
  213. auto dir = GetDirectoryRelativeWrapped(backing, Common::FS::GetParentPath(path));
  214. if (dir == nullptr) {
  215. return FileSys::ERROR_PATH_NOT_FOUND;
  216. }
  217. auto filename = Common::FS::GetFilename(path);
  218. // TODO(Subv): Some games use the '/' path, find out what this means.
  219. if (filename.empty()) {
  220. *out_entry_type = FileSys::EntryType::Directory;
  221. return ResultSuccess;
  222. }
  223. if (dir->GetFile(filename) != nullptr) {
  224. *out_entry_type = FileSys::EntryType::File;
  225. return ResultSuccess;
  226. }
  227. if (dir->GetSubdirectory(filename) != nullptr) {
  228. *out_entry_type = FileSys::EntryType::Directory;
  229. return ResultSuccess;
  230. }
  231. return FileSys::ERROR_PATH_NOT_FOUND;
  232. }
  233. Result VfsDirectoryServiceWrapper::GetFileTimeStampRaw(
  234. FileSys::FileTimeStampRaw* out_file_time_stamp_raw, const std::string& path) const {
  235. auto dir = GetDirectoryRelativeWrapped(backing, Common::FS::GetParentPath(path));
  236. if (dir == nullptr) {
  237. return FileSys::ERROR_PATH_NOT_FOUND;
  238. }
  239. FileSys::EntryType entry_type;
  240. if (GetEntryType(&entry_type, path) != ResultSuccess) {
  241. return FileSys::ERROR_PATH_NOT_FOUND;
  242. }
  243. *out_file_time_stamp_raw = dir->GetFileTimeStamp(Common::FS::GetFilename(path));
  244. return ResultSuccess;
  245. }
  246. FileSystemController::FileSystemController(Core::System& system_) : system{system_} {}
  247. FileSystemController::~FileSystemController() = default;
  248. Result FileSystemController::RegisterRomFS(std::unique_ptr<FileSys::RomFSFactory>&& factory) {
  249. romfs_factory = std::move(factory);
  250. LOG_DEBUG(Service_FS, "Registered RomFS");
  251. return ResultSuccess;
  252. }
  253. Result FileSystemController::RegisterSaveData(std::unique_ptr<FileSys::SaveDataFactory>&& factory) {
  254. ASSERT_MSG(save_data_factory == nullptr, "Tried to register a second save data");
  255. save_data_factory = std::move(factory);
  256. LOG_DEBUG(Service_FS, "Registered save data");
  257. return ResultSuccess;
  258. }
  259. Result FileSystemController::RegisterSDMC(std::unique_ptr<FileSys::SDMCFactory>&& factory) {
  260. ASSERT_MSG(sdmc_factory == nullptr, "Tried to register a second SDMC");
  261. sdmc_factory = std::move(factory);
  262. LOG_DEBUG(Service_FS, "Registered SDMC");
  263. return ResultSuccess;
  264. }
  265. Result FileSystemController::RegisterBIS(std::unique_ptr<FileSys::BISFactory>&& factory) {
  266. ASSERT_MSG(bis_factory == nullptr, "Tried to register a second BIS");
  267. bis_factory = std::move(factory);
  268. LOG_DEBUG(Service_FS, "Registered BIS");
  269. return ResultSuccess;
  270. }
  271. void FileSystemController::SetPackedUpdate(FileSys::VirtualFile update_raw) {
  272. LOG_TRACE(Service_FS, "Setting packed update for romfs");
  273. if (romfs_factory == nullptr)
  274. return;
  275. romfs_factory->SetPackedUpdate(std::move(update_raw));
  276. }
  277. FileSys::VirtualFile FileSystemController::OpenRomFSCurrentProcess() const {
  278. LOG_TRACE(Service_FS, "Opening RomFS for current process");
  279. if (romfs_factory == nullptr) {
  280. return nullptr;
  281. }
  282. return romfs_factory->OpenCurrentProcess(system.GetApplicationProcessProgramID());
  283. }
  284. FileSys::VirtualFile FileSystemController::OpenPatchedRomFS(u64 title_id,
  285. FileSys::ContentRecordType type) const {
  286. LOG_TRACE(Service_FS, "Opening patched RomFS for title_id={:016X}", title_id);
  287. if (romfs_factory == nullptr) {
  288. return nullptr;
  289. }
  290. return romfs_factory->OpenPatchedRomFS(title_id, type);
  291. }
  292. FileSys::VirtualFile FileSystemController::OpenPatchedRomFSWithProgramIndex(
  293. u64 title_id, u8 program_index, FileSys::ContentRecordType type) const {
  294. LOG_TRACE(Service_FS, "Opening patched RomFS for title_id={:016X}, program_index={}", title_id,
  295. program_index);
  296. if (romfs_factory == nullptr) {
  297. return nullptr;
  298. }
  299. return romfs_factory->OpenPatchedRomFSWithProgramIndex(title_id, program_index, type);
  300. }
  301. FileSys::VirtualFile FileSystemController::OpenRomFS(u64 title_id, FileSys::StorageId storage_id,
  302. FileSys::ContentRecordType type) const {
  303. LOG_TRACE(Service_FS, "Opening RomFS for title_id={:016X}, storage_id={:02X}, type={:02X}",
  304. title_id, storage_id, type);
  305. if (romfs_factory == nullptr) {
  306. return nullptr;
  307. }
  308. return romfs_factory->Open(title_id, storage_id, type);
  309. }
  310. Result FileSystemController::CreateSaveData(FileSys::VirtualDir* out_save_data,
  311. FileSys::SaveDataSpaceId space,
  312. const FileSys::SaveDataAttribute& save_struct) const {
  313. LOG_TRACE(Service_FS, "Creating Save Data for space_id={:01X}, save_struct={}", space,
  314. save_struct.DebugInfo());
  315. if (save_data_factory == nullptr) {
  316. return FileSys::ERROR_ENTITY_NOT_FOUND;
  317. }
  318. auto save_data = save_data_factory->Create(space, save_struct);
  319. if (save_data == nullptr) {
  320. return FileSys::ERROR_ENTITY_NOT_FOUND;
  321. }
  322. *out_save_data = save_data;
  323. return ResultSuccess;
  324. }
  325. Result FileSystemController::OpenSaveData(FileSys::VirtualDir* out_save_data,
  326. FileSys::SaveDataSpaceId space,
  327. const FileSys::SaveDataAttribute& attribute) const {
  328. LOG_TRACE(Service_FS, "Opening Save Data for space_id={:01X}, save_struct={}", space,
  329. attribute.DebugInfo());
  330. if (save_data_factory == nullptr) {
  331. return FileSys::ERROR_ENTITY_NOT_FOUND;
  332. }
  333. auto save_data = save_data_factory->Open(space, attribute);
  334. if (save_data == nullptr) {
  335. return FileSys::ERROR_ENTITY_NOT_FOUND;
  336. }
  337. *out_save_data = save_data;
  338. return ResultSuccess;
  339. }
  340. Result FileSystemController::OpenSaveDataSpace(FileSys::VirtualDir* out_save_data_space,
  341. FileSys::SaveDataSpaceId space) const {
  342. LOG_TRACE(Service_FS, "Opening Save Data Space for space_id={:01X}", space);
  343. if (save_data_factory == nullptr) {
  344. return FileSys::ERROR_ENTITY_NOT_FOUND;
  345. }
  346. auto save_data_space = save_data_factory->GetSaveDataSpaceDirectory(space);
  347. if (save_data_space == nullptr) {
  348. return FileSys::ERROR_ENTITY_NOT_FOUND;
  349. }
  350. *out_save_data_space = save_data_space;
  351. return ResultSuccess;
  352. }
  353. Result FileSystemController::OpenSDMC(FileSys::VirtualDir* out_sdmc) const {
  354. LOG_TRACE(Service_FS, "Opening SDMC");
  355. if (sdmc_factory == nullptr) {
  356. return FileSys::ERROR_SD_CARD_NOT_FOUND;
  357. }
  358. auto sdmc = sdmc_factory->Open();
  359. if (sdmc == nullptr) {
  360. return FileSys::ERROR_SD_CARD_NOT_FOUND;
  361. }
  362. *out_sdmc = sdmc;
  363. return ResultSuccess;
  364. }
  365. Result FileSystemController::OpenBISPartition(FileSys::VirtualDir* out_bis_partition,
  366. FileSys::BisPartitionId id) const {
  367. LOG_TRACE(Service_FS, "Opening BIS Partition with id={:08X}", id);
  368. if (bis_factory == nullptr) {
  369. return FileSys::ERROR_ENTITY_NOT_FOUND;
  370. }
  371. auto part = bis_factory->OpenPartition(id);
  372. if (part == nullptr) {
  373. return FileSys::ERROR_INVALID_ARGUMENT;
  374. }
  375. *out_bis_partition = part;
  376. return ResultSuccess;
  377. }
  378. Result FileSystemController::OpenBISPartitionStorage(
  379. FileSys::VirtualFile* out_bis_partition_storage, FileSys::BisPartitionId id) const {
  380. LOG_TRACE(Service_FS, "Opening BIS Partition Storage with id={:08X}", id);
  381. if (bis_factory == nullptr) {
  382. return FileSys::ERROR_ENTITY_NOT_FOUND;
  383. }
  384. auto part = bis_factory->OpenPartitionStorage(id, system.GetFilesystem());
  385. if (part == nullptr) {
  386. return FileSys::ERROR_INVALID_ARGUMENT;
  387. }
  388. *out_bis_partition_storage = part;
  389. return ResultSuccess;
  390. }
  391. u64 FileSystemController::GetFreeSpaceSize(FileSys::StorageId id) const {
  392. switch (id) {
  393. case FileSys::StorageId::None:
  394. case FileSys::StorageId::GameCard:
  395. return 0;
  396. case FileSys::StorageId::SdCard:
  397. if (sdmc_factory == nullptr)
  398. return 0;
  399. return sdmc_factory->GetSDMCFreeSpace();
  400. case FileSys::StorageId::Host:
  401. if (bis_factory == nullptr)
  402. return 0;
  403. return bis_factory->GetSystemNANDFreeSpace() + bis_factory->GetUserNANDFreeSpace();
  404. case FileSys::StorageId::NandSystem:
  405. if (bis_factory == nullptr)
  406. return 0;
  407. return bis_factory->GetSystemNANDFreeSpace();
  408. case FileSys::StorageId::NandUser:
  409. if (bis_factory == nullptr)
  410. return 0;
  411. return bis_factory->GetUserNANDFreeSpace();
  412. }
  413. return 0;
  414. }
  415. u64 FileSystemController::GetTotalSpaceSize(FileSys::StorageId id) const {
  416. switch (id) {
  417. case FileSys::StorageId::None:
  418. case FileSys::StorageId::GameCard:
  419. return 0;
  420. case FileSys::StorageId::SdCard:
  421. if (sdmc_factory == nullptr)
  422. return 0;
  423. return sdmc_factory->GetSDMCTotalSpace();
  424. case FileSys::StorageId::Host:
  425. if (bis_factory == nullptr)
  426. return 0;
  427. return bis_factory->GetFullNANDTotalSpace();
  428. case FileSys::StorageId::NandSystem:
  429. if (bis_factory == nullptr)
  430. return 0;
  431. return bis_factory->GetSystemNANDTotalSpace();
  432. case FileSys::StorageId::NandUser:
  433. if (bis_factory == nullptr)
  434. return 0;
  435. return bis_factory->GetUserNANDTotalSpace();
  436. }
  437. return 0;
  438. }
  439. FileSys::SaveDataSize FileSystemController::ReadSaveDataSize(FileSys::SaveDataType type,
  440. u64 title_id, u128 user_id) const {
  441. if (save_data_factory == nullptr) {
  442. return {0, 0};
  443. }
  444. const auto value = save_data_factory->ReadSaveDataSize(type, title_id, user_id);
  445. if (value.normal == 0 && value.journal == 0) {
  446. FileSys::SaveDataSize new_size{SUFFICIENT_SAVE_DATA_SIZE, SUFFICIENT_SAVE_DATA_SIZE};
  447. FileSys::NACP nacp;
  448. const auto res = system.GetAppLoader().ReadControlData(nacp);
  449. if (res != Loader::ResultStatus::Success) {
  450. const FileSys::PatchManager pm{system.GetApplicationProcessProgramID(),
  451. system.GetFileSystemController(),
  452. system.GetContentProvider()};
  453. const auto metadata = pm.GetControlMetadata();
  454. const auto& nacp_unique = metadata.first;
  455. if (nacp_unique != nullptr) {
  456. new_size = {nacp_unique->GetDefaultNormalSaveSize(),
  457. nacp_unique->GetDefaultJournalSaveSize()};
  458. }
  459. } else {
  460. new_size = {nacp.GetDefaultNormalSaveSize(), nacp.GetDefaultJournalSaveSize()};
  461. }
  462. WriteSaveDataSize(type, title_id, user_id, new_size);
  463. return new_size;
  464. }
  465. return value;
  466. }
  467. void FileSystemController::WriteSaveDataSize(FileSys::SaveDataType type, u64 title_id, u128 user_id,
  468. FileSys::SaveDataSize new_value) const {
  469. if (save_data_factory != nullptr)
  470. save_data_factory->WriteSaveDataSize(type, title_id, user_id, new_value);
  471. }
  472. void FileSystemController::SetGameCard(FileSys::VirtualFile file) {
  473. gamecard = std::make_unique<FileSys::XCI>(file);
  474. const auto dir = gamecard->ConcatenatedPseudoDirectory();
  475. gamecard_registered = std::make_unique<FileSys::RegisteredCache>(dir);
  476. gamecard_placeholder = std::make_unique<FileSys::PlaceholderCache>(dir);
  477. }
  478. FileSys::XCI* FileSystemController::GetGameCard() const {
  479. return gamecard.get();
  480. }
  481. FileSys::RegisteredCache* FileSystemController::GetGameCardContents() const {
  482. return gamecard_registered.get();
  483. }
  484. FileSys::PlaceholderCache* FileSystemController::GetGameCardPlaceholder() const {
  485. return gamecard_placeholder.get();
  486. }
  487. FileSys::RegisteredCache* FileSystemController::GetSystemNANDContents() const {
  488. LOG_TRACE(Service_FS, "Opening System NAND Contents");
  489. if (bis_factory == nullptr)
  490. return nullptr;
  491. return bis_factory->GetSystemNANDContents();
  492. }
  493. FileSys::RegisteredCache* FileSystemController::GetUserNANDContents() const {
  494. LOG_TRACE(Service_FS, "Opening User NAND Contents");
  495. if (bis_factory == nullptr)
  496. return nullptr;
  497. return bis_factory->GetUserNANDContents();
  498. }
  499. FileSys::RegisteredCache* FileSystemController::GetSDMCContents() const {
  500. LOG_TRACE(Service_FS, "Opening SDMC Contents");
  501. if (sdmc_factory == nullptr)
  502. return nullptr;
  503. return sdmc_factory->GetSDMCContents();
  504. }
  505. FileSys::PlaceholderCache* FileSystemController::GetSystemNANDPlaceholder() const {
  506. LOG_TRACE(Service_FS, "Opening System NAND Placeholder");
  507. if (bis_factory == nullptr)
  508. return nullptr;
  509. return bis_factory->GetSystemNANDPlaceholder();
  510. }
  511. FileSys::PlaceholderCache* FileSystemController::GetUserNANDPlaceholder() const {
  512. LOG_TRACE(Service_FS, "Opening User NAND Placeholder");
  513. if (bis_factory == nullptr)
  514. return nullptr;
  515. return bis_factory->GetUserNANDPlaceholder();
  516. }
  517. FileSys::PlaceholderCache* FileSystemController::GetSDMCPlaceholder() const {
  518. LOG_TRACE(Service_FS, "Opening SDMC Placeholder");
  519. if (sdmc_factory == nullptr)
  520. return nullptr;
  521. return sdmc_factory->GetSDMCPlaceholder();
  522. }
  523. FileSys::RegisteredCache* FileSystemController::GetRegisteredCacheForStorage(
  524. FileSys::StorageId id) const {
  525. switch (id) {
  526. case FileSys::StorageId::None:
  527. case FileSys::StorageId::Host:
  528. UNIMPLEMENTED();
  529. return nullptr;
  530. case FileSys::StorageId::GameCard:
  531. return GetGameCardContents();
  532. case FileSys::StorageId::NandSystem:
  533. return GetSystemNANDContents();
  534. case FileSys::StorageId::NandUser:
  535. return GetUserNANDContents();
  536. case FileSys::StorageId::SdCard:
  537. return GetSDMCContents();
  538. }
  539. return nullptr;
  540. }
  541. FileSys::PlaceholderCache* FileSystemController::GetPlaceholderCacheForStorage(
  542. FileSys::StorageId id) const {
  543. switch (id) {
  544. case FileSys::StorageId::None:
  545. case FileSys::StorageId::Host:
  546. UNIMPLEMENTED();
  547. return nullptr;
  548. case FileSys::StorageId::GameCard:
  549. return GetGameCardPlaceholder();
  550. case FileSys::StorageId::NandSystem:
  551. return GetSystemNANDPlaceholder();
  552. case FileSys::StorageId::NandUser:
  553. return GetUserNANDPlaceholder();
  554. case FileSys::StorageId::SdCard:
  555. return GetSDMCPlaceholder();
  556. }
  557. return nullptr;
  558. }
  559. FileSys::VirtualDir FileSystemController::GetSystemNANDContentDirectory() const {
  560. LOG_TRACE(Service_FS, "Opening system NAND content directory");
  561. if (bis_factory == nullptr)
  562. return nullptr;
  563. return bis_factory->GetSystemNANDContentDirectory();
  564. }
  565. FileSys::VirtualDir FileSystemController::GetUserNANDContentDirectory() const {
  566. LOG_TRACE(Service_FS, "Opening user NAND content directory");
  567. if (bis_factory == nullptr)
  568. return nullptr;
  569. return bis_factory->GetUserNANDContentDirectory();
  570. }
  571. FileSys::VirtualDir FileSystemController::GetSDMCContentDirectory() const {
  572. LOG_TRACE(Service_FS, "Opening SDMC content directory");
  573. if (sdmc_factory == nullptr)
  574. return nullptr;
  575. return sdmc_factory->GetSDMCContentDirectory();
  576. }
  577. FileSys::VirtualDir FileSystemController::GetNANDImageDirectory() const {
  578. LOG_TRACE(Service_FS, "Opening NAND image directory");
  579. if (bis_factory == nullptr)
  580. return nullptr;
  581. return bis_factory->GetImageDirectory();
  582. }
  583. FileSys::VirtualDir FileSystemController::GetSDMCImageDirectory() const {
  584. LOG_TRACE(Service_FS, "Opening SDMC image directory");
  585. if (sdmc_factory == nullptr)
  586. return nullptr;
  587. return sdmc_factory->GetImageDirectory();
  588. }
  589. FileSys::VirtualDir FileSystemController::GetContentDirectory(ContentStorageId id) const {
  590. switch (id) {
  591. case ContentStorageId::System:
  592. return GetSystemNANDContentDirectory();
  593. case ContentStorageId::User:
  594. return GetUserNANDContentDirectory();
  595. case ContentStorageId::SdCard:
  596. return GetSDMCContentDirectory();
  597. }
  598. return nullptr;
  599. }
  600. FileSys::VirtualDir FileSystemController::GetImageDirectory(ImageDirectoryId id) const {
  601. switch (id) {
  602. case ImageDirectoryId::NAND:
  603. return GetNANDImageDirectory();
  604. case ImageDirectoryId::SdCard:
  605. return GetSDMCImageDirectory();
  606. }
  607. return nullptr;
  608. }
  609. FileSys::VirtualDir FileSystemController::GetModificationLoadRoot(u64 title_id) const {
  610. LOG_TRACE(Service_FS, "Opening mod load root for tid={:016X}", title_id);
  611. if (bis_factory == nullptr)
  612. return nullptr;
  613. return bis_factory->GetModificationLoadRoot(title_id);
  614. }
  615. FileSys::VirtualDir FileSystemController::GetSDMCModificationLoadRoot(u64 title_id) const {
  616. LOG_TRACE(Service_FS, "Opening SDMC mod load root for tid={:016X}", title_id);
  617. if (sdmc_factory == nullptr) {
  618. return nullptr;
  619. }
  620. return sdmc_factory->GetSDMCModificationLoadRoot(title_id);
  621. }
  622. FileSys::VirtualDir FileSystemController::GetModificationDumpRoot(u64 title_id) const {
  623. LOG_TRACE(Service_FS, "Opening mod dump root for tid={:016X}", title_id);
  624. if (bis_factory == nullptr)
  625. return nullptr;
  626. return bis_factory->GetModificationDumpRoot(title_id);
  627. }
  628. FileSys::VirtualDir FileSystemController::GetBCATDirectory(u64 title_id) const {
  629. LOG_TRACE(Service_FS, "Opening BCAT root for tid={:016X}", title_id);
  630. if (bis_factory == nullptr)
  631. return nullptr;
  632. return bis_factory->GetBCATDirectory(title_id);
  633. }
  634. void FileSystemController::SetAutoSaveDataCreation(bool enable) {
  635. save_data_factory->SetAutoCreate(enable);
  636. }
  637. void FileSystemController::CreateFactories(FileSys::VfsFilesystem& vfs, bool overwrite) {
  638. if (overwrite) {
  639. bis_factory = nullptr;
  640. save_data_factory = nullptr;
  641. sdmc_factory = nullptr;
  642. }
  643. using YuzuPath = Common::FS::YuzuPath;
  644. const auto sdmc_dir_path = Common::FS::GetYuzuPath(YuzuPath::SDMCDir);
  645. const auto sdmc_load_dir_path = sdmc_dir_path / "atmosphere/contents";
  646. const auto rw_mode = FileSys::Mode::ReadWrite;
  647. auto nand_directory =
  648. vfs.OpenDirectory(Common::FS::GetYuzuPathString(YuzuPath::NANDDir), rw_mode);
  649. auto sd_directory = vfs.OpenDirectory(Common::FS::PathToUTF8String(sdmc_dir_path), rw_mode);
  650. auto load_directory =
  651. vfs.OpenDirectory(Common::FS::GetYuzuPathString(YuzuPath::LoadDir), FileSys::Mode::Read);
  652. auto sd_load_directory =
  653. vfs.OpenDirectory(Common::FS::PathToUTF8String(sdmc_load_dir_path), FileSys::Mode::Read);
  654. auto dump_directory =
  655. vfs.OpenDirectory(Common::FS::GetYuzuPathString(YuzuPath::DumpDir), rw_mode);
  656. if (bis_factory == nullptr) {
  657. bis_factory = std::make_unique<FileSys::BISFactory>(
  658. nand_directory, std::move(load_directory), std::move(dump_directory));
  659. system.RegisterContentProvider(FileSys::ContentProviderUnionSlot::SysNAND,
  660. bis_factory->GetSystemNANDContents());
  661. system.RegisterContentProvider(FileSys::ContentProviderUnionSlot::UserNAND,
  662. bis_factory->GetUserNANDContents());
  663. }
  664. if (save_data_factory == nullptr) {
  665. save_data_factory =
  666. std::make_unique<FileSys::SaveDataFactory>(system, std::move(nand_directory));
  667. }
  668. if (sdmc_factory == nullptr) {
  669. sdmc_factory = std::make_unique<FileSys::SDMCFactory>(std::move(sd_directory),
  670. std::move(sd_load_directory));
  671. system.RegisterContentProvider(FileSys::ContentProviderUnionSlot::SDMC,
  672. sdmc_factory->GetSDMCContents());
  673. }
  674. }
  675. void LoopProcess(Core::System& system) {
  676. auto server_manager = std::make_unique<ServerManager>(system);
  677. server_manager->RegisterNamedService("fsp-ldr", std::make_shared<FSP_LDR>(system));
  678. server_manager->RegisterNamedService("fsp:pr", std::make_shared<FSP_PR>(system));
  679. server_manager->RegisterNamedService("fsp-srv", std::make_shared<FSP_SRV>(system));
  680. ServerManager::RunServer(std::move(server_manager));
  681. }
  682. } // namespace Service::FileSystem