From 1f15674016e95ed199c0e495dc12987a4626bc5b Mon Sep 17 00:00:00 2001 From: J Reece Wilson Date: Sun, 17 Apr 2022 15:51:37 +0100 Subject: [PATCH] [+] Optional blocking operations for Linux async file objects --- Source/IO/FS/Async.Linux.cpp | 41 ++++++++++++++++++++++++++++++++ Source/IO/FS/FileStream.Unix.cpp | 8 +++---- Source/IO/FS/FileStream.Unix.hpp | 5 ++++ 3 files changed, 50 insertions(+), 4 deletions(-) mode change 100644 => 100755 Source/IO/FS/FileStream.Unix.cpp diff --git a/Source/IO/FS/Async.Linux.cpp b/Source/IO/FS/Async.Linux.cpp index 42d77dcf..d020b9e4 100644 --- a/Source/IO/FS/Async.Linux.cpp +++ b/Source/IO/FS/Async.Linux.cpp @@ -15,6 +15,7 @@ #include #include #include +#include "FileStream.Unix.hpp" namespace Aurora::IO::FS { @@ -46,6 +47,9 @@ namespace Aurora::IO::FS struct LinuxAsyncFileStream : public IAsyncFileStream { AuSPtr NewTransaction() override; + bool BlockingTruncate(AuUInt64 length) override; + bool BlockingRead(AuUInt64 offset, const Memory::MemoryViewStreamWrite ¶meters) override; + bool BlockingWrite(AuUInt64 offset, const Memory::MemoryViewStreamRead ¶meters) override; void Init(const AuSPtr &handle); @@ -180,6 +184,43 @@ namespace Aurora::IO::FS return shared; } + bool LinuxAsyncFileStream::BlockingTruncate(AuUInt64 length) + { + return ::ftruncate(this->handle_->handle, length) != -1; + } + + bool LinuxAsyncFileStream::BlockingRead(AuUInt64 offset, const Memory::MemoryViewStreamWrite ¶meters) + { + if (!PosixSetOffset(this->handle_->handle, offset)) + { + return false; + } + + AuUInt32 read; + if (!PosixRead(this->handle_->handle, parameters.ptr, parameters.length, &read)) + { + return false; + } + + return true; + } + + bool LinuxAsyncFileStream::BlockingWrite(AuUInt64 offset, const Memory::MemoryViewStreamRead ¶meters) + { + if (!PosixSetOffset(this->handle_->handle, offset)) + { + return false; + } + + AuUInt32 read; + if (!PosixWrite(this->handle_->handle, parameters.ptr, parameters.length, &read)) + { + return false; + } + + return true; + } + bool LinuxAsyncFileTransaction::Init(const AuSPtr &handle) { this->handle_ = handle; diff --git a/Source/IO/FS/FileStream.Unix.cpp b/Source/IO/FS/FileStream.Unix.cpp old mode 100644 new mode 100755 index 9e27970c..551032ab --- a/Source/IO/FS/FileStream.Unix.cpp +++ b/Source/IO/FS/FileStream.Unix.cpp @@ -41,7 +41,7 @@ namespace Aurora::IO::FS if (ret < 0) { - SysPushErrorIO("PosixLseek63 IO Error: %i", ret); + //SysPushErrorIO("PosixLseek63 IO Error: %i", ret); return false; } @@ -63,7 +63,7 @@ namespace Aurora::IO::FS return ret; } - static bool PosixSetOffset(int fd, AuUInt64 offset) + bool PosixSetOffset(int fd, AuUInt64 offset) { return PosixLseek63(fd, offset, SEEK_SET, nullptr); } @@ -84,7 +84,7 @@ namespace Aurora::IO::FS return status ? ret : 0; } - static bool PosixRead(int fd, void *buf, AuUInt32 count, AuUInt32 *pRead) + bool PosixRead(int fd, void *buf, AuUInt32 count, AuUInt32 *pRead) { auto ret = read(fd, buf, count); @@ -102,7 +102,7 @@ namespace Aurora::IO::FS return true; } - static bool PosixWrite(int fd, const void *buf, AuUInt32 count, AuUInt32 *pWritten) + bool PosixWrite(int fd, const void *buf, AuUInt32 count, AuUInt32 *pWritten) { auto ret = write(fd, buf, count); diff --git a/Source/IO/FS/FileStream.Unix.hpp b/Source/IO/FS/FileStream.Unix.hpp index 1e493661..8dc088ba 100644 --- a/Source/IO/FS/FileStream.Unix.hpp +++ b/Source/IO/FS/FileStream.Unix.hpp @@ -11,6 +11,11 @@ namespace Aurora::IO::FS { + // Code-sharing for async API fallbacks... + bool PosixSetOffset(int fd, AuUInt64 offset); + bool PosixRead(int fd, void *buf, AuUInt32 count, AuUInt32 *pRead); + bool PosixWrite(int fd, const void *buf, AuUInt32 count, AuUInt32 *pWritten); + class PosixFileStream : public IFileStream { public: