[+] PlatformFS abstraction / hook layer

This commit is contained in:
Reece Wilson 2024-12-01 17:57:50 +00:00
parent 8a94676549
commit 54672f812e
18 changed files with 564 additions and 187 deletions

View File

@ -0,0 +1,100 @@
/***
Copyright (C) 2021-2024 Jamie Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FSPlatform.hpp
Date: 2021-XX-XX - 2024-12-01
Author: Reece
***/
#pragma once
namespace Aurora::IO::FS
{
struct UpdateTimes;
struct IPlatformFS
{
// Aurora::Win32Open-style API (not CreateFileW)
// [*] Will miss the FILE_ATTRIBUTE_NORMAL flag
// [*] dwAttributes and dwFlags are unmerged for Win8-11 extensions
virtual AuOptional<void *> Win32OpenHook(const void *lpFileName,
AuUInt32 dwDesiredAccess,
AuUInt32 dwShareMode,
bool bInherit,
AuUInt32 dwCreationDisposition,
AuUInt32 dwFlags,
AuUInt32 dwAttributes) = 0;
virtual AuOptional<int> PosixOpenHook(const char *pPathName,
int flags,
AuUInt64 uFlags) = 0;
virtual IFileStream *OpenExNew(const AuROString &path,
EFileOpenMode openMode,
EFileAdvisoryLockLevel lock,
bool bCheck) = 0;
virtual void NukeStream(IFileStream *) = 0;
virtual IAsyncFileStream *OpenAsyncNew(const AuROString &path,
EFileOpenMode openMode,
AuOptional<bool> optbDirectIO,
AuOptional<EFileAdvisoryLockLevel> optLock) = 0;
virtual void NukeAsyncProvider(IAsyncFileStream *) = 0;
virtual AuSPtr<IReadDir> ReadDir(const AuROString &string) = 0;
virtual bool FilesInDirectory(const AuROString &string,
AuList<AuString> &files) = 0;
virtual bool DirsInDirectory(const AuROString &string,
AuList<AuString> &dirs) = 0;
virtual bool ReadFile(const AuROString &path,
AuByteBuffer &buffer) = 0;
virtual bool FileExists(const AuROString &path) = 0;
virtual bool DirExists(const AuROString &path) = 0;
virtual bool DirMk(const AuROString &path) = 0;
virtual bool Remove(const AuROString &path) = 0;
virtual bool Relink(const AuROString &src,
const AuROString &dest) = 0;
virtual bool Copy(const AuROString &src,
const AuROString &dest) = 0;
virtual bool StatFile(const AuROString &path,
Stat &stat) = 0;
virtual bool UpdateFileTimes(const AuROString &path,
const UpdateTimes &times) = 0;
virtual AuList<AuString> FileAttrsList(const AuROString &path) = 0;
virtual AuResult<Memory::ByteBuffer> FileAttrsGet(const AuROString &path,
const AuROString &attr) = 0;
virtual bool FileAttrsSet(const AuROString &path, const AuROString &attr,
const Memory::MemoryViewRead &view) = 0;
virtual bool FileAttrsDel(const AuROString &path, const AuROString &attr) = 0;
virtual bool BlockFile(const AuROString &path) = 0;
virtual bool UnblockFile(const AuROString &path) = 0;
virtual bool TrustFile(const AuROString &path) = 0;
virtual bool IsFileBlocked(const AuROString &path) = 0;
virtual bool IsFileTrusted(const AuROString &path) = 0;
};
AUKN_SYM AuSPtr<IPlatformFS> GetOSPlatform();
AUKN_SYM AuSPtr<IPlatformFS> GetCurrentPlatform();
AUKN_SYM void SetCurrentPlatform(AuSPtr<IPlatformFS> pThat);
}

View File

