2023-08-11 15:51:42 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: AuIOHandle.Unix.cpp
|
|
|
|
Date: 2023-8-11
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <RuntimeInternal.hpp>
|
|
|
|
#include "AuIOHandle.hpp"
|
|
|
|
#include "AuIOHandle.Unix.hpp"
|
|
|
|
|
|
|
|
#include "FS/FS.hpp"
|
|
|
|
#include "FS/FileAdvisory.Unix.hpp"
|
|
|
|
#include "FS/FileStream.Unix.hpp"
|
|
|
|
|
2023-12-13 04:25:11 +00:00
|
|
|
#include <Source/Grug/AuGrug.hpp>
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
#include <fcntl.h>
|
2023-08-15 14:04:13 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-08-27 11:41:51 +00:00
|
|
|
#if defined(AURORA_COMPILER_CLANG)
|
|
|
|
// warning: enumeration values 'kEnumCount' and 'kEnumInvalid' not handled in switch [-Wswitch
|
|
|
|
#pragma clang diagnostic ignored "-Wswitch"
|
|
|
|
// Yea, I don't give a shit.
|
|
|
|
#endif
|
|
|
|
|
2023-09-13 01:50:53 +00:00
|
|
|
#if !defined(FD_CLOEXEC)
|
|
|
|
#define FD_CLOEXEC 0
|
|
|
|
#endif
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
namespace Aurora::IO
|
|
|
|
{
|
2023-09-14 23:11:14 +00:00
|
|
|
AuUInt64 DupHandle(AuUInt64 uOSHandle, bool bWriteAccess, bool bShareAccess)
|
2023-09-13 01:50:53 +00:00
|
|
|
{
|
|
|
|
int fd = ::dup(uOSHandle);
|
2023-08-11 15:51:42 +00:00
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2023-09-14 23:11:14 +00:00
|
|
|
|
|
|
|
if (!SetFDShareAccess(uOSHandle, bShareAccess))
|
2023-09-13 01:50:53 +00:00
|
|
|
{
|
|
|
|
::close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-09-14 23:11:14 +00:00
|
|
|
|
|
|
|
return AuUInt64(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt64 AFileHandle::DupHandle(AuUInt64 uOSHandle, bool bWriteAccess)
|
|
|
|
{
|
|
|
|
return Aurora::IO::DupHandle(uOSHandle, bWriteAccess, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt64 AFileHandle::DupHandle(AuUInt64 uOSHandle, bool bWriteAccess, bool bShareAccess)
|
|
|
|
{
|
|
|
|
return Aurora::IO::DupHandle(uOSHandle, bWriteAccess, bShareAccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
int SetFDShareAccess(AuUInt64 uOSHandle, bool bShareAccess)
|
|
|
|
{
|
|
|
|
int flags = ::fcntl(uOSHandle, F_GETFL, 0);
|
|
|
|
if (flags == -1)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2023-09-13 01:50:53 +00:00
|
|
|
|
|
|
|
if (bShareAccess)
|
|
|
|
{
|
|
|
|
flags &= ~FD_CLOEXEC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flags |= FD_CLOEXEC;
|
|
|
|
}
|
|
|
|
|
2023-09-14 23:11:14 +00:00
|
|
|
flags = ::fcntl(uOSHandle, F_SETFL, flags);
|
2023-09-13 01:50:53 +00:00
|
|
|
if (flags == -1)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-14 23:11:14 +00:00
|
|
|
return 1;
|
2023-08-11 15:51:42 +00:00
|
|
|
}
|
|
|
|
|
2024-03-27 03:12:49 +00:00
|
|
|
void AFileHandle::CloseHandle(AuUInt64 uOSHandle, bool bFlushOnClose, bool bWriteEoS)
|
2023-08-11 15:51:42 +00:00
|
|
|
{
|
|
|
|
int fd = (int)uOSHandle;
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-13 04:25:11 +00:00
|
|
|
#if 0
|
2024-01-02 00:00:47 +00:00
|
|
|
if (bFlushOnClose)
|
|
|
|
{
|
|
|
|
::fsync(uOSHandle);
|
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
::close(fd);
|
2023-12-13 04:25:11 +00:00
|
|
|
#else
|
2024-03-27 03:12:49 +00:00
|
|
|
Grug::CloseHandle(uOSHandle, bFlushOnClose, bWriteEoS);
|
2023-12-13 04:25:11 +00:00
|
|
|
#endif
|
2023-08-11 15:51:42 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 01:50:53 +00:00
|
|
|
void AFileHandle::InitStdIn(bool bSharing)
|
|
|
|
{
|
|
|
|
this->uOSReadHandle = DupHandle(STDIN_FILENO, false, bSharing);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AFileHandle::InitStdOut(bool bError, bool bSharing)
|
|
|
|
{
|
|
|
|
this->uOSReadHandle = this->uOSWriteHandle = DupHandle(bError ? STDERR_FILENO : STDOUT_FILENO, true, bSharing);
|
|
|
|
}
|
|
|
|
|
2023-09-28 10:53:42 +00:00
|
|
|
bool AFileHandle::InitFromPath(HandleCreate create)
|
2023-08-27 11:41:51 +00:00
|
|
|
{
|
|
|
|
int iFileDescriptor { -1 };
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-08-27 11:41:51 +00:00
|
|
|
if (create.path.empty())
|
|
|
|
{
|
|
|
|
SysPushErrorArg("Cannot open an IO handle to the provided empty path");
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-08-27 11:41:51 +00:00
|
|
|
if (!FS::EFileOpenModeIsValid(create.eMode))
|
|
|
|
{
|
|
|
|
SysPushErrorParam("Invalid open mode");
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-08-27 11:41:51 +00:00
|
|
|
if (!FS::EFileAdvisoryLockLevelIsValid(create.eAdvisoryLevel))
|
|
|
|
{
|
|
|
|
SysPushErrorParam("Invalid lock mode");
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-08-27 11:41:51 +00:00
|
|
|
auto pathex = FS::NormalizePathRet(create.path);
|
|
|
|
if (pathex.empty())
|
|
|
|
{
|
|
|
|
SysPushErrorMemory();
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2024-03-09 00:46:50 +00:00
|
|
|
if (create.bDirectIOMode)
|
2023-08-27 11:41:51 +00:00
|
|
|
{
|
|
|
|
this->bDirectIO = true;
|
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-11-11 13:25:05 +00:00
|
|
|
int flags {};
|
|
|
|
|
2023-08-27 11:41:51 +00:00
|
|
|
switch (create.eMode)
|
|
|
|
{
|
|
|
|
case FS::EFileOpenMode::eRead:
|
|
|
|
{
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-08-27 11:41:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FS::EFileOpenMode::eReadWrite:
|
|
|
|
case FS::EFileOpenMode::eWrite:
|
|
|
|
{
|
|
|
|
if (create.bAlwaysCreateDirTree)
|
2023-08-11 15:51:42 +00:00
|
|
|
{
|
2023-08-27 11:41:51 +00:00
|
|
|
FS::CreateDirectories(pathex, true);
|
2023-08-11 15:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (create.bFailIfNonEmptyFile)
|
|
|
|
{
|
2023-11-11 13:25:05 +00:00
|
|
|
#if defined(O_EXCL)
|
|
|
|
flags = O_EXCL;
|
|
|
|
#else
|
2023-08-27 11:41:51 +00:00
|
|
|
if (AuFS::FileExists(pathex.c_str()))
|
2023-08-11 15:51:42 +00:00
|
|
|
{
|
|
|
|
SysPushErrorResourceExists("File {} already exists", create.path);
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-11 13:25:05 +00:00
|
|
|
#endif
|
2023-08-11 15:51:42 +00:00
|
|
|
}
|
2023-08-27 11:41:51 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
2023-11-11 13:25:05 +00:00
|
|
|
|
|
|
|
flags |= create.eMode == FS::EFileOpenMode::eRead ? O_RDONLY : (O_RDWR | O_CREAT);
|
|
|
|
flags |= O_CLOEXEC;
|
2024-01-02 00:00:47 +00:00
|
|
|
|
|
|
|
#if defined(O_DIRECT)
|
2023-11-11 13:25:05 +00:00
|
|
|
flags |= this->bDirectIO ? O_DIRECT : 0;
|
2024-01-02 00:00:47 +00:00
|
|
|
#else
|
|
|
|
// TBD
|
|
|
|
#endif
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2024-03-15 06:18:32 +00:00
|
|
|
iFileDescriptor = PosixOpen(pathex.c_str(),
|
|
|
|
flags,
|
|
|
|
0664);
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-08-27 11:41:51 +00:00
|
|
|
if (iFileDescriptor < 0)
|
|
|
|
{
|
2023-12-13 04:25:11 +00:00
|
|
|
if (create.bFailIfNonEmptyFile)
|
2023-11-11 13:25:05 +00:00
|
|
|
{
|
2023-12-13 04:25:11 +00:00
|
|
|
if (errno == EEXIST)
|
|
|
|
{
|
|
|
|
SysPushErrorResourceExists("File {} already exists", create.path);
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-11 13:25:05 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 04:25:11 +00:00
|
|
|
if (errno != EISDIR &&
|
|
|
|
errno != ENOENT)
|
2023-11-11 13:25:05 +00:00
|
|
|
{
|
2023-12-13 04:25:11 +00:00
|
|
|
RuntimeWaitForSecondaryTick();
|
|
|
|
|
2024-03-15 06:18:32 +00:00
|
|
|
iFileDescriptor = PosixOpen(pathex.c_str(),
|
|
|
|
flags,
|
|
|
|
0664);
|
2023-11-11 13:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (iFileDescriptor < 0)
|
|
|
|
{
|
2023-12-13 04:25:11 +00:00
|
|
|
if (errno == EEXIST)
|
|
|
|
{
|
|
|
|
SysPushErrorResourceExists("File {} already exists", create.path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno == EISDIR)
|
|
|
|
{
|
|
|
|
flags &= ~(O_WRONLY | O_RDWR);
|
2024-03-15 06:18:32 +00:00
|
|
|
iFileDescriptor = PosixOpen(pathex.c_str(),
|
|
|
|
flags,
|
|
|
|
0664);
|
2023-12-13 04:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (iFileDescriptor < 0)
|
|
|
|
{
|
|
|
|
if (errno == ENFILE)
|
|
|
|
{
|
|
|
|
SysPushErrorIOResourceFailure("low resources");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SysPushErrorIO("Couldn't open file: {} ({}) {}", path, pathex, errno);
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-11 13:25:05 +00:00
|
|
|
}
|
2023-08-27 11:41:51 +00:00
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-08-27 11:41:51 +00:00
|
|
|
if (!FS::ApplyDumbAdvisoryLock(iFileDescriptor, create.eAdvisoryLevel))
|
|
|
|
{
|
2024-07-03 19:07:59 +00:00
|
|
|
RuntimeWaitForSecondaryTick();
|
|
|
|
|
|
|
|
if (!FS::ApplyDumbAdvisoryLock(iFileDescriptor, create.eAdvisoryLevel))
|
|
|
|
{
|
|
|
|
SysPushErrorIO("Couldn't open file: {}. File node (not section) is locked.", path);
|
|
|
|
::close(iFileDescriptor);
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
}
|
2023-08-15 14:04:13 +00:00
|
|
|
|
2023-08-27 11:41:51 +00:00
|
|
|
if (create.bFailIfNonEmptyFile)
|
|
|
|
{
|
2024-03-05 18:37:44 +00:00
|
|
|
if (PosixGetLength(iFileDescriptor))
|
2023-08-27 11:41:51 +00:00
|
|
|
{
|
|
|
|
SysPushErrorResourceExists("File {} already exists", create.path);
|
|
|
|
::close(iFileDescriptor);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-01-02 00:00:47 +00:00
|
|
|
|
2023-08-27 11:41:51 +00:00
|
|
|
switch (create.eMode)
|
|
|
|
{
|
|
|
|
case FS::EFileOpenMode::eRead:
|
|
|
|
{
|
|
|
|
this->uOSReadHandle = AuUInt64(iFileDescriptor);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FS::EFileOpenMode::eReadWrite:
|
|
|
|
{
|
|
|
|
this->uOSWriteHandle = AuUInt64(iFileDescriptor);
|
|
|
|
this->uOSReadHandle = AuUInt64(iFileDescriptor);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FS::EFileOpenMode::eWrite:
|
|
|
|
{
|
|
|
|
this->uOSWriteHandle = AuUInt64(iFileDescriptor);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
2023-08-15 14:04:13 +00:00
|
|
|
|
2023-08-27 11:41:51 +00:00
|
|
|
this->bIsAsync = create.bAsyncHandle;
|
2024-03-27 03:12:49 +00:00
|
|
|
this->bShouldWriteEoS = create.bWriteEoSOnClose;
|
|
|
|
this->bFlushOnClose = create.bFlushOnClose;
|
2023-08-27 11:41:51 +00:00
|
|
|
|
|
|
|
this->path = create.path;
|
|
|
|
return this->IsValid();
|
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
AUKN_SYM bool IsHandleTTY(AuUInt uHandle)
|
2023-08-15 14:04:13 +00:00
|
|
|
{
|
2023-08-29 00:58:48 +00:00
|
|
|
if ((AuSInt)uHandle < 0)
|
2023-08-15 14:04:13 +00:00
|
|
|
{
|
2023-09-07 22:20:26 +00:00
|
|
|
SysPushErrorArg();
|
2023-08-15 14:04:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
return ::isatty((int)uHandle);
|
2023-08-15 14:04:13 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
AUKN_SYM bool IsHandlePipe(AuUInt uHandle)
|
2023-08-15 14:04:13 +00:00
|
|
|
{
|
2023-08-29 00:24:52 +00:00
|
|
|
struct stat st;
|
2023-08-15 14:04:13 +00:00
|
|
|
|
2023-08-29 00:58:48 +00:00
|
|
|
if ((AuSInt)uHandle < 0)
|
2023-08-15 14:04:13 +00:00
|
|
|
{
|
2023-09-07 22:20:26 +00:00
|
|
|
SysPushErrorArg();
|
2023-08-29 00:24:52 +00:00
|
|
|
return false;
|
2023-08-15 14:04:13 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
if (::fstat((int)uHandle, &st) != 0)
|
2023-08-15 14:04:13 +00:00
|
|
|
{
|
2023-08-29 00:24:52 +00:00
|
|
|
SysPushErrorIO("fstat failed");
|
2023-08-15 14:04:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
return S_ISFIFO(st.st_mode);
|
2023-08-15 14:04:13 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
AUKN_SYM bool IsHandleFile(AuUInt uHandle)
|
2023-08-15 14:04:13 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
2023-08-29 00:58:48 +00:00
|
|
|
if ((AuSInt)uHandle < 0)
|
2023-08-15 14:04:13 +00:00
|
|
|
{
|
2023-09-07 22:20:26 +00:00
|
|
|
SysPushErrorArg();
|
2023-08-15 14:04:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
if (::fstat((int)uHandle, &st) != 0)
|
2023-08-15 14:04:13 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO("fstat failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
return S_ISREG(st.st_mode);
|
2023-08-15 14:04:13 +00:00
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
}
|