From 69bfe075b5c3f6b17ce269950d1f8c9aab18e2de Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 17 Jul 2018 15:42:15 -0400 Subject: General Filesystem and Save Data Fixes (#670) --- src/core/file_sys/errors.h | 2 + src/core/file_sys/filesystem.h | 31 ----------- src/core/file_sys/romfs_factory.cpp | 17 ++---- src/core/file_sys/romfs_factory.h | 11 ++-- src/core/file_sys/savedata_factory.cpp | 95 ++++++++++++++++++++++++++-------- src/core/file_sys/savedata_factory.h | 46 ++++++++++++---- src/core/file_sys/sdmc_factory.cpp | 16 +----- src/core/file_sys/sdmc_factory.h | 11 ++-- 8 files changed, 123 insertions(+), 106 deletions(-) (limited to 'src/core/file_sys') diff --git a/src/core/file_sys/errors.h b/src/core/file_sys/errors.h index d3e9a3829..1f3b8fa84 100644 --- a/src/core/file_sys/errors.h +++ b/src/core/file_sys/errors.h @@ -12,6 +12,8 @@ namespace ErrCodes { enum { NotFound = 1, SaveDataNotFound = 1002, + SdCardNotFound = 2001, + RomFSNotFound = 2520, }; } diff --git a/src/core/file_sys/filesystem.h b/src/core/file_sys/filesystem.h index 295a3133e..1a32a373b 100644 --- a/src/core/file_sys/filesystem.h +++ b/src/core/file_sys/filesystem.h @@ -167,35 +167,4 @@ public: virtual ResultVal GetEntryType(const std::string& path) const = 0; }; -class FileSystemFactory : NonCopyable { -public: - virtual ~FileSystemFactory() {} - - /** - * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.) - */ - virtual std::string GetName() const = 0; - - /** - * Tries to open the archive of this type with the specified path - * @param path Path to the archive - * @return An ArchiveBackend corresponding operating specified archive path. - */ - virtual ResultVal> Open(const Path& path) = 0; - - /** - * Deletes the archive contents and then re-creates the base folder - * @param path Path to the archive - * @return ResultCode of the operation, 0 on success - */ - virtual ResultCode Format(const Path& path) = 0; - - /** - * Retrieves the format info about the archive with the specified path - * @param path Path to the archive - * @return Format information about the archive or error code - */ - virtual ResultVal GetFormatInfo(const Path& path) const = 0; -}; - } // namespace FileSys diff --git a/src/core/file_sys/romfs_factory.cpp b/src/core/file_sys/romfs_factory.cpp index 84ae0d99b..946fc0452 100644 --- a/src/core/file_sys/romfs_factory.cpp +++ b/src/core/file_sys/romfs_factory.cpp @@ -11,28 +11,17 @@ namespace FileSys { -RomFS_Factory::RomFS_Factory(Loader::AppLoader& app_loader) { +RomFSFactory::RomFSFactory(Loader::AppLoader& app_loader) { // Load the RomFS from the app if (Loader::ResultStatus::Success != app_loader.ReadRomFS(romfs_file, data_offset, data_size)) { LOG_ERROR(Service_FS, "Unable to read RomFS!"); } } -ResultVal> RomFS_Factory::Open(const Path& path) { +ResultVal> RomFSFactory::Open(u64 title_id) { + // TODO(DarkLordZach): Use title id. auto archive = std::make_unique(romfs_file, data_offset, data_size); return MakeResult>(std::move(archive)); } -ResultCode RomFS_Factory::Format(const Path& path) { - LOG_ERROR(Service_FS, "Unimplemented Format archive {}", GetName()); - // TODO(bunnei): Find the right error code for this - return ResultCode(-1); -} - -ResultVal RomFS_Factory::GetFormatInfo(const Path& path) const { - LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive {}", GetName()); - // TODO(bunnei): Find the right error code for this - return ResultCode(-1); -} - } // namespace FileSys diff --git a/src/core/file_sys/romfs_factory.h b/src/core/file_sys/romfs_factory.h index e0698e642..c9e20c3ab 100644 --- a/src/core/file_sys/romfs_factory.h +++ b/src/core/file_sys/romfs_factory.h @@ -15,16 +15,11 @@ namespace FileSys { /// File system interface to the RomFS archive -class RomFS_Factory final : public FileSystemFactory { +class RomFSFactory { public: - explicit RomFS_Factory(Loader::AppLoader& app_loader); + explicit RomFSFactory(Loader::AppLoader& app_loader); - std::string GetName() const override { - return "ArchiveFactory_RomFS"; - } - ResultVal> Open(const Path& path) override; - ResultCode Format(const Path& path) override; - ResultVal GetFormatInfo(const Path& path) const override; + ResultVal> Open(u64 title_id); private: std::shared_ptr romfs_file; diff --git a/src/core/file_sys/savedata_factory.cpp b/src/core/file_sys/savedata_factory.cpp index f3aa213af..3ad37b28c 100644 --- a/src/core/file_sys/savedata_factory.cpp +++ b/src/core/file_sys/savedata_factory.cpp @@ -12,11 +12,49 @@ namespace FileSys { -SaveData_Factory::SaveData_Factory(std::string nand_directory) +std::string SaveDataDescriptor::DebugInfo() { + return fmt::format("[type={:02X}, title_id={:016X}, user_id={:016X}{:016X}, save_id={:016X}]", + static_cast(type), title_id, user_id[1], user_id[0], save_id); +} + +SaveDataFactory::SaveDataFactory(std::string nand_directory) : nand_directory(std::move(nand_directory)) {} -ResultVal> SaveData_Factory::Open(const Path& path) { - std::string save_directory = GetFullPath(); +ResultVal> SaveDataFactory::Open(SaveDataSpaceId space, + SaveDataDescriptor meta) { + if (meta.type == SaveDataType::SystemSaveData || meta.type == SaveDataType::SaveData) { + if (meta.zero_1 != 0) { + LOG_WARNING(Service_FS, + "Possibly incorrect SaveDataDescriptor, type is " + "SystemSaveData||SaveData but offset 0x28 is non-zero ({:016X}).", + meta.zero_1); + } + if (meta.zero_2 != 0) { + LOG_WARNING(Service_FS, + "Possibly incorrect SaveDataDescriptor, type is " + "SystemSaveData||SaveData but offset 0x30 is non-zero ({:016X}).", + meta.zero_2); + } + if (meta.zero_3 != 0) { + LOG_WARNING(Service_FS, + "Possibly incorrect SaveDataDescriptor, type is " + "SystemSaveData||SaveData but offset 0x38 is non-zero ({:016X}).", + meta.zero_3); + } + } + + if (meta.type == SaveDataType::SystemSaveData && meta.title_id != 0) { + LOG_WARNING(Service_FS, + "Possibly incorrect SaveDataDescriptor, type is SystemSaveData but title_id is " + "non-zero ({:016X}).", + meta.title_id); + } + + std::string save_directory = + GetFullPath(space, meta.type, meta.title_id, meta.user_id, meta.save_id); + + // TODO(DarkLordZach): Try to not create when opening, there are dedicated create save methods. + // But, user_ids don't match so this works for now. if (!FileUtil::Exists(save_directory)) { // TODO(bunnei): This is a work-around to always create a save data directory if it does not @@ -26,6 +64,12 @@ ResultVal> SaveData_Factory::Open(const Path& FileUtil::CreateFullPath(save_directory); } + // TODO(DarkLordZach): For some reason, CreateFullPath doesn't create the last bit. Should be + // fixed with VFS. + if (!FileUtil::IsDirectory(save_directory)) { + FileUtil::CreateDir(save_directory); + } + // Return an error if the save data doesn't actually exist. if (!FileUtil::IsDirectory(save_directory)) { // TODO(Subv): Find out correct error code. @@ -36,28 +80,35 @@ ResultVal> SaveData_Factory::Open(const Path& return MakeResult>(std::move(archive)); } -ResultCode SaveData_Factory::Format(const Path& path) { - LOG_WARNING(Service_FS, "Format archive {}", GetName()); - // Create the save data directory. - if (!FileUtil::CreateFullPath(GetFullPath())) { - // TODO(Subv): Find the correct error code. - return ResultCode(-1); - } +std::string SaveDataFactory::GetFullPath(SaveDataSpaceId space, SaveDataType type, u64 title_id, + u128 user_id, u64 save_id) const { + // According to switchbrew, if a save is of type SaveData and the title id field is 0, it should + // be interpreted as the title id of the current process. + if (type == SaveDataType::SaveData && title_id == 0) + title_id = Core::CurrentProcess()->program_id; - return RESULT_SUCCESS; -} + std::string prefix; -ResultVal SaveData_Factory::GetFormatInfo(const Path& path) const { - LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive {}", GetName()); - // TODO(bunnei): Find the right error code for this - return ResultCode(-1); -} + switch (space) { + case SaveDataSpaceId::NandSystem: + prefix = nand_directory + "system/save/"; + break; + case SaveDataSpaceId::NandUser: + prefix = nand_directory + "user/save/"; + break; + default: + ASSERT_MSG(false, "Unrecognized SaveDataSpaceId: {:02X}", static_cast(space)); + } -std::string SaveData_Factory::GetFullPath() const { - u64 title_id = Core::CurrentProcess()->program_id; - // TODO(Subv): Somehow obtain this value. - u32 user = 0; - return fmt::format("{}save/{:016X}/{:08X}/", nand_directory, title_id, user); + switch (type) { + case SaveDataType::SystemSaveData: + return fmt::format("{}{:016X}/{:016X}{:016X}", prefix, save_id, user_id[1], user_id[0]); + case SaveDataType::SaveData: + return fmt::format("{}{:016X}/{:016X}{:016X}/{:016X}", prefix, 0, user_id[1], user_id[0], + title_id); + default: + ASSERT_MSG(false, "Unrecognized SaveDataType: {:02X}", static_cast(type)); + } } } // namespace FileSys diff --git a/src/core/file_sys/savedata_factory.h b/src/core/file_sys/savedata_factory.h index 73a42aab6..b96721ac0 100644 --- a/src/core/file_sys/savedata_factory.h +++ b/src/core/file_sys/savedata_factory.h @@ -12,22 +12,50 @@ namespace FileSys { +enum class SaveDataSpaceId : u8 { + NandSystem = 0, + NandUser = 1, + SdCard = 2, + TemporaryStorage = 3, +}; + +enum class SaveDataType : u8 { + SystemSaveData = 0, + SaveData = 1, + BcatDeliveryCacheStorage = 2, + DeviceSaveData = 3, + TemporaryStorage = 4, + CacheStorage = 5, +}; + +struct SaveDataDescriptor { + u64_le title_id; + u128 user_id; + u64_le save_id; + SaveDataType type; + INSERT_PADDING_BYTES(7); + u64_le zero_1; + u64_le zero_2; + u64_le zero_3; + + std::string DebugInfo(); +}; +static_assert(sizeof(SaveDataDescriptor) == 0x40, "SaveDataDescriptor has incorrect size."); + /// File system interface to the SaveData archive -class SaveData_Factory final : public FileSystemFactory { +class SaveDataFactory { public: - explicit SaveData_Factory(std::string nand_directory); + explicit SaveDataFactory(std::string nand_directory); - std::string GetName() const override { - return "SaveData_Factory"; - } - ResultVal> Open(const Path& path) override; - ResultCode Format(const Path& path) override; - ResultVal GetFormatInfo(const Path& path) const override; + ResultVal> Open(SaveDataSpaceId space, + SaveDataDescriptor meta); private: std::string nand_directory; + std::string sd_directory; - std::string GetFullPath() const; + std::string GetFullPath(SaveDataSpaceId space, SaveDataType type, u64 title_id, u128 user_id, + u64 save_id) const; }; } // namespace FileSys diff --git a/src/core/file_sys/sdmc_factory.cpp b/src/core/file_sys/sdmc_factory.cpp index 2e5ffb764..ac6f2f971 100644 --- a/src/core/file_sys/sdmc_factory.cpp +++ b/src/core/file_sys/sdmc_factory.cpp @@ -12,9 +12,9 @@ namespace FileSys { -SDMC_Factory::SDMC_Factory(std::string sd_directory) : sd_directory(std::move(sd_directory)) {} +SDMCFactory::SDMCFactory(std::string sd_directory) : sd_directory(std::move(sd_directory)) {} -ResultVal> SDMC_Factory::Open(const Path& path) { +ResultVal> SDMCFactory::Open() { // Create the SD Card directory if it doesn't already exist. if (!FileUtil::IsDirectory(sd_directory)) { FileUtil::CreateFullPath(sd_directory); @@ -24,16 +24,4 @@ ResultVal> SDMC_Factory::Open(const Path& pat return MakeResult>(std::move(archive)); } -ResultCode SDMC_Factory::Format(const Path& path) { - LOG_ERROR(Service_FS, "Unimplemented Format archive {}", GetName()); - // TODO(Subv): Find the right error code for this - return ResultCode(-1); -} - -ResultVal SDMC_Factory::GetFormatInfo(const Path& path) const { - LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive {}", GetName()); - // TODO(bunnei): Find the right error code for this - return ResultCode(-1); -} - } // namespace FileSys diff --git a/src/core/file_sys/sdmc_factory.h b/src/core/file_sys/sdmc_factory.h index 93becda25..09bec7fce 100644 --- a/src/core/file_sys/sdmc_factory.h +++ b/src/core/file_sys/sdmc_factory.h @@ -13,16 +13,11 @@ namespace FileSys { /// File system interface to the SDCard archive -class SDMC_Factory final : public FileSystemFactory { +class SDMCFactory { public: - explicit SDMC_Factory(std::string sd_directory); + explicit SDMCFactory(std::string sd_directory); - std::string GetName() const override { - return "SDMC_Factory"; - } - ResultVal> Open(const Path& path) override; - ResultCode Format(const Path& path) override; - ResultVal GetFormatInfo(const Path& path) const override; + ResultVal> Open(); private: std::string sd_directory; -- cgit v1.2.3