@ -98,6 +98,24 @@ namespace Aurora::IO
}
}
HANDLE Win32OpenHooked(LPCWSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
bool bInherit,
DWORD dwCreationDisposition,
DWORD dwFlags,
DWORD dwAttributes)
{
if (auto optHandle = AuFS::GetCurrentPlatform()->Win32OpenHook(lpFileName, dwDesiredAccess, dwShareMode, bInherit, dwCreationDisposition, dwFlags, dwAttributes))
{
return optHandle.Value();
}
else
{
return Win32Open(lpFileName, dwDesiredAccess, dwShareMode, bInherit, dwCreationDisposition, dwFlags, dwAttributes);
}
}
bool AFileHandle::InitFromPath(HandleCreate create)
{
HANDLE hFileHandle;
@ -164,13 +182,13 @@ namespace Aurora::IO
{
case FS::EFileOpenMode::eRead:
{
hFileHandle = Win32Open(win32Path.c_str(),
GENERIC_READ,
dwShare,
false,
OPEN_EXISTING,
dwFlags,
dwAttrs);
hFileHandle = Win32OpenHooked(win32Path.c_str(),
GENERIC_READ,
dwShare,
false,
OPEN_EXISTING,
dwFlags,
dwAttrs);
if (hFileHandle != INVALID_HANDLE_VALUE)
{
@ -188,13 +206,13 @@ namespace Aurora::IO
if (create.bFailIfNonEmptyFile)
{
hFileHandle = Win32Open(win32Path.c_str(),
GENERIC_WRITE | GENERIC_READ | DELETE,
false,
false,
CREATE_NEW,
dwFlags,
dwAttrs);
hFileHandle = Win32OpenHooked(win32Path.c_str(),
GENERIC_WRITE | GENERIC_READ | DELETE,
false,
false,
CREATE_NEW,
dwFlags,
dwAttrs);
if (hFileHandle == INVALID_HANDLE_VALUE)
{
@ -208,23 +226,23 @@ namespace Aurora::IO
}
else
{
hFileHandle = Win32Open(win32Path.c_str(),
GENERIC_WRITE | GENERIC_READ | DELETE,
dwShare,
false,
OPEN_EXISTING,
dwFlags,
dwAttrs);
hFileHandle = Win32OpenHooked(win32Path.c_str(),
GENERIC_WRITE | GENERIC_READ | DELETE,
dwShare,
false,
OPEN_EXISTING,
dwFlags,
dwAttrs);
if (hFileHandle == INVALID_HANDLE_VALUE)
{
hFileHandle = Win32Open(win32Path.c_str(),
GENERIC_WRITE | GENERIC_READ | DELETE,
dwShare,
false,
CREATE_NEW,
dwFlags,
dwAttrs);
hFileHandle = Win32OpenHooked(win32Path.c_str(),
GENERIC_WRITE | GENERIC_READ | DELETE,
dwShare,
false,
CREATE_NEW,
dwFlags,
dwAttrs);
}
}
@ -245,13 +263,13 @@ namespace Aurora::IO
if (create.bFailIfNonEmptyFile)
{
hFileHandle = Win32Open(win32Path.c_str(),
GENERIC_WRITE | DELETE,
NULL,
false,
CREATE_NEW,
dwFlags,
dwAttrs);
hFileHandle = Win32OpenHooked(win32Path.c_str(),
GENERIC_WRITE | DELETE,
NULL,
false,
CREATE_NEW,
dwFlags,
dwAttrs);
if (hFileHandle == INVALID_HANDLE_VALUE)
{
@ -265,23 +283,23 @@ namespace Aurora::IO
}
else
{
hFileHandle = Win32Open(win32Path.c_str(),
GENERIC_WRITE | FILE_READ_ATTRIBUTES | DELETE,
dwShare,
false,
OPEN_EXISTING,
dwFlags,
dwAttrs);
hFileHandle = Win32OpenHooked(win32Path.c_str(),
GENERIC_WRITE | FILE_READ_ATTRIBUTES | DELETE,
dwShare,
false,
OPEN_EXISTING,
dwFlags,
dwAttrs);
if (hFileHandle == INVALID_HANDLE_VALUE)
{
hFileHandle = Win32Open(win32Path.c_str(),
GENERIC_WRITE | DELETE,
dwShare,
false,
CREATE_NEW,
dwFlags,
dwAttrs);
hFileHandle = Win32OpenHooked(win32Path.c_str(),
GENERIC_WRITE | DELETE,
dwShare,
false,
CREATE_NEW,
dwFlags,
dwAttrs);
}
}

View File

@ -114,6 +114,18 @@ namespace Aurora::IO
this->uOSReadHandle = this->uOSWriteHandle = DupHandle(bError ? STDERR_FILENO : STDOUT_FILENO, true, bSharing);
}
int PosixOpenHooked(const char *pathname, int flags, mode_t mode)
{
if (auto optFD = AuFS::GetCurrentPlatform()->PosixOpenHook(pathname, flags, mode))
{
return optFD.Value();
}
else
{
return PosixOpen(pathname, flags, mode);
}
}
bool AFileHandle::InitFromPath(HandleCreate create)
{
int iFileDescriptor { -1 };
@ -191,9 +203,9 @@ namespace Aurora::IO
// TBD
#endif
iFileDescriptor = PosixOpen(pathex.c_str(),
flags,
0664);
iFileDescriptor = PosixOpenHooked(pathex.c_str(),
flags,
0664);
if (iFileDescriptor < 0)
{
@ -211,9 +223,9 @@ namespace Aurora::IO
{
RuntimeWaitForSecondaryTick();
iFileDescriptor = PosixOpen(pathex.c_str(),
flags,
0664);
iFileDescriptor = PosixOpenHooked(pathex.c_str(),
flags,
0664);
}
if (iFileDescriptor < 0)
@ -227,9 +239,9 @@ namespace Aurora::IO
if (errno == EISDIR)
{
flags &= ~(O_WRONLY | O_RDWR);
iFileDescriptor = PosixOpen(pathex.c_str(),
flags,
0664);
iFileDescriptor = PosixOpenHooked(pathex.c_str(),
flags,
0664);
}
if (iFileDescriptor < 0)

View File

@ -617,7 +617,7 @@ namespace Aurora::IO::FS
}
}
AUKN_SYM IAsyncFileStream *OpenAsyncNew(const AuROString &path, EFileOpenMode openMode, AuOptional<bool> optbDirectIO, AuOptional<EFileAdvisoryLockLevel> optLock)
IAsyncFileStream *PlatformFS::OpenAsyncNew(const AuROString &path, EFileOpenMode openMode, AuOptional<bool> optbDirectIO, AuOptional<EFileAdvisoryLockLevel> optLock)
{
auto bDirectIO = optbDirectIO.ValueOr(true);
auto lock = optLock.ValueOr(EFileAdvisoryLockLevel::eNoSafety);
@ -653,7 +653,7 @@ namespace Aurora::IO::FS
return pStream;
}
AUKN_SYM void OpenAsyncRelease(IAsyncFileStream *handle)
void PlatformFS::NukeAsyncProvider(IAsyncFileStream *handle)
{
AuSafeDelete<LinuxAsyncFileStream *>(handle);
}

View File

@ -732,7 +732,7 @@ namespace Aurora::IO::FS
}
}
AUKN_SYM IAsyncFileStream *OpenAsyncNew(const AuROString &path, EFileOpenMode openMode, AuOptional<bool> optbDirectIO, AuOptional<EFileAdvisoryLockLevel> optLock)
IAsyncFileStream *PlatformFS::OpenAsyncNew(const AuROString &path, EFileOpenMode openMode, AuOptional<bool> optbDirectIO, AuOptional<EFileAdvisoryLockLevel> optLock)
{
auto bDirectIO = optbDirectIO.ValueOr(true);
auto lock = optLock.ValueOr(EFileAdvisoryLockLevel::eNoSafety);
@ -768,7 +768,7 @@ namespace Aurora::IO::FS
return pStream;
}
AUKN_SYM void OpenAsyncRelease(IAsyncFileStream *handle)
void PlatformFS::NukeAsyncProvider(IAsyncFileStream *handle)
{
AuSafeDelete<NtAsyncFileStream *>(handle);
}

View File

@ -112,7 +112,7 @@ namespace Aurora::IO::FS
}
};
AUKN_SYM AuSPtr<IReadDir> ReadDir(const AuROString &string)
AuSPtr<IReadDir> PlatformFS::ReadDir(const AuROString &string)
{
if (string.empty())
{
@ -149,7 +149,7 @@ namespace Aurora::IO::FS
return pObj;
}
AUKN_SYM bool FilesInDirectory(const AuROString &string, AuList<AuString> &files)
bool PlatformFS::FilesInDirectory(const AuROString &string, AuList<AuString> &files)
{
auto itr = ReadDir(string);
if (!itr)
@ -174,7 +174,7 @@ namespace Aurora::IO::FS
return true;
}
AUKN_SYM bool DirsInDirectory(const AuROString &string, AuList<AuString> &dirs)
bool PlatformFS::DirsInDirectory(const AuROString &string, AuList<AuString> &dirs)
{
auto itr = ReadDir(string);
if (!itr)
@ -199,7 +199,7 @@ namespace Aurora::IO::FS
return true;
}
AUKN_SYM bool ReadFile(const AuROString &path, AuByteBuffer &buffer)
bool PlatformFS::ReadFile(const AuROString &path, AuByteBuffer &buffer)
{
std::wstring win32Path;
LARGE_INTEGER length;
@ -294,7 +294,7 @@ namespace Aurora::IO::FS
return status;
}
AUKN_SYM bool FileExists(const AuROString &path)
bool PlatformFS::FileExists(const AuROString &path)
{
try
{
@ -322,7 +322,7 @@ namespace Aurora::IO::FS
}
}
AUKN_SYM bool DirExists(const AuROString &path)
bool PlatformFS::DirExists(const AuROString &path)
{
try
{
@ -350,7 +350,7 @@ namespace Aurora::IO::FS
}
}
AUKN_SYM bool DirMk(const AuROString &path)
bool PlatformFS::DirMk(const AuROString &path)
{
try
{
@ -376,7 +376,7 @@ namespace Aurora::IO::FS
}
}
AUKN_SYM bool Remove(const AuROString &path)
bool PlatformFS::Remove(const AuROString &path)
{
try
{
@ -433,7 +433,7 @@ namespace Aurora::IO::FS
}
}
AUKN_SYM bool Relink(const AuROString &src, const AuROString &dest)
bool PlatformFS::Relink(const AuROString &src, const AuROString &dest)
{
try
{
@ -468,7 +468,7 @@ namespace Aurora::IO::FS
}
}
AUKN_SYM bool Copy(const AuROString &src, const AuROString &dest)
bool PlatformFS::Copy(const AuROString &src, const AuROString &dest)
{
try
{
@ -502,7 +502,7 @@ namespace Aurora::IO::FS
}
}
AUKN_SYM bool StatFile(const AuROString &path, Stat &stat)
bool PlatformFS::StatFile(const AuROString &path, Stat &stat)
{
WIN32_FILE_ATTRIBUTE_DATA data;

View File

@ -189,12 +189,12 @@ namespace Aurora::IO::FS
return pObj;
}
AUKN_SYM AuSPtr<IReadDir> ReadDir(const AuROString &string)
AuSPtr<IReadDir> PlatformFS::ReadDir(const AuROString &string)
{
return ReadDirEx(string, false);
}
AUKN_SYM bool FilesInDirectory(const AuROString &string, AuList<AuString> &files)
bool PlatformFS::FilesInDirectory(const AuROString &string, AuList<AuString> &files)
{
auto itr = ReadDirEx(string, true);
if (!itr)
@ -219,7 +219,7 @@ namespace Aurora::IO::FS
return true;
}
AUKN_SYM bool DirsInDirectory(const AuROString &string, AuList<AuString> &dirs)
bool PlatformFS::DirsInDirectory(const AuROString &string, AuList<AuString> &dirs)
{
auto itr = ReadDirEx(string, true);
if (!itr)
@ -272,7 +272,7 @@ namespace Aurora::IO::FS
return true;
}
AUKN_SYM bool ReadFile(const AuROString &path, AuByteBuffer &buffer)
bool PlatformFS::ReadFile(const AuROString &path, AuByteBuffer &buffer)
{
AuMemoryViewWrite writeView;
@ -344,7 +344,7 @@ namespace Aurora::IO::FS
return dir ? S_ISDIR(s.st_mode) : S_ISREG(s.st_mode);
}
AUKN_SYM bool FileExists(const AuROString &path)
bool PlatformFS::FileExists(const AuROString &path)
{
if (path.empty())
{
@ -362,7 +362,7 @@ namespace Aurora::IO::FS
return UnixExists(pathExpanded, false);
}
AUKN_SYM bool DirExists(const AuROString &path)
bool PlatformFS::DirExists(const AuROString &path)
{
if (path.empty())
{
@ -380,7 +380,7 @@ namespace Aurora::IO::FS
return UnixExists(pathExpanded, true);
}
AUKN_SYM bool DirMk(const AuROString &path)
bool PlatformFS::DirMk(const AuROString &path)
{
if (path.empty())
{
@ -398,7 +398,7 @@ namespace Aurora::IO::FS
return CreateDirectories(pathExpanded, false);
}
AUKN_SYM bool Remove(const AuROString &path)
bool PlatformFS::Remove(const AuROString &path)
{
struct stat s;
@ -458,7 +458,7 @@ namespace Aurora::IO::FS
return false;
}
AUKN_SYM bool Relink(const AuROString &src, const AuROString &dest)
bool PlatformFS::Relink(const AuROString &src, const AuROString &dest)
{
if (src.empty() ||
dest.empty())
@ -484,7 +484,7 @@ namespace Aurora::IO::FS
#if defined(AURORA_IS_LINUX_DERIVED)
AUKN_SYM bool Copy(const AuROString &src, const AuROString &dest)
bool PlatformFS::Copy(const AuROString &src, const AuROString &dest)
{
if (src.empty() ||
dest.empty())
@ -537,7 +537,7 @@ namespace Aurora::IO::FS
#elif defined(AURORA_IS_BSD_DERIVED)
AUKN_SYM bool Copy(const AuROString &src, const AuROString &dest)
bool PlatformFS::Copy(const AuROString &src, const AuROString &dest)
{
if (src.empty() ||
dest.empty())
@ -589,7 +589,7 @@ namespace Aurora::IO::FS
#else
AUKN_SYM bool Copy(const AuROString &src, const AuROString &dest)
bool PlatformFS::Copy(const AuROString &src, const AuROString &dest)
{
// TODO: not that i care
return false;
@ -597,7 +597,7 @@ namespace Aurora::IO::FS
#endif
AUKN_SYM bool StatFile(const AuROString &pathRel, Stat &stat)
bool PlatformFS::StatFile(const AuROString &pathRel, Stat &stat)
{
stat = {};

View File

@ -7,6 +7,9 @@
***/
#pragma once
#include "FSPlatform.hpp"
#if !defined(NO_FSTREAM) && (!defined(__has_include) || __has_include(<fstream>))
#include <fstream>
#if !defined(HAS_FSTREAM)

214
Source/IO/FS/FSPlatform.cpp Normal file
View File

@ -0,0 +1,214 @@
/***
Copyright (C) 2021-2024 Jamie Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FSPlatform.cpp
Date: 2024-12-01
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "FSPlatform.hpp"
namespace Aurora::IO::FS
{
static AuMutex gPlatformMutex;
static AuSPtr<IPlatformFS> gPtrPlatform;
PlatformFS gPlatform;
AUKN_SYM AuSPtr<IPlatformFS> GetOSPlatform()
{
return AuUnsafeRaiiToShared(&gPlatform);
}
AUKN_SYM AuSPtr<IPlatformFS> GetCurrentPlatform()
{
// perf hack: dont enter the global lock if we know we dont need to copy the control block
if (gPtrPlatform.get())
{
AU_LOCK_GLOBAL_GUARD(gPlatformMutex);
return gPtrPlatform;
}
else
{
return AuUnsafeRaiiToShared(&gPlatform);
}
}
AUKN_SYM void SetCurrentPlatform(AuSPtr<IPlatformFS> pThat)
{
AU_LOCK_GLOBAL_GUARD(gPlatformMutex);
gPtrPlatform = pThat;
}
AuOptional<void *> PlatformFS::Win32OpenHook(const void *lpFileName,
AuUInt32 dwDesiredAccess,
AuUInt32 dwShareMode,
bool bInherit,
AuUInt32 dwCreationDisposition,
AuUInt32 dwFlags,
AuUInt32 dwAttributes)
{
return {};
}
AuOptional<int> PlatformFS::PosixOpenHook(const char *pPathName,
int flags,
AuUInt64 uFlags)
{
return {};
}
AUKN_SYM AuSPtr<IReadDir> ReadDir(const AuROString &path)
{
return GetCurrentPlatform()->ReadDir(path);
}
AUKN_SYM bool FilesInDirectory(const AuROString &path,
AuList<AuString> &files)
{
return GetCurrentPlatform()->FilesInDirectory(path, files);
}
AUKN_SYM bool DirsInDirectory(const AuROString &path,
AuList<AuString> &dirs)
{
return GetCurrentPlatform()->DirsInDirectory(path, dirs);
}
AUKN_SYM bool ReadFile(const AuROString &path,
AuByteBuffer &buffer)
{
return GetCurrentPlatform()->ReadFile(path, buffer);
}
AUKN_SYM bool FileExists(const AuROString &path)
{
return GetCurrentPlatform()->FileExists(path);
}
AUKN_SYM bool DirExists(const AuROString &path)
{
return GetCurrentPlatform()->DirExists(path);
}
AUKN_SYM bool DirMk(const AuROString &path)
{
return GetCurrentPlatform()->DirMk(path);
}
AUKN_SYM bool Remove(const AuROString &path)
{
return GetCurrentPlatform()->Remove(path);
}
AUKN_SYM bool Relink(const AuROString &src,
const AuROString &dest)
{
return GetCurrentPlatform()->Relink(src, dest);
}
AUKN_SYM bool Copy(const AuROString &src,
const AuROString &dest)
{
return GetCurrentPlatform()->Copy(src, dest);
}
AUKN_SYM bool StatFile(const AuROString &path,
Stat &stat)
{
return GetCurrentPlatform()->StatFile(path, stat);
}
AUKN_SYM bool UpdateFileTimes(const AuROString &path,
const UpdateTimes &times)
{
return GetCurrentPlatform()->UpdateFileTimes(path, times);
}
AUKN_SYM AuList<AuString> FileAttrsList(const AuROString &path)
{
return GetCurrentPlatform()->FileAttrsList(path);
}
AUKN_SYM AuResult<Memory::ByteBuffer> FileAttrsGet(const AuROString &path,
const AuROString &attr)
{
return GetCurrentPlatform()->FileAttrsGet(path, attr);
}
AUKN_SYM bool FileAttrsSet(const AuROString &path, const AuROString &attr,
const Memory::MemoryViewRead &view)
{
return GetCurrentPlatform()->FileAttrsSet(path, attr, view);
}
AUKN_SYM bool FileAttrsDel(const AuROString &path,
const AuROString &attr)
{
return GetCurrentPlatform()->FileAttrsDel(path, attr);
}
AUKN_SYM bool BlockFile(const AuROString &path)
{
return GetCurrentPlatform()->BlockFile(path);
}
AUKN_SYM bool UnblockFile(const AuROString &path)
{
return GetCurrentPlatform()->UnblockFile(path);
}
AUKN_SYM bool TrustFile(const AuROString &path)
{
return GetCurrentPlatform()->TrustFile(path);
}
AUKN_SYM bool IsFileBlocked(const AuROString &path)
{
return GetCurrentPlatform()->IsFileBlocked(path);
}
AUKN_SYM bool IsFileTrusted(const AuROString &path)
{
return GetCurrentPlatform()->IsFileTrusted(path);
}
AUKN_SYM IFileStream *CreateNew(const AuROString &path)
{
return GetCurrentPlatform()->OpenExNew(path, EFileOpenMode::eWrite, EFileAdvisoryLockLevel::eBlockReadWrite, true);
}
AUKN_SYM void CreateRelease(IFileStream *that)
{
GetCurrentPlatform()->NukeStream(that);
}
AUKN_SYM IFileStream *OpenNew(const AuROString &path, EFileOpenMode openMode, EFileAdvisoryLockLevel lock)
{
return GetCurrentPlatform()->OpenExNew(path, openMode, lock, false);
}
AUKN_SYM void OpenRelease(IFileStream *that)
{
GetCurrentPlatform()->NukeStream(that);
}
AUKN_SYM IFileStream *OpenReadNew(const AuROString &path, EFileAdvisoryLockLevel level)
{
return GetCurrentPlatform()->OpenExNew(path, EFileOpenMode::eRead, level, false);
}
AUKN_SYM void OpenReadRelease(IFileStream *that)
{
GetCurrentPlatform()->NukeStream(that);
}
AUKN_SYM IFileStream *OpenWriteNew(const AuROString &path, EFileAdvisoryLockLevel level)
{
return GetCurrentPlatform()->OpenExNew(path, EFileOpenMode::eWrite, level, false);
}
AUKN_SYM void OpenWriteRelease(IFileStream *that)
{
GetCurrentPlatform()->NukeStream(that);
}
}

100
Source/IO/FS/FSPlatform.hpp Normal file
View File

@ -0,0 +1,100 @@
/***
Copyright (C) 2021-2024 Jamie Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FSPlatform.hpp
Date: 2024-12-01
Author: Reece
***/
#pragma once
#include <Aurora/IO/FS/Hook/FSPlatform.hpp>
namespace Aurora::IO::FS
{
struct UpdateTimes;
struct PlatformFS : IPlatformFS
{
AuOptional<void *> Win32OpenHook(const void *lpFileName,
AuUInt32 dwDesiredAccess,
AuUInt32 dwShareMode,
bool bInherit,
AuUInt32 dwCreationDisposition,
AuUInt32 dwFlags,
AuUInt32 dwAttributes) override;
AuOptional<int> PosixOpenHook(const char *pPathName,
int flags,
AuUInt64 uFlags) override;
IFileStream *OpenExNew(const AuROString &path,
EFileOpenMode openMode,
EFileAdvisoryLockLevel lock,
bool bCheck) override;
void NukeStream(IFileStream *) override;
IAsyncFileStream *OpenAsyncNew(const AuROString &path,
EFileOpenMode openMode,
AuOptional<bool> optbDirectIO,
AuOptional<EFileAdvisoryLockLevel> optLock) override;
void NukeAsyncProvider(IAsyncFileStream *) override;
AuSPtr<IReadDir> ReadDir(const AuROString &string) override;
bool FilesInDirectory(const AuROString &string,
AuList<AuString> &files) override;
bool DirsInDirectory(const AuROString &string,
AuList<AuString> &dirs) override;
bool ReadFile(const AuROString &path,
AuByteBuffer &buffer) override;
bool FileExists(const AuROString &path) override;
bool DirExists(const AuROString &path) override;
bool DirMk(const AuROString &path) override;
bool Remove(const AuROString &path) override;
bool Relink(const AuROString &src,
const AuROString &dest) override;
bool Copy(const AuROString &src,
const AuROString &dest) override;
bool StatFile(const AuROString &path,
Stat &stat) override;
bool UpdateFileTimes(const AuROString &path,
const UpdateTimes &times) override;
AuList<AuString> FileAttrsList(const AuROString &path) override;
AuResult<Memory::ByteBuffer> FileAttrsGet(const AuROString &path,
const AuROString &attr) override;
bool FileAttrsSet(const AuROString &path,
const AuROString &attr,
const Memory::MemoryViewRead &view) override;
bool FileAttrsDel(const AuROString &path,
const AuROString &attr) override;
bool BlockFile(const AuROString &path) override;
bool UnblockFile(const AuROString &path) override;
bool TrustFile(const AuROString &path) override;
bool IsFileBlocked(const AuROString &path) override;
bool IsFileTrusted(const AuROString &path) override;
};
extern PlatformFS gPlatform;
}

View File

@ -25,7 +25,7 @@ namespace Aurora::IO::FS
return temp;
}
AUKN_SYM bool UpdateFileTimes(const AuROString &path, const UpdateTimes &times)
bool PlatformFS::UpdateFileTimes(const AuROString &path, const UpdateTimes &times)
{
HANDLE hFile;
FILETIME created;

View File

@ -31,7 +31,7 @@ namespace Aurora::IO::FS
temp->tv_usec = ts.tv_nsec / 1'000ull;
}
AUKN_SYM bool UpdateFileTimes(const AuROString &path, const UpdateTimes &times)
bool PlatformFS::UpdateFileTimes(const AuROString &path, const UpdateTimes &times)
{
if (times.createdNs)
{

View File

@ -11,7 +11,7 @@
namespace Aurora::IO::FS
{
AUKN_SYM AuList<AuString> FileAttrsList(const AuROString &path)
AuList<AuString> PlatformFS::FileAttrsList(const AuROString &path)
{
AuList<AuString> names;
WIN32_FIND_STREAM_DATA data;
@ -108,7 +108,7 @@ namespace Aurora::IO::FS
return names;
}
AUKN_SYM AuResult<Memory::ByteBuffer> FileAttrsGet(const AuROString &path, const AuROString &attr)
AuResult<Memory::ByteBuffer> PlatformFS::FileAttrsGet(const AuROString &path, const AuROString &attr)
{
try
{
@ -127,7 +127,7 @@ namespace Aurora::IO::FS
}
}
AUKN_SYM bool FileAttrsSet(const AuROString &path, const AuROString &attr, const Memory::MemoryViewRead &view)
bool PlatformFS::FileAttrsSet(const AuROString &path, const AuROString &attr, const Memory::MemoryViewRead &view)
{
try
{
@ -139,7 +139,7 @@ namespace Aurora::IO::FS
}
}
AUKN_SYM bool FileAttrsDel(const AuROString &path, const AuROString &attr)
bool PlatformFS::FileAttrsDel(const AuROString &path, const AuROString &attr)
{
try
{

View File

@ -13,7 +13,7 @@
namespace Aurora::IO::FS
{
AUKN_SYM AuList<AuString> FileAttrsList(const AuROString &path)
AuList<AuString> PlatformFS::FileAttrsList(const AuROString &path)
{
static const auto kLength = 10 * 1024;
AuString buffer;
@ -46,7 +46,7 @@ namespace Aurora::IO::FS
return AuSplitStringLegacy(buffer, "\00");
}
AUKN_SYM AuResult<AuByteBuffer> FileAttrsGet(const AuROString &path, const AuROString &attr)
AuResult<AuByteBuffer> PlatformFS::FileAttrsGet(const AuROString &path, const AuROString &attr)
{
AuString copy(attr);
AuByteBuffer buffer;
@ -87,7 +87,7 @@ namespace Aurora::IO::FS
return AuMove(buffer);
}
AUKN_SYM bool FileAttrsSet(const AuROString &path, const AuROString &attr, const Memory::MemoryViewRead &view)
bool PlatformFS::FileAttrsSet(const AuROString &path, const AuROString &attr, const Memory::MemoryViewRead &view)
{
AuString copy(attr);
@ -112,7 +112,7 @@ namespace Aurora::IO::FS
return true;
}
AUKN_SYM bool FileAttrsDel(const AuROString &path, const AuROString &attr)
bool PlatformFS::FileAttrsDel(const AuROString &path, const AuROString &attr)
{
AuString copy(attr);

View File

@ -566,10 +566,10 @@ namespace Aurora::IO::FS
AuSafeDelete<WinFileStream *>(that);
}
static IFileStream *OpenNewEx(const AuROString &path,
EFileOpenMode openMode,
EFileAdvisoryLockLevel lock,
bool bCheck)
IFileStream *PlatformFS::OpenExNew(const AuROString &path,
EFileOpenMode openMode,
EFileAdvisoryLockLevel lock,
bool bCheck)
{
try
{
@ -609,42 +609,7 @@ namespace Aurora::IO::FS
}
}
AUKN_SYM IFileStream *CreateNew(const AuROString &path)
{
return OpenNewEx(path, EFileOpenMode::eWrite, EFileAdvisoryLockLevel::eBlockReadWrite, true);
}
AUKN_SYM void CreateRelease(IFileStream *that)
{
AuSafeDelete<WinFileStream *>(that);
}
AUKN_SYM IFileStream *OpenNew(const AuROString &path, EFileOpenMode openMode, EFileAdvisoryLockLevel lock)
{
return OpenNewEx(path, openMode, lock, false);
}
AUKN_SYM void OpenRelease(IFileStream *that)
{
AuSafeDelete<WinFileStream *>(that);
}
AUKN_SYM IFileStream *OpenReadNew(const AuROString &path, EFileAdvisoryLockLevel level)
{
return OpenNew(path, EFileOpenMode::eRead, level);
}
AUKN_SYM void OpenReadRelease(IFileStream * that)
{
AuSafeDelete<WinFileStream *>(that);
}
AUKN_SYM IFileStream *OpenWriteNew(const AuROString &path, EFileAdvisoryLockLevel level)
{
return OpenNew(path, EFileOpenMode::eWrite, level);
}
AUKN_SYM void OpenWriteRelease(IFileStream *that)
void PlatformFS::NukeStream(IFileStream *that)
{
AuSafeDelete<WinFileStream *>(that);
}

View File

@ -147,7 +147,7 @@ namespace Aurora::IO::FS
AU_LOCK_GUARD(this->mutex_);
parameters.outVariable = 0;
if (!this->pHandle_)
{
SysPushErrorUninitialized();
@ -433,10 +433,10 @@ namespace Aurora::IO::FS
AuSafeDelete<PosixFileStream *>(that);
}
static IFileStream *OpenNewEx(const AuROString &path,
EFileOpenMode openMode,
EFileAdvisoryLockLevel lock,
bool bCheck)
IFileStream *PlatformFS::OpenExNew(const AuROString &path,
EFileOpenMode openMode,
EFileAdvisoryLockLevel lock,
bool bCheck)
{
try
{
@ -476,42 +476,7 @@ namespace Aurora::IO::FS
}
}
AUKN_SYM IFileStream *CreateNew(const AuROString &path)
{
return OpenNewEx(path, EFileOpenMode::eWrite, EFileAdvisoryLockLevel::eBlockReadWrite, true);
}
AUKN_SYM void CreateRelease(IFileStream *that)
{
AuSafeDelete<PosixFileStream *>(that);
}
AUKN_SYM IFileStream *OpenNew(const AuROString &path, EFileOpenMode openMode, EFileAdvisoryLockLevel lock)
{
return OpenNewEx(path, openMode, lock, false);
}
AUKN_SYM void OpenRelease(IFileStream *that)
{
AuSafeDelete<PosixFileStream *>(that);
}
AUKN_SYM IFileStream *OpenReadNew(const AuROString &path, EFileAdvisoryLockLevel level)
{
return OpenNewEx(path, EFileOpenMode::eRead, level, false);
}
AUKN_SYM void OpenReadRelease(IFileStream * that)
{
AuSafeDelete<PosixFileStream *>(that);
}
AUKN_SYM IFileStream *OpenWriteNew(const AuROString &path, EFileAdvisoryLockLevel level)
{
return OpenNewEx(path, EFileOpenMode::eWrite, level, false);
}
AUKN_SYM void OpenWriteRelease(IFileStream *that)
void PlatformFS::NukeStream(IFileStream *that)
{
AuSafeDelete<PosixFileStream *>(that);
}

View File

@ -11,17 +11,17 @@
namespace Aurora::IO::FS
{
AUKN_SYM bool BlockFile(const AuROString &path)
bool PlatformFS::BlockFile(const AuROString &path)
{
return AuFS::WriteFile(AuString(path) + ":Zone.Identifier", "[ZoneTransfer]\r\nZoneId=3\r\n");
}
AUKN_SYM bool UnblockFile(const AuROString &path)
bool PlatformFS::UnblockFile(const AuROString &path)
{
return AuFS::WriteFile(AuString(path) + ":Zone.Identifier", "[ZoneTransfer]\r\nZoneId=0\r\n");
}
AUKN_SYM bool TrustFile(const AuROString &path)
bool PlatformFS::TrustFile(const AuROString &path)
{
AuString idc;
@ -38,7 +38,7 @@ namespace Aurora::IO::FS
return true;
}
AUKN_SYM bool IsFileBlocked(const AuROString &path)
bool PlatformFS::IsFileBlocked(const AuROString &path)
{
AuString content;
@ -58,7 +58,7 @@ namespace Aurora::IO::FS
AuStringContains(content, "ZoneId=4\r\n"); // untrusted
}
AUKN_SYM bool IsFileTrusted(const AuROString &path)
bool PlatformFS::IsFileTrusted(const AuROString &path)
{
AuString content;

View File

@ -17,7 +17,7 @@
namespace Aurora::IO::FS
{
AUKN_SYM bool BlockFile(const AuROString &path)
bool PlatformFS::BlockFile(const AuROString &path)
{
auto srcPath = NormalizePathRet(path);
if (srcPath.empty())
@ -81,7 +81,7 @@ namespace Aurora::IO::FS
return true;
}
AUKN_SYM bool UnblockFile(const AuROString &path)
bool PlatformFS::UnblockFile(const AuROString &path)
{
auto srcPath = NormalizePathRet(path);
if (srcPath.empty())
@ -109,7 +109,7 @@ namespace Aurora::IO::FS
return AuFS::FileExists(srcPath);
}
AUKN_SYM bool TrustFile(const AuROString &path)
bool PlatformFS::TrustFile(const AuROString &path)
{
struct stat s;
mode_t mode { 0755 };
@ -136,7 +136,7 @@ namespace Aurora::IO::FS
return true;
}
AUKN_SYM bool IsFileBlocked(const AuROString &path)
bool PlatformFS::IsFileBlocked(const AuROString &path)
{
auto srcPath = NormalizePathRet(path);
if (srcPath.empty())
@ -160,7 +160,7 @@ namespace Aurora::IO::FS
return false;
}
AUKN_SYM bool IsFileTrusted(const AuROString &path)
bool PlatformFS::IsFileTrusted(const AuROString &path)
{
struct stat s;
auto srcPath = NormalizePathRet(path);