[*] Harden pipe reads under FS streams

This commit is contained in:
Reece Wilson 2023-10-20 11:37:52 +01:00
parent 871479172b
commit 88094c48ae
2 changed files with 56 additions and 21 deletions

View File

@ -145,13 +145,15 @@ namespace Aurora::IO::FS
{ {
OVERLAPPED overlapped {}; OVERLAPPED overlapped {};
AuUInt64 uOffset {}; AuUInt64 uOffset {};
parameters.outVariable = 0;
if (!this->pHandle_) if (!this->pHandle_)
{ {
SysPushErrorUninitialized(); SysPushErrorUninitialized();
return 0; return 0;
} }
auto hHandle = this->GetWin32Handle(true); auto hHandle = this->GetWin32Handle(true);
HANDLE hEventHandle { INVALID_HANDLE_VALUE }; HANDLE hEventHandle { INVALID_HANDLE_VALUE };
@ -161,6 +163,22 @@ namespace Aurora::IO::FS
return {}; return {};
} }
AuUInt64 uLength = parameters.length;
if (this->pHandle_->IsPipe())
{
DWORD uAvail {};
if (!PeekNamedPipe(hHandle, NULL, NULL, NULL, &uAvail, NULL))
{
return false;
}
uLength = AuMin(uLength, uAvail);
if (!uLength)
{
return true;
}
}
bool bIsAsync = this->pHandle_->IsAsync(); bool bIsAsync = this->pHandle_->IsAsync();
if (bIsAsync) if (bIsAsync)
{ {
@ -172,9 +190,6 @@ namespace Aurora::IO::FS
} }
} }
parameters.outVariable = 0;
AuUInt64 uLength = parameters.length;
while (uLength) while (uLength)
{ {
DWORD read {}; DWORD read {};
@ -247,6 +262,8 @@ namespace Aurora::IO::FS
{ {
OVERLAPPED overlapped {}; OVERLAPPED overlapped {};
AuUInt64 uOffset {}; AuUInt64 uOffset {};
parameters.outVariable = 0;
auto optHandle = this->GetHandle()->GetOSWriteHandleSafe(); auto optHandle = this->GetHandle()->GetOSWriteHandleSafe();
if (!optHandle) if (!optHandle)
@ -275,7 +292,6 @@ namespace Aurora::IO::FS
return false; return false;
} }
} }
parameters.outVariable = 0;
AuUInt64 uLength = parameters.length; AuUInt64 uLength = parameters.length;
while (uLength) while (uLength)

View File

@ -15,6 +15,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/ioctl.h>
#if !defined(_AURUNTIME_GENERICFILESTREAM) #if !defined(_AURUNTIME_GENERICFILESTREAM)
@ -237,7 +238,6 @@ namespace Aurora::IO::FS
parameters.outVariable = 0; parameters.outVariable = 0;
if (!this->pHandle_) if (!this->pHandle_)
{ {
SysPushErrorUninitialized(); SysPushErrorUninitialized();
@ -251,30 +251,49 @@ namespace Aurora::IO::FS
return 0; return 0;
} }
auto length = parameters.length; AuUInt uOffset {};
AuUInt offset {0}; AuUInt64 uLength = parameters.length;
while (length)
if (this->pHandle_->IsPipe())
{ {
AuUInt32 read; #if defined(FIONREAD)
int available {};
int blockSize = AuMin(kFileCopyBlock, length); if (::ioctl(fd, FIONREAD, &available) < 0)
if (!PosixRead(fd, &reinterpret_cast<char *>(parameters.ptr)[offset], blockSize, &read))
{ {
SysPushErrorNested("File Error: {}", path_); return 0;
}
uLength = AuMin(uLength, available);
if (!uLength)
{
return 0;
}
#endif
}
while (uLength)
{
AuUInt32 uRead;
if (!PosixRead(fd,
&reinterpret_cast<char *>(parameters.ptr)[uOffset],
AuMin(kFileCopyBlock, uLength),
&uRead))
{
SysPushErrorNested("File Error: {}", this->path_);
break; break;
} }
if (read == 0) if (uRead == 0)
{ {
break; break;
} }
offset += read; uOffset += uRead;
length -= read; uLength -= uRead;
} }
parameters.outVariable = offset; parameters.outVariable = uOffset;
return true; return true;
} }
@ -282,6 +301,8 @@ namespace Aurora::IO::FS
{ {
AU_LOCK_GUARD(this->spinlock_); AU_LOCK_GUARD(this->spinlock_);
parameters.outVariable = 0;
if (!this->pHandle_) if (!this->pHandle_)
{ {
SysPushErrorUninitialized(); SysPushErrorUninitialized();
@ -295,8 +316,6 @@ namespace Aurora::IO::FS
return 0; return 0;
} }
parameters.outVariable = 0;
AuUInt length = parameters.length; AuUInt length = parameters.length;
AuUInt offset {0}; AuUInt offset {0};
while (length) while (length)