[+] NT: IOHandle::IsFile(), IOHandle::IsTTY(), IOHandle::IsPipe()

[*] Revert to supporting write-only handles
This commit is contained in:
Reece Wilson 2023-08-15 14:41:49 +01:00
parent 9cea2fe82a
commit 3df8621944
7 changed files with 204 additions and 17 deletions

View File

@ -108,6 +108,8 @@ namespace Aurora::IO
virtual AuUInt64 GetOSHandle() = 0;
virtual AuOptionalEx<AuUInt64> GetOSHandleSafe() = 0;
virtual AuUInt64 GetOSReadHandle() = 0;
virtual AuOptionalEx<AuUInt64> GetOSReadHandleSafe() = 0;
@ -123,6 +125,12 @@ namespace Aurora::IO
virtual bool IsAsync() = 0;
virtual AuString GetPath() = 0;
virtual bool IsFile() = 0;
virtual bool IsTTY() = 0;
virtual bool IsPipe() = 0;
};
AUKN_SHARED_SOO(IOHandle, IIOHandle, 256);

View File

@ -245,8 +245,109 @@ namespace Aurora::IO
return this->IsValid();
}
bool IsFile() override;
bool IsTTY() override;
bool IsPipe() override;
AuOptionalEx<bool> optIsFile {};
AuOptionalEx<bool> optIsPipe {};
AuOptionalEx<bool> optIsTTY {};
};
bool NTIOHandle::IsFile()
{
bool bIsFile {};
DWORD dwType {};
HANDLE hHandle {};
if (auto file = this->optIsFile)
{
return file.value();
}
if (auto optHandle = this->GetOSHandleSafe())
{
hHandle = (HANDLE)optHandle.value();
}
else
{
SysPushErrorUninitialized();
return false;
}
if (!(dwType = GetFileType(hHandle)))
{
return false;
}
bIsFile = dwType == FILE_TYPE_DISK;
this->optIsFile = bIsFile;
return bIsFile;
}
bool NTIOHandle::IsTTY()
{
bool bIsTTY {};
DWORD dwType {};
HANDLE hHandle {};
if (auto file = this->optIsTTY)
{
return file.value();
}
if (auto optHandle = this->GetOSHandleSafe())
{
hHandle = (HANDLE)optHandle.value();
}
else
{
SysPushErrorUninitialized();
return false;
}
if (!(dwType = GetFileType(hHandle)))
{
return false;
}
bIsTTY = dwType == FILE_TYPE_CHAR;
this->optIsTTY = bIsTTY;
return bIsTTY;
}
bool NTIOHandle::IsPipe()
{
bool bIsPipe {};
DWORD dwType {};
HANDLE hHandle {};
if (auto file = this->optIsPipe)
{
return file.value();
}
if (auto optHandle = this->GetOSHandleSafe())
{
hHandle = (HANDLE)optHandle.value();
}
else
{
SysPushErrorUninitialized();
return false;
}
if (!(dwType = GetFileType(hHandle)))
{
return false;
}
bIsPipe = dwType == FILE_TYPE_PIPE;
this->optIsPipe = bIsPipe;
return bIsPipe;
}
AUKN_SYM IIOHandle *IOHandleNew()
{
return _new NTIOHandle();

View File

@ -126,6 +126,21 @@ namespace Aurora::IO
return true;
}
AuOptionalEx<AuUInt64> AFileHandle::GetOSHandleSafe()
{
if (auto write = this->uOSWriteHandle)
{
return write;
}
if (auto read = this->uOSReadHandle)
{
return read;
}
return {};
}
AuUInt64 AFileHandle::GetOSHandle()
{
return this->uOSReadHandle.ValueOr(this->uOSWriteHandle.Value());

View File

@ -32,6 +32,8 @@ namespace Aurora::IO
AuUInt64 GetOSHandle() override;
AuOptionalEx<AuUInt64> GetOSHandleSafe() override;
AuUInt64 GetOSReadHandle() override;
AuOptionalEx<AuUInt64> GetOSReadHandleSafe() override;

View File

@ -30,10 +30,25 @@ namespace Aurora::IO::FS
{
LARGE_INTEGER distance {};
LARGE_INTEGER pos {};
HANDLE hHandle {};
auto hHandle = this->GetWin32Handle();
if (auto pHandle = this->pHandle_)
{
if (!pHandle->IsFile())
{
SysPushErrorIOResourceRejected();
return 0;
}
if (hHandle == INVALID_HANDLE_VALUE)
hHandle = this->GetWin32Handle();
if (hHandle == INVALID_HANDLE_VALUE)
{
SysPushErrorInvalidFd();
return 0;
}
}
else
{
SysPushErrorUninitialized();
return 0;
@ -55,13 +70,28 @@ namespace Aurora::IO::FS
{
LARGE_INTEGER distance {};
LARGE_INTEGER pos {};
HANDLE hHandle {};
auto hHandle = this->GetWin32Handle();
if (auto pHandle = this->pHandle_)
{
if (!pHandle->IsFile())
{
SysPushErrorIOResourceRejected();
return 0;
}
if (hHandle == INVALID_HANDLE_VALUE)
hHandle = this->GetWin32Handle();
if (hHandle == INVALID_HANDLE_VALUE)
{
SysPushErrorInvalidFd();
return 0;
}
}
else
{
SysPushErrorUninitialized();
return false;
return 0;
}
distance.QuadPart = offset;
@ -78,10 +108,25 @@ namespace Aurora::IO::FS
AuUInt64 WinFileStream::GetLength()
{
LARGE_INTEGER length;
HANDLE hHandle {};
auto hHandle = this->GetWin32Handle();
if (hHandle == INVALID_HANDLE_VALUE)
if (auto pHandle = this->pHandle_)
{
if (!pHandle->IsFile())
{
SysPushErrorIOResourceRejected();
return 0;
}
hHandle = this->GetWin32Handle();
if (hHandle == INVALID_HANDLE_VALUE)
{
SysPushErrorInvalidFd();
return 0;
}
}
else
{
SysPushErrorUninitialized();
return 0;
@ -98,12 +143,18 @@ namespace Aurora::IO::FS
bool WinFileStream::Read(const Memory::MemoryViewStreamWrite &parameters)
{
auto hHandle = this->GetWin32Handle();
if (!this->pHandle_)
{
SysPushErrorUninitialized();
return 0;
}
auto hHandle = this->GetWin32Handle(true);
HANDLE hEventHandle { INVALID_HANDLE_VALUE };
if (hHandle == INVALID_HANDLE_VALUE)
{
SysPushErrorUninitialized();
SysPushErrorIOResourceRejected();
return {};
}
@ -339,11 +390,21 @@ namespace Aurora::IO::FS
return this->pHandle_;
}
HANDLE WinFileStream::GetWin32Handle()
HANDLE WinFileStream::GetWin32Handle(bool bReadOnly)
{
return this->pHandle_ ?
(HANDLE)this->pHandle_->GetOSWriteHandleSafe().ValueOr(this->pHandle_->GetOSReadHandleSafe().ValueOr((AuUInt)INVALID_HANDLE_VALUE)) :
INVALID_HANDLE_VALUE;
if (this->pHandle_)
{
if (bReadOnly)
{
return (HANDLE)this->pHandle_->GetOSReadHandleSafe().ValueOr((AuUInt)INVALID_HANDLE_VALUE);
}
else
{
return (HANDLE)this->pHandle_->GetOSWriteHandleSafe().ValueOr(this->pHandle_->GetOSReadHandleSafe().ValueOr((AuUInt)INVALID_HANDLE_VALUE));
}
}
return INVALID_HANDLE_VALUE;
}
AUKN_SYM AuSPtr<IFileStream> OpenBlockingFileStreamFromHandle(AuSPtr<IIOHandle> pIOHandle)

View File

@ -28,7 +28,7 @@ namespace Aurora::IO::FS
void MakeTemporary() override;
AuSPtr<IIOHandle> GetHandle() override;
HANDLE GetWin32Handle();
HANDLE GetWin32Handle(bool bReadOnly = false);
private:
AuSPtr<IIOHandle> pHandle_;

View File

@ -215,10 +215,10 @@ namespace Aurora::IO::FS
return 0;
}
int fd = this->GetUnixHandle();
int fd = (int)this->pHandle_->GetOSReadHandleSafe().ValueOr((AuUInt)-1);
if (fd == -1)
{
SysPushErrorUninitialized();
SysPushErrorIOResourceRejected();
return 0;
}