[+] IFileStream::ToStreamReader()

[+] IFileStreamToStreamWriter()
[+] IProcess::ToStreamReader(EStandardHandle stream)
[+] IProcess::ToStreamWriter()
This commit is contained in:
Reece Wilson 2023-12-02 16:40:58 +00:00
parent 9fea60a9ed
commit baf31e32d0
10 changed files with 160 additions and 11 deletions

View File

@ -15,58 +15,68 @@ namespace Aurora::IO::FS
* @brief Read memoryview from the filestream at an auto-incrementing offset * @brief Read memoryview from the filestream at an auto-incrementing offset
* @param parameters * @param parameters
* @return * @return
*/ */
virtual bool Read(const Memory::MemoryViewStreamWrite &parameters) = 0; virtual bool Read(const Memory::MemoryViewStreamWrite &parameters) = 0;
/** /**
* @brief Writes memoryview to the filestream at an auto-incrementing offset * @brief Writes memoryview to the filestream at an auto-incrementing offset
* @param parameters * @param parameters
* @return * @return
*/ */
virtual bool Write(const Memory::MemoryViewStreamRead &parameters) = 0; virtual bool Write(const Memory::MemoryViewStreamRead &parameters) = 0;
/** /**
* @brief Returns the length of the file * @brief Returns the length of the file
* @return * @return
*/ */
virtual AuUInt64 GetLength() = 0; virtual AuUInt64 GetLength() = 0;
/** /**
* @brief Returns the offset of the internal streams * @brief Returns the offset of the internal streams
* @return * @return
*/ */
virtual AuUInt64 GetOffset() = 0; virtual AuUInt64 GetOffset() = 0;
/** /**
* @brief Updates the stream pointer * @brief Updates the stream pointer
* @param offset * @param offset
* @return * @return
*/ */
virtual bool SetOffset(AuUInt64 offset) = 0; virtual bool SetOffset(AuUInt64 offset) = 0;
/** /**
* @brief Flush read/write streams * @brief Flush read/write streams
*/ */
virtual void Flush() = 0; virtual void Flush() = 0;
/** /**
* @brief Termiantes/truncates the file at the current offset * @brief Termiantes/truncates the file at the current offset
*/ */
virtual void WriteEoS() = 0; virtual void WriteEoS() = 0;
/** /**
* @brief Close IFileStream resource * @brief Close IFileStream resource
*/ */
virtual void Close() = 0; virtual void Close() = 0;
/** /**
* @brief Indicates that the file should automatically be deleted upon destruction of the stream * @brief Indicates that the file should automatically be deleted upon destruction of the stream
*/ */
virtual void MakeTemporary() = 0; virtual void MakeTemporary() = 0;
/** /**
* @brief Returns the IO handle backing the file stream, if available. * @brief Returns the IO handle backing the file stream, if available.
*/ */
virtual AuSPtr<IIOHandle> GetHandle() = 0; virtual AuSPtr<IIOHandle> GetHandle() = 0;
/**
* @brief
*/
virtual IO::IStreamReader *ToStreamReader() = 0;
/**
* @brief
*/
virtual IO::IStreamWriter *ToStreamWriter() = 0;
}; };
} }

View File

@ -106,5 +106,17 @@ namespace Aurora::Processes
* @return * @return
*/ */
virtual AuSPtr<IO::IIOHandle> GetErrorStreamHandle() = 0; virtual AuSPtr<IO::IIOHandle> GetErrorStreamHandle() = 0;
/**
* @brief
* @return
*/
virtual AuSPtr<IO::IStreamReader> ToStreamReader(EStandardHandle stream) = 0;
/**
* @brief
* @return
*/
virtual AuSPtr<IO::IStreamWriter> ToStreamWriter() = 0;
}; };
} }

View File

