AuroraRuntime/Source/IO/FS/FS.Unix.cpp

668 lines
16 KiB
C++
Executable File

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FS.Unix.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "FS.hpp"
#include "FS.Generic.hpp"
#include <Source/Time/Time.hpp>
#if !defined(_AURUNTIME_GENERICFS)
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#if defined(AURORA_IS_LINUX_DERIVED)
#include <sys/sendfile.h>
#endif
namespace Aurora::IO::FS
{
bool _MkDir(const AuROString &path)
{
AuROString subdir;
if ((path.size() > 1) &&
((path[path.size() - 1] == '/') ||
(path[path.size() - 1] == '\\')))
{
subdir = AuROString(path).substr(0, path.size() - 1);
}
else
{
subdir = path;
}
GoUpToSeparator(subdir, subdir);
mode_t mode { 0775 };
struct stat s;
if (::stat(AuString(subdir).c_str(), &s) != -1)
{
mode = s.st_mode;
}
return ::mkdir(AuString(path).c_str(), mode) == 0;
}
struct ReadDirStructure : IReadDir
{
DIR *pDIR {};
struct dirent *pDE {};
bool bFirstTick { true };
bool bDead { false };
StatEx stat;
AuString sPath;
bool bFast {};
AuUInt32 uErrorCount {};
~ReadDirStructure()
{
if (this->pDIR)
{
::closedir(this->pDIR);
}
}
virtual AuUInt32 GetErrorCount() override
{
return this->uErrorCount;
}
virtual AuList<AuString> GetErrorPaths() override
{
if (this->uErrorCount)
{
return { this->sPath };
}
else
{
return {};
}
}
virtual StatEx *Next() override
{
AU_DEBUG_MEMCRUNCH;
bool bTryAgain {};
if (this->bDead)
{
return {};
}
do
{
errno = 0;
if (!(this->pDE = ::readdir(this->pDIR)))
{
this->bDead = true;
if (errno)
{
this->uErrorCount++;
}
return {};
}
if (this->pDE->d_name == AuString(".") ||
this->pDE->d_name == AuString(".."))
{
bTryAgain = true;
continue;
}
stat.bExists = true;
stat.fileName = this->pDE->d_name;
if (stat.fileName.empty())
{
bTryAgain = true;
continue;
}
AuString name = stat.fileName;
AuReplaceAll(name, "\\", "\\\\");
stat.path = this->sPath + name;
if (this->bFast)
{
// nvm wont work. still need the is type dir/file flags
// return &stat;
}
if (!StatFile(stat.path.c_str(), stat))
{
bTryAgain = true;
}
}
while (AuExchange(bTryAgain, false));
return &stat;
}
};
static AuSPtr<IReadDir> ReadDirEx(const AuROString &string, bool bFast)
{
if (string.empty())
{
SysPushErrorArg("Cannot open an IO handle to the provided empty path");
return {};
}
auto pObj = AuMakeShared<ReadDirStructure>();
if (!pObj)
{
SysPushErrorMem();
return {};
}
pObj->bFast = bFast;
if (!AuIOFS::NormalizePath(pObj->sPath, string))
{
SysPushErrorMem();
return {};
}
if (!AuTryInsert(pObj->sPath, '/'))
{
SysPushErrorMem();
return {};
}
pObj->pDIR = ::opendir(pObj->sPath.c_str());
if (!pObj->pDIR)
{
SysPushErrorIO();
return {};
}
return pObj;
}
AUKN_SYM AuSPtr<IReadDir> ReadDir(const AuROString &string)
{
return ReadDirEx(string, false);
}
AUKN_SYM bool FilesInDirectory(const AuROString &string, AuList<AuString> &files)
{
auto itr = ReadDirEx(string, true);
if (!itr)
{
return false;
}
// SECURITY(): if next fails, its indistinguishable from end of file list, and will return true. it kinda sucks
while (auto stat = itr->Next())
{
if (!stat->bExistsFile)
{
continue;
}
if (!AuTryInsert(files, stat->fileName))
{
return false;
}
}
return true;
}
AUKN_SYM bool DirsInDirectory(const AuROString &string, AuList<AuString> &dirs)
{
auto itr = ReadDirEx(string, true);
if (!itr)
{
return false;
}
// SECURITY(): if next fails, its indistinguishable from end of file list, and will return true. it kinda sucks
while (auto stat = itr->Next())
{
if (!stat->bExistsDirectory)
{
continue;
}
if (!AuTryInsert(dirs, stat->fileName))
{
return false;
}
}
return true;
}
bool SpecialsInDirectory(const AuROString &string, AuList<AuString> &dirs)
{
auto itr = ReadDirEx(string, true);
if (!itr)
{
AuLogDbg("failed to open special read dir");
return false;
}
// SECURITY(): if next fails, its indistinguishable from end of file list, and will return true. it kinda sucks
while (auto stat = itr->Next())
{
#if 0
if (!stat->bExistsSystemResource)
{
continue;
}
#endif
if (!AuTryInsert(dirs, stat->fileName))
{
return false;
}
}
return true;
}
AUKN_SYM bool ReadFile(const AuROString &path, AuByteBuffer &buffer)
{
AuMemoryViewWrite writeView;
if (path.empty())
{
SysPushErrorArg("Cannot open an IO handle to the provided empty path");
return false;
}
bool bIsStupidFD =
AuStartsWith(path, "/proc/") ||
AuStartsWith(path, "/sys/") ||
AuStartsWith(path, "/dev/");
auto pFile = OpenReadUnique(path, bIsStupidFD ? EFileAdvisoryLockLevel::eNoSafety : EFileAdvisoryLockLevel::eBlockWrite);
SysCheckReturn(pFile, false);
bool bIsZero = buffer.readPtr == buffer.base;
auto qwLength = pFile->GetLength();
// NOTE: Linux filesystems are such a cluster fuck of unimplemented interfaces and half-assed drivers
// It's not unusual for these "files" to not support the required seek operations across NIX-like oses.
if (qwLength == 0)
{
if (bIsStupidFD)
{
qwLength = 4096 * 10;
}
else
{
return true;
}
}
writeView = buffer.GetOrAllocateLinearWriteable(qwLength);
if (!writeView)
{
return {};
}
AuUInt uLength { qwLength };
if (!pFile->Read(Memory::MemoryViewStreamWrite { writeView, uLength }))
{
SysPushErrorIO();
return false;
}
// NOTE: File devices love to lie
// Do not entertain an arbitrarily large page length provided by non-regular fds
buffer.writePtr += uLength;
if (bIsZero)
{
AuTryDownsize(buffer, uLength);
}
return true;
}
static bool UnixExists(const AuString &path, bool dir)
{
struct stat s;
int err = ::stat(path.c_str(), &s);
if (-1 == err)
{
SysAssert(ENOENT == errno, "General File IO Error, path {}", path);
return false;
}
return dir ? S_ISDIR(s.st_mode) : S_ISREG(s.st_mode);
}
AUKN_SYM bool FileExists(const AuROString &path)
{
if (path.empty())
{
SysPushErrorArg("Cannot open an IO handle to the provided empty path");
return false;
}
auto pathExpanded = NormalizePathRet(path);
if (pathExpanded.empty())
{
SysPushErrorMemory();
return false;
}
return UnixExists(pathExpanded, false);
}
AUKN_SYM bool DirExists(const AuROString &path)
{
if (path.empty())
{
SysPushErrorArg("Cannot open an IO handle to the provided empty path");
return false;
}
auto pathExpanded = NormalizePathRet(path);
if (pathExpanded.empty())
{
SysPushErrorMemory();
return false;
}
return UnixExists(pathExpanded, true);
}
AUKN_SYM bool DirMk(const AuROString &path)
{
if (path.empty())
{
SysPushErrorArg("Cannot open an IO handle to the provided empty path");
return false;
}
auto pathExpanded = NormalizePathRet(path);
if (pathExpanded.empty())
{
SysPushErrorMemory();
return false;
}
return CreateDirectories(pathExpanded, false);
}
AUKN_SYM bool Remove(const AuROString &path)
{
struct stat s;
if (path.empty())
{
SysPushErrorArg("Cannot open an IO handle to the provided empty path");
return false;
}
auto pathExpanded = NormalizePathRet(path);
if (pathExpanded.empty())
{
SysPushErrorMemory();
return false;
}
Aurora::RuntimeWaitForSecondaryTick();
if (::stat(pathExpanded.c_str(), &s) == -1)
{
if (errno == ENOENT)
{
return true;
}
}
else
{
if (S_ISDIR(s.st_mode))
{
if (::rmdir(pathExpanded.c_str()) == 0)
{
return true;
}
else if (errno == ENOTDIR)
{
if (PosixUnlink(pathExpanded.c_str()) == 0)
{
return true;
}
}
}
if (S_ISREG(s.st_mode))
{
if (PosixUnlink(pathExpanded.c_str()) == 0)
{
return true;
}
}
}
if (::remove(pathExpanded.c_str()) == 0)
{
return true;
}
return false;
}
AUKN_SYM bool Relink(const AuROString &src, const AuROString &dest)
{
if (src.empty() ||
dest.empty())
{
SysPushErrorArg("Cannot open an IO handle to the provided empty path");
return false;
}
Aurora::RuntimeWaitForSecondaryTick();
auto pathSrcExpanded = NormalizePathRet(src);
auto pathExpanded = NormalizePathRet(dest);
if (pathSrcExpanded.empty() ||
pathExpanded.empty())
{
SysPushErrorMemory();
return false;
}
return ::rename(pathSrcExpanded.c_str(), pathExpanded.c_str()) != -1;
}
#if defined(AURORA_IS_LINUX_DERIVED)
AUKN_SYM bool Copy(const AuROString &src, const AuROString &dest)
{
if (src.empty() ||
dest.empty())
{
SysPushErrorArg("Cannot open an IO handle to the provided empty path");
return false;
}
auto pathSrcExpanded = NormalizePathRet(src);
auto pathExpanded = NormalizePathRet(dest);
if (pathSrcExpanded.empty() ||
pathExpanded.empty())
{
SysPushErrorMemory();
return false;
}
CreateDirectories(pathExpanded, true);
int input, output;
if ((input = ::open(pathSrcExpanded.c_str(), O_RDONLY | O_CLOEXEC)) == -1)
{
return false;
}
struct stat fileinfo = { 0 };
if (::fstat(input, &fileinfo) != 0)
{
::close(input);
SysPushErrorIO("fstat failed");
return false;
}
if ((output = ::creat(pathExpanded.c_str(), fileinfo.st_mode)) == -1)
{
::close(input);
SysPushErrorIO("creat failed");
return false;
}
off_t bytesCopied = 0;
auto result = ::sendfile(output, input, &bytesCopied, fileinfo.st_size) != -1;
::close(input);
::close(output);
return bytesCopied == fileinfo.st_size;
}
#elif defined(AURORA_IS_BSD_DERIVED)
AUKN_SYM bool Copy(const AuROString &src, const AuROString &dest)
{
if (src.empty() ||
dest.empty())
{
SysPushErrorArg("Cannot open an IO handle to the provided empty path");
return false;
}
auto pathSrcExpanded = NormalizePathRet(src);
auto pathExpanded = NormalizePathRet(dest);
if (pathSrcExpanded.empty() ||
pathExpanded.empty())
{
SysPushErrorMemory();
return false;
}
CreateDirectories(pathExpanded, true);
int input, output;
if ((input = ::open(pathSrcExpanded.c_str(), O_RDONLY | O_CLOEXEC)) == -1)
{
return false;
}
struct stat fileinfo = { 0 };
if (::fstat(input, &fileinfo) != 0)
{
close(input)
SysPushErrorIO("fstat failed");
return false;
}
if ((output = ::creat(pathExpanded.c_str(), fileinfo.st_mode)) == -1)
{
close(input);
SysPushErrorIO("creat failed");
return false;
}
auto result = ::fcopyfile(input, output, 0, COPYFILE_ALL) == 0;
::close(input);
::close(output);
return result;
}
#else
AUKN_SYM bool Copy(const AuROString &src, const AuROString &dest)
{
// TODO: not that i care
return false;
}
#endif
AUKN_SYM bool StatFile(const AuROString &pathRel, Stat &stat)
{
stat = {};
if (pathRel.empty())
{
SysPushErrorArg("Cannot open an IO handle to the provided empty path");
return false;
}
auto path = NormalizePathRet(pathRel);
if (path.empty())
{
SysPushErrorMemory();
return false;
}
struct stat s;
auto err = ::stat(path.c_str(), &s);
if (err == -1)
{
if (ENOENT != errno)
{
SysPushErrorIO("Critical IO error while stating file (errno: {}, path: {})", errno, path);
}
return false;
}
stat.bExistsFile = S_ISREG(s.st_mode);
stat.bExistsDirectory = S_ISDIR(s.st_mode);
stat.bExistsSystemResource = S_ISSOCK(s.st_mode) || S_ISBLK(s.st_mode) || S_ISCHR(s.st_mode);
stat.bExists = stat.bExistsFile || stat.bExistsDirectory || stat.bExistsSystemResource;
#if 0
if (!stat.bExists)
{
SysPushErrorIO("Missing attribute type in stat mode {} (of path {})", s.st_mode, path);
return false;
}
#else
stat.bExists = true;
#endif
stat.uSize = s.st_size;
#if defined(AURORA_IS_LINUX_DERIVED)
stat.createdNs = AuUInt64(s.st_ctim.tv_nsec) + AuMSToNS<AuUInt64>(Time::CTimeToMS(s.st_ctim.tv_sec));
stat.modifiedNs = AuUInt64(s.st_mtim.tv_nsec) + AuMSToNS<AuUInt64>(Time::CTimeToMS(s.st_mtim.tv_sec));
stat.accessedNs = AuUInt64(s.st_atim.tv_nsec) + AuMSToNS<AuUInt64>(Time::CTimeToMS(s.st_atim.tv_sec));
#else
stat.createdNs = AuMSToNS<AuUInt64>(Time::CTimeToMS(s.st_ctime));
stat.modifiedNs = AuMSToNS<AuUInt64>(Time::CTimeToMS(s.st_mtime));
stat.accessedNs = AuMSToNS<AuUInt64>(Time::CTimeToMS(s.st_atime));
#endif
err = lstat(path.c_str(), &s);
if (err != -1)
{
stat.bSymLink = S_ISLNK(s.st_mode);
}
return true;
}
}
#endif