diff --git a/Source/Core/Core/IOS/FS/FileSystem.cpp b/Source/Core/Core/IOS/FS/FileSystem.cpp index 552d68ec8b..f2beb7d0c8 100644 --- a/Source/Core/Core/IOS/FS/FileSystem.cpp +++ b/Source/Core/Core/IOS/FS/FileSystem.cpp @@ -28,7 +28,7 @@ FileHandle::FileHandle(FileHandle&& other) : m_fs{other.m_fs}, m_fd{other.m_fd} FileHandle& FileHandle::operator=(FileHandle&& other) { - if (*this != other) + if (std::tie(m_fs, m_fd) != std::tie(other.m_fs, other.m_fd)) *this = std::move(other); return *this; } @@ -46,6 +46,16 @@ Fd FileHandle::Release() return fd; } +Result FileHandle::Seek(u32 offset, SeekMode mode) const +{ + return m_fs->SeekFile(*m_fd, offset, mode); +} + +Result FileHandle::GetStatus() const +{ + return m_fs->GetFileStatus(*m_fd); +} + void FileSystem::Init() { if (Delete(0, 0, "/tmp") == ResultCode::Success) diff --git a/Source/Core/Core/IOS/FS/FileSystem.h b/Source/Core/Core/IOS/FS/FileSystem.h index 87890cdbad..eb14dc6673 100644 --- a/Source/Core/Core/IOS/FS/FileSystem.h +++ b/Source/Core/Core/IOS/FS/FileSystem.h @@ -108,10 +108,18 @@ public: FileHandle& operator=(const FileHandle&) = delete; FileHandle& operator=(FileHandle&&); - operator Fd() const { return m_fd.value(); } /// Release the FD so that it is not automatically closed. Fd Release(); + template + Result Read(T* ptr, size_t count) const; + + template + Result Write(const T* ptr, size_t count) const; + + Result Seek(u32 offset, SeekMode mode) const; + Result GetStatus() const; + private: FileSystem* m_fs; std::optional m_fd; @@ -140,26 +148,6 @@ public: /// Get status for a file descriptor. virtual Result GetFileStatus(Fd fd) = 0; - template - Result ReadFile(Fd fd, T* ptr, u32 count) - { - const Result bytes = ReadBytesFromFile(fd, reinterpret_cast(ptr), sizeof(T) * count); - if (!bytes) - return bytes.Error(); - if (*bytes != sizeof(T) * count) - return ResultCode::ShortRead; - return count; - } - - template - Result WriteFile(Fd fd, const T* ptr, u32 count) - { - const auto result = WriteBytesToFile(fd, reinterpret_cast(ptr), sizeof(T) * count); - if (!result) - return result.Error(); - return count; - } - /// Create a file with the specified path and metadata. virtual ResultCode CreateFile(Uid caller_uid, Gid caller_gid, const std::string& path, FileAttribute attribute, Mode owner_mode, Mode group_mode, @@ -195,6 +183,28 @@ protected: void Init(); }; +template +Result FileHandle::Read(T* ptr, size_t count) const +{ + const Result bytes = m_fs->ReadBytesFromFile(*m_fd, reinterpret_cast(ptr), + static_cast(sizeof(T) * count)); + if (!bytes) + return bytes.Error(); + if (*bytes != sizeof(T) * count) + return ResultCode::ShortRead; + return count; +} + +template +Result FileHandle::Write(const T* ptr, size_t count) const +{ + const auto result = m_fs->WriteBytesToFile(*m_fd, reinterpret_cast(ptr), + static_cast(sizeof(T) * count)); + if (!result) + return result.Error(); + return count; +} + enum class Location { Configured,