AuroraRuntime/Source/IO/FS/FS.cpp

740 lines
21 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FS.cpp
Date: 2021-6-16
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"
2021-09-30 14:57:41 +00:00
#include "Source/Locale/Locale.hpp"
2021-09-06 10:58:08 +00:00
2021-06-27 21:25:29 +00:00
namespace Aurora::IO::FS
{
static bool IsMagicCharacter(char c)
{
2022-08-02 04:58:00 +00:00
return (c == '.') ||
(c == '!') ||
(c == '~') ||
(c == '?') ||
(c == '^');
2021-06-27 21:25:29 +00:00
}
2021-07-13 13:07:40 +00:00
static void ResolveAbsolutePath(bool requireMountExpand, bool requireSanitization, const AuString &path, AuString &result)
2021-06-27 21:25:29 +00:00
{
AuString buffer;
bool isResource {};
buffer.reserve(4096);
result.reserve(4096);
/**
Resolve special character prefixes
*/
if (requireMountExpand)
2021-06-27 21:25:29 +00:00
{
int iOffset { 0 };
/**
* Strip protocol prefix
*/
if (AuStartsWith(path, "file://"))
{
iOffset = 7;
}
if ((path.size() > iOffset + 1) &&
(path[iOffset + 1] == kPathSplitter))
2021-06-27 21:25:29 +00:00
{
// Working directory
if (path[iOffset] == '.')
2021-06-27 21:25:29 +00:00
{
if (!Process::GetWorkingDirectory(buffer))
{
result.clear();
return;
}
2021-06-27 21:25:29 +00:00
}
// Binary directory
else if (path[iOffset] == '^')
{
2023-12-13 18:11:35 +00:00
if (auto pProcPath = Process::GetProcessDirectory())
{
buffer = *pProcPath;
}
else
{
result.clear();
return;
}
}
2021-06-27 21:25:29 +00:00
// Local Aurora profile
else if (path[iOffset] == '~')
2021-06-27 21:25:29 +00:00
{
if (!FS::GetProfileDomain(buffer))
{
result.clear();
return;
}
2021-06-27 21:25:29 +00:00
}
// Global Aurora profile
else if (path[iOffset] == '!')
2021-06-27 21:25:29 +00:00
{
if (!FS::GetSystemDomain(buffer))
{
result.clear();
return;
}
2021-06-27 21:25:29 +00:00
}
else if (path[iOffset] == '?')
2021-06-27 21:25:29 +00:00
{
// ...except for this one
isResource = true;
}
else
{
buffer.insert(buffer.begin(), path.begin() + iOffset, path.begin() + iOffset + 2); // ???
2021-06-27 21:25:29 +00:00
}
buffer.insert(buffer.end(), path.begin() + iOffset + 2, path.end());
}
else if (iOffset)
{
buffer.insert(buffer.end(), path.begin() + iOffset, path.end());
2021-06-27 21:25:29 +00:00
}
else
{
buffer += path;
}
}
else
{
buffer += path;
}
2021-07-13 13:07:40 +00:00
if (requireSanitization)
2021-06-27 21:25:29 +00:00
{
2021-07-13 13:07:40 +00:00
/**
Quickly handle the edge case in some modern UNIX derived systems, and in
Windows UNC paths, where paths may start with '//' or '\\' respectively
*/
2021-09-06 10:58:08 +00:00
if (AuStartsWith(buffer, kDoublePathSplitter))
2021-07-13 13:07:40 +00:00
{
result += kDoublePathSplitter;
}
else if (buffer.size() && buffer[0] == kPathSplitter)
{
result += kPathSplitter;
}
2021-06-27 21:25:29 +00:00
2021-07-13 13:07:40 +00:00
/**
Technically, UTF-8 strings may contain the "NUL" byte.
2021-06-27 21:25:29 +00:00
2021-07-13 13:07:40 +00:00
o Character numbers from U+0000 to U+007F (US-ASCII repertoire)
correspond to octets 00 to 7F (7 bit US-ASCII paths). A direct
consequence is that a plain ASCII string is also a valid UTF-8
string.
2021-06-27 21:25:29 +00:00
2021-07-13 13:07:40 +00:00
You know what would suck? If we were targeting an esoteric platform,
say a kernel, and we we're trying to avoid string exploits by potential
attackers. I'm sure that would never happen to poor anticheat devs.
*/
2021-09-06 10:58:08 +00:00
const auto parts = AuSplitString(buffer, AuString(1, kPathSplitter), true /*ignore empty tokens*/); /// zzzz im going to SLEEP
2021-07-13 13:07:40 +00:00
for (const auto &ch : parts) // can you tell why FIO shouldn't be in hot paths yet?
2021-06-27 21:25:29 +00:00
{
2021-07-13 13:07:40 +00:00
if (ch == "..")
2021-06-27 21:25:29 +00:00
{
2023-03-05 09:16:09 +00:00
if (!result.size()) continue;
auto i = result.size() - 1;
if (i != 0)
{
i -= (result[result.size() - 1] == kPathSplitter);
}
2022-12-21 14:27:26 +00:00
while (i > 0 && result[i] != kPathSplitter)
2021-07-13 13:07:40 +00:00
{
--i;
}
2021-06-27 21:25:29 +00:00
2021-09-06 10:58:08 +00:00
if (i >= 0)
{
2021-07-13 13:07:40 +00:00
result.resize(i);
result += kPathSplitter;
2021-07-13 13:07:40 +00:00
}
2021-06-27 21:25:29 +00:00
2021-07-13 13:07:40 +00:00
continue;
}
2021-06-27 21:25:29 +00:00
2021-07-13 13:07:40 +00:00
if ((ch.size() == 1) && (IsMagicCharacter(ch[0])))
{
continue;
}
result += ch;
result += kPathSplitter;
2021-06-27 21:25:29 +00:00
}
2021-07-13 13:07:40 +00:00
auto i = result.size() - 1;
if (result[i] == kPathSplitter)
{
result.resize(i);
}
2021-06-27 21:25:29 +00:00
}
2021-07-13 13:07:40 +00:00
else // !requireSanitization
2021-06-27 21:25:29 +00:00
{
2021-07-13 13:07:40 +00:00
result = buffer;
2021-06-27 21:25:29 +00:00
}
if (isResource)
{
AuString path;
if (!FS::GetSystemResourcePath(result, path))
{
result.clear();
}
else
{
result = path;
}
}
#if defined(AURORA_IS_MODERNNT_DERIVED)
if (result.size() >= MAX_PATH)
{
if (!AuStartsWith(result, "\\\\") &&
result[1] == ':')
{
result = "\\\\?\\" + result;
}
}
#endif
2021-06-27 21:25:29 +00:00
}
void /* internal, local export */ _NormalizePath(AuString &str)
{
bool requiresExpanding = false;
2021-07-13 13:07:40 +00:00
bool requiresMountUpdate = false;
2021-06-27 21:25:29 +00:00
requiresExpanding = str.size() && IsMagicCharacter(str[0]);
if (str.size() == 1)
{
if (str[0] == '.'
// Win32 has no concept of a rootfs
// However, all users expect paths in userspace programs to assume CWD
// Since we are not on a NIX operating system, we can assume these mean CWD
#if defined(AURORA_IS_MODERNNT_DERIVED)
||
str[0] == '/' ||
str[1] == '\\'
#endif
)
{
AuProcess::GetWorkingDirectory(str);
return;
}
if (str[0] == '^')
{
2023-12-13 18:11:35 +00:00
if (auto pProcPath = Process::GetProcessDirectory())
{
str = *pProcPath;
}
return;
}
if (str[0] == '~')
{
AuFS::GetProfileDomain(str);
return;
}
if (str[0] == '!')
{
AuFS::GetSystemDomain(str);
return;
}
}
2021-06-27 21:25:29 +00:00
// best case -> O(n) wherein we merely check each BYTE with a handful of branch conditions
int doubleDots = 0;
int doubleSlash = 0;
for (auto &character : str)
{
if ((character == '\\') || (character == '/'))
{
character = kPathSplitter;
doubleSlash++;
}
else
{
doubleSlash = 0;
}
if (character == '.')
{
doubleDots++;
}
else
{
doubleDots = 0;
}
requiresExpanding |= doubleDots >= 2;
requiresExpanding |= doubleSlash >= 2;
}
// plus this, i guess
if (str.size() >= 2)
2021-06-27 21:25:29 +00:00
{
if ((str[1] == '\\') || (str[1] == '/'))
2021-06-27 21:25:29 +00:00
{
auto c = str[0];
if ((c == '.') || (c == '~') || (c == '!') || (c == '?' || (c == '^')))
2021-06-27 21:25:29 +00:00
{
2021-07-13 13:07:40 +00:00
requiresMountUpdate = true;
2021-06-27 21:25:29 +00:00
}
}
}
if (!requiresMountUpdate)
{
requiresMountUpdate = AuStartsWith(str, "file://");
}
2021-06-27 21:25:29 +00:00
// worst case -> yea have fun
2021-07-13 13:07:40 +00:00
if (requiresExpanding || requiresMountUpdate)
2021-06-27 21:25:29 +00:00
{
AuString temp;
2021-07-13 13:07:40 +00:00
ResolveAbsolutePath(requiresMountUpdate, requiresExpanding, str, temp);
2021-06-27 21:25:29 +00:00
str = temp;
}
}
2021-09-06 10:58:08 +00:00
AUKN_SYM bool ReadString(const AuString &path, AuString &buffer)
{
AuByteBuffer fileBuffer;
2021-09-06 10:58:08 +00:00
if (!ReadFile(path, fileBuffer))
{
return false;
}
return Locale::Encoding::DecodeUTF8(fileBuffer.data(), fileBuffer.size(), buffer, Locale::ECodePage::eUTF8).first != 0;
}
2021-09-06 10:58:08 +00:00
AUKN_SYM bool WriteString(const AuString &path, const AuString &str)
{
char bom[3]
{
'\xEF', '\xBB', '\xBF'
};
auto pStream = FS::OpenWriteUnique(path);
if (!pStream)
2021-09-06 10:58:08 +00:00
{
return false;
}
2021-09-06 10:58:08 +00:00
bool ok {};
ok = pStream->Write(AuMemoryViewStreamRead { bom });
ok &= pStream->Write(AuMemoryViewStreamRead { str });
pStream->Flush();
pStream->WriteEoS();
2021-09-06 10:58:08 +00:00
return ok;
}
AUKN_SYM bool WriteNewString(const AuString &path, const AuString &str)
{
2023-08-11 05:21:42 +00:00
AuIO::IOHandle handle;
bool bOk {};
AuUInt uLength {};
static const char bom[3]
{
'\xEF', '\xBB', '\xBF'
};
2023-08-11 05:21:42 +00:00
auto createRequest = AuIO::IIOHandle::HandleCreate::ReadWrite(path);
createRequest.bAlwaysCreateDirTree = true;
createRequest.bFailIfNonEmptyFile = true;
if (!handle->InitFromPath(createRequest))
{
return false;
}
auto pStream = FS::OpenBlockingFileStreamFromHandle(AuUnsafeRaiiToShared(handle.AsPointer()));
if (!pStream)
{
return false;
}
2023-08-11 05:21:42 +00:00
bOk = pStream->Write(AuMemoryViewStreamRead { bom });
bOk &= pStream->Write(AuMemoryViewStreamRead { str, uLength });
bOk &= uLength == str.length();
pStream->WriteEoS();
pStream->Flush();
return bOk;
}
AUKN_SYM bool WriteNewFile(const AuString &path, const Memory::MemoryViewRead &blob)
{
bool bOk {};
AuUInt uLength {};
AuIO::IOHandle handle;
auto createRequest = AuIO::IIOHandle::HandleCreate::ReadWrite(path);
createRequest.bAlwaysCreateDirTree = true;
createRequest.bFailIfNonEmptyFile = true;
if (!handle->InitFromPath(createRequest))
{
2023-08-11 05:21:42 +00:00
return false;
}
2023-08-11 05:21:42 +00:00
auto pStream = FS::OpenBlockingFileStreamFromHandle(AuUnsafeRaiiToShared(handle.AsPointer()));
if (!pStream)
{
return false;
}
2023-09-18 05:09:29 +00:00
bOk = pStream->Write(AuMemoryViewStreamRead { blob, uLength });
2023-08-11 05:21:42 +00:00
pStream->WriteEoS();
pStream->Flush();
2023-08-11 05:21:42 +00:00
bOk &= uLength == blob.length;
return bOk;
}
AUKN_SYM bool WriteFile(const AuString &path, const Memory::MemoryViewRead &blob)
{
bool bOk {};
AuUInt uLength {};
2023-08-11 05:21:42 +00:00
AuIO::IOHandle handle;
auto createRequest = AuIO::IIOHandle::HandleCreate::ReadWrite(path);
createRequest.bAlwaysCreateDirTree = true;
createRequest.bFailIfNonEmptyFile = false;
if (!handle->InitFromPath(createRequest))
{
return false;
}
auto pStream = FS::OpenBlockingFileStreamFromHandle(AuUnsafeRaiiToShared(handle.AsPointer()));
if (!pStream)
{
return false;
}
2023-09-18 05:09:29 +00:00
bOk = pStream->Write(AuMemoryViewStreamRead { blob, uLength });
pStream->WriteEoS();
2023-08-11 05:21:42 +00:00
pStream->Flush();
bOk &= uLength == blob.length;
2023-08-11 05:21:42 +00:00
return bOk;
}
2023-10-13 17:04:00 +00:00
AUKN_SYM void CopyDirectory(const AuList<AuPair<AuString, AuString>> &pendingWork,
bool bUseResult,
CopyDirResult *out)
{
AU_DEBUG_MEMCRUNCH;
SysCheckArgNotNull(out, );
AuList<AuPair<AuString, AuString>> copyWork = pendingWork;
while (copyWork.size())
{
auto now = AuExchange(copyWork, {});
for (const auto &[rawPath, rawPathDest] : now)
{
AuList<AuString> files;
if (AuFS::FilesInDirectory(rawPath, files))
{
for (const auto &file : files)
{
if (!AuFS::Copy(rawPath + AuString({ AuFS::kPathSplitter }) + file, rawPathDest + AuString({ AuFS::kPathSplitter }) + file))
{
if (!bUseResult)
{
SysPushErrorIO("failed to copy: {}", file);
}
else
{
out->errorCopyPaths.push_back(rawPath + AuString({ AuFS::kPathSplitter }) + file);
}
}
else if (bUseResult)
{
out->copyPathsSuccess.push_back(rawPathDest + AuString({ AuFS::kPathSplitter }) + file);
}
}
}
AuList<AuString> dirs;
if (AuFS::DirsInDirectory(rawPath, dirs))
{
for (const auto &dir : dirs)
{
copyWork.push_back(AuMakePair(rawPath + AuString({ AuFS::kPathSplitter }) + dir, rawPathDest + AuString({ AuFS::kPathSplitter }) + dir));
}
}
else
{
if (bUseResult)
{
2023-10-13 17:11:22 +00:00
out->errorTraversePaths.push_back(rawPath);
}
}
}
}
}
AUKN_SYM void MoveDirectory(const AuList<AuPair<AuString, AuString>> &pendingWork,
bool bUseResult,
CopyDirResult *out)
{
AU_DEBUG_MEMCRUNCH;
SysCheckArgNotNull(out, );
AuList<AuPair<AuString, AuString>> copyWork = pendingWork;
AuList<AuString> dirsAll;
2023-10-13 17:11:22 +00:00
while (copyWork.size())
{
auto now = AuExchange(copyWork, {});
for (const auto &[rawPath, rawPathDest] : now)
{
2023-12-01 06:55:16 +00:00
auto rawPathA = NormalizePathRet(rawPath);
if (rawPathA.empty())
{
out->errorCopyPaths.push_back(rawPath);
continue;
}
2023-12-01 06:14:03 +00:00
#if defined(AURORA_PLATFORM_WIN32)
{
2023-12-01 06:55:16 +00:00
auto winLinkA = AuLocale::ConvertFromUTF8(rawPathA);
auto winLinkB = AuLocale::ConvertFromUTF8(NormalizePathRet(rawPathDest));
if (winLinkA.empty() ||
winLinkB.empty())
2023-12-01 06:14:03 +00:00
{
out->errorCopyPaths.push_back(rawPath);
continue;
}
2023-12-01 06:55:16 +00:00
if (::MoveFileExW(winLinkA.c_str(), winLinkB.c_str(), MOVEFILE_WRITE_THROUGH))
{
out->copyPathsSuccess.push_back(rawPath);
continue;
}
2023-12-01 06:14:03 +00:00
}
#endif
2023-10-13 17:11:22 +00:00
AuList<AuString> files;
if (AuFS::FilesInDirectory(rawPath, files))
{
for (const auto &file : files)
{
if (!AuFS::Relink(rawPath + AuString({ AuFS::kPathSplitter }) + file, rawPathDest + AuString({ AuFS::kPathSplitter }) + file))
{
if (!bUseResult)
{
SysPushErrorIO("failed to move: {}", file);
}
else
{
out->errorCopyPaths.push_back(rawPath + AuString({ AuFS::kPathSplitter }) + file);
}
}
else if (bUseResult)
{
out->copyPathsSuccess.push_back(rawPathDest + AuString({ AuFS::kPathSplitter }) + file);
}
}
}
AuList<AuString> dirs;
if (AuFS::DirsInDirectory(rawPath, dirs))
{
for (const auto &dir : dirs)
{
auto fullDirPath = rawPath + AuString({ AuFS::kPathSplitter }) + dir;
copyWork.push_back(AuMakePair(fullDirPath, rawPathDest + AuString({ AuFS::kPathSplitter }) + dir));
dirsAll.push_back(fullDirPath);
2023-10-13 17:11:22 +00:00
}
}
else
{
if (bUseResult)
{
2023-10-13 17:04:00 +00:00
out->errorTraversePaths.push_back(rawPath);
}
}
}
}
bool bSuccess {};
do
{
bSuccess = false;
for (auto itr = dirsAll.begin();
itr != dirsAll.end();
)
{
if (AuFS::Remove(*itr))
{
bSuccess |= true;
itr = dirsAll.erase(itr);
}
else
{
itr++;
}
}
}
while (bSuccess &&
dirsAll.size());
for (const auto &dir : dirsAll)
{
if (AuFS::DirExists(dir))
{
out->errorTraversePaths.push_back(dir);
}
}
2023-10-13 17:04:00 +00:00
}
AUKN_SYM bool NormalizePath(AuString &out, const AuString &in)
{
try
{
out = NormalizePathRet(in);
return out.size();
}
catch (...)
{
return false;
}
}
static AuUInt GetLastSplitterIndex(const AuString &path)
{
AuUInt indexA {}, indexB {};
auto a = path.find_last_of('\\');
if (a != AuString::npos) indexA = a;
auto b = path.find_last_of('/');
if (b != AuString::npos) indexB = b;
2022-03-10 16:02:20 +00:00
return AuMax(indexA, indexB);
}
AUKN_SYM bool GetFileFromPath(AuString &out, const AuString &path)
{
try
{
if (path.empty()) return false;
if (path[path.size() - 1] == '.') return false;
AuUInt max = GetLastSplitterIndex(path);
if (max == path.size()) return false;
out = path.substr(max + 1);
return true;
}
catch (...)
{
return false;
}
}
AUKN_SYM bool GetDirectoryFromPath(AuString &out, const AuString &path)
{
try
{
if (path.empty()) return false;
if (path[path.size() - 1] == '.')
{
if (path.size() > 2 && (path[path.size() - 2] == '\\' || path[path.size() - 2] == '/'))
{
out = path.substr(0, path.size() - 1);
}
else
{
return false;
}
}
AuUInt max = GetLastSplitterIndex(path);
if (!max)
{
return false;
}
if (max == path.size())
{
out = path;
return true;
}
out = path.substr(0, max + 1);
return true;
}
catch (...)
{
out.clear();
return false;
}
}
AUKN_SYM bool GoUpToSeparator(AuString &out, const AuString &path)
{
try
{
if (path.empty()) return false;
if (path[path.size() - 1] == '.')
{
if (path.size() > 2 && (path[path.size() - 2] == '\\' || path[path.size() - 2] == '/'))
{
out = path.substr(0, path.size() - 2);
}
else
{
return false;
}
}
AuUInt max = GetLastSplitterIndex(path);
if (!max)
{
return false;
}
if (max == path.size())
{
out = path.substr(0, max - 1);
return true;
}
out = path.substr(0, max);
return true;
}
catch (...)
{
out.clear();
return false;
}
}
2021-06-27 21:25:29 +00:00
}