[+] Optional blocking operations for Linux async file objects

This commit is contained in:
Reece Wilson 2022-04-17 15:51:37 +01:00
parent e90be1801a
commit 1f15674016
3 changed files with 50 additions and 4 deletions

View File

@ -15,6 +15,7 @@
#include <Source/IO/UNIX/IOSubmit.Linux.hpp>
#include <unistd.h>
#include <fcntl.h>
#include "FileStream.Unix.hpp"
namespace Aurora::IO::FS
{
@ -46,6 +47,9 @@ namespace Aurora::IO::FS
struct LinuxAsyncFileStream : public IAsyncFileStream
{
AuSPtr<IAsyncTransaction> NewTransaction() override;
bool BlockingTruncate(AuUInt64 length) override;
bool BlockingRead(AuUInt64 offset, const Memory::MemoryViewStreamWrite &parameters) override;
bool BlockingWrite(AuUInt64 offset, const Memory::MemoryViewStreamRead &parameters) override;
void Init(const AuSPtr<FileHandle> &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 &parameters)
{
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 &parameters)
{
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<FileHandle> &handle)
{
this->handle_ = handle;

8
Source/IO/FS/FileStream.Unix.cpp Normal file → Executable file
View File

@ -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);

View File

@ -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: