HLE/FS: Return the proper error codes on file Read/Write operations.
These operations are limited by the open flags specified while opening the file.
This commit is contained in:
		| @@ -124,17 +124,23 @@ bool DiskFile::Open() { | |||||||
|     return file->IsOpen(); |     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); |     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); |     file->Seek(offset, SEEK_SET); | ||||||
|     size_t written = file->WriteBytes(buffer, length); |     size_t written = file->WriteBytes(buffer, length); | ||||||
|     if (flush) |     if (flush) | ||||||
|         file->Flush(); |         file->Flush(); | ||||||
|     return written; |     return MakeResult<size_t>(written); | ||||||
| } | } | ||||||
|  |  | ||||||
| u64 DiskFile::GetSize() const { | u64 DiskFile::GetSize() const { | ||||||
|   | |||||||
| @@ -55,8 +55,8 @@ public: | |||||||
|     DiskFile(const DiskArchive& archive, const Path& path, const Mode mode); |     DiskFile(const DiskArchive& archive, const Path& path, const Mode mode); | ||||||
|  |  | ||||||
|     bool Open() override; |     bool Open() override; | ||||||
|     size_t Read(u64 offset, size_t length, u8* buffer) const override; |     ResultVal<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> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; | ||||||
|     u64 GetSize() const override; |     u64 GetSize() const override; | ||||||
|     bool SetSize(u64 size) const override; |     bool SetSize(u64 size) const override; | ||||||
|     bool Close() const override; |     bool Close() const override; | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ | |||||||
| #include <cstddef> | #include <cstddef> | ||||||
|  |  | ||||||
| #include "common/common_types.h" | #include "common/common_types.h" | ||||||
|  | #include "core/hle/result.h" | ||||||
|  |  | ||||||
| //////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||||||
| // FileSys namespace | // FileSys namespace | ||||||
| @@ -29,9 +30,9 @@ public: | |||||||
|      * @param offset Offset in bytes to start reading data from |      * @param offset Offset in bytes to start reading data from | ||||||
|      * @param length Length in bytes of data to read from file |      * @param length Length in bytes of data to read from file | ||||||
|      * @param buffer Buffer to read data into |      * @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 |      * Write data to the file | ||||||
| @@ -39,9 +40,9 @@ public: | |||||||
|      * @param length Length in bytes of data to write to file |      * @param length Length in bytes of data to write to file | ||||||
|      * @param flush The flush parameters (0 == do not flush) |      * @param flush The flush parameters (0 == do not flush) | ||||||
|      * @param buffer Buffer to read data from |      * @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 |      * Get the size of the file in bytes | ||||||
|   | |||||||
| @@ -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); |     LOG_TRACE(Service_FS, "called offset=%llu, length=%zu", offset, length); | ||||||
|     romfs_file->Seek(data_offset + offset, SEEK_SET); |     romfs_file->Seek(data_offset + offset, SEEK_SET); | ||||||
|     size_t read_length = (size_t)std::min((u64)length, data_size - offset); |     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"); |     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 { | u64 IVFCFile::GetSize() const { | ||||||
|   | |||||||
| @@ -56,8 +56,8 @@ public: | |||||||
|         : romfs_file(file), data_offset(offset), data_size(size) {} |         : romfs_file(file), data_offset(offset), data_size(size) {} | ||||||
|  |  | ||||||
|     bool Open() override { return true; } |     bool Open() override { return true; } | ||||||
|     size_t Read(u64 offset, size_t length, u8* buffer) const override; |     ResultVal<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> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; | ||||||
|     u64 GetSize() const override; |     u64 GetSize() const override; | ||||||
|     bool SetSize(u64 size) const override; |     bool SetSize(u64 size) const override; | ||||||
|     bool Close() const override { return false; } |     bool Close() const override { return false; } | ||||||
|   | |||||||
| @@ -21,6 +21,7 @@ enum class ErrorDescription : u32 { | |||||||
|     WrongAddress = 53, |     WrongAddress = 53, | ||||||
|     FS_NotFound = 120, |     FS_NotFound = 120, | ||||||
|     FS_AlreadyExists = 190, |     FS_AlreadyExists = 190, | ||||||
|  |     FS_InvalidOpenFlags = 230, | ||||||
|     FS_NotAFile = 250, |     FS_NotAFile = 250, | ||||||
|     FS_NotFormatted = 340, ///< This is used by the FS service when creating a SaveData archive |     FS_NotFormatted = 340, ///< This is used by the FS service when creating a SaveData archive | ||||||
|     InvalidSection = 1000, |     InvalidSection = 1000, | ||||||
|   | |||||||
| @@ -103,7 +103,14 @@ ResultVal<bool> File::SyncRequest() { | |||||||
|             u32 address = cmd_buff[5]; |             u32 address = cmd_buff[5]; | ||||||
|             LOG_TRACE(Service_FS, "Read %s %s: offset=0x%llx length=%d address=0x%x", |             LOG_TRACE(Service_FS, "Read %s %s: offset=0x%llx length=%d address=0x%x", | ||||||
|                       GetTypeName().c_str(), GetName().c_str(), offset, length, address); |                       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; |             break; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -116,7 +123,13 @@ ResultVal<bool> File::SyncRequest() { | |||||||
|             u32 address = cmd_buff[6]; |             u32 address = cmd_buff[6]; | ||||||
|             LOG_TRACE(Service_FS, "Write %s %s: offset=0x%llx length=%d address=0x%x, flush=0x%x", |             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); |                       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; |             break; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user