filesystem.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <utility>
  5. #include "common/assert.h"
  6. #include "common/fs/path_util.h"
  7. #include "common/settings.h"
  8. #include "core/core.h"
  9. #include "core/file_sys/bis_factory.h"
  10. #include "core/file_sys/card_image.h"
  11. #include "core/file_sys/control_metadata.h"
  12. #include "core/file_sys/errors.h"
  13. #include "core/file_sys/mode.h"
  14. #include "core/file_sys/patch_manager.h"
  15. #include "core/file_sys/registered_cache.h"
  16. #include "core/file_sys/romfs_factory.h"
  17. #include "core/file_sys/savedata_factory.h"
  18. #include "core/file_sys/sdmc_factory.h"
  19. #include "core/file_sys/vfs.h"
  20. #include "core/file_sys/vfs_offset.h"
  21. #include "core/hle/service/filesystem/filesystem.h"
  22. #include "core/hle/service/filesystem/fsp_ldr.h"
  23. #include "core/hle/service/filesystem/fsp_pr.h"
  24. #include "core/hle/service/filesystem/fsp_srv.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. ResultCode 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. const auto entry_type = GetEntryType(path);
  50. if (entry_type.Code() == 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. ResultCode 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. ResultCode 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. ResultCode 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. ResultCode 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. ResultCode 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. ResultCode 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 succeded.");
  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. ResultCode 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. ResultVal<FileSys::VirtualFile> VfsDirectoryServiceWrapper::OpenFile(const std::string& path_,
  182. 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. return std::make_shared<FileSys::OffsetVfsFile>(file, 0, file->GetSize());
  194. }
  195. return file;
  196. }
  197. ResultVal<FileSys::VirtualDir> VfsDirectoryServiceWrapper::OpenDirectory(const std::string& path_) {
  198. std::string path(Common::FS::SanitizePath(path_));
  199. auto dir = GetDirectoryRelativeWrapped(backing, path);
  200. if (dir == nullptr) {
  201. // TODO(DarkLordZach): Find a better error code for this
  202. return FileSys::ERROR_PATH_NOT_FOUND;
  203. }
  204. return dir;
  205. }
  206. ResultVal<FileSys::EntryType> VfsDirectoryServiceWrapper::GetEntryType(
  207. const std::string& path_) const {
  208. std::string path(Common::FS::SanitizePath(path_));
  209. auto dir = GetDirectoryRelativeWrapped(backing, Common::FS::GetParentPath(path));
  210. if (dir == nullptr)
  211. return FileSys::ERROR_PATH_NOT_FOUND;
  212. auto filename = Common::FS::GetFilename(path);
  213. // TODO(Subv): Some games use the '/' path, find out what this means.
  214. if (filename.empty())
  215. return FileSys::EntryType::Directory;
  216. if (dir->GetFile(filename) != nullptr)
  217. return FileSys::EntryType::File;
  218. if (dir->GetSubdirectory(filename) != nullptr)
  219. return FileSys::EntryType::Directory;
  220. return FileSys::ERROR_PATH_NOT_FOUND;
  221. }
  222. ResultVal<FileSys::FileTimeStampRaw> VfsDirectoryServiceWrapper::GetFileTimeStampRaw(
  223. const std::string& path) const {
  224. auto dir = GetDirectoryRelativeWrapped(backing, Common::FS::GetParentPath(path));
  225. if (dir == nullptr) {
  226. return FileSys::ERROR_PATH_NOT_FOUND;
  227. }
  228. if (GetEntryType(path).Failed()) {
  229. return FileSys::ERROR_PATH_NOT_FOUND;
  230. }
  231. return dir->GetFileTimeStamp(Common::FS::GetFilename(path));
  232. }
  233. FileSystemController::FileSystemController(Core::System& system_) : system{system_} {}
  234. FileSystemController::~FileSystemController() = default;
  235. ResultCode FileSystemController::RegisterRomFS(std::unique_ptr<FileSys::RomFSFactory>&& factory) {
  236. romfs_factory = std::move(factory);
  237. LOG_DEBUG(Service_FS, "Registered RomFS");
  238. return ResultSuccess;
  239. }
  240. ResultCode FileSystemController::RegisterSaveData(
  241. std::unique_ptr<FileSys::SaveDataFactory>&& factory) {
  242. ASSERT_MSG(save_data_factory == nullptr, "Tried to register a second save data");
  243. save_data_factory = std::move(factory);
  244. LOG_DEBUG(Service_FS, "Registered save data");
  245. return ResultSuccess;
  246. }
  247. ResultCode FileSystemController::RegisterSDMC(std::unique_ptr<FileSys::SDMCFactory>&& factory) {
  248. ASSERT_MSG(sdmc_factory == nullptr, "Tried to register a second SDMC");
  249. sdmc_factory = std::move(factory);
  250. LOG_DEBUG(Service_FS, "Registered SDMC");
  251. return ResultSuccess;
  252. }
  253. ResultCode FileSystemController::RegisterBIS(std::unique_ptr<FileSys::BISFactory>&& factory) {
  254. ASSERT_MSG(bis_factory == nullptr, "Tried to register a second BIS");
  255. bis_factory = std::move(factory);
  256. LOG_DEBUG(Service_FS, "Registered BIS");
  257. return ResultSuccess;
  258. }
  259. void FileSystemController::SetPackedUpdate(FileSys::VirtualFile update_raw) {
  260. LOG_TRACE(Service_FS, "Setting packed update for romfs");
  261. if (romfs_factory == nullptr)
  262. return;
  263. romfs_factory->SetPackedUpdate(std::move(update_raw));
  264. }
  265. ResultVal<FileSys::VirtualFile> FileSystemController::OpenRomFSCurrentProcess() const {
  266. LOG_TRACE(Service_FS, "Opening RomFS for current process");
  267. if (romfs_factory == nullptr) {
  268. // TODO(bunnei): Find a better error code for this
  269. return ResultUnknown;
  270. }
  271. return romfs_factory->OpenCurrentProcess(system.GetCurrentProcessProgramID());
  272. }
  273. ResultVal<FileSys::VirtualFile> FileSystemController::OpenPatchedRomFS(
  274. u64 title_id, FileSys::ContentRecordType type) const {
  275. LOG_TRACE(Service_FS, "Opening patched RomFS for title_id={:016X}", title_id);
  276. if (romfs_factory == nullptr) {
  277. // TODO: Find a better error code for this
  278. return ResultUnknown;
  279. }
  280. return romfs_factory->OpenPatchedRomFS(title_id, type);
  281. }
  282. ResultVal<FileSys::VirtualFile> FileSystemController::OpenPatchedRomFSWithProgramIndex(
  283. u64 title_id, u8 program_index, FileSys::ContentRecordType type) const {
  284. LOG_TRACE(Service_FS, "Opening patched RomFS for title_id={:016X}, program_index={}", title_id,
  285. program_index);
  286. if (romfs_factory == nullptr) {
  287. // TODO: Find a better error code for this
  288. return ResultUnknown;
  289. }
  290. return romfs_factory->OpenPatchedRomFSWithProgramIndex(title_id, program_index, type);
  291. }
  292. ResultVal<FileSys::VirtualFile> FileSystemController::OpenRomFS(
  293. u64 title_id, FileSys::StorageId storage_id, FileSys::ContentRecordType type) const {
  294. LOG_TRACE(Service_FS, "Opening RomFS for title_id={:016X}, storage_id={:02X}, type={:02X}",
  295. title_id, storage_id, type);
  296. if (romfs_factory == nullptr) {
  297. // TODO(bunnei): Find a better error code for this
  298. return ResultUnknown;
  299. }
  300. return romfs_factory->Open(title_id, storage_id, type);
  301. }
  302. ResultVal<FileSys::VirtualDir> FileSystemController::CreateSaveData(
  303. FileSys::SaveDataSpaceId space, const FileSys::SaveDataAttribute& save_struct) const {
  304. LOG_TRACE(Service_FS, "Creating Save Data for space_id={:01X}, save_struct={}", space,
  305. save_struct.DebugInfo());
  306. if (save_data_factory == nullptr) {
  307. return FileSys::ERROR_ENTITY_NOT_FOUND;
  308. }
  309. return save_data_factory->Create(space, save_struct);
  310. }
  311. ResultVal<FileSys::VirtualDir> FileSystemController::OpenSaveData(
  312. FileSys::SaveDataSpaceId space, const FileSys::SaveDataAttribute& attribute) const {
  313. LOG_TRACE(Service_FS, "Opening Save Data for space_id={:01X}, save_struct={}", space,
  314. attribute.DebugInfo());
  315. if (save_data_factory == nullptr) {
  316. return FileSys::ERROR_ENTITY_NOT_FOUND;
  317. }
  318. return save_data_factory->Open(space, attribute);
  319. }
  320. ResultVal<FileSys::VirtualDir> FileSystemController::OpenSaveDataSpace(
  321. FileSys::SaveDataSpaceId space) const {
  322. LOG_TRACE(Service_FS, "Opening Save Data Space for space_id={:01X}", space);
  323. if (save_data_factory == nullptr) {
  324. return FileSys::ERROR_ENTITY_NOT_FOUND;
  325. }
  326. return save_data_factory->GetSaveDataSpaceDirectory(space);
  327. }
  328. ResultVal<FileSys::VirtualDir> FileSystemController::OpenSDMC() const {
  329. LOG_TRACE(Service_FS, "Opening SDMC");
  330. if (sdmc_factory == nullptr) {
  331. return FileSys::ERROR_SD_CARD_NOT_FOUND;
  332. }
  333. return sdmc_factory->Open();
  334. }
  335. ResultVal<FileSys::VirtualDir> FileSystemController::OpenBISPartition(
  336. FileSys::BisPartitionId id) const {
  337. LOG_TRACE(Service_FS, "Opening BIS Partition with id={:08X}", id);
  338. if (bis_factory == nullptr) {
  339. return FileSys::ERROR_ENTITY_NOT_FOUND;
  340. }
  341. auto part = bis_factory->OpenPartition(id);
  342. if (part == nullptr) {
  343. return FileSys::ERROR_INVALID_ARGUMENT;
  344. }
  345. return part;
  346. }
  347. ResultVal<FileSys::VirtualFile> FileSystemController::OpenBISPartitionStorage(
  348. FileSys::BisPartitionId id) const {
  349. LOG_TRACE(Service_FS, "Opening BIS Partition Storage with id={:08X}", id);
  350. if (bis_factory == nullptr) {
  351. return FileSys::ERROR_ENTITY_NOT_FOUND;
  352. }
  353. auto part = bis_factory->OpenPartitionStorage(id, system.GetFilesystem());
  354. if (part == nullptr) {
  355. return FileSys::ERROR_INVALID_ARGUMENT;
  356. }
  357. return part;
  358. }
  359. u64 FileSystemController::GetFreeSpaceSize(FileSys::StorageId id) const {
  360. switch (id) {
  361. case FileSys::StorageId::None:
  362. case FileSys::StorageId::GameCard:
  363. return 0;
  364. case FileSys::StorageId::SdCard:
  365. if (sdmc_factory == nullptr)
  366. return 0;
  367. return sdmc_factory->GetSDMCFreeSpace();
  368. case FileSys::StorageId::Host:
  369. if (bis_factory == nullptr)
  370. return 0;
  371. return bis_factory->GetSystemNANDFreeSpace() + bis_factory->GetUserNANDFreeSpace();
  372. case FileSys::StorageId::NandSystem:
  373. if (bis_factory == nullptr)
  374. return 0;
  375. return bis_factory->GetSystemNANDFreeSpace();
  376. case FileSys::StorageId::NandUser:
  377. if (bis_factory == nullptr)
  378. return 0;
  379. return bis_factory->GetUserNANDFreeSpace();
  380. }
  381. return 0;
  382. }
  383. u64 FileSystemController::GetTotalSpaceSize(FileSys::StorageId id) const {
  384. switch (id) {
  385. case FileSys::StorageId::None:
  386. case FileSys::StorageId::GameCard:
  387. return 0;
  388. case FileSys::StorageId::SdCard:
  389. if (sdmc_factory == nullptr)
  390. return 0;
  391. return sdmc_factory->GetSDMCTotalSpace();
  392. case FileSys::StorageId::Host:
  393. if (bis_factory == nullptr)
  394. return 0;
  395. return bis_factory->GetFullNANDTotalSpace();
  396. case FileSys::StorageId::NandSystem:
  397. if (bis_factory == nullptr)
  398. return 0;
  399. return bis_factory->GetSystemNANDTotalSpace();
  400. case FileSys::StorageId::NandUser:
  401. if (bis_factory == nullptr)
  402. return 0;
  403. return bis_factory->GetUserNANDTotalSpace();
  404. }
  405. return 0;
  406. }
  407. FileSys::SaveDataSize FileSystemController::ReadSaveDataSize(FileSys::SaveDataType type,
  408. u64 title_id, u128 user_id) const {
  409. if (save_data_factory == nullptr) {
  410. return {0, 0};
  411. }
  412. const auto value = save_data_factory->ReadSaveDataSize(type, title_id, user_id);
  413. if (value.normal == 0 && value.journal == 0) {
  414. FileSys::SaveDataSize new_size{SUFFICIENT_SAVE_DATA_SIZE, SUFFICIENT_SAVE_DATA_SIZE};
  415. FileSys::NACP nacp;
  416. const auto res = system.GetAppLoader().ReadControlData(nacp);
  417. if (res != Loader::ResultStatus::Success) {
  418. const FileSys::PatchManager pm{system.GetCurrentProcessProgramID(),
  419. system.GetFileSystemController(),
  420. system.GetContentProvider()};
  421. const auto metadata = pm.GetControlMetadata();
  422. const auto& nacp_unique = metadata.first;
  423. if (nacp_unique != nullptr) {
  424. new_size = {nacp_unique->GetDefaultNormalSaveSize(),
  425. nacp_unique->GetDefaultJournalSaveSize()};
  426. }
  427. } else {
  428. new_size = {nacp.GetDefaultNormalSaveSize(), nacp.GetDefaultJournalSaveSize()};
  429. }
  430. WriteSaveDataSize(type, title_id, user_id, new_size);
  431. return new_size;
  432. }
  433. return value;
  434. }
  435. void FileSystemController::WriteSaveDataSize(FileSys::SaveDataType type, u64 title_id, u128 user_id,
  436. FileSys::SaveDataSize new_value) const {
  437. if (save_data_factory != nullptr)
  438. save_data_factory->WriteSaveDataSize(type, title_id, user_id, new_value);
  439. }
  440. void FileSystemController::SetGameCard(FileSys::VirtualFile file) {
  441. gamecard = std::make_unique<FileSys::XCI>(file);
  442. const auto dir = gamecard->ConcatenatedPseudoDirectory();
  443. gamecard_registered = std::make_unique<FileSys::RegisteredCache>(dir);
  444. gamecard_placeholder = std::make_unique<FileSys::PlaceholderCache>(dir);
  445. }
  446. FileSys::XCI* FileSystemController::GetGameCard() const {
  447. return gamecard.get();
  448. }
  449. FileSys::RegisteredCache* FileSystemController::GetGameCardContents() const {
  450. return gamecard_registered.get();
  451. }
  452. FileSys::PlaceholderCache* FileSystemController::GetGameCardPlaceholder() const {
  453. return gamecard_placeholder.get();
  454. }
  455. FileSys::RegisteredCache* FileSystemController::GetSystemNANDContents() const {
  456. LOG_TRACE(Service_FS, "Opening System NAND Contents");
  457. if (bis_factory == nullptr)
  458. return nullptr;
  459. return bis_factory->GetSystemNANDContents();
  460. }
  461. FileSys::RegisteredCache* FileSystemController::GetUserNANDContents() const {
  462. LOG_TRACE(Service_FS, "Opening User NAND Contents");
  463. if (bis_factory == nullptr)
  464. return nullptr;
  465. return bis_factory->GetUserNANDContents();
  466. }
  467. FileSys::RegisteredCache* FileSystemController::GetSDMCContents() const {
  468. LOG_TRACE(Service_FS, "Opening SDMC Contents");
  469. if (sdmc_factory == nullptr)
  470. return nullptr;
  471. return sdmc_factory->GetSDMCContents();
  472. }
  473. FileSys::PlaceholderCache* FileSystemController::GetSystemNANDPlaceholder() const {
  474. LOG_TRACE(Service_FS, "Opening System NAND Placeholder");
  475. if (bis_factory == nullptr)
  476. return nullptr;
  477. return bis_factory->GetSystemNANDPlaceholder();
  478. }
  479. FileSys::PlaceholderCache* FileSystemController::GetUserNANDPlaceholder() const {
  480. LOG_TRACE(Service_FS, "Opening User NAND Placeholder");
  481. if (bis_factory == nullptr)
  482. return nullptr;
  483. return bis_factory->GetUserNANDPlaceholder();
  484. }
  485. FileSys::PlaceholderCache* FileSystemController::GetSDMCPlaceholder() const {
  486. LOG_TRACE(Service_FS, "Opening SDMC Placeholder");
  487. if (sdmc_factory == nullptr)
  488. return nullptr;
  489. return sdmc_factory->GetSDMCPlaceholder();
  490. }
  491. FileSys::RegisteredCache* FileSystemController::GetRegisteredCacheForStorage(
  492. FileSys::StorageId id) const {
  493. switch (id) {
  494. case FileSys::StorageId::None:
  495. case FileSys::StorageId::Host:
  496. UNIMPLEMENTED();
  497. return nullptr;
  498. case FileSys::StorageId::GameCard:
  499. return GetGameCardContents();
  500. case FileSys::StorageId::NandSystem:
  501. return GetSystemNANDContents();
  502. case FileSys::StorageId::NandUser:
  503. return GetUserNANDContents();
  504. case FileSys::StorageId::SdCard:
  505. return GetSDMCContents();
  506. }
  507. return nullptr;
  508. }
  509. FileSys::PlaceholderCache* FileSystemController::GetPlaceholderCacheForStorage(
  510. FileSys::StorageId id) const {
  511. switch (id) {
  512. case FileSys::StorageId::None:
  513. case FileSys::StorageId::Host:
  514. UNIMPLEMENTED();
  515. return nullptr;
  516. case FileSys::StorageId::GameCard:
  517. return GetGameCardPlaceholder();
  518. case FileSys::StorageId::NandSystem:
  519. return GetSystemNANDPlaceholder();
  520. case FileSys::StorageId::NandUser:
  521. return GetUserNANDPlaceholder();
  522. case FileSys::StorageId::SdCard:
  523. return GetSDMCPlaceholder();
  524. }
  525. return nullptr;
  526. }
  527. FileSys::VirtualDir FileSystemController::GetSystemNANDContentDirectory() const {
  528. LOG_TRACE(Service_FS, "Opening system NAND content directory");
  529. if (bis_factory == nullptr)
  530. return nullptr;
  531. return bis_factory->GetSystemNANDContentDirectory();
  532. }
  533. FileSys::VirtualDir FileSystemController::GetUserNANDContentDirectory() const {
  534. LOG_TRACE(Service_FS, "Opening user NAND content directory");
  535. if (bis_factory == nullptr)
  536. return nullptr;
  537. return bis_factory->GetUserNANDContentDirectory();
  538. }
  539. FileSys::VirtualDir FileSystemController::GetSDMCContentDirectory() const {
  540. LOG_TRACE(Service_FS, "Opening SDMC content directory");
  541. if (sdmc_factory == nullptr)
  542. return nullptr;
  543. return sdmc_factory->GetSDMCContentDirectory();
  544. }
  545. FileSys::VirtualDir FileSystemController::GetNANDImageDirectory() const {
  546. LOG_TRACE(Service_FS, "Opening NAND image directory");
  547. if (bis_factory == nullptr)
  548. return nullptr;
  549. return bis_factory->GetImageDirectory();
  550. }
  551. FileSys::VirtualDir FileSystemController::GetSDMCImageDirectory() const {
  552. LOG_TRACE(Service_FS, "Opening SDMC image directory");
  553. if (sdmc_factory == nullptr)
  554. return nullptr;
  555. return sdmc_factory->GetImageDirectory();
  556. }
  557. FileSys::VirtualDir FileSystemController::GetContentDirectory(ContentStorageId id) const {
  558. switch (id) {
  559. case ContentStorageId::System:
  560. return GetSystemNANDContentDirectory();
  561. case ContentStorageId::User:
  562. return GetUserNANDContentDirectory();
  563. case ContentStorageId::SdCard:
  564. return GetSDMCContentDirectory();
  565. }
  566. return nullptr;
  567. }
  568. FileSys::VirtualDir FileSystemController::GetImageDirectory(ImageDirectoryId id) const {
  569. switch (id) {
  570. case ImageDirectoryId::NAND:
  571. return GetNANDImageDirectory();
  572. case ImageDirectoryId::SdCard:
  573. return GetSDMCImageDirectory();
  574. }
  575. return nullptr;
  576. }
  577. FileSys::VirtualDir FileSystemController::GetModificationLoadRoot(u64 title_id) const {
  578. LOG_TRACE(Service_FS, "Opening mod load root for tid={:016X}", title_id);
  579. if (bis_factory == nullptr)
  580. return nullptr;
  581. return bis_factory->GetModificationLoadRoot(title_id);
  582. }
  583. FileSys::VirtualDir FileSystemController::GetSDMCModificationLoadRoot(u64 title_id) const {
  584. LOG_TRACE(Service_FS, "Opening SDMC mod load root for tid={:016X}", title_id);
  585. if (sdmc_factory == nullptr) {
  586. return nullptr;
  587. }
  588. return sdmc_factory->GetSDMCModificationLoadRoot(title_id);
  589. }
  590. FileSys::VirtualDir FileSystemController::GetModificationDumpRoot(u64 title_id) const {
  591. LOG_TRACE(Service_FS, "Opening mod dump root for tid={:016X}", title_id);
  592. if (bis_factory == nullptr)
  593. return nullptr;
  594. return bis_factory->GetModificationDumpRoot(title_id);
  595. }
  596. FileSys::VirtualDir FileSystemController::GetBCATDirectory(u64 title_id) const {
  597. LOG_TRACE(Service_FS, "Opening BCAT root for tid={:016X}", title_id);
  598. if (bis_factory == nullptr)
  599. return nullptr;
  600. return bis_factory->GetBCATDirectory(title_id);
  601. }
  602. void FileSystemController::SetAutoSaveDataCreation(bool enable) {
  603. save_data_factory->SetAutoCreate(enable);
  604. }
  605. void FileSystemController::CreateFactories(FileSys::VfsFilesystem& vfs, bool overwrite) {
  606. if (overwrite) {
  607. bis_factory = nullptr;
  608. save_data_factory = nullptr;
  609. sdmc_factory = nullptr;
  610. }
  611. using YuzuPath = Common::FS::YuzuPath;
  612. const auto sdmc_dir_path = Common::FS::GetYuzuPath(YuzuPath::SDMCDir);
  613. const auto sdmc_load_dir_path = sdmc_dir_path / "atmosphere/contents";
  614. const auto rw_mode = FileSys::Mode::ReadWrite;
  615. auto nand_directory =
  616. vfs.OpenDirectory(Common::FS::GetYuzuPathString(YuzuPath::NANDDir), rw_mode);
  617. auto sd_directory = vfs.OpenDirectory(Common::FS::PathToUTF8String(sdmc_dir_path), rw_mode);
  618. auto load_directory =
  619. vfs.OpenDirectory(Common::FS::GetYuzuPathString(YuzuPath::LoadDir), FileSys::Mode::Read);
  620. auto sd_load_directory =
  621. vfs.OpenDirectory(Common::FS::PathToUTF8String(sdmc_load_dir_path), FileSys::Mode::Read);
  622. auto dump_directory =
  623. vfs.OpenDirectory(Common::FS::GetYuzuPathString(YuzuPath::DumpDir), rw_mode);
  624. if (bis_factory == nullptr) {
  625. bis_factory = std::make_unique<FileSys::BISFactory>(
  626. nand_directory, std::move(load_directory), std::move(dump_directory));
  627. system.RegisterContentProvider(FileSys::ContentProviderUnionSlot::SysNAND,
  628. bis_factory->GetSystemNANDContents());
  629. system.RegisterContentProvider(FileSys::ContentProviderUnionSlot::UserNAND,
  630. bis_factory->GetUserNANDContents());
  631. }
  632. if (save_data_factory == nullptr) {
  633. save_data_factory =
  634. std::make_unique<FileSys::SaveDataFactory>(system, std::move(nand_directory));
  635. }
  636. if (sdmc_factory == nullptr) {
  637. sdmc_factory = std::make_unique<FileSys::SDMCFactory>(std::move(sd_directory),
  638. std::move(sd_load_directory));
  639. system.RegisterContentProvider(FileSys::ContentProviderUnionSlot::SDMC,
  640. sdmc_factory->GetSDMCContents());
  641. }
  642. }
  643. void InstallInterfaces(Core::System& system) {
  644. std::make_shared<FSP_LDR>(system)->InstallAsService(system.ServiceManager());
  645. std::make_shared<FSP_PR>(system)->InstallAsService(system.ServiceManager());
  646. std::make_shared<FSP_SRV>(system)->InstallAsService(system.ServiceManager());
  647. }
  648. } // namespace Service::FileSystem