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

This commit is contained in:
Reece Wilson 2023-08-15 15:04:13 +01:00
parent 78634d11db
commit e825531558

View File

@ -14,6 +14,8 @@
#include "FS/FileStream.Unix.hpp" #include "FS/FileStream.Unix.hpp"
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
namespace Aurora::IO namespace Aurora::IO
{ {
@ -158,8 +160,105 @@ namespace Aurora::IO
return this->IsValid(); return this->IsValid();
} }
bool IsFile() override;
bool IsTTY() override;
bool IsPipe() override;
AuOptionalEx<bool> optIsFile {};
AuOptionalEx<bool> optIsPipe {};
AuOptionalEx<bool> optIsTTY {};
}; };
bool UnixIOHandle::IsFile()
{
bool bIsFile {};
struct stat st;
int iFileDescriptor {};
if (auto file = this->optIsFile)
{
return file.value();
}
if (auto optHandle = this->GetOSHandleSafe())
{
iFileDescriptor = (int)optHandle.value();
}
else
{
SysPushErrorUninitialized();
return false;
}
if (::fstat(iFileDescriptor, &st) != 0)
{
SysPushErrorIO("fstat failed");
return false;
}
bIsFile = S_ISREG(st.st_mode);
this->optIsFile = bIsFile;
return bIsFile;
}
bool UnixIOHandle::IsTTY()
{
bool bIsTTY {};
int iFileDescriptor {};
if (auto file = this->optIsTTY)
{
return file.value();
}
if (auto optHandle = this->GetOSHandleSafe())
{
iFileDescriptor = (int)optHandle.value();
}
else
{
SysPushErrorUninitialized();
return false;
}
bIsTTY = ::isatty(iFileDescriptor);
this->optIsTTY = bIsTTY;
return bIsTTY;
}
bool UnixIOHandle::IsPipe()
{
bool bIsPipe {};
struct stat st;
int iFileDescriptor {};
if (auto file = this->optIsPipe)
{
return file.value();
}
if (auto optHandle = this->GetOSHandleSafe())
{
iFileDescriptor = (int)optHandle.value();
}
else
{
SysPushErrorUninitialized();
return false;
}
if (::fstat(iFileDescriptor, &st) != 0)
{
SysPushErrorIO("fstat failed");
return false;
}
bIsPipe = S_ISFIFO(st.st_mode);
this->optIsPipe = bIsPipe;
return bIsPipe;
}
AUKN_SYM IIOHandle *IOHandleNew() AUKN_SYM IIOHandle *IOHandleNew()
{ {
return _new UnixIOHandle(); return _new UnixIOHandle();