diff options
Diffstat (limited to '')
-rw-r--r-- | src/core/file_sys/fs_directory.h | 4 | ||||
-rw-r--r-- | src/core/file_sys/fs_filesystem.h | 27 | ||||
-rw-r--r-- | src/core/file_sys/fs_memory_management.h | 8 | ||||
-rw-r--r-- | src/core/file_sys/fs_path.h | 2 | ||||
-rw-r--r-- | src/core/file_sys/fs_path_utility.h | 19 | ||||
-rw-r--r-- | src/core/file_sys/fs_save_data_types.h | 188 | ||||
-rw-r--r-- | src/core/file_sys/fs_string_util.h | 15 | ||||
-rw-r--r-- | src/core/file_sys/fsa/fs_i_directory.h | 91 | ||||
-rw-r--r-- | src/core/file_sys/fsa/fs_i_file.h | 167 | ||||
-rw-r--r-- | src/core/file_sys/fsa/fs_i_filesystem.h | 206 | ||||
-rw-r--r-- | src/core/file_sys/fssrv/fssrv_sf_path.h | 36 | ||||
-rw-r--r-- | src/core/file_sys/fssystem/fssystem_aes_xts_storage.h | 1 | ||||
-rw-r--r-- | src/core/file_sys/fssystem/fssystem_hierarchical_sha256_storage.cpp | 4 | ||||
-rw-r--r-- | src/core/file_sys/program_metadata.cpp | 4 | ||||
-rw-r--r-- | src/core/file_sys/savedata_factory.cpp | 90 | ||||
-rw-r--r-- | src/core/file_sys/savedata_factory.h | 68 |
16 files changed, 779 insertions, 151 deletions
diff --git a/src/core/file_sys/fs_directory.h b/src/core/file_sys/fs_directory.h index 25c9cb18a..3f90abb8f 100644 --- a/src/core/file_sys/fs_directory.h +++ b/src/core/file_sys/fs_directory.h @@ -3,6 +3,10 @@ #pragma once +#include <string_view> +#include "common/common_funcs.h" +#include "common/common_types.h" + namespace FileSys { constexpr inline size_t EntryNameLengthMax = 0x300; diff --git a/src/core/file_sys/fs_filesystem.h b/src/core/file_sys/fs_filesystem.h index 7f237b7fa..329b5aca5 100644 --- a/src/core/file_sys/fs_filesystem.h +++ b/src/core/file_sys/fs_filesystem.h @@ -23,6 +23,8 @@ enum class OpenDirectoryMode : u64 { File = (1 << 1), All = (Directory | File), + + NotRequireFileSize = (1ULL << 31), }; DECLARE_ENUM_FLAG_OPERATORS(OpenDirectoryMode) @@ -36,4 +38,29 @@ enum class CreateOption : u8 { BigFile = (1 << 0), }; +struct FileSystemAttribute { + u8 dir_entry_name_length_max_defined; + u8 file_entry_name_length_max_defined; + u8 dir_path_name_length_max_defined; + u8 file_path_name_length_max_defined; + INSERT_PADDING_BYTES_NOINIT(0x5); + u8 utf16_dir_entry_name_length_max_defined; + u8 utf16_file_entry_name_length_max_defined; + u8 utf16_dir_path_name_length_max_defined; + u8 utf16_file_path_name_length_max_defined; + INSERT_PADDING_BYTES_NOINIT(0x18); + s32 dir_entry_name_length_max; + s32 file_entry_name_length_max; + s32 dir_path_name_length_max; + s32 file_path_name_length_max; + INSERT_PADDING_WORDS_NOINIT(0x5); + s32 utf16_dir_entry_name_length_max; + s32 utf16_file_entry_name_length_max; + s32 utf16_dir_path_name_length_max; + s32 utf16_file_path_name_length_max; + INSERT_PADDING_WORDS_NOINIT(0x18); + INSERT_PADDING_WORDS_NOINIT(0x1); +}; +static_assert(sizeof(FileSystemAttribute) == 0xC0, "FileSystemAttribute has incorrect size"); + } // namespace FileSys diff --git a/src/core/file_sys/fs_memory_management.h b/src/core/file_sys/fs_memory_management.h index f03c6354b..080017c5d 100644 --- a/src/core/file_sys/fs_memory_management.h +++ b/src/core/file_sys/fs_memory_management.h @@ -10,7 +10,7 @@ namespace FileSys { constexpr size_t RequiredAlignment = alignof(u64); -void* AllocateUnsafe(size_t size) { +inline void* AllocateUnsafe(size_t size) { // Allocate void* const ptr = ::operator new(size, std::align_val_t{RequiredAlignment}); @@ -21,16 +21,16 @@ void* AllocateUnsafe(size_t size) { return ptr; } -void DeallocateUnsafe(void* ptr, size_t size) { +inline void DeallocateUnsafe(void* ptr, size_t size) { // Deallocate the pointer ::operator delete(ptr, std::align_val_t{RequiredAlignment}); } -void* Allocate(size_t size) { +inline void* Allocate(size_t size) { return AllocateUnsafe(size); } -void Deallocate(void* ptr, size_t size) { +inline void Deallocate(void* ptr, size_t size) { // If the pointer is non-null, deallocate it if (ptr != nullptr) { DeallocateUnsafe(ptr, size); diff --git a/src/core/file_sys/fs_path.h b/src/core/file_sys/fs_path.h index 56ba08a6a..1566e82b9 100644 --- a/src/core/file_sys/fs_path.h +++ b/src/core/file_sys/fs_path.h @@ -381,7 +381,7 @@ public: // Check that it's possible for us to remove a child auto* p = m_write_buffer.Get(); - s32 len = std::strlen(p); + s32 len = static_cast<s32>(std::strlen(p)); R_UNLESS(len != 1 || (p[0] != '/' && p[0] != '.'), ResultNotImplemented); // Handle a trailing separator diff --git a/src/core/file_sys/fs_path_utility.h b/src/core/file_sys/fs_path_utility.h index e9011d065..cdfd8c772 100644 --- a/src/core/file_sys/fs_path_utility.h +++ b/src/core/file_sys/fs_path_utility.h @@ -426,9 +426,10 @@ public: R_SUCCEED(); } - static Result Normalize(char* dst, size_t* out_len, const char* path, size_t max_out_size, - bool is_windows_path, bool is_drive_relative_path, - bool allow_all_characters = false) { + static constexpr Result Normalize(char* dst, size_t* out_len, const char* path, + size_t max_out_size, bool is_windows_path, + bool is_drive_relative_path, + bool allow_all_characters = false) { // Use StringTraits names for remainder of scope using namespace StringTraits; @@ -447,7 +448,7 @@ public: char* replacement_path = nullptr; size_t replacement_path_size = 0; - SCOPE_EXIT({ + SCOPE_EXIT { if (replacement_path != nullptr) { if (std::is_constant_evaluated()) { delete[] replacement_path; @@ -455,7 +456,7 @@ public: Deallocate(replacement_path, replacement_path_size); } } - }); + }; // Perform path replacement, if necessary if (IsParentDirectoryPathReplacementNeeded(cur_path)) { @@ -1102,8 +1103,8 @@ public: R_SUCCEED(); } - static Result Normalize(char* dst, size_t dst_size, const char* path, size_t path_len, - const PathFlags& flags) { + static constexpr Result Normalize(char* dst, size_t dst_size, const char* path, size_t path_len, + const PathFlags& flags) { // Use StringTraits names for remainder of scope using namespace StringTraits; @@ -1199,7 +1200,7 @@ public: const size_t replaced_src_len = path_len - (src - path); char* replaced_src = nullptr; - SCOPE_EXIT({ + SCOPE_EXIT { if (replaced_src != nullptr) { if (std::is_constant_evaluated()) { delete[] replaced_src; @@ -1207,7 +1208,7 @@ public: Deallocate(replaced_src, replaced_src_len); } } - }); + }; if (std::is_constant_evaluated()) { replaced_src = new char[replaced_src_len]; diff --git a/src/core/file_sys/fs_save_data_types.h b/src/core/file_sys/fs_save_data_types.h new file mode 100644 index 000000000..493dba34f --- /dev/null +++ b/src/core/file_sys/fs_save_data_types.h @@ -0,0 +1,188 @@ +// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include <array> +#include <fmt/format.h> +#include "common/common_funcs.h" +#include "common/common_types.h" + +namespace FileSys { + +using SaveDataId = u64; +using SystemSaveDataId = u64; +using SystemBcatSaveDataId = SystemSaveDataId; +using ProgramId = u64; + +enum class SaveDataSpaceId : u8 { + System = 0, + User = 1, + SdSystem = 2, + Temporary = 3, + SdUser = 4, + + ProperSystem = 100, + SafeMode = 101, +}; + +enum class SaveDataType : u8 { + System = 0, + Account = 1, + Bcat = 2, + Device = 3, + Temporary = 4, + Cache = 5, + SystemBcat = 6, +}; + +enum class SaveDataRank : u8 { + Primary = 0, + Secondary = 1, +}; + +struct SaveDataSize { + u64 normal; + u64 journal; +}; +static_assert(sizeof(SaveDataSize) == 0x10, "SaveDataSize has invalid size."); + +using UserId = u128; +static_assert(std::is_trivially_copyable_v<UserId>, "Data type must be trivially copyable."); +static_assert(sizeof(UserId) == 0x10, "UserId has invalid size."); + +constexpr inline SystemSaveDataId InvalidSystemSaveDataId = 0; +constexpr inline UserId InvalidUserId = {}; + +enum class SaveDataFlags : u32 { + None = (0 << 0), + KeepAfterResettingSystemSaveData = (1 << 0), + KeepAfterRefurbishment = (1 << 1), + KeepAfterResettingSystemSaveDataWithoutUserSaveData = (1 << 2), + NeedsSecureDelete = (1 << 3), +}; + +enum class SaveDataMetaType : u8 { + None = 0, + Thumbnail = 1, + ExtensionContext = 2, +}; + +struct SaveDataMetaInfo { + u32 size; + SaveDataMetaType type; + INSERT_PADDING_BYTES(0xB); +}; +static_assert(std::is_trivially_copyable_v<SaveDataMetaInfo>, + "Data type must be trivially copyable."); +static_assert(sizeof(SaveDataMetaInfo) == 0x10, "SaveDataMetaInfo has invalid size."); + +struct SaveDataCreationInfo { + s64 size; + s64 journal_size; + s64 block_size; + u64 owner_id; + u32 flags; + SaveDataSpaceId space_id; + bool pseudo; + INSERT_PADDING_BYTES(0x1A); +}; +static_assert(std::is_trivially_copyable_v<SaveDataCreationInfo>, + "Data type must be trivially copyable."); +static_assert(sizeof(SaveDataCreationInfo) == 0x40, "SaveDataCreationInfo has invalid size."); + +struct SaveDataAttribute { + ProgramId program_id; + UserId user_id; + SystemSaveDataId system_save_data_id; + SaveDataType type; + SaveDataRank rank; + u16 index; + INSERT_PADDING_BYTES(0x1C); + + static constexpr SaveDataAttribute Make(ProgramId program_id, SaveDataType type, UserId user_id, + SystemSaveDataId system_save_data_id, u16 index, + SaveDataRank rank) { + return { + .program_id = program_id, + .user_id = user_id, + .system_save_data_id = system_save_data_id, + .type = type, + .rank = rank, + .index = index, + }; + } + + static constexpr SaveDataAttribute Make(ProgramId program_id, SaveDataType type, UserId user_id, + SystemSaveDataId system_save_data_id, u16 index) { + return Make(program_id, type, user_id, system_save_data_id, index, SaveDataRank::Primary); + } + + static constexpr SaveDataAttribute Make(ProgramId program_id, SaveDataType type, UserId user_id, + SystemSaveDataId system_save_data_id) { + return Make(program_id, type, user_id, system_save_data_id, 0, SaveDataRank::Primary); + } + + std::string DebugInfo() const { + return fmt::format( + "[title_id={:016X}, user_id={:016X}{:016X}, save_id={:016X}, type={:02X}, " + "rank={}, index={}]", + program_id, user_id[1], user_id[0], system_save_data_id, static_cast<u8>(type), + static_cast<u8>(rank), index); + } +}; +static_assert(sizeof(SaveDataAttribute) == 0x40); +static_assert(std::is_trivially_destructible<SaveDataAttribute>::value); + +constexpr inline bool operator<(const SaveDataAttribute& lhs, const SaveDataAttribute& rhs) { + return std::tie(lhs.program_id, lhs.user_id, lhs.system_save_data_id, lhs.index, lhs.rank) < + std::tie(rhs.program_id, rhs.user_id, rhs.system_save_data_id, rhs.index, rhs.rank); +} + +constexpr inline bool operator==(const SaveDataAttribute& lhs, const SaveDataAttribute& rhs) { + return std::tie(lhs.program_id, lhs.user_id, lhs.system_save_data_id, lhs.type, lhs.rank, + lhs.index) == std::tie(rhs.program_id, rhs.user_id, rhs.system_save_data_id, + rhs.type, rhs.rank, rhs.index); +} + +constexpr inline bool operator!=(const SaveDataAttribute& lhs, const SaveDataAttribute& rhs) { + return !(lhs == rhs); +} + +struct SaveDataExtraData { + SaveDataAttribute attr; + u64 owner_id; + s64 timestamp; + u32 flags; + INSERT_PADDING_BYTES(4); + s64 available_size; + s64 journal_size; + s64 commit_id; + INSERT_PADDING_BYTES(0x190); +}; +static_assert(sizeof(SaveDataExtraData) == 0x200, "SaveDataExtraData has invalid size."); +static_assert(std::is_trivially_copyable_v<SaveDataExtraData>, + "Data type must be trivially copyable."); + +struct SaveDataFilter { + bool use_program_id; + bool use_save_data_type; + bool use_user_id; + bool use_save_data_id; + bool use_index; + SaveDataRank rank; + SaveDataAttribute attribute; +}; +static_assert(sizeof(SaveDataFilter) == 0x48, "SaveDataFilter has invalid size."); +static_assert(std::is_trivially_copyable_v<SaveDataFilter>, + "Data type must be trivially copyable."); + +struct HashSalt { + static constexpr size_t Size = 32; + + std::array<u8, Size> value; +}; +static_assert(std::is_trivially_copyable_v<HashSalt>, "Data type must be trivially copyable."); +static_assert(sizeof(HashSalt) == HashSalt::Size); + +} // namespace FileSys diff --git a/src/core/file_sys/fs_string_util.h b/src/core/file_sys/fs_string_util.h index 874e09054..c751a8f1a 100644 --- a/src/core/file_sys/fs_string_util.h +++ b/src/core/file_sys/fs_string_util.h @@ -20,6 +20,11 @@ constexpr int Strlen(const T* str) { } template <typename T> +constexpr int Strnlen(const T* str, std::size_t count) { + return Strnlen(str, static_cast<int>(count)); +} + +template <typename T> constexpr int Strnlen(const T* str, int count) { ASSERT(str != nullptr); ASSERT(count >= 0); @@ -33,6 +38,11 @@ constexpr int Strnlen(const T* str, int count) { } template <typename T> +constexpr int Strncmp(const T* lhs, const T* rhs, std::size_t count) { + return Strncmp(lhs, rhs, static_cast<int>(count)); +} + +template <typename T> constexpr int Strncmp(const T* lhs, const T* rhs, int count) { ASSERT(lhs != nullptr); ASSERT(rhs != nullptr); @@ -52,6 +62,11 @@ constexpr int Strncmp(const T* lhs, const T* rhs, int count) { } template <typename T> +static constexpr int Strlcpy(T* dst, const T* src, std::size_t count) { + return Strlcpy<T>(dst, src, static_cast<int>(count)); +} + +template <typename T> static constexpr int Strlcpy(T* dst, const T* src, int count) { ASSERT(dst != nullptr); ASSERT(src != nullptr); diff --git a/src/core/file_sys/fsa/fs_i_directory.h b/src/core/file_sys/fsa/fs_i_directory.h new file mode 100644 index 000000000..c8e895eab --- /dev/null +++ b/src/core/file_sys/fsa/fs_i_directory.h @@ -0,0 +1,91 @@ +// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "common/common_types.h" +#include "core/file_sys/errors.h" +#include "core/file_sys/fs_directory.h" +#include "core/file_sys/fs_file.h" +#include "core/file_sys/fs_filesystem.h" +#include "core/file_sys/savedata_factory.h" +#include "core/file_sys/vfs/vfs.h" +#include "core/hle/result.h" + +namespace FileSys::Fsa { + +class IDirectory { +public: + explicit IDirectory(VirtualDir backend_, OpenDirectoryMode mode) + : backend(std::move(backend_)) { + // TODO(DarkLordZach): Verify that this is the correct behavior. + // Build entry index now to save time later. + if (True(mode & OpenDirectoryMode::Directory)) { + BuildEntryIndex(backend->GetSubdirectories(), DirectoryEntryType::Directory); + } + if (True(mode & OpenDirectoryMode::File)) { + BuildEntryIndex(backend->GetFiles(), DirectoryEntryType::File); + } + } + virtual ~IDirectory() {} + + Result Read(s64* out_count, DirectoryEntry* out_entries, s64 max_entries) { + R_UNLESS(out_count != nullptr, ResultNullptrArgument); + if (max_entries == 0) { + *out_count = 0; + R_SUCCEED(); + } + R_UNLESS(out_entries != nullptr, ResultNullptrArgument); + R_UNLESS(max_entries > 0, ResultInvalidArgument); + R_RETURN(this->DoRead(out_count, out_entries, max_entries)); + } + + Result GetEntryCount(s64* out) { + R_UNLESS(out != nullptr, ResultNullptrArgument); + R_RETURN(this->DoGetEntryCount(out)); + } + +private: + Result DoRead(s64* out_count, DirectoryEntry* out_entries, s64 max_entries) { + const u64 actual_entries = + std::min(static_cast<u64>(max_entries), entries.size() - next_entry_index); + const auto* begin = reinterpret_cast<u8*>(entries.data() + next_entry_index); + const auto* end = reinterpret_cast<u8*>(entries.data() + next_entry_index + actual_entries); + const auto range_size = static_cast<std::size_t>(std::distance(begin, end)); + + next_entry_index += actual_entries; + *out_count = actual_entries; + + std::memcpy(out_entries, begin, range_size); + + R_SUCCEED(); + } + + Result DoGetEntryCount(s64* out) { + *out = entries.size() - next_entry_index; + R_SUCCEED(); + } + + // TODO: Remove this when VFS is gone + template <typename T> + void BuildEntryIndex(const std::vector<T>& new_data, DirectoryEntryType type) { + entries.reserve(entries.size() + new_data.size()); + + for (const auto& new_entry : new_data) { + auto name = new_entry->GetName(); + + if (type == DirectoryEntryType::File && name == GetSaveDataSizeFileName()) { + continue; + } + + entries.emplace_back(name, static_cast<s8>(type), + type == DirectoryEntryType::Directory ? 0 : new_entry->GetSize()); + } + } + + VirtualDir backend; + std::vector<DirectoryEntry> entries; + u64 next_entry_index = 0; +}; + +} // namespace FileSys::Fsa diff --git a/src/core/file_sys/fsa/fs_i_file.h b/src/core/file_sys/fsa/fs_i_file.h new file mode 100644 index 000000000..1188ae8ca --- /dev/null +++ b/src/core/file_sys/fsa/fs_i_file.h @@ -0,0 +1,167 @@ +// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "common/overflow.h" +#include "core/file_sys/errors.h" +#include "core/file_sys/fs_file.h" +#include "core/file_sys/fs_filesystem.h" +#include "core/file_sys/fs_operate_range.h" +#include "core/file_sys/vfs/vfs.h" +#include "core/file_sys/vfs/vfs_types.h" +#include "core/hle/result.h" + +namespace FileSys::Fsa { + +class IFile { +public: + explicit IFile(VirtualFile backend_) : backend(std::move(backend_)) {} + virtual ~IFile() {} + + Result Read(size_t* out, s64 offset, void* buffer, size_t size, const ReadOption& option) { + // Check that we have an output pointer + R_UNLESS(out != nullptr, ResultNullptrArgument); + + // If we have nothing to read, just succeed + if (size == 0) { + *out = 0; + R_SUCCEED(); + } + + // Check that the read is valid + R_UNLESS(buffer != nullptr, ResultNullptrArgument); + R_UNLESS(offset >= 0, ResultOutOfRange); + R_UNLESS(Common::CanAddWithoutOverflow<s64>(offset, size), ResultOutOfRange); + + // Do the read + R_RETURN(this->DoRead(out, offset, buffer, size, option)); + } + + Result Read(size_t* out, s64 offset, void* buffer, size_t size) { + R_RETURN(this->Read(out, offset, buffer, size, ReadOption::None)); + } + + Result GetSize(s64* out) { + R_UNLESS(out != nullptr, ResultNullptrArgument); + R_RETURN(this->DoGetSize(out)); + } + + Result Flush() { + R_RETURN(this->DoFlush()); + } + + Result Write(s64 offset, const void* buffer, size_t size, const WriteOption& option) { + // Handle the zero-size case + if (size == 0) { + if (option.HasFlushFlag()) { + R_TRY(this->Flush()); + } + R_SUCCEED(); + } + + // Check the write is valid + R_UNLESS(buffer != nullptr, ResultNullptrArgument); + R_UNLESS(offset >= 0, ResultOutOfRange); + R_UNLESS(Common::CanAddWithoutOverflow<s64>(offset, size), ResultOutOfRange); + + R_RETURN(this->DoWrite(offset, buffer, size, option)); + } + + Result SetSize(s64 size) { + R_UNLESS(size >= 0, ResultOutOfRange); + R_RETURN(this->DoSetSize(size)); + } + + Result OperateRange(void* dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, + const void* src, size_t src_size) { + R_RETURN(this->DoOperateRange(dst, dst_size, op_id, offset, size, src, src_size)); + } + + Result OperateRange(OperationId op_id, s64 offset, s64 size) { + R_RETURN(this->DoOperateRange(nullptr, 0, op_id, offset, size, nullptr, 0)); + } + +protected: + Result DryRead(size_t* out, s64 offset, size_t size, const ReadOption& option, + OpenMode open_mode) { + // Check that we can read + R_UNLESS(static_cast<u32>(open_mode & OpenMode::Read) != 0, ResultReadNotPermitted); + + // Get the file size, and validate our offset + s64 file_size = 0; + R_TRY(this->DoGetSize(std::addressof(file_size))); + R_UNLESS(offset <= file_size, ResultOutOfRange); + + *out = static_cast<size_t>(std::min(file_size - offset, static_cast<s64>(size))); + R_SUCCEED(); + } + + Result DrySetSize(s64 size, OpenMode open_mode) { + // Check that we can write + R_UNLESS(static_cast<u32>(open_mode & OpenMode::Write) != 0, ResultWriteNotPermitted); + R_SUCCEED(); + } + + Result DryWrite(bool* out_append, s64 offset, size_t size, const WriteOption& option, + OpenMode open_mode) { + // Check that we can write + R_UNLESS(static_cast<u32>(open_mode & OpenMode::Write) != 0, ResultWriteNotPermitted); + + // Get the file size + s64 file_size = 0; + R_TRY(this->DoGetSize(&file_size)); + + // Determine if we need to append + *out_append = false; + if (file_size < offset + static_cast<s64>(size)) { + R_UNLESS(static_cast<u32>(open_mode & OpenMode::AllowAppend) != 0, + ResultFileExtensionWithoutOpenModeAllowAppend); + *out_append = true; + } + + R_SUCCEED(); + } + +private: + Result DoRead(size_t* out, s64 offset, void* buffer, size_t size, const ReadOption& option) { + const auto read_size = backend->Read(static_cast<u8*>(buffer), size, offset); + *out = read_size; + + R_SUCCEED(); + } + + Result DoGetSize(s64* out) { + *out = backend->GetSize(); + R_SUCCEED(); + } + + Result DoFlush() { + // Exists for SDK compatibiltity -- No need to flush file. + R_SUCCEED(); + } + + Result DoWrite(s64 offset, const void* buffer, size_t size, const WriteOption& option) { + const std::size_t written = backend->Write(static_cast<const u8*>(buffer), size, offset); + + ASSERT_MSG(written == size, + "Could not write all bytes to file (requested={:016X}, actual={:016X}).", size, + written); + + R_SUCCEED(); + } + + Result DoSetSize(s64 size) { + backend->Resize(size); + R_SUCCEED(); + } + + Result DoOperateRange(void* dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, + const void* src, size_t src_size) { + R_THROW(ResultNotImplemented); + } + + VirtualFile backend; +}; + +} // namespace FileSys::Fsa diff --git a/src/core/file_sys/fsa/fs_i_filesystem.h b/src/core/file_sys/fsa/fs_i_filesystem.h new file mode 100644 index 000000000..8172190f4 --- /dev/null +++ b/src/core/file_sys/fsa/fs_i_filesystem.h @@ -0,0 +1,206 @@ +// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "core/file_sys/errors.h" +#include "core/file_sys/fs_filesystem.h" +#include "core/file_sys/fs_path.h" +#include "core/file_sys/vfs/vfs_types.h" +#include "core/hle/result.h" +#include "core/hle/service/filesystem/filesystem.h" + +namespace FileSys::Fsa { + +class IFile; +class IDirectory; + +enum class QueryId : u32 { + SetConcatenationFileAttribute = 0, + UpdateMac = 1, + IsSignedSystemPartitionOnSdCardValid = 2, + QueryUnpreparedFileInformation = 3, +}; + +class IFileSystem { +public: + explicit IFileSystem(VirtualDir backend_) : backend{std::move(backend_)} {} + virtual ~IFileSystem() {} + + Result CreateFile(const Path& path, s64 size, CreateOption option) { + R_UNLESS(size >= 0, ResultOutOfRange); + R_RETURN(this->DoCreateFile(path, size, static_cast<int>(option))); + } + + Result CreateFile(const Path& path, s64 size) { + R_RETURN(this->CreateFile(path, size, CreateOption::None)); + } + + Result DeleteFile(const Path& path) { + R_RETURN(this->DoDeleteFile(path)); + } + + Result CreateDirectory(const Path& path) { + R_RETURN(this->DoCreateDirectory(path)); + } + + Result DeleteDirectory(const Path& path) { + R_RETURN(this->DoDeleteDirectory(path)); + } + + Result DeleteDirectoryRecursively(const Path& path) { + R_RETURN(this->DoDeleteDirectoryRecursively(path)); + } + + Result RenameFile(const Path& old_path, const Path& new_path) { + R_RETURN(this->DoRenameFile(old_path, new_path)); + } + + Result RenameDirectory(const Path& old_path, const Path& new_path) { + R_RETURN(this->DoRenameDirectory(old_path, new_path)); + } + + Result GetEntryType(DirectoryEntryType* out, const Path& path) { + R_RETURN(this->DoGetEntryType(out, path)); + } + + Result OpenFile(VirtualFile* out_file, const Path& path, OpenMode mode) { + R_UNLESS(out_file != nullptr, ResultNullptrArgument); + R_UNLESS(static_cast<u32>(mode & OpenMode::ReadWrite) != 0, ResultInvalidOpenMode); + R_UNLESS(static_cast<u32>(mode & ~OpenMode::All) == 0, ResultInvalidOpenMode); + R_RETURN(this->DoOpenFile(out_file, path, mode)); + } + + Result OpenDirectory(VirtualDir* out_dir, const Path& path, OpenDirectoryMode mode) { + R_UNLESS(out_dir != nullptr, ResultNullptrArgument); + R_UNLESS(static_cast<u64>(mode & OpenDirectoryMode::All) != 0, ResultInvalidOpenMode); + R_UNLESS(static_cast<u64>( + mode & ~(OpenDirectoryMode::All | OpenDirectoryMode::NotRequireFileSize)) == 0, + ResultInvalidOpenMode); + R_RETURN(this->DoOpenDirectory(out_dir, path, mode)); + } + + Result Commit() { + R_RETURN(this->DoCommit()); + } + + Result GetFreeSpaceSize(s64* out, const Path& path) { + R_UNLESS(out != nullptr, ResultNullptrArgument); + R_RETURN(this->DoGetFreeSpaceSize(out, path)); + } + + Result GetTotalSpaceSize(s64* out, const Path& path) { + R_UNLESS(out != nullptr, ResultNullptrArgument); + R_RETURN(this->DoGetTotalSpaceSize(out, path)); + } + + Result CleanDirectoryRecursively(const Path& path) { + R_RETURN(this->DoCleanDirectoryRecursively(path)); + } + + Result GetFileTimeStampRaw(FileTimeStampRaw* out, const Path& path) { + R_UNLESS(out != nullptr, ResultNullptrArgument); + R_RETURN(this->DoGetFileTimeStampRaw(out, path)); + } + + Result QueryEntry(char* dst, size_t dst_size, const char* src, size_t src_size, QueryId query, + const Path& path) { + R_RETURN(this->DoQueryEntry(dst, dst_size, src, src_size, query, path)); + } + + // These aren't accessible as commands + Result CommitProvisionally(s64 counter) { + R_RETURN(this->DoCommitProvisionally(counter)); + } + + Result Rollback() { + R_RETURN(this->DoRollback()); + } + + Result Flush() { + R_RETURN(this->DoFlush()); + } + +private: + Result DoCreateFile(const Path& path, s64 size, int flags) { + R_RETURN(backend.CreateFile(path.GetString(), size)); + } + + Result DoDeleteFile(const Path& path) { + R_RETURN(backend.DeleteFile(path.GetString())); + } + + Result DoCreateDirectory(const Path& path) { + R_RETURN(backend.CreateDirectory(path.GetString())); + } + + Result DoDeleteDirectory(const Path& path) { + R_RETURN(backend.DeleteDirectory(path.GetString())); + } + + Result DoDeleteDirectoryRecursively(const Path& path) { + R_RETURN(backend.DeleteDirectoryRecursively(path.GetString())); + } + + Result DoRenameFile(const Path& old_path, const Path& new_path) { + R_RETURN(backend.RenameFile(old_path.GetString(), new_path.GetString())); + } + + Result DoRenameDirectory(const Path& old_path, const Path& new_path) { + R_RETURN(backend.RenameDirectory(old_path.GetString(), new_path.GetString())); + } + + Result DoGetEntryType(DirectoryEntryType* out, const Path& path) { + R_RETURN(backend.GetEntryType(out, path.GetString())); + } + + Result DoOpenFile(VirtualFile* out_file, const Path& path, OpenMode mode) { + R_RETURN(backend.OpenFile(out_file, path.GetString(), mode)); + } + + Result DoOpenDirectory(VirtualDir* out_directory, const Path& path, OpenDirectoryMode mode) { + R_RETURN(backend.OpenDirectory(out_directory, path.GetString())); + } + + Result DoCommit() { + R_THROW(ResultNotImplemented); + } + + Result DoGetFreeSpaceSize(s64* out, const Path& path) { + R_THROW(ResultNotImplemented); + } + + Result DoGetTotalSpaceSize(s64* out, const Path& path) { + R_THROW(ResultNotImplemented); + } + + Result DoCleanDirectoryRecursively(const Path& path) { + R_RETURN(backend.CleanDirectoryRecursively(path.GetString())); + } + + Result DoGetFileTimeStampRaw(FileTimeStampRaw* out, const Path& path) { + R_RETURN(backend.GetFileTimeStampRaw(out, path.GetString())); + } + + Result DoQueryEntry(char* dst, size_t dst_size, const char* src, size_t src_size, QueryId query, + const Path& path) { + R_THROW(ResultNotImplemented); + } + + // These aren't accessible as commands + Result DoCommitProvisionally(s64 counter) { + R_THROW(ResultNotImplemented); + } + + Result DoRollback() { + R_THROW(ResultNotImplemented); + } + + Result DoFlush() { + R_THROW(ResultNotImplemented); + } + + Service::FileSystem::VfsDirectoryServiceWrapper backend; +}; + +} // namespace FileSys::Fsa diff --git a/src/core/file_sys/fssrv/fssrv_sf_path.h b/src/core/file_sys/fssrv/fssrv_sf_path.h new file mode 100644 index 000000000..a0c0b2dac --- /dev/null +++ b/src/core/file_sys/fssrv/fssrv_sf_path.h @@ -0,0 +1,36 @@ +// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "core/file_sys/fs_directory.h" + +namespace FileSys::Sf { + +struct Path { + char str[EntryNameLengthMax + 1]; + + static constexpr Path Encode(const char* p) { + Path path = {}; + for (size_t i = 0; i < sizeof(path) - 1; i++) { + path.str[i] = p[i]; + if (p[i] == '\x00') { + break; + } + } + return path; + } + + static constexpr size_t GetPathLength(const Path& path) { + size_t len = 0; + for (size_t i = 0; i < sizeof(path) - 1 && path.str[i] != '\x00'; i++) { + len++; + } + return len; + } +}; +static_assert(std::is_trivially_copyable_v<Path>, "Path must be trivially copyable."); + +using FspPath = Path; + +} // namespace FileSys::Sf diff --git a/src/core/file_sys/fssystem/fssystem_aes_xts_storage.h b/src/core/file_sys/fssystem/fssystem_aes_xts_storage.h index f342efb57..0e83ca1b9 100644 --- a/src/core/file_sys/fssystem/fssystem_aes_xts_storage.h +++ b/src/core/file_sys/fssystem/fssystem_aes_xts_storage.h @@ -3,6 +3,7 @@ #pragma once +#include <mutex> #include <optional> #include "core/crypto/aes_util.h" diff --git a/src/core/file_sys/fssystem/fssystem_hierarchical_sha256_storage.cpp b/src/core/file_sys/fssystem/fssystem_hierarchical_sha256_storage.cpp index caea0b8f8..a68fd973c 100644 --- a/src/core/file_sys/fssystem/fssystem_hierarchical_sha256_storage.cpp +++ b/src/core/file_sys/fssystem/fssystem_hierarchical_sha256_storage.cpp @@ -36,7 +36,9 @@ Result HierarchicalSha256Storage::Initialize(VirtualFile* base_storages, s32 lay // Get the base storage size. m_base_storage_size = base_storages[2]->GetSize(); { - auto size_guard = SCOPE_GUARD({ m_base_storage_size = 0; }); + auto size_guard = SCOPE_GUARD { + m_base_storage_size = 0; + }; R_UNLESS(m_base_storage_size <= static_cast<s64>(HashSize) << m_log_size_ratio << m_log_size_ratio, ResultHierarchicalSha256BaseStorageTooLarge); diff --git a/src/core/file_sys/program_metadata.cpp b/src/core/file_sys/program_metadata.cpp index ae4e441c9..289969cc4 100644 --- a/src/core/file_sys/program_metadata.cpp +++ b/src/core/file_sys/program_metadata.cpp @@ -98,7 +98,9 @@ Loader::ResultStatus ProgramMetadata::Load(VirtualFile file) { Loader::ResultStatus ProgramMetadata::Reload(VirtualFile file) { const u64 original_program_id = aci_header.title_id; - SCOPE_EXIT({ aci_header.title_id = original_program_id; }); + SCOPE_EXIT { + aci_header.title_id = original_program_id; + }; return this->Load(file); } diff --git a/src/core/file_sys/savedata_factory.cpp b/src/core/file_sys/savedata_factory.cpp index cbf411a20..106922e04 100644 --- a/src/core/file_sys/savedata_factory.cpp +++ b/src/core/file_sys/savedata_factory.cpp @@ -14,48 +14,11 @@ namespace FileSys { namespace { -void PrintSaveDataAttributeWarnings(SaveDataAttribute meta) { - if (meta.type == SaveDataType::SystemSaveData || meta.type == SaveDataType::SaveData) { - if (meta.zero_1 != 0) { - LOG_WARNING(Service_FS, - "Possibly incorrect SaveDataAttribute, 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 SaveDataAttribute, 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 SaveDataAttribute, 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 SaveDataAttribute, type is SystemSaveData but title_id is " - "non-zero ({:016X}).", - meta.title_id); - } - - if (meta.type == SaveDataType::DeviceSaveData && meta.user_id != u128{0, 0}) { - LOG_WARNING(Service_FS, - "Possibly incorrect SaveDataAttribute, type is DeviceSaveData but user_id is " - "non-zero ({:016X}{:016X})", - meta.user_id[1], meta.user_id[0]); - } -} - bool ShouldSaveDataBeAutomaticallyCreated(SaveDataSpaceId space, const SaveDataAttribute& attr) { - return attr.type == SaveDataType::CacheStorage || attr.type == SaveDataType::TemporaryStorage || - (space == SaveDataSpaceId::NandUser && ///< Normal Save Data -- Current Title & User - (attr.type == SaveDataType::SaveData || attr.type == SaveDataType::DeviceSaveData) && - attr.title_id == 0 && attr.save_id == 0); + return attr.type == SaveDataType::Cache || attr.type == SaveDataType::Temporary || + (space == SaveDataSpaceId::User && ///< Normal Save Data -- Current Title & User + (attr.type == SaveDataType::Account || attr.type == SaveDataType::Device) && + attr.program_id == 0 && attr.system_save_data_id == 0); } std::string GetFutureSaveDataPath(SaveDataSpaceId space_id, SaveDataType type, u64 title_id, @@ -63,7 +26,7 @@ std::string GetFutureSaveDataPath(SaveDataSpaceId space_id, SaveDataType type, u // Only detect nand user saves. const auto space_id_path = [space_id]() -> std::string_view { switch (space_id) { - case SaveDataSpaceId::NandUser: + case SaveDataSpaceId::User: return "/user/save"; default: return ""; @@ -79,9 +42,9 @@ std::string GetFutureSaveDataPath(SaveDataSpaceId space_id, SaveDataType type, u // Only detect account/device saves from the future location. switch (type) { - case SaveDataType::SaveData: + case SaveDataType::Account: return fmt::format("{}/account/{}/{:016X}/0", space_id_path, uuid.RawString(), title_id); - case SaveDataType::DeviceSaveData: + case SaveDataType::Device: return fmt::format("{}/device/{:016X}/0", space_id_path, title_id); default: return ""; @@ -90,13 +53,6 @@ std::string GetFutureSaveDataPath(SaveDataSpaceId space_id, SaveDataType type, u } // Anonymous namespace -std::string SaveDataAttribute::DebugInfo() const { - return fmt::format("[title_id={:016X}, user_id={:016X}{:016X}, save_id={:016X}, type={:02X}, " - "rank={}, index={}]", - title_id, user_id[1], user_id[0], save_id, static_cast<u8>(type), - static_cast<u8>(rank), index); -} - SaveDataFactory::SaveDataFactory(Core::System& system_, ProgramId program_id_, VirtualDir save_directory_) : system{system_}, program_id{program_id_}, dir{std::move(save_directory_)} { @@ -108,18 +64,16 @@ SaveDataFactory::SaveDataFactory(Core::System& system_, ProgramId program_id_, SaveDataFactory::~SaveDataFactory() = default; VirtualDir SaveDataFactory::Create(SaveDataSpaceId space, const SaveDataAttribute& meta) const { - PrintSaveDataAttributeWarnings(meta); - - const auto save_directory = - GetFullPath(program_id, dir, space, meta.type, meta.title_id, meta.user_id, meta.save_id); + const auto save_directory = GetFullPath(program_id, dir, space, meta.type, meta.program_id, + meta.user_id, meta.system_save_data_id); return dir->CreateDirectoryRelative(save_directory); } VirtualDir SaveDataFactory::Open(SaveDataSpaceId space, const SaveDataAttribute& meta) const { - const auto save_directory = - GetFullPath(program_id, dir, space, meta.type, meta.title_id, meta.user_id, meta.save_id); + const auto save_directory = GetFullPath(program_id, dir, space, meta.type, meta.program_id, + meta.user_id, meta.system_save_data_id); auto out = dir->GetDirectoryRelative(save_directory); @@ -136,11 +90,11 @@ VirtualDir SaveDataFactory::GetSaveDataSpaceDirectory(SaveDataSpaceId space) con std::string SaveDataFactory::GetSaveDataSpaceIdPath(SaveDataSpaceId space) { switch (space) { - case SaveDataSpaceId::NandSystem: + case SaveDataSpaceId::System: return "/system/"; - case SaveDataSpaceId::NandUser: + case SaveDataSpaceId::User: return "/user/"; - case SaveDataSpaceId::TemporaryStorage: + case SaveDataSpaceId::Temporary: return "/temp/"; default: ASSERT_MSG(false, "Unrecognized SaveDataSpaceId: {:02X}", static_cast<u8>(space)); @@ -153,7 +107,7 @@ std::string SaveDataFactory::GetFullPath(ProgramId program_id, VirtualDir dir, u128 user_id, u64 save_id) { // 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 || type == SaveDataType::DeviceSaveData) { + if (type == SaveDataType::Account || type == SaveDataType::Device) { if (title_id == 0) { title_id = program_id; } @@ -173,16 +127,16 @@ std::string SaveDataFactory::GetFullPath(ProgramId program_id, VirtualDir dir, std::string out = GetSaveDataSpaceIdPath(space); switch (type) { - case SaveDataType::SystemSaveData: + case SaveDataType::System: return fmt::format("{}save/{:016X}/{:016X}{:016X}", out, save_id, user_id[1], user_id[0]); - case SaveDataType::SaveData: - case SaveDataType::DeviceSaveData: + case SaveDataType::Account: + case SaveDataType::Device: return fmt::format("{}save/{:016X}/{:016X}{:016X}/{:016X}", out, 0, user_id[1], user_id[0], title_id); - case SaveDataType::TemporaryStorage: + case SaveDataType::Temporary: return fmt::format("{}{:016X}/{:016X}{:016X}/{:016X}", out, 0, user_id[1], user_id[0], title_id); - case SaveDataType::CacheStorage: + case SaveDataType::Cache: return fmt::format("{}save/cache/{:016X}", out, title_id); default: ASSERT_MSG(false, "Unrecognized SaveDataType: {:02X}", static_cast<u8>(type)); @@ -202,7 +156,7 @@ std::string SaveDataFactory::GetUserGameSaveDataRoot(u128 user_id, bool future) SaveDataSize SaveDataFactory::ReadSaveDataSize(SaveDataType type, u64 title_id, u128 user_id) const { const auto path = - GetFullPath(program_id, dir, SaveDataSpaceId::NandUser, type, title_id, user_id, 0); + GetFullPath(program_id, dir, SaveDataSpaceId::User, type, title_id, user_id, 0); const auto relative_dir = GetOrCreateDirectoryRelative(dir, path); const auto size_file = relative_dir->GetFile(GetSaveDataSizeFileName()); @@ -221,7 +175,7 @@ SaveDataSize SaveDataFactory::ReadSaveDataSize(SaveDataType type, u64 title_id, void SaveDataFactory::WriteSaveDataSize(SaveDataType type, u64 title_id, u128 user_id, SaveDataSize new_value) const { const auto path = - GetFullPath(program_id, dir, SaveDataSpaceId::NandUser, type, title_id, user_id, 0); + GetFullPath(program_id, dir, SaveDataSpaceId::User, type, title_id, user_id, 0); const auto relative_dir = GetOrCreateDirectoryRelative(dir, path); const auto size_file = relative_dir->CreateFile(GetSaveDataSizeFileName()); diff --git a/src/core/file_sys/savedata_factory.h b/src/core/file_sys/savedata_factory.h index 5ab7e4d32..15dd4ec7d 100644 --- a/src/core/file_sys/savedata_factory.h +++ b/src/core/file_sys/savedata_factory.h @@ -7,6 +7,7 @@ #include <string> #include "common/common_funcs.h" #include "common/common_types.h" +#include "core/file_sys/fs_save_data_types.h" #include "core/file_sys/vfs/vfs.h" #include "core/hle/result.h" @@ -16,73 +17,6 @@ class System; namespace FileSys { -enum class SaveDataSpaceId : u8 { - NandSystem = 0, - NandUser = 1, - SdCardSystem = 2, - TemporaryStorage = 3, - SdCardUser = 4, - ProperSystem = 100, - SafeMode = 101, -}; - -enum class SaveDataType : u8 { - SystemSaveData = 0, - SaveData = 1, - BcatDeliveryCacheStorage = 2, - DeviceSaveData = 3, - TemporaryStorage = 4, - CacheStorage = 5, - SystemBcat = 6, -}; - -enum class SaveDataRank : u8 { - Primary = 0, - Secondary = 1, -}; - -enum class SaveDataFlags : u32 { - None = (0 << 0), - KeepAfterResettingSystemSaveData = (1 << 0), - KeepAfterRefurbishment = (1 << 1), - KeepAfterResettingSystemSaveDataWithoutUserSaveData = (1 << 2), - NeedsSecureDelete = (1 << 3), -}; - -struct SaveDataAttribute { - u64 title_id; - u128 user_id; - u64 save_id; - SaveDataType type; - SaveDataRank rank; - u16 index; - INSERT_PADDING_BYTES_NOINIT(4); - u64 zero_1; - u64 zero_2; - u64 zero_3; - - std::string DebugInfo() const; -}; -static_assert(sizeof(SaveDataAttribute) == 0x40, "SaveDataAttribute has incorrect size."); - -struct SaveDataExtraData { - SaveDataAttribute attr; - u64 owner_id; - s64 timestamp; - SaveDataFlags flags; - INSERT_PADDING_BYTES_NOINIT(4); - s64 available_size; - s64 journal_size; - s64 commit_id; - std::array<u8, 0x190> unused; -}; -static_assert(sizeof(SaveDataExtraData) == 0x200, "SaveDataExtraData has incorrect size."); - -struct SaveDataSize { - u64 normal; - u64 journal; -}; - constexpr const char* GetSaveDataSizeFileName() { return ".yuzu_save_size"; } |