diff options
Diffstat (limited to '')
-rw-r--r-- | src/core/file_sys/disk_archive.cpp | 14 | ||||
-rw-r--r-- | src/core/file_sys/disk_archive.h | 4 | ||||
-rw-r--r-- | src/core/file_sys/file_backend.h | 9 | ||||
-rw-r--r-- | src/core/file_sys/ivfc_archive.cpp | 9 | ||||
-rw-r--r-- | src/core/file_sys/ivfc_archive.h | 4 | ||||
-rw-r--r-- | src/core/hle/result.h | 1 | ||||
-rw-r--r-- | src/core/hle/service/fs/archive.cpp | 17 |
7 files changed, 40 insertions, 18 deletions
diff --git a/src/core/file_sys/disk_archive.cpp b/src/core/file_sys/disk_archive.cpp index 0c55a4863..2dca895fd 100644 --- a/src/core/file_sys/disk_archive.cpp +++ b/src/core/file_sys/disk_archive.cpp @@ -124,17 +124,23 @@ bool DiskFile::Open() { return file->IsOpen(); } -size_t DiskFile::Read(const u64 offset, const size_t length, u8* buffer) const { +ResultVal<size_t> DiskFile::Read(const u64 offset, const size_t length, u8* buffer) const { + if (!mode.read_flag && !mode.write_flag) + return ResultCode(ErrorDescription::FS_InvalidOpenFlags, ErrorModule::FS, ErrorSummary::Canceled, ErrorLevel::Status); + file->Seek(offset, SEEK_SET); - return file->ReadBytes(buffer, length); + return MakeResult<size_t>(file->ReadBytes(buffer, length)); } -size_t DiskFile::Write(const u64 offset, const size_t length, const bool flush, const u8* buffer) const { +ResultVal<size_t> DiskFile::Write(const u64 offset, const size_t length, const bool flush, const u8* buffer) const { + if (!mode.write_flag) + return ResultCode(ErrorDescription::FS_InvalidOpenFlags, ErrorModule::FS, ErrorSummary::Canceled, ErrorLevel::Status); + file->Seek(offset, SEEK_SET); size_t written = file->WriteBytes(buffer, length); if (flush) file->Flush(); - return written; + return MakeResult<size_t>(written); } u64 DiskFile::GetSize() const { diff --git a/src/core/file_sys/disk_archive.h b/src/core/file_sys/disk_archive.h index c0a3d3f7b..96d86ad21 100644 --- a/src/core/file_sys/disk_archive.h +++ b/src/core/file_sys/disk_archive.h @@ -55,8 +55,8 @@ public: DiskFile(const DiskArchive& archive, const Path& path, const Mode mode); bool Open() override; - size_t Read(u64 offset, size_t length, u8* buffer) const override; - size_t Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; + ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override; + ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; u64 GetSize() const override; bool SetSize(u64 size) const override; bool Close() const override; diff --git a/src/core/file_sys/file_backend.h b/src/core/file_sys/file_backend.h index df7165df3..21864a73c 100644 --- a/src/core/file_sys/file_backend.h +++ b/src/core/file_sys/file_backend.h @@ -7,6 +7,7 @@ #include <cstddef> #include "common/common_types.h" +#include "core/hle/result.h" //////////////////////////////////////////////////////////////////////////////////////////////////// // FileSys namespace @@ -29,9 +30,9 @@ public: * @param offset Offset in bytes to start reading data from * @param length Length in bytes of data to read from file * @param buffer Buffer to read data into - * @return Number of bytes read + * @return Number of bytes read, or error code */ - virtual size_t Read(u64 offset, size_t length, u8* buffer) const = 0; + virtual ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const = 0; /** * Write data to the file @@ -39,9 +40,9 @@ public: * @param length Length in bytes of data to write to file * @param flush The flush parameters (0 == do not flush) * @param buffer Buffer to read data from - * @return Number of bytes written + * @return Number of bytes written, or error code */ - virtual size_t Write(u64 offset, size_t length, bool flush, const u8* buffer) const = 0; + virtual ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const = 0; /** * Get the size of the file in bytes diff --git a/src/core/file_sys/ivfc_archive.cpp b/src/core/file_sys/ivfc_archive.cpp index f2f96ef1a..e7b37e09d 100644 --- a/src/core/file_sys/ivfc_archive.cpp +++ b/src/core/file_sys/ivfc_archive.cpp @@ -68,17 +68,18 @@ u64 IVFCArchive::GetFreeBytes() const { //////////////////////////////////////////////////////////////////////////////////////////////////// -size_t IVFCFile::Read(const u64 offset, const size_t length, u8* buffer) const { +ResultVal<size_t> IVFCFile::Read(const u64 offset, const size_t length, u8* buffer) const { LOG_TRACE(Service_FS, "called offset=%llu, length=%zu", offset, length); romfs_file->Seek(data_offset + offset, SEEK_SET); size_t read_length = (size_t)std::min((u64)length, data_size - offset); - return romfs_file->ReadBytes(buffer, read_length); + return MakeResult<size_t>(romfs_file->ReadBytes(buffer, read_length)); } -size_t IVFCFile::Write(const u64 offset, const size_t length, const bool flush, const u8* buffer) const { +ResultVal<size_t> IVFCFile::Write(const u64 offset, const size_t length, const bool flush, const u8* buffer) const { LOG_ERROR(Service_FS, "Attempted to write to IVFC file"); - return 0; + // TODO(Subv): Find error code + return MakeResult<size_t>(0); } u64 IVFCFile::GetSize() const { diff --git a/src/core/file_sys/ivfc_archive.h b/src/core/file_sys/ivfc_archive.h index 5d3a5b61e..acc7e60f5 100644 --- a/src/core/file_sys/ivfc_archive.h +++ b/src/core/file_sys/ivfc_archive.h @@ -56,8 +56,8 @@ public: : romfs_file(file), data_offset(offset), data_size(size) {} bool Open() override { return true; } - size_t Read(u64 offset, size_t length, u8* buffer) const override; - size_t Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; + ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override; + ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; u64 GetSize() const override; bool SetSize(u64 size) const override; bool Close() const override { return false; } diff --git a/src/core/hle/result.h b/src/core/hle/result.h index b9ee5f7d9..b68c0ff0d 100644 --- a/src/core/hle/result.h +++ b/src/core/hle/result.h @@ -21,6 +21,7 @@ enum class ErrorDescription : u32 { WrongAddress = 53, FS_NotFound = 120, FS_AlreadyExists = 190, + FS_InvalidOpenFlags = 230, FS_NotAFile = 250, FS_NotFormatted = 340, ///< This is used by the FS service when creating a SaveData archive InvalidSection = 1000, diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp index cb98fa7aa..8c38c3ba4 100644 --- a/src/core/hle/service/fs/archive.cpp +++ b/src/core/hle/service/fs/archive.cpp @@ -103,7 +103,14 @@ ResultVal<bool> File::SyncRequest() { u32 address = cmd_buff[5]; LOG_TRACE(Service_FS, "Read %s %s: offset=0x%llx length=%d address=0x%x", GetTypeName().c_str(), GetName().c_str(), offset, length, address); - cmd_buff[2] = static_cast<u32>(backend->Read(offset, length, Memory::GetPointer(address))); + if (offset + length > backend->GetSize()) + LOG_ERROR(Service_FS, "Reading from out of bounds offset=0x%llX length=0x%08X file_size=0x%llX", offset, length, backend->GetSize()); + ResultVal<size_t> read = backend->Read(offset, length, Memory::GetPointer(address)); + if (read.Failed()) { + cmd_buff[1] = read.Code().raw; + return read.Code(); + } + cmd_buff[2] = static_cast<u32>(read.MoveFrom()); break; } @@ -116,7 +123,13 @@ ResultVal<bool> File::SyncRequest() { u32 address = cmd_buff[6]; LOG_TRACE(Service_FS, "Write %s %s: offset=0x%llx length=%d address=0x%x, flush=0x%x", GetTypeName().c_str(), GetName().c_str(), offset, length, address, flush); - cmd_buff[2] = static_cast<u32>(backend->Write(offset, length, flush != 0, Memory::GetPointer(address))); + + ResultVal<size_t> written = backend->Write(offset, length, flush != 0, Memory::GetPointer(address)); + if (written.Failed()) { + cmd_buff[1] = written.Code().raw; + return written.Code(); + } + cmd_buff[2] = static_cast<u32>(written.MoveFrom()); break; } |