@ -16,6 +16,13 @@ namespace Aurora::IO::FS
{ {
static const AuUInt64 kFileCopyBlock = 0xFFFF * 128; static const AuUInt64 kFileCopyBlock = 0xFFFF * 128;
WinFileStream::WinFileStream() :
reader_(AuUnsafeRaiiToShared(this)),
writer_(AuUnsafeRaiiToShared(this))
{
}
WinFileStream::~WinFileStream() WinFileStream::~WinFileStream()
{ {
Close(); Close();
@ -432,6 +439,16 @@ namespace Aurora::IO::FS
return this->pHandle_; return this->pHandle_;
} }
IO::IStreamReader *WinFileStream::ToStreamReader()
{
return &this->reader_;
}
IO::IStreamWriter *WinFileStream::ToStreamWriter()
{
return &this->writer_;
}
HANDLE WinFileStream::GetWin32Handle(bool bReadOnly) HANDLE WinFileStream::GetWin32Handle(bool bReadOnly)
{ {
if (auto pHandle = this->pHandle_) if (auto pHandle = this->pHandle_)

View File

@ -13,6 +13,7 @@ namespace Aurora::IO::FS
{ {
struct WinFileStream : IFileStream struct WinFileStream : IFileStream
{ {
WinFileStream();
~WinFileStream(); ~WinFileStream();
void Init(AuSPtr<IIOHandle> pHandle); void Init(AuSPtr<IIOHandle> pHandle);
@ -27,6 +28,8 @@ namespace Aurora::IO::FS
void WriteEoS() override; void WriteEoS() override;
void MakeTemporary() override; void MakeTemporary() override;
AuSPtr<IIOHandle> GetHandle() override; AuSPtr<IIOHandle> GetHandle() override;
IO::IStreamReader *ToStreamReader() override;
IO::IStreamWriter *ToStreamWriter() override;
HANDLE GetWin32Handle(bool bReadOnly = false); HANDLE GetWin32Handle(bool bReadOnly = false);
@ -34,6 +37,8 @@ namespace Aurora::IO::FS
AuSPtr<IIOHandle> pHandle_; AuSPtr<IIOHandle> pHandle_;
bool bShouldDelete {}; bool bShouldDelete {};
HANDLE hEventHandle_ { INVALID_HANDLE_VALUE }; HANDLE hEventHandle_ { INVALID_HANDLE_VALUE };
AuFS::FileReader reader_;
AuFS::FileWriter writer_;
}; };
} }
#endif #endif

View File

@ -119,6 +119,12 @@ namespace Aurora::IO::FS
return true; return true;
} }
PosixFileStream::PosixFileStream() :
reader_(AuUnsafeRaiiToShared(this)),
writer_(AuUnsafeRaiiToShared(this))
{
}
PosixFileStream::~PosixFileStream() PosixFileStream::~PosixFileStream()
{ {
@ -416,6 +422,16 @@ namespace Aurora::IO::FS
return this->pHandle_; return this->pHandle_;
} }
IO::IStreamReader *PosixFileStream::ToStreamReader()
{
return &this->reader_;
}
IO::IStreamWriter *PosixFileStream::ToStreamWriter()
{
return &this->writer_;
}
AUKN_SYM AuSPtr<IFileStream> OpenBlockingFileStreamFromHandle(AuSPtr<IIOHandle> pIOHandle) AUKN_SYM AuSPtr<IFileStream> OpenBlockingFileStreamFromHandle(AuSPtr<IIOHandle> pIOHandle)
{ {
auto pStream = AuMakeShared<PosixFileStream>(); auto pStream = AuMakeShared<PosixFileStream>();

View File

@ -19,6 +19,7 @@ namespace Aurora::IO::FS
struct PosixFileStream : IFileStream struct PosixFileStream : IFileStream
{ {
PosixFileStream();
~PosixFileStream(); ~PosixFileStream();
bool Init(AuSPtr<IIOHandle> pHandle); bool Init(AuSPtr<IIOHandle> pHandle);
@ -33,6 +34,8 @@ namespace Aurora::IO::FS
void WriteEoS() override; void WriteEoS() override;
void MakeTemporary() override; void MakeTemporary() override;
AuSPtr<IIOHandle> GetHandle() override; AuSPtr<IIOHandle> GetHandle() override;
IO::IStreamReader *ToStreamReader() override;
IO::IStreamWriter *ToStreamWriter() override;
int GetUnixHandle(); int GetUnixHandle();
private: private:
@ -40,6 +43,8 @@ namespace Aurora::IO::FS
AuString path_; AuString path_;
AuThreadPrimitives::SpinLock spinlock_; AuThreadPrimitives::SpinLock spinlock_;
bool bMadeTemporary {}; bool bMadeTemporary {};
AuFS::FileReader reader_;
AuFS::FileWriter writer_;
}; };
} }
#endif #endif

View File

@ -506,6 +506,45 @@ namespace Aurora::Processes
return this->fsErrorHandle_; return this->fsErrorHandle_;
} }
AuSPtr<IO::IStreamReader> ProcessImpl::ToStreamReader(EStandardHandle stream)
{
if (stream == EStandardHandle::eErrorStream)
{
if (auto pThat = this->GetErrorStreamHandle())
{
if (auto pThat2 = AuFS::OpenBlockingFileStreamFromHandle(pThat))
{
return { pThat2, pThat2->ToStreamReader() };
}
}
}
else
{
if (auto pThat = this->GetOutputAndInputHandles())
{
if (auto pThat2 = AuFS::OpenBlockingFileStreamFromHandle(pThat))
{
return { pThat2, pThat2->ToStreamReader() };
}
}
}
return {};
}
AuSPtr<IO::IStreamWriter> ProcessImpl::ToStreamWriter()
{
if (auto pThat = this->GetErrorStreamHandle())
{
if (auto pThat2 = AuFS::OpenBlockingFileStreamFromHandle(pThat))
{
return { pThat2, pThat2->ToStreamWriter() };
}
}
return {};
}
bool ProcessImpl::Start() bool ProcessImpl::Start()
{ {
if (this->process_ != INVALID_HANDLE_VALUE) if (this->process_ != INVALID_HANDLE_VALUE)

View File

@ -38,6 +38,9 @@ namespace Aurora::Processes
AuSPtr<IO::IIOHandle> GetOutputAndInputHandles() override; AuSPtr<IO::IIOHandle> GetOutputAndInputHandles() override;
AuSPtr<IO::IIOHandle> GetErrorStreamHandle() override; AuSPtr<IO::IIOHandle> GetErrorStreamHandle() override;
AuSPtr<IO::IStreamReader> ToStreamReader(EStandardHandle stream) override;
AuSPtr<IO::IStreamWriter> ToStreamWriter() override;
bool Start() override; bool Start() override;
bool Init(); bool Init();

View File

@ -481,6 +481,45 @@ namespace Aurora::Processes
return this->fsErrorHandle_; return this->fsErrorHandle_;
} }
AuSPtr<IO::IStreamReader> ProcessImpl::ToStreamReader(EStandardHandle stream)
{
if (stream == EStandardHandle::eErrorStream)
{
if (auto pThat = this->GetErrorStreamHandle())
{
if (auto pThat2 = AuFS::OpenBlockingFileStreamFromHandle(pThat))
{
return { pThat2, pThat2->ToStreamReader() };
}
}
}
else
{
if (auto pThat = this->GetOutputAndInputHandles())
{
if (auto pThat2 = AuFS::OpenBlockingFileStreamFromHandle(pThat))
{
return { pThat2, pThat2->ToStreamReader() };
}
}
}
return {};
}
AuSPtr<IO::IStreamWriter> ProcessImpl::ToStreamWriter()
{
if (auto pThat = this->GetErrorStreamHandle())
{
if (auto pThat2 = AuFS::OpenBlockingFileStreamFromHandle(pThat))
{
return { pThat2, pThat2->ToStreamWriter() };
}
}
return {};
}
void ProcessImpl::ForkMain() void ProcessImpl::ForkMain()
{ {
{ {

View File

@ -49,6 +49,9 @@ namespace Aurora::Processes
AuSPtr<IO::IIOHandle> GetOutputAndInputHandles() override; AuSPtr<IO::IIOHandle> GetOutputAndInputHandles() override;
AuSPtr<IO::IIOHandle> GetErrorStreamHandle() override; AuSPtr<IO::IIOHandle> GetErrorStreamHandle() override;
AuSPtr<IO::IStreamReader> ToStreamReader(EStandardHandle stream) override;
AuSPtr<IO::IStreamWriter> ToStreamWriter() override;
bool Start() override; bool Start() override;
bool Init(); bool Init();