diff options
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/core/file_sys/fs_filesystem.h | 2 | ||||
-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_string_util.h | 15 | ||||
-rw-r--r-- | src/core/file_sys/fsa/fs_i_directory.h | 89 | ||||
-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 | 207 | ||||
-rw-r--r-- | src/core/hle/service/filesystem/fsp/fs_i_directory.cpp | 58 | ||||
-rw-r--r-- | src/core/hle/service/filesystem/fsp/fs_i_directory.h | 7 | ||||
-rw-r--r-- | src/core/hle/service/filesystem/fsp/fs_i_file.cpp | 74 | ||||
-rw-r--r-- | src/core/hle/service/filesystem/fsp/fs_i_file.h | 5 | ||||
-rw-r--r-- | src/core/hle/service/filesystem/fsp/fs_i_filesystem.cpp | 41 | ||||
-rw-r--r-- | src/core/hle/service/filesystem/fsp/fs_i_filesystem.h | 5 |
14 files changed, 554 insertions, 130 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 23f717472..24dcc405f 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -59,8 +59,12 @@ add_library(core STATIC file_sys/fs_path.h file_sys/fs_path_utility.h file_sys/fs_string_util.h + file_sys/fsa/fs_i_directory.h + file_sys/fsa/fs_i_file.h + file_sys/fsa/fs_i_filesystem.h file_sys/fsmitm_romfsbuild.cpp file_sys/fsmitm_romfsbuild.h + file_sys/fssrv/fssrv_sf_path.h file_sys/fssystem/fs_i_storage.h file_sys/fssystem/fs_types.h file_sys/fssystem/fssystem_aes_ctr_counter_extended_storage.cpp diff --git a/src/core/file_sys/fs_filesystem.h b/src/core/file_sys/fs_filesystem.h index 7f237b7fa..7065ef6a6 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 = (1 << 31), }; DECLARE_ENUM_FLAG_OPERATORS(OpenDirectoryMode) 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_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..a4135efec --- /dev/null +++ b/src/core/file_sys/fsa/fs_i_directory.h @@ -0,0 +1,89 @@ +// 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: + 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(entries, backend->GetSubdirectories(), DirectoryEntryType::Directory); + } + if (True(mode & OpenDirectoryMode::File)) { + BuildEntryIndex(entries, 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: + virtual 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); + auto* begin = reinterpret_cast<u8*>(entries.data() + next_entry_index); + + next_entry_index += actual_entries; + *out_count = actual_entries; + + out_entries = reinterpret_cast<DirectoryEntry*>(begin); + + R_SUCCEED(); + } + + virtual Result DoGetEntryCount(s64* out) { + *out = entries.size() - next_entry_index; + R_SUCCEED(); + } + + // TODO: Remove this when VFS is gone + template <typename T> + void BuildEntryIndex(std::vector<DirectoryEntry>& entries, 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..6dd0f6439 --- /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: + 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) { + std::vector<u8> output = backend->ReadBytes(size, offset); + *out = output.size(); + buffer = output.data(); + 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..e92284459 --- /dev/null +++ b/src/core/file_sys/fsa/fs_i_filesystem.h @@ -0,0 +1,207 @@ +// 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 { + SetConcatenationFileAttribute = 0, + UpdateMac = 1, + IsSignedSystemPartitionOnSdCardValid = 2, + QueryUnpreparedFileInformation = 3, +}; + +class IFileSystem { +public: + 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/hle/service/filesystem/fsp/fs_i_directory.cpp b/src/core/hle/service/filesystem/fsp/fs_i_directory.cpp index 39690018b..661da5326 100644 --- a/src/core/hle/service/filesystem/fsp/fs_i_directory.cpp +++ b/src/core/hle/service/filesystem/fsp/fs_i_directory.cpp @@ -8,43 +8,15 @@ namespace Service::FileSystem { -template <typename T> -static void BuildEntryIndex(std::vector<FileSys::DirectoryEntry>& entries, - const std::vector<T>& new_data, FileSys::DirectoryEntryType type) { - entries.reserve(entries.size() + new_data.size()); - - for (const auto& new_entry : new_data) { - auto name = new_entry->GetName(); - - if (type == FileSys::DirectoryEntryType::File && - name == FileSys::GetSaveDataSizeFileName()) { - continue; - } - - entries.emplace_back(name, static_cast<s8>(type), - type == FileSys::DirectoryEntryType::Directory ? 0 - : new_entry->GetSize()); - } -} - -IDirectory::IDirectory(Core::System& system_, FileSys::VirtualDir backend_, +IDirectory::IDirectory(Core::System& system_, FileSys::VirtualDir directory_, FileSys::OpenDirectoryMode mode) - : ServiceFramework{system_, "IDirectory"}, backend(std::move(backend_)) { + : ServiceFramework{system_, "IDirectory"}, + backend(std::make_unique<FileSys::Fsa::IDirectory>(directory_, mode)) { static const FunctionInfo functions[] = { {0, &IDirectory::Read, "Read"}, {1, &IDirectory::GetEntryCount, "GetEntryCount"}, }; RegisterHandlers(functions); - - // TODO(DarkLordZach): Verify that this is the correct behavior. - // Build entry index now to save time later. - if (True(mode & FileSys::OpenDirectoryMode::Directory)) { - BuildEntryIndex(entries, backend->GetSubdirectories(), - FileSys::DirectoryEntryType::Directory); - } - if (True(mode & FileSys::OpenDirectoryMode::File)) { - BuildEntryIndex(entries, backend->GetFiles(), FileSys::DirectoryEntryType::File); - } } void IDirectory::Read(HLERequestContext& ctx) { @@ -53,32 +25,26 @@ void IDirectory::Read(HLERequestContext& ctx) { // Calculate how many entries we can fit in the output buffer const u64 count_entries = ctx.GetWriteBufferNumElements<FileSys::DirectoryEntry>(); - // Cap at total number of entries. - const u64 actual_entries = std::min(count_entries, entries.size() - next_entry_index); - - // Determine data start and end - 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; + s64 out_count{}; + FileSys::DirectoryEntry* out_entries = nullptr; + const auto result = backend->Read(&out_count, out_entries, count_entries); // Write the data to memory - ctx.WriteBuffer(begin, range_size); + ctx.WriteBuffer(out_entries, out_count); IPC::ResponseBuilder rb{ctx, 4}; - rb.Push(ResultSuccess); - rb.Push(actual_entries); + rb.Push(result); + rb.Push(out_count); } void IDirectory::GetEntryCount(HLERequestContext& ctx) { LOG_DEBUG(Service_FS, "called"); - u64 count = entries.size() - next_entry_index; + s64 out_count{}; IPC::ResponseBuilder rb{ctx, 4}; - rb.Push(ResultSuccess); - rb.Push(count); + rb.Push(backend->GetEntryCount(&out_count)); + rb.Push(out_count); } } // namespace Service::FileSystem diff --git a/src/core/hle/service/filesystem/fsp/fs_i_directory.h b/src/core/hle/service/filesystem/fsp/fs_i_directory.h index 793ecfcd7..0dec4367b 100644 --- a/src/core/hle/service/filesystem/fsp/fs_i_directory.h +++ b/src/core/hle/service/filesystem/fsp/fs_i_directory.h @@ -3,6 +3,7 @@ #pragma once +#include "core/file_sys/fsa/fs_i_directory.h" #include "core/file_sys/vfs/vfs.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/hle/service/service.h" @@ -15,13 +16,11 @@ namespace Service::FileSystem { class IDirectory final : public ServiceFramework<IDirectory> { public: - explicit IDirectory(Core::System& system_, FileSys::VirtualDir backend_, + explicit IDirectory(Core::System& system_, FileSys::VirtualDir directory_, FileSys::OpenDirectoryMode mode); private: - FileSys::VirtualDir backend; - std::vector<FileSys::DirectoryEntry> entries; - u64 next_entry_index = 0; + std::unique_ptr<FileSys::Fsa::IDirectory> backend; void Read(HLERequestContext& ctx); void GetEntryCount(HLERequestContext& ctx); diff --git a/src/core/hle/service/filesystem/fsp/fs_i_file.cpp b/src/core/hle/service/filesystem/fsp/fs_i_file.cpp index 9a18f6ec5..8fb8620de 100644 --- a/src/core/hle/service/filesystem/fsp/fs_i_file.cpp +++ b/src/core/hle/service/filesystem/fsp/fs_i_file.cpp @@ -7,8 +7,8 @@ namespace Service::FileSystem { -IFile::IFile(Core::System& system_, FileSys::VirtualFile backend_) - : ServiceFramework{system_, "IFile"}, backend(std::move(backend_)) { +IFile::IFile(Core::System& system_, FileSys::VirtualFile file_) + : ServiceFramework{system_, "IFile"}, backend{std::make_unique<FileSys::Fsa::IFile>(file_)} { static const FunctionInfo functions[] = { {0, &IFile::Read, "Read"}, {1, &IFile::Write, "Write"}, @@ -29,79 +29,40 @@ void IFile::Read(HLERequestContext& ctx) { LOG_DEBUG(Service_FS, "called, option={}, offset=0x{:X}, length={}", option, offset, length); - // Error checking - if (length < 0) { - LOG_ERROR(Service_FS, "Length is less than 0, length={}", length); - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(FileSys::ResultInvalidSize); - return; - } - if (offset < 0) { - LOG_ERROR(Service_FS, "Offset is less than 0, offset={}", offset); - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(FileSys::ResultInvalidOffset); - return; - } - // Read the data from the Storage backend - std::vector<u8> output = backend->ReadBytes(length, offset); + std::vector<u8> output(length); + std::size_t bytes_read; + const auto result = backend->Read(&bytes_read, offset, output.data(), length); // Write the data to memory ctx.WriteBuffer(output); IPC::ResponseBuilder rb{ctx, 4}; - rb.Push(ResultSuccess); - rb.Push(static_cast<u64>(output.size())); + rb.Push(result); + rb.Push(static_cast<u64>(bytes_read)); } void IFile::Write(HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - const u64 option = rp.Pop<u64>(); + const auto option = rp.PopRaw<FileSys::WriteOption>(); + [[maybe_unused]] const u32 unused = rp.Pop<u32>(); const s64 offset = rp.Pop<s64>(); const s64 length = rp.Pop<s64>(); - LOG_DEBUG(Service_FS, "called, option={}, offset=0x{:X}, length={}", option, offset, length); - - // Error checking - if (length < 0) { - LOG_ERROR(Service_FS, "Length is less than 0, length={}", length); - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(FileSys::ResultInvalidSize); - return; - } - if (offset < 0) { - LOG_ERROR(Service_FS, "Offset is less than 0, offset={}", offset); - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(FileSys::ResultInvalidOffset); - return; - } + LOG_DEBUG(Service_FS, "called, option={}, offset=0x{:X}, length={}", option.value, offset, + length); const auto data = ctx.ReadBuffer(); - ASSERT_MSG(static_cast<s64>(data.size()) <= length, - "Attempting to write more data than requested (requested={:016X}, actual={:016X}).", - length, data.size()); - - // Write the data to the Storage backend - const auto write_size = - static_cast<std::size_t>(std::distance(data.begin(), data.begin() + length)); - const std::size_t written = backend->Write(data.data(), write_size, offset); - - ASSERT_MSG(static_cast<s64>(written) == length, - "Could not write all bytes to file (requested={:016X}, actual={:016X}).", length, - written); - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); + rb.Push(backend->Write(offset, data.data(), length, option)); } void IFile::Flush(HLERequestContext& ctx) { LOG_DEBUG(Service_FS, "called"); - // Exists for SDK compatibiltity -- No need to flush file. - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); + rb.Push(backend->Flush()); } void IFile::SetSize(HLERequestContext& ctx) { @@ -109,18 +70,17 @@ void IFile::SetSize(HLERequestContext& ctx) { const u64 size = rp.Pop<u64>(); LOG_DEBUG(Service_FS, "called, size={}", size); - backend->Resize(size); - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); + rb.Push(backend->SetSize(size)); } void IFile::GetSize(HLERequestContext& ctx) { - const u64 size = backend->GetSize(); + s64 size; + const auto result = backend->GetSize(&size); LOG_DEBUG(Service_FS, "called, size={}", size); IPC::ResponseBuilder rb{ctx, 4}; - rb.Push(ResultSuccess); + rb.Push(result); rb.Push<u64>(size); } diff --git a/src/core/hle/service/filesystem/fsp/fs_i_file.h b/src/core/hle/service/filesystem/fsp/fs_i_file.h index 5e5430c67..887fd3ba2 100644 --- a/src/core/hle/service/filesystem/fsp/fs_i_file.h +++ b/src/core/hle/service/filesystem/fsp/fs_i_file.h @@ -3,6 +3,7 @@ #pragma once +#include "core/file_sys/fsa/fs_i_file.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/hle/service/service.h" @@ -10,10 +11,10 @@ namespace Service::FileSystem { class IFile final : public ServiceFramework<IFile> { public: - explicit IFile(Core::System& system_, FileSys::VirtualFile backend_); + explicit IFile(Core::System& system_, FileSys::VirtualFile file_); private: - FileSys::VirtualFile backend; + std::unique_ptr<FileSys::Fsa::IFile> backend; void Read(HLERequestContext& ctx); void Write(HLERequestContext& ctx); diff --git a/src/core/hle/service/filesystem/fsp/fs_i_filesystem.cpp b/src/core/hle/service/filesystem/fsp/fs_i_filesystem.cpp index efa394dd1..1e69d22b8 100644 --- a/src/core/hle/service/filesystem/fsp/fs_i_filesystem.cpp +++ b/src/core/hle/service/filesystem/fsp/fs_i_filesystem.cpp @@ -9,9 +9,9 @@ namespace Service::FileSystem { -IFileSystem::IFileSystem(Core::System& system_, FileSys::VirtualDir backend_, SizeGetter size_) - : ServiceFramework{system_, "IFileSystem"}, backend{std::move(backend_)}, size{std::move( - size_)} { +IFileSystem::IFileSystem(Core::System& system_, FileSys::VirtualDir dir_, SizeGetter size_) + : ServiceFramework{system_, "IFileSystem"}, + backend{std::make_unique<FileSys::Fsa::IFileSystem>(dir_)}, size{std::move(size_)} { static const FunctionInfo functions[] = { {0, &IFileSystem::CreateFile, "CreateFile"}, {1, &IFileSystem::DeleteFile, "DeleteFile"}, @@ -39,6 +39,7 @@ void IFileSystem::CreateFile(HLERequestContext& ctx) { const auto file_buffer = ctx.ReadBuffer(); const std::string name = Common::StringFromBuffer(file_buffer); + const auto path = FileSys::Path(name.c_str()); const u64 file_mode = rp.Pop<u64>(); const u32 file_size = rp.Pop<u32>(); @@ -47,67 +48,75 @@ void IFileSystem::CreateFile(HLERequestContext& ctx) { file_size); IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(backend.CreateFile(name, file_size)); + rb.Push(backend->CreateFile(path, file_size)); } void IFileSystem::DeleteFile(HLERequestContext& ctx) { const auto file_buffer = ctx.ReadBuffer(); const std::string name = Common::StringFromBuffer(file_buffer); + const auto path = FileSys::Path(name.c_str()); LOG_DEBUG(Service_FS, "called. file={}", name); IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(backend.DeleteFile(name)); + rb.Push(backend->DeleteFile(path)); } void IFileSystem::CreateDirectory(HLERequestContext& ctx) { const auto file_buffer = ctx.ReadBuffer(); const std::string name = Common::StringFromBuffer(file_buffer); + const auto path = FileSys::Path(name.c_str()); LOG_DEBUG(Service_FS, "called. directory={}", name); IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(backend.CreateDirectory(name)); + rb.Push(backend->CreateDirectory(path)); } void IFileSystem::DeleteDirectory(HLERequestContext& ctx) { const auto file_buffer = ctx.ReadBuffer(); const std::string name = Common::StringFromBuffer(file_buffer); + const auto path = FileSys::Path(name.c_str()); LOG_DEBUG(Service_FS, "called. directory={}", name); IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(backend.DeleteDirectory(name)); + rb.Push(backend->DeleteDirectory(path)); } void IFileSystem::DeleteDirectoryRecursively(HLERequestContext& ctx) { const auto file_buffer = ctx.ReadBuffer(); const std::string name = Common::StringFromBuffer(file_buffer); + const auto path = FileSys::Path(name.c_str()); LOG_DEBUG(Service_FS, "called. directory={}", name); IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(backend.DeleteDirectoryRecursively(name)); + rb.Push(backend->DeleteDirectoryRecursively(path)); } void IFileSystem::CleanDirectoryRecursively(HLERequestContext& ctx) { const auto file_buffer = ctx.ReadBuffer(); const std::string name = Common::StringFromBuffer(file_buffer); + const auto path = FileSys::Path(name.c_str()); LOG_DEBUG(Service_FS, "called. Directory: {}", name); IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(backend.CleanDirectoryRecursively(name)); + rb.Push(backend->CleanDirectoryRecursively(path)); } void IFileSystem::RenameFile(HLERequestContext& ctx) { const std::string src_name = Common::StringFromBuffer(ctx.ReadBuffer(0)); const std::string dst_name = Common::StringFromBuffer(ctx.ReadBuffer(1)); + const auto src_path = FileSys::Path(src_name.c_str()); + const auto dst_path = FileSys::Path(dst_name.c_str()); + LOG_DEBUG(Service_FS, "called. file '{}' to file '{}'", src_name, dst_name); IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(backend.RenameFile(src_name, dst_name)); + rb.Push(backend->RenameFile(src_path, dst_path)); } void IFileSystem::OpenFile(HLERequestContext& ctx) { @@ -115,13 +124,14 @@ void IFileSystem::OpenFile(HLERequestContext& ctx) { const auto file_buffer = ctx.ReadBuffer(); const std::string name = Common::StringFromBuffer(file_buffer); + const auto path = FileSys::Path(name.c_str()); const auto mode = static_cast<FileSys::OpenMode>(rp.Pop<u32>()); LOG_DEBUG(Service_FS, "called. file={}, mode={}", name, mode); FileSys::VirtualFile vfs_file{}; - auto result = backend.OpenFile(&vfs_file, name, mode); + auto result = backend->OpenFile(&vfs_file, path, mode); if (result != ResultSuccess) { IPC::ResponseBuilder rb{ctx, 2}; rb.Push(result); @@ -140,12 +150,13 @@ void IFileSystem::OpenDirectory(HLERequestContext& ctx) { const auto file_buffer = ctx.ReadBuffer(); const std::string name = Common::StringFromBuffer(file_buffer); + const auto path = FileSys::Path(name.c_str()); const auto mode = rp.PopRaw<FileSys::OpenDirectoryMode>(); LOG_DEBUG(Service_FS, "called. directory={}, mode={}", name, mode); FileSys::VirtualDir vfs_dir{}; - auto result = backend.OpenDirectory(&vfs_dir, name); + auto result = backend->OpenDirectory(&vfs_dir, path, mode); if (result != ResultSuccess) { IPC::ResponseBuilder rb{ctx, 2}; rb.Push(result); @@ -162,11 +173,12 @@ void IFileSystem::OpenDirectory(HLERequestContext& ctx) { void IFileSystem::GetEntryType(HLERequestContext& ctx) { const auto file_buffer = ctx.ReadBuffer(); const std::string name = Common::StringFromBuffer(file_buffer); + const auto path = FileSys::Path(name.c_str()); LOG_DEBUG(Service_FS, "called. file={}", name); FileSys::DirectoryEntryType vfs_entry_type{}; - auto result = backend.GetEntryType(&vfs_entry_type, name); + auto result = backend->GetEntryType(&vfs_entry_type, path); if (result != ResultSuccess) { IPC::ResponseBuilder rb{ctx, 2}; rb.Push(result); @@ -204,11 +216,12 @@ void IFileSystem::GetTotalSpaceSize(HLERequestContext& ctx) { void IFileSystem::GetFileTimeStampRaw(HLERequestContext& ctx) { const auto file_buffer = ctx.ReadBuffer(); const std::string name = Common::StringFromBuffer(file_buffer); + const auto path = FileSys::Path(name.c_str()); LOG_WARNING(Service_FS, "(Partial Implementation) called. file={}", name); FileSys::FileTimeStampRaw vfs_timestamp{}; - auto result = backend.GetFileTimeStampRaw(&vfs_timestamp, name); + auto result = backend->GetFileTimeStampRaw(&vfs_timestamp, path); if (result != ResultSuccess) { IPC::ResponseBuilder rb{ctx, 2}; rb.Push(result); diff --git a/src/core/hle/service/filesystem/fsp/fs_i_filesystem.h b/src/core/hle/service/filesystem/fsp/fs_i_filesystem.h index b06b3ef0e..d500be725 100644 --- a/src/core/hle/service/filesystem/fsp/fs_i_filesystem.h +++ b/src/core/hle/service/filesystem/fsp/fs_i_filesystem.h @@ -3,6 +3,7 @@ #pragma once +#include "core/file_sys/fsa/fs_i_filesystem.h" #include "core/file_sys/vfs/vfs.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/hle/service/filesystem/fsp/fsp_util.h" @@ -12,7 +13,7 @@ namespace Service::FileSystem { class IFileSystem final : public ServiceFramework<IFileSystem> { public: - explicit IFileSystem(Core::System& system_, FileSys::VirtualDir backend_, SizeGetter size_); + explicit IFileSystem(Core::System& system_, FileSys::VirtualDir dir_, SizeGetter size_); void CreateFile(HLERequestContext& ctx); void DeleteFile(HLERequestContext& ctx); @@ -31,7 +32,7 @@ public: void GetFileSystemAttribute(HLERequestContext& ctx); private: - VfsDirectoryServiceWrapper backend; + std::unique_ptr<FileSys::Fsa::IFileSystem> backend; SizeGetter size; }; |