AuroraRuntime/Source/IO/AuIOHandle.cpp

313 lines
7.3 KiB
C++

/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuIOHandle.cpp
Date: 2023-7-28
Author: Reece
***/
#include <RuntimeInternal.hpp>
#include "AuIOHandle.hpp"
#include "FS/FileAdvisory.hpp"
namespace Aurora::IO
{
AFileHandle::~AFileHandle()
{
if (this->uOSWriteHandle.has_value() && this->uOSReadHandle.has_value() &&
this->uOSReadHandle.value() == this->uOSWriteHandle.value())
{
this->CloseHandle(this->uOSReadHandle.value());
AuResetMember(this->uOSReadHandle);
AuResetMember(this->uOSWriteHandle);
}
if (this->uOSReadHandle)
{
this->CloseHandle(this->uOSReadHandle.value());
AuResetMember(this->uOSReadHandle);
}
if (this->uOSWriteHandle)
{
this->CloseHandle(this->uOSWriteHandle.value());
AuResetMember(this->uOSWriteHandle);
}
}
bool AFileHandle::InitFromHandle(AuSPtr<IIOHandle> pHandle)
{
auto pSrc = AuStaticCast<AFileHandle>(pHandle);
auto pDest = this;
if (this->IsValid())
{
return false;
}
pDest->pThat = pHandle;
pDest->uOSReadHandle = pSrc->uOSReadHandle;
pDest->uOSWriteHandle = pSrc->uOSWriteHandle;
pDest->bIsAsync = pSrc->bIsAsync;
return true;
}
bool AFileHandle::InitFromCopy(AuUInt64 uOSHandle)
{
if (this->IsValid())
{
return false;
}
if (auto uOSWriteHandle = this->DupHandle(uOSHandle, true))
{
this->uOSReadHandle = uOSWriteHandle;
this->uOSWriteHandle = uOSWriteHandle;
return true;
}
else
{
return {};
}
}
bool AFileHandle::InitFromMove(AuUInt64 uOSHandle)
{
if (this->IsValid())
{
return false;
}
this->uOSReadHandle = uOSHandle;
this->uOSWriteHandle = uOSHandle;
return true;
}
bool AFileHandle::InitFromPair(AuOptionalEx<AuUInt64> optOSReadHandle,
AuOptionalEx<AuUInt64> optOSWriteHandle)
{
if (this->IsValid())
{
return false;
}
if (optOSReadHandle)
{
if (auto uOSReadHandle2 = this->DupHandle(optOSReadHandle.value(), false))
{
this->uOSReadHandle = uOSReadHandle2;
}
else
{
return {};
}
}
if (optOSWriteHandle)
{
if (auto uOSWriteHandle2 = this->DupHandle(optOSWriteHandle.value(), true))
{
this->uOSWriteHandle = uOSWriteHandle2;
}
else
{
this->CloseHandle(this->uOSReadHandle.value());
AuResetMember(this->uOSReadHandle);
return {};
}
}
return true;
}
bool AFileHandle::InitFromPairMove(AuOptionalEx<AuUInt64> uOSReadHandle,
AuOptionalEx<AuUInt64> uOSWriteHandle)
{
if (this->IsValid())
{
return false;
}
this->uOSReadHandle = uOSReadHandle;
this->uOSWriteHandle = uOSWriteHandle;
return true;
}
bool AFileHandle::InitFromStreamEnum(EStandardStream eStream)
{
switch (eStream)
{
case EStandardStream::eInputStream:
this->InitStdIn();
return this->IsValid();
case EStandardStream::eErrorStream:
case EStandardStream::eOutputStream:
this->InitStdOut(eStream == EStandardStream::eErrorStream);
return this->IsValid();
default:
SysPushErrorArg();
return false;
}
}
bool AFileHandle::IsFile() const
{
bool bIsFile {};
if (auto optFile = this->optIsFile)
{
return optFile.value();
}
if (auto optHandle = this->GetOSHandleSafe())
{
bIsFile = IsHandleFile(optHandle.value());
}
else
{
SysPushErrorUninitialized();
return false;
}
this->optIsFile = bIsFile;
return bIsFile;
}
bool AFileHandle::IsTTY() const
{
bool bIsTTY {};
if (auto optTTY = this->optIsTTY)
{
return optTTY.value();
}
if (auto optHandle = this->GetOSHandleSafe())
{
bIsTTY = IsHandleTTY(optHandle.value());
}
else
{
SysPushErrorUninitialized();
return false;
}
this->optIsTTY = bIsTTY;
return bIsTTY;
}
bool AFileHandle::IsPipe() const
{
bool bIsPipe {};
if (auto optPipe = this->optIsPipe)
{
return optPipe.value();
}
if (auto optHandle = this->GetOSHandleSafe())
{
bIsPipe = IsHandlePipe(optHandle.value());
}
else
{
SysPushErrorUninitialized();
return false;
}
this->optIsPipe = bIsPipe;
return bIsPipe;
}
AuOptionalEx<AuUInt64> AFileHandle::GetOSHandleSafe() const
{
if (auto write = this->uOSWriteHandle)
{
return write;
}
if (auto read = this->uOSReadHandle)
{
return read;
}
return {};
}
AuUInt64 AFileHandle::GetOSHandle() const
{
return this->uOSReadHandle.ValueOr(this->uOSWriteHandle.value());
}
AuUInt64 AFileHandle::GetOSReadHandle() const
{
return this->uOSReadHandle.value();
}
AuOptionalEx<AuUInt64> AFileHandle::GetOSReadHandleSafe() const
{
return this->uOSReadHandle;
}
AuUInt64 AFileHandle::GetOSWriteHandle() const
{
return this->uOSWriteHandle.value();
}
AuOptionalEx<AuUInt64> AFileHandle::GetOSWriteHandleSafe() const
{
return this->uOSWriteHandle;
}
bool AFileHandle::IsValid() const
{
return this->uOSReadHandle ||
this->uOSWriteHandle;
}
bool AFileHandle::HasUniqueWriteHandle() const
{
return bool(this->uOSWriteHandle);
}
bool AFileHandle::IsAsync() const
{
return this->bIsAsync;
}
AuString AFileHandle::GetPath() const
{
return this->path;
}
bool AFileHandle::SectionLock(AuUInt64 uOffset,
AuUInt64 uLength,
FS::EFileAdvisoryLockLevel level)
{
return FS::ApplyFileSectionLock(AuUnsafeRaiiToShared(this), level, uOffset, uLength);
}
bool AFileHandle::SectionUnlock(AuUInt64 uOffset,
AuUInt64 uLength)
{
return FS::UnapplyFileSectionLock(AuUnsafeRaiiToShared(this), uOffset, uLength);
}
AUKN_SYM IIOHandle *IOHandleNew()
{
return _new AFileHandle();
}
AUKN_SYM void IOHandleRelease(IIOHandle *pIOHandle)
{
AuSafeDelete<AFileHandle *>(pIOHandle);
}
AUKN_SYM void IOHandleCopy(IIOHandle *dest, const IIOHandle *source)
{
AuResetMember(*AuStaticCast<AFileHandle>(dest));
dest->InitFromPair(source->GetOSReadHandleSafe(), source->GetOSWriteHandleSafe());
}
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, IOHandle, AFileHandle)
}