From 54672f812eae684b83cfe5cd47a4da17f9aa2038 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Sun, 1 Dec 2024 17:57:50 +0000 Subject: [PATCH] [+] PlatformFS abstraction / hook layer --- Include/Aurora/IO/FS/Hook/FSPlatform.hpp | 100 +++++++++++ Source/IO/AuIOHandle.NT.cpp | 116 ++++++------ Source/IO/AuIOHandle.Unix.cpp | 30 +++- Source/IO/FS/Async.Linux.cpp | 4 +- Source/IO/FS/Async.NT.cpp | 4 +- Source/IO/FS/FS.NT.cpp | 22 +-- Source/IO/FS/FS.Unix.cpp | 26 +-- Source/IO/FS/FS.hpp | 3 + Source/IO/FS/FSPlatform.cpp | 214 +++++++++++++++++++++++ Source/IO/FS/FSPlatform.hpp | 100 +++++++++++ Source/IO/FS/FSTimes.NT.cpp | 2 +- Source/IO/FS/FSTimes.Unix.cpp | 2 +- Source/IO/FS/FileAttrs.NT.cpp | 8 +- Source/IO/FS/FileAttrs.Unix.cpp | 8 +- Source/IO/FS/FileStream.NT.cpp | 45 +---- Source/IO/FS/FileStream.Unix.cpp | 47 +---- Source/IO/FS/FileTrust.NT.cpp | 10 +- Source/IO/FS/FileTrust.Unix.cpp | 10 +- 18 files changed, 564 insertions(+), 187 deletions(-) create mode 100644 Include/Aurora/IO/FS/Hook/FSPlatform.hpp create mode 100644 Source/IO/FS/FSPlatform.cpp create mode 100644 Source/IO/FS/FSPlatform.hpp diff --git a/Include/Aurora/IO/FS/Hook/FSPlatform.hpp b/Include/Aurora/IO/FS/Hook/FSPlatform.hpp new file mode 100644 index 00000000..45bdada2 --- /dev/null +++ b/Include/Aurora/IO/FS/Hook/FSPlatform.hpp @@ -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 Win32OpenHook(const void *lpFileName, + AuUInt32 dwDesiredAccess, + AuUInt32 dwShareMode, + bool bInherit, + AuUInt32 dwCreationDisposition, + AuUInt32 dwFlags, + AuUInt32 dwAttributes) = 0; + + virtual AuOptional 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 optbDirectIO, + AuOptional optLock) = 0; + + virtual void NukeAsyncProvider(IAsyncFileStream *) = 0; + + virtual AuSPtr ReadDir(const AuROString &string) = 0; + + virtual bool FilesInDirectory(const AuROString &string, + AuList &files) = 0; + + virtual bool DirsInDirectory(const AuROString &string, + AuList &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 ×) = 0; + + virtual AuList FileAttrsList(const AuROString &path) = 0; + + virtual AuResult 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 GetOSPlatform(); + AUKN_SYM AuSPtr GetCurrentPlatform(); + AUKN_SYM void SetCurrentPlatform(AuSPtr pThat); +} \ No newline at end of file diff --git a/Source/IO/AuIOHandle.NT.cpp b/Source/IO/AuIOHandle.NT.cpp index e163a188..95155cae 100644 --- a/Source/IO/AuIOHandle.NT.cpp +++ b/Source/IO/AuIOHandle.NT.cpp @@ -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); } } diff --git a/Source/IO/AuIOHandle.Unix.cpp b/Source/IO/AuIOHandle.Unix.cpp index 7549a9c6..8e74cd45 100644 --- a/Source/IO/AuIOHandle.Unix.cpp +++ b/Source/IO/AuIOHandle.Unix.cpp @@ -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) diff --git a/Source/IO/FS/Async.Linux.cpp b/Source/IO/FS/Async.Linux.cpp index d1e8bbeb..ae7ffb69 100644 --- a/Source/IO/FS/Async.Linux.cpp +++ b/Source/IO/FS/Async.Linux.cpp @@ -617,7 +617,7 @@ namespace Aurora::IO::FS } } - AUKN_SYM IAsyncFileStream *OpenAsyncNew(const AuROString &path, EFileOpenMode openMode, AuOptional optbDirectIO, AuOptional optLock) + IAsyncFileStream *PlatformFS::OpenAsyncNew(const AuROString &path, EFileOpenMode openMode, AuOptional optbDirectIO, AuOptional 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(handle); } diff --git a/Source/IO/FS/Async.NT.cpp b/Source/IO/FS/Async.NT.cpp index 130da925..de215494 100644 --- a/Source/IO/FS/Async.NT.cpp +++ b/Source/IO/FS/Async.NT.cpp @@ -732,7 +732,7 @@ namespace Aurora::IO::FS } } - AUKN_SYM IAsyncFileStream *OpenAsyncNew(const AuROString &path, EFileOpenMode openMode, AuOptional optbDirectIO, AuOptional optLock) + IAsyncFileStream *PlatformFS::OpenAsyncNew(const AuROString &path, EFileOpenMode openMode, AuOptional optbDirectIO, AuOptional 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(handle); } diff --git a/Source/IO/FS/FS.NT.cpp b/Source/IO/FS/FS.NT.cpp index 49d2e6ec..88f13fb2 100644 --- a/Source/IO/FS/FS.NT.cpp +++ b/Source/IO/FS/FS.NT.cpp @@ -112,7 +112,7 @@ namespace Aurora::IO::FS } }; - AUKN_SYM AuSPtr ReadDir(const AuROString &string) + AuSPtr 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 &files) + bool PlatformFS::FilesInDirectory(const AuROString &string, AuList &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 &dirs) + bool PlatformFS::DirsInDirectory(const AuROString &string, AuList &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; diff --git a/Source/IO/FS/FS.Unix.cpp b/Source/IO/FS/FS.Unix.cpp index a04324ef..c9251c63 100755 --- a/Source/IO/FS/FS.Unix.cpp +++ b/Source/IO/FS/FS.Unix.cpp @@ -189,12 +189,12 @@ namespace Aurora::IO::FS return pObj; } - AUKN_SYM AuSPtr ReadDir(const AuROString &string) + AuSPtr PlatformFS::ReadDir(const AuROString &string) { return ReadDirEx(string, false); } - AUKN_SYM bool FilesInDirectory(const AuROString &string, AuList &files) + bool PlatformFS::FilesInDirectory(const AuROString &string, AuList &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 &dirs) + bool PlatformFS::DirsInDirectory(const AuROString &string, AuList &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 = {}; diff --git a/Source/IO/FS/FS.hpp b/Source/IO/FS/FS.hpp index 5bedf2bc..a60e2e88 100644 --- a/Source/IO/FS/FS.hpp +++ b/Source/IO/FS/FS.hpp @@ -7,6 +7,9 @@ ***/ #pragma once + +#include "FSPlatform.hpp" + #if !defined(NO_FSTREAM) && (!defined(__has_include) || __has_include()) #include #if !defined(HAS_FSTREAM) diff --git a/Source/IO/FS/FSPlatform.cpp b/Source/IO/FS/FSPlatform.cpp new file mode 100644 index 00000000..2861c2bf --- /dev/null +++ b/Source/IO/FS/FSPlatform.cpp @@ -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 +#include "FSPlatform.hpp" + +namespace Aurora::IO::FS +{ + static AuMutex gPlatformMutex; + static AuSPtr gPtrPlatform; + PlatformFS gPlatform; + + AUKN_SYM AuSPtr GetOSPlatform() + { + return AuUnsafeRaiiToShared(&gPlatform); + } + + AUKN_SYM AuSPtr 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 pThat) + { + AU_LOCK_GLOBAL_GUARD(gPlatformMutex); + gPtrPlatform = pThat; + } + + AuOptional PlatformFS::Win32OpenHook(const void *lpFileName, + AuUInt32 dwDesiredAccess, + AuUInt32 dwShareMode, + bool bInherit, + AuUInt32 dwCreationDisposition, + AuUInt32 dwFlags, + AuUInt32 dwAttributes) + { + return {}; + } + + AuOptional PlatformFS::PosixOpenHook(const char *pPathName, + int flags, + AuUInt64 uFlags) + { + return {}; + } + + AUKN_SYM AuSPtr ReadDir(const AuROString &path) + { + return GetCurrentPlatform()->ReadDir(path); + } + + AUKN_SYM bool FilesInDirectory(const AuROString &path, + AuList &files) + { + return GetCurrentPlatform()->FilesInDirectory(path, files); + } + + AUKN_SYM bool DirsInDirectory(const AuROString &path, + AuList &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 ×) + { + return GetCurrentPlatform()->UpdateFileTimes(path, times); + } + + AUKN_SYM AuList FileAttrsList(const AuROString &path) + { + return GetCurrentPlatform()->FileAttrsList(path); + } + + AUKN_SYM AuResult 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); + } +} \ No newline at end of file diff --git a/Source/IO/FS/FSPlatform.hpp b/Source/IO/FS/FSPlatform.hpp new file mode 100644 index 00000000..b0a5bc02 --- /dev/null +++ b/Source/IO/FS/FSPlatform.hpp @@ -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 + +namespace Aurora::IO::FS +{ + struct UpdateTimes; + + struct PlatformFS : IPlatformFS + { + AuOptional Win32OpenHook(const void *lpFileName, + AuUInt32 dwDesiredAccess, + AuUInt32 dwShareMode, + bool bInherit, + AuUInt32 dwCreationDisposition, + AuUInt32 dwFlags, + AuUInt32 dwAttributes) override; + + AuOptional 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 optbDirectIO, + AuOptional optLock) override; + + void NukeAsyncProvider(IAsyncFileStream *) override; + + + AuSPtr ReadDir(const AuROString &string) override; + + bool FilesInDirectory(const AuROString &string, + AuList &files) override; + + bool DirsInDirectory(const AuROString &string, + AuList &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 ×) override; + + AuList FileAttrsList(const AuROString &path) override; + + AuResult 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; +} diff --git a/Source/IO/FS/FSTimes.NT.cpp b/Source/IO/FS/FSTimes.NT.cpp index 3129c88a..500ffb06 100644 --- a/Source/IO/FS/FSTimes.NT.cpp +++ b/Source/IO/FS/FSTimes.NT.cpp @@ -25,7 +25,7 @@ namespace Aurora::IO::FS return temp; } - AUKN_SYM bool UpdateFileTimes(const AuROString &path, const UpdateTimes ×) + bool PlatformFS::UpdateFileTimes(const AuROString &path, const UpdateTimes ×) { HANDLE hFile; FILETIME created; diff --git a/Source/IO/FS/FSTimes.Unix.cpp b/Source/IO/FS/FSTimes.Unix.cpp index be255e7d..a24df8f0 100644 --- a/Source/IO/FS/FSTimes.Unix.cpp +++ b/Source/IO/FS/FSTimes.Unix.cpp @@ -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 ×) + bool PlatformFS::UpdateFileTimes(const AuROString &path, const UpdateTimes ×) { if (times.createdNs) { diff --git a/Source/IO/FS/FileAttrs.NT.cpp b/Source/IO/FS/FileAttrs.NT.cpp index 38924eef..906586f3 100644 --- a/Source/IO/FS/FileAttrs.NT.cpp +++ b/Source/IO/FS/FileAttrs.NT.cpp @@ -11,7 +11,7 @@ namespace Aurora::IO::FS { - AUKN_SYM AuList FileAttrsList(const AuROString &path) + AuList PlatformFS::FileAttrsList(const AuROString &path) { AuList names; WIN32_FIND_STREAM_DATA data; @@ -108,7 +108,7 @@ namespace Aurora::IO::FS return names; } - AUKN_SYM AuResult FileAttrsGet(const AuROString &path, const AuROString &attr) + AuResult 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 { diff --git a/Source/IO/FS/FileAttrs.Unix.cpp b/Source/IO/FS/FileAttrs.Unix.cpp index c1aa079b..fae666e7 100644 --- a/Source/IO/FS/FileAttrs.Unix.cpp +++ b/Source/IO/FS/FileAttrs.Unix.cpp @@ -13,7 +13,7 @@ namespace Aurora::IO::FS { - AUKN_SYM AuList FileAttrsList(const AuROString &path) + AuList 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 FileAttrsGet(const AuROString &path, const AuROString &attr) + AuResult 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); diff --git a/Source/IO/FS/FileStream.NT.cpp b/Source/IO/FS/FileStream.NT.cpp index 284b25ba..9ffc93af 100644 --- a/Source/IO/FS/FileStream.NT.cpp +++ b/Source/IO/FS/FileStream.NT.cpp @@ -566,10 +566,10 @@ namespace Aurora::IO::FS AuSafeDelete(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(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(that); - } - - AUKN_SYM IFileStream *OpenReadNew(const AuROString &path, EFileAdvisoryLockLevel level) - { - return OpenNew(path, EFileOpenMode::eRead, level); - } - - AUKN_SYM void OpenReadRelease(IFileStream * that) - { - AuSafeDelete(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(that); } diff --git a/Source/IO/FS/FileStream.Unix.cpp b/Source/IO/FS/FileStream.Unix.cpp index b8f272c8..ab8e9ade 100755 --- a/Source/IO/FS/FileStream.Unix.cpp +++ b/Source/IO/FS/FileStream.Unix.cpp @@ -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(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(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(that); - } - - AUKN_SYM IFileStream *OpenReadNew(const AuROString &path, EFileAdvisoryLockLevel level) - { - return OpenNewEx(path, EFileOpenMode::eRead, level, false); - } - - AUKN_SYM void OpenReadRelease(IFileStream * that) - { - AuSafeDelete(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(that); } diff --git a/Source/IO/FS/FileTrust.NT.cpp b/Source/IO/FS/FileTrust.NT.cpp index 1e6e58db..61a051fa 100644 --- a/Source/IO/FS/FileTrust.NT.cpp +++ b/Source/IO/FS/FileTrust.NT.cpp @@ -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; diff --git a/Source/IO/FS/FileTrust.Unix.cpp b/Source/IO/FS/FileTrust.Unix.cpp index 61b65a0f..83677159 100644 --- a/Source/IO/FS/FileTrust.Unix.cpp +++ b/Source/IO/FS/FileTrust.Unix.cpp @@ -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);