2023-07-29 07:02:47 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: AuIOHandle.NT.cpp
|
|
|
|
Date: 2023-7-28
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <RuntimeInternal.hpp>
|
|
|
|
#include "AuIOHandle.hpp"
|
|
|
|
#include "AuIOHandle.NT.hpp"
|
|
|
|
|
|
|
|
#include "FS/FS.hpp"
|
|
|
|
#include "FS/FileAdvisory.NT.hpp"
|
2023-12-13 02:31:59 +00:00
|
|
|
#include <Source/Grug/AuGrug.hpp>
|
2023-07-29 07:02:47 +00:00
|
|
|
|
|
|
|
namespace Aurora::IO
|
|
|
|
{
|
|
|
|
AuUInt64 AFileHandle::DupHandle(AuUInt64 uOSHandle, bool bWriteAccess)
|
2023-09-13 01:27:18 +00:00
|
|
|
{
|
|
|
|
return DupHandle(uOSHandle, bWriteAccess, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt64 AFileHandle::DupHandle(AuUInt64 uOSHandle, bool bWriteAccess, bool bShareAccess)
|
2023-07-29 07:02:47 +00:00
|
|
|
{
|
|
|
|
HANDLE hTargetHandle = (HANDLE)uOSHandle;
|
|
|
|
HANDLE hTargetProcess = ::GetCurrentProcess();
|
|
|
|
HANDLE hHandle {};
|
|
|
|
|
|
|
|
if (!::DuplicateHandle(hTargetProcess,
|
|
|
|
hTargetHandle,
|
|
|
|
hTargetProcess,
|
|
|
|
&hHandle,
|
|
|
|
bWriteAccess ? GENERIC_WRITE | GENERIC_READ : GENERIC_READ,
|
2023-09-13 01:27:18 +00:00
|
|
|
bShareAccess ? TRUE : FALSE,
|
|
|
|
0))
|
2023-07-29 07:02:47 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AuUInt64(hHandle);
|
|
|
|
}
|
|
|
|
|
2024-03-27 03:12:49 +00:00
|
|
|
void AFileHandle::CloseHandle(AuUInt64 uOSHandle, bool bFlushOnClose, bool bWriteEoS)
|
2023-07-29 07:02:47 +00:00
|
|
|
{
|
2023-12-13 02:31:59 +00:00
|
|
|
#if 0
|
2023-07-29 07:02:47 +00:00
|
|
|
HANDLE hHandle = (HANDLE)uOSHandle;
|
|
|
|
AuWin32CloseHandle(hHandle);
|
2023-12-13 02:31:59 +00:00
|
|
|
#else
|
2024-03-27 03:12:49 +00:00
|
|
|
Grug::CloseHandle(uOSHandle, bFlushOnClose, bWriteEoS);
|
2023-12-13 02:31:59 +00:00
|
|
|
#endif
|
2023-07-29 07:02:47 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 01:50:53 +00:00
|
|
|
void AFileHandle::InitStdIn(bool bSharing)
|
|
|
|
{
|
|
|
|
HANDLE hHandle =
|
|
|
|
#if defined(AURORA_PLATFORM_WIN32)
|
|
|
|
GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
#else
|
|
|
|
Win32Open(L"CONIN$",
|
|
|
|
GENERIC_READ,
|
|
|
|
FILE_SHARE_READ,
|
2023-09-13 03:01:02 +00:00
|
|
|
bSharing,
|
2023-09-13 01:50:53 +00:00
|
|
|
OPEN_EXISTING,
|
|
|
|
0,
|
|
|
|
0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (hHandle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
auto uHandle = AuBuild::kCurrentPlatform == AuBuild::EPlatform::ePlatformWin32 ?
|
|
|
|
DupHandle((AuUInt64)hHandle, false, bSharing) :
|
|
|
|
(AuUInt64)hHandle;
|
|
|
|
this->uOSReadHandle = uHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AFileHandle::InitStdOut(bool bError, bool bSharing)
|
|
|
|
{
|
|
|
|
HANDLE hHandle =
|
|
|
|
#if defined(AURORA_PLATFORM_WIN32)
|
|
|
|
GetStdHandle(bError ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
|
|
|
|
#else
|
|
|
|
Win32Open(bError ? L"CONERR$" : L"CONOUT$",
|
|
|
|
GENERIC_READ | GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ,
|
2023-09-13 03:01:02 +00:00
|
|
|
bSharing,
|
2023-09-13 01:50:53 +00:00
|
|
|
OPEN_EXISTING,
|
|
|
|
0,
|
|
|
|
0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (hHandle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
auto uHandle = AuBuild::kCurrentPlatform == AuBuild::EPlatform::ePlatformWin32 ?
|
|
|
|
DupHandle((AuUInt64)hHandle, true, bSharing) :
|
|
|
|
(AuUInt64)hHandle;
|
|
|
|
this->uOSWriteHandle = this->uOSReadHandle = uHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 10:53:42 +00:00
|
|
|
bool AFileHandle::InitFromPath(HandleCreate create)
|
2023-08-29 00:24:52 +00:00
|
|
|
{
|
|
|
|
HANDLE hFileHandle;
|
|
|
|
|
|
|
|
DWORD dwFlags {};
|
2023-09-13 01:27:18 +00:00
|
|
|
DWORD dwAttrs {};
|
2023-08-29 00:24:52 +00:00
|
|
|
DWORD dwShare {};
|
|
|
|
|
|
|
|
if (create.path.empty())
|
2023-07-29 07:02:47 +00:00
|
|
|
{
|
2023-08-29 00:24:52 +00:00
|
|
|
SysPushErrorArg("Cannot open an IO handle to the provided empty path");
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
if (!FS::EFileOpenModeIsValid(create.eMode))
|
|
|
|
{
|
|
|
|
SysPushErrorParam("Invalid open mode");
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
if (!FS::EFileAdvisoryLockLevelIsValid(create.eAdvisoryLevel))
|
|
|
|
{
|
|
|
|
SysPushErrorParam("Invalid lock mode");
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
auto pathex = FS::NormalizePathRet(create.path);
|
|
|
|
if (pathex.empty())
|
|
|
|
{
|
|
|
|
SysPushErrorMemory();
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
auto win32Path = Locale::ConvertFromUTF8(pathex);
|
|
|
|
if (win32Path.empty())
|
|
|
|
{
|
|
|
|
SysPushErrorMemory();
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
hFileHandle = INVALID_HANDLE_VALUE;
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
dwShare = FS::NtLockAdvisoryToShare(create.eAdvisoryLevel);
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
if (create.bAsyncHandle)
|
|
|
|
{
|
|
|
|
dwFlags |= FILE_FLAG_OVERLAPPED;
|
|
|
|
}
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
if (create.bDirectIOMode)
|
|
|
|
{
|
|
|
|
dwFlags |= FILE_FLAG_NO_BUFFERING;
|
|
|
|
this->bDirectIO = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dwFlags)
|
|
|
|
{
|
2023-09-13 01:27:18 +00:00
|
|
|
dwAttrs |= FILE_ATTRIBUTE_NORMAL;
|
2023-08-29 00:24:52 +00:00
|
|
|
}
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-12-13 04:25:11 +00:00
|
|
|
for (AU_ITERATE_N(i, 2))
|
2023-08-29 00:24:52 +00:00
|
|
|
{
|
2023-12-13 04:25:11 +00:00
|
|
|
switch (create.eMode)
|
2023-07-29 07:02:47 +00:00
|
|
|
{
|
2023-12-13 04:25:11 +00:00
|
|
|
case FS::EFileOpenMode::eRead:
|
2023-07-29 07:02:47 +00:00
|
|
|
{
|
2023-09-13 01:27:18 +00:00
|
|
|
hFileHandle = Win32Open(win32Path.c_str(),
|
2023-12-13 04:25:11 +00:00
|
|
|
GENERIC_READ,
|
|
|
|
dwShare,
|
2023-09-13 01:27:18 +00:00
|
|
|
false,
|
2023-12-13 04:25:11 +00:00
|
|
|
OPEN_EXISTING,
|
2023-09-13 01:27:18 +00:00
|
|
|
dwFlags,
|
|
|
|
dwAttrs);
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-12-13 04:25:11 +00:00
|
|
|
if (hFileHandle != INVALID_HANDLE_VALUE)
|
2023-07-29 07:02:47 +00:00
|
|
|
{
|
2023-12-13 04:25:11 +00:00
|
|
|
this->uOSReadHandle = AuUInt64(hFileHandle);
|
2023-07-29 07:02:47 +00:00
|
|
|
}
|
2023-12-13 04:25:11 +00:00
|
|
|
|
|
|
|
break;
|
2023-07-29 07:02:47 +00:00
|
|
|
}
|
2023-12-13 04:25:11 +00:00
|
|
|
case FS::EFileOpenMode::eReadWrite:
|
2023-07-29 07:02:47 +00:00
|
|
|
{
|
2023-12-13 04:25:11 +00:00
|
|
|
if (create.bAlwaysCreateDirTree)
|
|
|
|
{
|
|
|
|
FS::CreateDirectories(pathex, true);
|
|
|
|
}
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-12-13 04:25:11 +00:00
|
|
|
if (create.bFailIfNonEmptyFile)
|
2023-07-29 07:02:47 +00:00
|
|
|
{
|
2023-09-13 01:27:18 +00:00
|
|
|
hFileHandle = Win32Open(win32Path.c_str(),
|
|
|
|
GENERIC_WRITE | GENERIC_READ | DELETE,
|
2023-12-13 04:25:11 +00:00
|
|
|
false,
|
2023-09-13 01:27:18 +00:00
|
|
|
false,
|
|
|
|
CREATE_NEW,
|
|
|
|
dwFlags,
|
|
|
|
dwAttrs);
|
2023-12-13 04:25:11 +00:00
|
|
|
|
|
|
|
if (hFileHandle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
if (GetLastError() == ERROR_FILE_EXISTS ||
|
|
|
|
AuFS::FileExists(pathex.c_str()))
|
|
|
|
{
|
|
|
|
SysPushErrorResourceExists("File {} already exists", create.path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-07-29 07:02:47 +00:00
|
|
|
}
|
2023-12-13 04:25:11 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
hFileHandle = Win32Open(win32Path.c_str(),
|
|
|
|
GENERIC_WRITE | GENERIC_READ | DELETE,
|
|
|
|
dwShare,
|
|
|
|
false,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
dwFlags,
|
|
|
|
dwAttrs);
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-12-13 04:25:11 +00:00
|
|
|
if (hFileHandle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
hFileHandle = Win32Open(win32Path.c_str(),
|
|
|
|
GENERIC_WRITE | GENERIC_READ | DELETE,
|
|
|
|
dwShare,
|
|
|
|
false,
|
|
|
|
CREATE_NEW,
|
|
|
|
dwFlags,
|
|
|
|
dwAttrs);
|
|
|
|
}
|
|
|
|
}
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-12-13 04:25:11 +00:00
|
|
|
if (hFileHandle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
this->uOSReadHandle = AuUInt64(hFileHandle);
|
|
|
|
this->uOSWriteHandle = AuUInt64(hFileHandle);
|
|
|
|
}
|
2023-08-29 00:24:52 +00:00
|
|
|
|
2023-12-13 04:25:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FS::EFileOpenMode::eWrite:
|
2023-07-29 07:02:47 +00:00
|
|
|
{
|
2023-12-13 04:25:11 +00:00
|
|
|
if (create.bAlwaysCreateDirTree)
|
|
|
|
{
|
|
|
|
FS::CreateDirectories(pathex, true);
|
|
|
|
}
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-12-13 04:25:11 +00:00
|
|
|
if (create.bFailIfNonEmptyFile)
|
2023-07-29 07:02:47 +00:00
|
|
|
{
|
2023-12-13 04:25:11 +00:00
|
|
|
hFileHandle = Win32Open(win32Path.c_str(),
|
|
|
|
GENERIC_WRITE | DELETE,
|
|
|
|
NULL,
|
|
|
|
false,
|
|
|
|
CREATE_NEW,
|
|
|
|
dwFlags,
|
|
|
|
dwAttrs);
|
|
|
|
|
|
|
|
if (hFileHandle == INVALID_HANDLE_VALUE)
|
2023-07-29 07:02:47 +00:00
|
|
|
{
|
2023-12-13 04:25:11 +00:00
|
|
|
if (GetLastError() == ERROR_FILE_EXISTS ||
|
|
|
|
AuFS::FileExists(pathex.c_str()))
|
|
|
|
{
|
|
|
|
SysPushErrorResourceExists("File {} already exists", create.path);
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-29 07:02:47 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-13 04:25:11 +00:00
|
|
|
else
|
2023-07-29 07:02:47 +00:00
|
|
|
{
|
2023-09-13 01:27:18 +00:00
|
|
|
hFileHandle = Win32Open(win32Path.c_str(),
|
2023-12-13 04:25:11 +00:00
|
|
|
GENERIC_WRITE | FILE_READ_ATTRIBUTES | DELETE,
|
2023-09-13 01:27:18 +00:00
|
|
|
dwShare,
|
|
|
|
false,
|
2023-12-13 04:25:11 +00:00
|
|
|
OPEN_EXISTING,
|
2023-09-13 01:27:18 +00:00
|
|
|
dwFlags,
|
|
|
|
dwAttrs);
|
2023-12-13 04:25:11 +00:00
|
|
|
|
|
|
|
if (hFileHandle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
hFileHandle = Win32Open(win32Path.c_str(),
|
|
|
|
GENERIC_WRITE | DELETE,
|
|
|
|
dwShare,
|
|
|
|
false,
|
|
|
|
CREATE_NEW,
|
|
|
|
dwFlags,
|
|
|
|
dwAttrs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hFileHandle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
this->uOSWriteHandle = AuUInt64(hFileHandle);
|
2023-07-29 07:02:47 +00:00
|
|
|
}
|
2023-12-13 04:25:11 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2023-07-29 07:02:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 04:25:11 +00:00
|
|
|
if (hFileHandle == INVALID_HANDLE_VALUE)
|
2023-07-29 07:02:47 +00:00
|
|
|
{
|
2023-12-13 04:25:11 +00:00
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
RuntimeWaitForSecondaryTick();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SysPushErrorIO("Couldn't open: {}", create.path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
2023-07-29 07:02:47 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-15 13:41:49 +00:00
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
this->bIsAsync = create.bAsyncHandle;
|
2024-03-27 03:12:49 +00:00
|
|
|
this->bShouldWriteEoS = create.bWriteEoSOnClose;
|
|
|
|
this->bFlushOnClose = create.bFlushOnClose;
|
2023-08-15 13:41:49 +00:00
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
this->path = create.path;
|
2023-07-29 07:02:47 +00:00
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
return this->IsValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool IsHandleTTY(AuUInt uHandle)
|
2023-08-15 13:41:49 +00:00
|
|
|
{
|
2023-08-29 00:24:52 +00:00
|
|
|
bool bIsPipe {};
|
2023-08-15 13:41:49 +00:00
|
|
|
DWORD dwType {};
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
if (!uHandle ||
|
|
|
|
uHandle == (AuUInt)INVALID_HANDLE_VALUE)
|
2023-08-15 13:41:49 +00:00
|
|
|
{
|
2023-09-07 22:20:26 +00:00
|
|
|
SysPushErrorArg();
|
2023-08-15 13:41:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
if (!(dwType = ::GetFileType((HANDLE)uHandle)))
|
2023-08-15 13:41:49 +00:00
|
|
|
{
|
2023-09-07 22:20:26 +00:00
|
|
|
SysPushErrorGeneric();
|
2023-08-29 00:24:52 +00:00
|
|
|
return {};
|
2023-08-15 13:41:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
return dwType == FILE_TYPE_CHAR;
|
2023-08-15 13:41:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
AUKN_SYM bool IsHandlePipe(AuUInt uHandle)
|
2023-08-15 13:41:49 +00:00
|
|
|
{
|
2023-08-29 00:24:52 +00:00
|
|
|
bool bIsPipe {};
|
2023-08-15 13:41:49 +00:00
|
|
|
DWORD dwType {};
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
if (!uHandle ||
|
|
|
|
uHandle == (AuUInt)INVALID_HANDLE_VALUE)
|
2023-08-15 13:41:49 +00:00
|
|
|
{
|
2023-09-07 22:20:26 +00:00
|
|
|
SysPushErrorArg();
|
2023-08-15 13:41:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
if (!(dwType = ::GetFileType((HANDLE)uHandle)))
|
2023-08-15 13:41:49 +00:00
|
|
|
{
|
2023-09-07 22:20:26 +00:00
|
|
|
SysPushErrorGeneric();
|
2023-08-29 00:24:52 +00:00
|
|
|
return {};
|
2023-08-15 13:41:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
return dwType == FILE_TYPE_PIPE;
|
2023-08-15 13:41:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
AUKN_SYM bool IsHandleFile(AuUInt uHandle)
|
2023-08-15 13:41:49 +00:00
|
|
|
{
|
|
|
|
bool bIsPipe {};
|
|
|
|
DWORD dwType {};
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
if (!uHandle ||
|
|
|
|
uHandle == (AuUInt)INVALID_HANDLE_VALUE)
|
2023-08-15 13:41:49 +00:00
|
|
|
{
|
2023-09-07 22:20:26 +00:00
|
|
|
SysPushErrorArg();
|
2023-08-15 13:41:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
if (!(dwType = ::GetFileType((HANDLE)uHandle)))
|
2023-08-15 13:41:49 +00:00
|
|
|
{
|
2023-09-07 22:20:26 +00:00
|
|
|
SysPushErrorGeneric();
|
2023-08-29 00:24:52 +00:00
|
|
|
return {};
|
2023-08-15 13:41:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 00:24:52 +00:00
|
|
|
return dwType == FILE_TYPE_DISK;
|
2023-08-15 13:41:49 +00:00
|
|
|
}
|
2023-07-29 07:02:47 +00:00
|
|
|
}
|