diff --git a/Include/Aurora/IO/IOHandle.hpp b/Include/Aurora/IO/IOHandle.hpp index 8cb189c1..9655ef4e 100644 --- a/Include/Aurora/IO/IOHandle.hpp +++ b/Include/Aurora/IO/IOHandle.hpp @@ -159,7 +159,7 @@ namespace Aurora::IO virtual AuUInt64 GetFileLength() = 0; }; - AUKN_SHARED_SOO_CC(IOHandle, IIOHandle, 256); + AUKN_SHARED_SOO_CC(IOHandle, IIOHandle, 192); AUKN_SYM bool IsHandleTTY(AuUInt uHandle); diff --git a/Source/IO/AuIOHandle.cpp b/Source/IO/AuIOHandle.cpp index 062e7aa1..5a427860 100644 --- a/Source/IO/AuIOHandle.cpp +++ b/Source/IO/AuIOHandle.cpp @@ -19,6 +19,11 @@ namespace Aurora::IO { + AFileHandle::AFileHandle() + { + this->uThreadId = AuUInt(AuThreads::GetThreadId()); + } + AFileHandle::~AFileHandle() { if (this->SharingIsShared()) @@ -411,6 +416,11 @@ namespace Aurora::IO return 0; } + bool AFileHandle::ShouldClone() + { + return this->uThreadId != AuUInt(AuThreads::GetThreadId()); + } + AUKN_SYM IIOHandle *IOHandleNew() { return _new AFileHandle(); diff --git a/Source/IO/AuIOHandle.hpp b/Source/IO/AuIOHandle.hpp index 13369bbd..3b2f8bc0 100644 --- a/Source/IO/AuIOHandle.hpp +++ b/Source/IO/AuIOHandle.hpp @@ -16,6 +16,8 @@ namespace Aurora::IO { struct AFileHandle : IIOHandle { + AFileHandle(); + ~AFileHandle(); bool InitFromHandle(AuSPtr pHandle) override; @@ -79,6 +81,8 @@ namespace Aurora::IO AuUInt64 GetFileLength() override; + bool ShouldClone(); + AuOptionalEx uOSWriteHandle; AuOptionalEx uOSReadHandle; AuSPtr pThat; @@ -90,6 +94,7 @@ namespace Aurora::IO mutable AuOptional optIsPipe; mutable AuOptional optIsTTY; mutable AuSPtr pIPCString; + AuUInt uThreadId {}; protected: // Implement me: diff --git a/Source/IO/FS/FileStream.NT.cpp b/Source/IO/FS/FileStream.NT.cpp index 3347f1bb..cdcac66e 100644 --- a/Source/IO/FS/FileStream.NT.cpp +++ b/Source/IO/FS/FileStream.NT.cpp @@ -9,6 +9,7 @@ #include "FS.hpp" #include "FileStream.NT.hpp" #include "FileAdvisory.NT.hpp" +#include #if !defined(_AURUNTIME_GENERICFILESTREAM) @@ -515,9 +516,20 @@ namespace Aurora::IO::FS AUKN_SYM AuSPtr OpenBlockingFileStreamFromHandle(AuSPtr pIOHandle) { + SysCheckArgNotNull(pIOHandle, {}); + auto pStream = AuMakeShared(); SysCheckNotNullMemory(pStream, {}); + if (!AuStaticCast(pIOHandle)->pIPCPipe && + AuStaticCast(pIOHandle)->ShouldClone()) + { + auto pOld = pIOHandle; + pIOHandle = AuMakeShared(); + SysCheckNotNullMemory(pIOHandle, {}); + pIOHandle->InitFromPair(pOld->GetOSReadHandleSafe(), pOld->GetOSWriteHandle()); + } + pStream->Init(pIOHandle); return pStream; } diff --git a/Source/IO/FS/FileStream.Unix.cpp b/Source/IO/FS/FileStream.Unix.cpp index 19810133..b787132c 100755 --- a/Source/IO/FS/FileStream.Unix.cpp +++ b/Source/IO/FS/FileStream.Unix.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #if !defined(_AURUNTIME_GENERICFILESTREAM) @@ -385,11 +386,18 @@ namespace Aurora::IO::FS AUKN_SYM AuSPtr OpenBlockingFileStreamFromHandle(AuSPtr pIOHandle) { + SysCheckArgNotNull(pIOHandle, {}); + auto pStream = AuMakeShared(); - if (!pStream) + SysCheckNotNullMemory(pStream, {}); + + if (!AuStaticCast(pIOHandle)->pIPCPipe && + AuStaticCast(pIOHandle)->ShouldClone()) { - SysPushErrorMemory(); - return nullptr; + auto pOld = pIOHandle; + pIOHandle = AuMakeShared(); + SysCheckNotNullMemory(pIOHandle, {}); + pIOHandle->InitFromPair(pOld->GetOSReadHandleSafe(), pOld->GetOSWriteHandle()); } pStream->Init(pIOHandle);