2021-06-27 21:25:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
File: FS.NT.cpp
|
2021-06-27 21:25:29 +00:00
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2021-06-27 21:25:29 +00:00
|
|
|
#include "FS.hpp"
|
|
|
|
#include "FS.Generic.hpp"
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/Time/Time.hpp>
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
#if !defined(_AURUNTIME_GENERICFS)
|
|
|
|
namespace Aurora::IO::FS
|
|
|
|
{
|
2021-09-13 20:11:12 +00:00
|
|
|
static const AuUInt64 kFileCopyBlock = 0xFFFF; // 64KiB
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
bool _MkDir(const AuString &str)
|
|
|
|
{
|
|
|
|
return CreateDirectoryW(Locale::ConvertFromUTF8(str).c_str(), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool FilesInDirectory(const AuString &string, AuList<AuString> &files)
|
|
|
|
{
|
|
|
|
if (IterateDirEntriesSTL(string, true, files))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
// TODO: Implement a fast path for NT. std::filesystem is unusably slow
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool DirsInDirectory(const AuString &string, AuList<AuString> &dirs)
|
|
|
|
{
|
|
|
|
if (IterateDirEntriesSTL(string, false, dirs))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
// TODO: Implement a fast path for NT. std::filesystem is unusably slow
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-20 20:31:18 +00:00
|
|
|
AUKN_SYM bool WriteFile(const AuString &path, const Memory::MemoryViewRead &blob)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
bool status;
|
|
|
|
size_t offset;
|
|
|
|
HANDLE fileHandle;
|
|
|
|
std::wstring win32Path;
|
|
|
|
AuString pathNormalized;
|
2022-02-20 20:31:18 +00:00
|
|
|
AuUInt length;
|
2021-09-06 10:58:08 +00:00
|
|
|
|
|
|
|
status = false;
|
|
|
|
pathNormalized = NormalizePathRet(path);
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2022-02-20 21:08:44 +00:00
|
|
|
if (pathNormalized.empty())
|
2022-01-24 18:37:06 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2022-02-20 21:08:44 +00:00
|
|
|
win32Path = Locale::ConvertFromUTF8(pathNormalized);
|
|
|
|
|
|
|
|
if (win32Path.empty())
|
2022-01-24 18:37:06 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateDirectories(pathNormalized, true);
|
|
|
|
|
[*/+/-] MEGA COMMIT. ~2 weeks compressed.
The intention is to quickly improve and add util apis, enhance functionality given current demands, go back to the build pipeline, finish that, publish runtime tests, and then use what we have to go back to to linux support with a more stable api.
[+] AuMakeSharedArray
[+] Technet ArgvQuote
[+] Grug subsystem (UNIX signal thread async safe ipc + telemetry flusher + log flusher.)
[+] auEndianness -> Endian swap utils
[+] AuGet<N>(...)
[*] AUE_DEFINE conversion for
ECompresionType, EAnsiColor, EHashType, EStreamError, EHexDump
[+] ConsoleMessage ByteBuffer serialization
[+] CmdLine subsystem for parsing command line arguments and simple switch/flag checks
[*] Split logger from console subsystem
[+] StartupParameters -> A part of a clean up effort under Process
[*] Refactor SysErrors header + get caller hack
[+] Atomic APIs
[+] popcnt
[+] Ring Buffer sink
[+] Added more standard errors
Catch,
Submission,
LockError,
NoAccess,
ResourceMissing,
ResourceLocked,
MalformedData,
InSandboxContext,
ParseError
[+] Added ErrorCategorySet, ErrorCategoryClear, GetStackTrace
[+] IExitSubscriber, ETriggerLevel
[*] Write bias the high performance RWLockImpl read-lock operation operation
[+] ExitHandlerAdd/ExitHandlerRemove (exit subsystem)
[*] Updated API style
Digests
[+] CpuId::CpuBitCount
[+] GetUserProgramsFolder
[+] GetPackagePath
[*] Split IStreamReader with an inl file
[*] BlobWriter/BlobReader/BlobArbitraryReader can now take shared pointers to bytebuffers. default constructor allocates a new scalable bytebuffer
[+] ICharacterProvider
[+] ICharacterProviderEx
[+] IBufferedCharacterConsumer
[+] ProviderFromSharedString
[+] ProviderFromString
[+] BufferConsumerFromProvider
[*] Parse Subsystem uses character io bufferer
[*] Rewritten NT's high perf semaphore to use userland SRW/ConVars [like mutex, based on generic semaphore]
[+] ByteBuffer::ResetReadPointer
[*] Bug fix bytebuffer base not reset on free and some scaling issues
[+] ProcessMap -> Added kSectionNameStack, kSectionNameFile, kSectionNameHeap for Section
[*] ProcessMap -> Refactor Segment to Section. I was stupid for keeping a type conflict hack API facing
[+] Added 64 *byte* fast RNG seeds
[+] File Advisorys/File Lock Awareness
[+] Added extended IAuroraThread from OS identifier caches for debug purposes
[*] Tweaked how memory is reported on Windows. Better consistency of what values mean across functions.
[*] Broke AuroraUtils/Typedefs out into a separate library
[*] Update build script
[+] Put some more effort into adding detail to the readme before rewriting it, plus, added some media
[*] Improved public API documentation
[*] Bug fix `SetConsoleCtrlHandler`
[+] Locale TimeDateToFileNameISO8601
[+] Console config stdOutShortTime
[*] Begin using internal UTF8/16 decoders when platform support isnt available (instead of stl)
[*] Bug fixes in decoders
[*] Major bug fix, AuMax
[+] RateLimiter
[+] Binary file sink
[+] Log directory sink
[*] Data header usability (more operators)
[+] AuRemoveRange
[+] AuRemove
[+] AuTryRemove
[+] AuTryRemoveRange
[+] auCastUtils
[+] Finish NewLSWin32Source
[+] AuTryFindByTupleN, AuTryRemoveByTupleN
[+] Separated AuRead/Write types, now in auTypeUtils
[+] Added GetPosition/SetPosition to FileWriter
[*] Fix stupid AuMin in place of AuMax in SpawnThread.Unix.Cpp
[*] Refactored Arbitrary readers to SeekingReaders (as in, they could be atomic and/or parallelized, and accept an arbitrary position as a work parameter -> not Seekable, as in, you can simply set the position)
[*] Hack back in the sched deinit
[+] File AIO loop source interop
[+] Begin to prototype a LoopQueue object I had in mind for NT, untested btw
[+] Stub code for networking
[+] Compression BaseStream/IngestableStreamBase
[*] Major: read/write locks now support write-entrant read routines.
[*] Compression subsystem now uses the MemoryView concept
[*] Rewrite the base stream compressions, made them less broken
[*] Update hashing api
[*] WriterTryGoForward and ReaderTryGoForward now revert to the previous relative index instead of panicing
[+] Added new AuByteBuffer apis
Trim, Pad, WriteFrom, WriteString, [TODO: ReadString]
[+] Added ByteBufferPushReadState
[+] Added ByteBufferPushWriteState
[*] Move from USC-16 to full UTF-16. Win32 can handle full UTF-16.
[*] ELogLevel is now an Aurora enum
[+] Raised arbitrary limit in header to 255, the max filter buffer
[+] Explicit GZip support
[+] Explicit Zip support
[+] Added [some] compressors
et al
2022-02-17 00:11:40 +00:00
|
|
|
fileHandle = CreateFileW(win32Path.c_str(), GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
2021-06-30 16:34:49 +00:00
|
|
|
if (fileHandle == INVALID_HANDLE_VALUE)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO("Couldn't open handle: {}", path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-20 20:31:18 +00:00
|
|
|
length = blob.length;
|
2021-09-06 10:58:08 +00:00
|
|
|
offset = 0;
|
2021-06-27 21:25:29 +00:00
|
|
|
while (length)
|
|
|
|
{
|
|
|
|
DWORD written;
|
|
|
|
|
2022-01-19 17:08:13 +00:00
|
|
|
int blockSize = AuMin(static_cast<AuUInt64>(kFileCopyBlock), static_cast<AuUInt64>(length));
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2022-02-20 20:31:18 +00:00
|
|
|
if (!::WriteFile(fileHandle, &reinterpret_cast<const char *>(blob.ptr)[offset], blockSize, &written, NULL))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (written != blockSize)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += written;
|
|
|
|
length -= written;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = true;
|
|
|
|
|
|
|
|
out:
|
2021-09-13 20:11:12 +00:00
|
|
|
AuWin32CloseHandle(fileHandle);
|
2021-06-27 21:25:29 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:45:52 +00:00
|
|
|
AUKN_SYM bool ReadFile(const AuString &path, AuByteBuffer &buffer)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
std::wstring win32Path;
|
|
|
|
LARGE_INTEGER length;
|
|
|
|
bool status;
|
|
|
|
size_t offset;
|
|
|
|
|
|
|
|
status = false;
|
|
|
|
offset = 0;
|
|
|
|
win32Path = Locale::ConvertFromUTF8(NormalizePathRet(path));
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2022-02-20 21:08:44 +00:00
|
|
|
if (win32Path.empty())
|
2022-01-24 18:37:06 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
auto fileHandle = CreateFileW(win32Path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2021-06-30 16:34:49 +00:00
|
|
|
if (fileHandle == INVALID_HANDLE_VALUE)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO("Couldn't open handle: {}", path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GetFileSizeEx(fileHandle, &length))
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
if (!AuTryResize(buffer, length.QuadPart))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (length.QuadPart)
|
|
|
|
{
|
|
|
|
DWORD read;
|
|
|
|
|
2022-01-19 17:08:13 +00:00
|
|
|
int blockSize = AuMin(static_cast<AuUInt64>(kFileCopyBlock), static_cast<AuUInt64>(length.QuadPart));
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
if (!::ReadFile(fileHandle, &buffer[offset], blockSize, &read, NULL))
|
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
AuLogWarn("ReadFile IO Error: 0x{:x} {}", GetLastError(), path);
|
2021-06-27 21:25:29 +00:00
|
|
|
SysPushErrorIO();
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += read;
|
|
|
|
length.QuadPart -= read;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = true;
|
|
|
|
|
|
|
|
out:
|
2021-09-13 20:11:12 +00:00
|
|
|
AuWin32CloseHandle(fileHandle);
|
2021-06-27 21:25:29 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool FileExists(const AuString &path)
|
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
DWORD dwAttrib = GetFileAttributesW(Locale::ConvertFromUTF8(NormalizePathRet(path)).c_str());
|
|
|
|
return ((dwAttrib != INVALID_FILE_ATTRIBUTES) &&
|
|
|
|
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool DirExists(const AuString &path)
|
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
DWORD dwAttrib = GetFileAttributesW(Locale::ConvertFromUTF8(NormalizePathRet(path)).c_str());
|
|
|
|
return ((dwAttrib != INVALID_FILE_ATTRIBUTES) &&
|
|
|
|
(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool DirMk(const AuString &path)
|
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return CreateDirectories(NormalizePathRet(path), false);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool Remove(const AuString &path)
|
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
try
|
|
|
|
{
|
2022-02-20 21:52:39 +00:00
|
|
|
auto translatedPath = Locale::ConvertFromUTF8(NormalizePathRet(path));
|
|
|
|
if (!DeleteFileW(translatedPath.c_str()))
|
|
|
|
{
|
|
|
|
return RemoveDirectoryW(translatedPath.c_str());
|
|
|
|
}
|
2022-02-27 07:45:35 +00:00
|
|
|
return false;
|
2022-01-24 18:37:06 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool Relink(const AuString &src, const AuString &dest)
|
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return MoveFileW(Locale::ConvertFromUTF8(NormalizePathRet(src)).c_str(), Locale::ConvertFromUTF8(NormalizePathRet(dest)).c_str());
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool Copy(const AuString &src, const AuString &dest)
|
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return CopyFileW(Locale::ConvertFromUTF8(NormalizePathRet(src)).c_str(), Locale::ConvertFromUTF8(NormalizePathRet(dest)).c_str(), true);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool StatFile(const AuString &path, Stat &stat)
|
|
|
|
{
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA data;
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
stat = {};
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
if (!GetFileAttributesExW(Locale::ConvertFromUTF8(NormalizePathRet(path)).c_str(), GetFileExInfoStandard, &data))
|
|
|
|
{
|
|
|
|
stat.exists = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
stat.exists = true;
|
|
|
|
stat.existsDirectory = data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
stat.existsFile = !stat.existsDirectory;
|
|
|
|
stat.symLink = data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT;
|
|
|
|
|
|
|
|
stat.size = AuUInt64(data.nFileSizeLow) | (AuUInt64(data.nFileSizeHigh) << 32);
|
|
|
|
|
|
|
|
stat.created = Time::ConvertTimestamp(data.ftCreationTime);
|
|
|
|
stat.modified = Time::ConvertTimestamp(data.ftLastWriteTime);
|
|
|
|
stat.accessed = Time::ConvertTimestamp(data.ftLastAccessTime);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|