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

476 lines
12 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.Unix.cpp
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)
#include <sys/types.h>
Further Linux support [+] Begin work on IO futexes for io release on process/thread exit [+] Linux ::readdir iteration [+] AuConsole buffering API [*] Fix sleep as to not get interrupted by signals [*] Switch the type of FS lock used under Linux [*] Linux: Use new IPCHandle encoding scheme [*] Fix undefined behaviour: unintialized timeout values (AuLoop/Linux) [*] Fix undefined behaviour: ConsoleTTY clear line was called of a color of a random value on stack [-] Remainings of std dir iterator [*] Fix pthread_kill (aka send signal to pthread handle) always kills process. This is what you expect bc signal handler inheritance. [*] Reformat the build Aurora.json file [+] Added clang warning ignores to the build file [*] Fix: UNIX need to use STDOUT_FILENO. Was using CRT handle in place of fd by mistake. [+] Linux implementation for IO yield (AuIO::IOYield() - UNIX::LinuxOverlappedYield()) [*] Fix: Linux async end of stream processing. res 0 = zero bytes consumed. <= was detecting this as an error of code 0. Should succeed with zero bytes. [+] Linux LoopQueue missing epilogue hook for the IO processor [*] Various refactors and minor bug fixes [*] Linux fix: Handle pipe EOS as zero [*] Linux fix: thread termination via a user signal of 77. Need a force terminate. [*] IPC handle: fix improper int to bool cast in the header setup within ToString [*] Linux fix: HWInfo CPU topology regression [-] Linux fix: remove SIGABRT handler [*] Missing override in compression, exit, and consoletty headers. [+] Unix Syslog logger backend
2022-08-02 04:52:17 +00:00
#include <dirent.h>
2021-06-27 21:25:29 +00:00
#include <sys/stat.h>
2021-09-06 10:58:08 +00:00
#include <unistd.h>
#include <fcntl.h>
2021-06-27 21:25:29 +00:00
#if defined(AURORA_IS_LINUX_DERIVED)
#include <sys/sendfile.h>
#endif
2021-06-27 21:25:29 +00:00
namespace Aurora::IO::FS
{
bool _MkDir(const AuString &path)
2021-06-27 21:25:29 +00:00
{
AuString subdir;
if ((path.size() > 1) &&
((path[path.size() - 1] == '/') ||
(path[path.size() - 1] == '\\')))
{
subdir = path.substr(0, path.size() - 1);
}
else
{
subdir = path;
}
GoUpToSeparator(subdir, subdir);
mode_t mode { 0775 };
struct stat s;
if (::stat(subdir.c_str(), &s) != -1)
{
mode = s.st_mode;
}
return ::mkdir(path.c_str(), mode) == 0;
2021-06-27 21:25:29 +00:00
}
Further Linux support [+] Begin work on IO futexes for io release on process/thread exit [+] Linux ::readdir iteration [+] AuConsole buffering API [*] Fix sleep as to not get interrupted by signals [*] Switch the type of FS lock used under Linux [*] Linux: Use new IPCHandle encoding scheme [*] Fix undefined behaviour: unintialized timeout values (AuLoop/Linux) [*] Fix undefined behaviour: ConsoleTTY clear line was called of a color of a random value on stack [-] Remainings of std dir iterator [*] Fix pthread_kill (aka send signal to pthread handle) always kills process. This is what you expect bc signal handler inheritance. [*] Reformat the build Aurora.json file [+] Added clang warning ignores to the build file [*] Fix: UNIX need to use STDOUT_FILENO. Was using CRT handle in place of fd by mistake. [+] Linux implementation for IO yield (AuIO::IOYield() - UNIX::LinuxOverlappedYield()) [*] Fix: Linux async end of stream processing. res 0 = zero bytes consumed. <= was detecting this as an error of code 0. Should succeed with zero bytes. [+] Linux LoopQueue missing epilogue hook for the IO processor [*] Various refactors and minor bug fixes [*] Linux fix: Handle pipe EOS as zero [*] Linux fix: thread termination via a user signal of 77. Need a force terminate. [*] IPC handle: fix improper int to bool cast in the header setup within ToString [*] Linux fix: HWInfo CPU topology regression [-] Linux fix: remove SIGABRT handler [*] Missing override in compression, exit, and consoletty headers. [+] Unix Syslog logger backend
2022-08-02 04:52:17 +00:00
struct ReadDirStructure : IReadDir
{
DIR *pDIR {};
struct dirent *pDE {};
bool bFirstTick { true };
bool bDead { false };
StatEx stat;
AuString sPath;
bool bFast {};
~ReadDirStructure()
{
if (this->pDIR)
{
::closedir(this->pDIR);
}
}
virtual StatEx *Next() override
{
bool bTryAgain {};
if (this->bDead)
{
return {};
}
do
{
if (!(this->pDE = ::readdir(this->pDIR)))
{
this->bDead = true;
return {};
}
if (this->pDE->d_name == AuString(".") ||
this->pDE->d_name == AuString(".."))
{
bTryAgain = true;
continue;
}
stat.bExists = true;
Further Linux support [+] Begin work on IO futexes for io release on process/thread exit [+] Linux ::readdir iteration [+] AuConsole buffering API [*] Fix sleep as to not get interrupted by signals [*] Switch the type of FS lock used under Linux [*] Linux: Use new IPCHandle encoding scheme [*] Fix undefined behaviour: unintialized timeout values (AuLoop/Linux) [*] Fix undefined behaviour: ConsoleTTY clear line was called of a color of a random value on stack [-] Remainings of std dir iterator [*] Fix pthread_kill (aka send signal to pthread handle) always kills process. This is what you expect bc signal handler inheritance. [*] Reformat the build Aurora.json file [+] Added clang warning ignores to the build file [*] Fix: UNIX need to use STDOUT_FILENO. Was using CRT handle in place of fd by mistake. [+] Linux implementation for IO yield (AuIO::IOYield() - UNIX::LinuxOverlappedYield()) [*] Fix: Linux async end of stream processing. res 0 = zero bytes consumed. <= was detecting this as an error of code 0. Should succeed with zero bytes. [+] Linux LoopQueue missing epilogue hook for the IO processor [*] Various refactors and minor bug fixes [*] Linux fix: Handle pipe EOS as zero [*] Linux fix: thread termination via a user signal of 77. Need a force terminate. [*] IPC handle: fix improper int to bool cast in the header setup within ToString [*] Linux fix: HWInfo CPU topology regression [-] Linux fix: remove SIGABRT handler [*] Missing override in compression, exit, and consoletty headers. [+] Unix Syslog logger backend
2022-08-02 04:52:17 +00:00
stat.fileName = this->pDE->d_name;
if (stat.fileName.empty())
{
bTryAgain = true;
continue;
}
stat.path = NormalizePathRet(this->sPath + stat.fileName);
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 AuString &string, bool bFast)
{
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 AuString &string)
{
return ReadDirEx(string, false);
}
2021-06-27 21:25:29 +00:00
AUKN_SYM bool FilesInDirectory(const AuString &string, AuList<AuString> &files)
{
Further Linux support [+] Begin work on IO futexes for io release on process/thread exit [+] Linux ::readdir iteration [+] AuConsole buffering API [*] Fix sleep as to not get interrupted by signals [*] Switch the type of FS lock used under Linux [*] Linux: Use new IPCHandle encoding scheme [*] Fix undefined behaviour: unintialized timeout values (AuLoop/Linux) [*] Fix undefined behaviour: ConsoleTTY clear line was called of a color of a random value on stack [-] Remainings of std dir iterator [*] Fix pthread_kill (aka send signal to pthread handle) always kills process. This is what you expect bc signal handler inheritance. [*] Reformat the build Aurora.json file [+] Added clang warning ignores to the build file [*] Fix: UNIX need to use STDOUT_FILENO. Was using CRT handle in place of fd by mistake. [+] Linux implementation for IO yield (AuIO::IOYield() - UNIX::LinuxOverlappedYield()) [*] Fix: Linux async end of stream processing. res 0 = zero bytes consumed. <= was detecting this as an error of code 0. Should succeed with zero bytes. [+] Linux LoopQueue missing epilogue hook for the IO processor [*] Various refactors and minor bug fixes [*] Linux fix: Handle pipe EOS as zero [*] Linux fix: thread termination via a user signal of 77. Need a force terminate. [*] IPC handle: fix improper int to bool cast in the header setup within ToString [*] Linux fix: HWInfo CPU topology regression [-] Linux fix: remove SIGABRT handler [*] Missing override in compression, exit, and consoletty headers. [+] Unix Syslog logger backend
2022-08-02 04:52:17 +00:00
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)
Further Linux support [+] Begin work on IO futexes for io release on process/thread exit [+] Linux ::readdir iteration [+] AuConsole buffering API [*] Fix sleep as to not get interrupted by signals [*] Switch the type of FS lock used under Linux [*] Linux: Use new IPCHandle encoding scheme [*] Fix undefined behaviour: unintialized timeout values (AuLoop/Linux) [*] Fix undefined behaviour: ConsoleTTY clear line was called of a color of a random value on stack [-] Remainings of std dir iterator [*] Fix pthread_kill (aka send signal to pthread handle) always kills process. This is what you expect bc signal handler inheritance. [*] Reformat the build Aurora.json file [+] Added clang warning ignores to the build file [*] Fix: UNIX need to use STDOUT_FILENO. Was using CRT handle in place of fd by mistake. [+] Linux implementation for IO yield (AuIO::IOYield() - UNIX::LinuxOverlappedYield()) [*] Fix: Linux async end of stream processing. res 0 = zero bytes consumed. <= was detecting this as an error of code 0. Should succeed with zero bytes. [+] Linux LoopQueue missing epilogue hook for the IO processor [*] Various refactors and minor bug fixes [*] Linux fix: Handle pipe EOS as zero [*] Linux fix: thread termination via a user signal of 77. Need a force terminate. [*] IPC handle: fix improper int to bool cast in the header setup within ToString [*] Linux fix: HWInfo CPU topology regression [-] Linux fix: remove SIGABRT handler [*] Missing override in compression, exit, and consoletty headers. [+] Unix Syslog logger backend
2022-08-02 04:52:17 +00:00
{
continue;
}
if (!AuTryInsert(files, stat->fileName))
{
return false;
}
}
return true;
2021-06-27 21:25:29 +00:00
}
AUKN_SYM bool DirsInDirectory(const AuString &string, AuList<AuString> &dirs)
{
Further Linux support [+] Begin work on IO futexes for io release on process/thread exit [+] Linux ::readdir iteration [+] AuConsole buffering API [*] Fix sleep as to not get interrupted by signals [*] Switch the type of FS lock used under Linux [*] Linux: Use new IPCHandle encoding scheme [*] Fix undefined behaviour: unintialized timeout values (AuLoop/Linux) [*] Fix undefined behaviour: ConsoleTTY clear line was called of a color of a random value on stack [-] Remainings of std dir iterator [*] Fix pthread_kill (aka send signal to pthread handle) always kills process. This is what you expect bc signal handler inheritance. [*] Reformat the build Aurora.json file [+] Added clang warning ignores to the build file [*] Fix: UNIX need to use STDOUT_FILENO. Was using CRT handle in place of fd by mistake. [+] Linux implementation for IO yield (AuIO::IOYield() - UNIX::LinuxOverlappedYield()) [*] Fix: Linux async end of stream processing. res 0 = zero bytes consumed. <= was detecting this as an error of code 0. Should succeed with zero bytes. [+] Linux LoopQueue missing epilogue hook for the IO processor [*] Various refactors and minor bug fixes [*] Linux fix: Handle pipe EOS as zero [*] Linux fix: thread termination via a user signal of 77. Need a force terminate. [*] IPC handle: fix improper int to bool cast in the header setup within ToString [*] Linux fix: HWInfo CPU topology regression [-] Linux fix: remove SIGABRT handler [*] Missing override in compression, exit, and consoletty headers. [+] Unix Syslog logger backend
2022-08-02 04:52:17 +00:00
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)
Further Linux support [+] Begin work on IO futexes for io release on process/thread exit [+] Linux ::readdir iteration [+] AuConsole buffering API [*] Fix sleep as to not get interrupted by signals [*] Switch the type of FS lock used under Linux [*] Linux: Use new IPCHandle encoding scheme [*] Fix undefined behaviour: unintialized timeout values (AuLoop/Linux) [*] Fix undefined behaviour: ConsoleTTY clear line was called of a color of a random value on stack [-] Remainings of std dir iterator [*] Fix pthread_kill (aka send signal to pthread handle) always kills process. This is what you expect bc signal handler inheritance. [*] Reformat the build Aurora.json file [+] Added clang warning ignores to the build file [*] Fix: UNIX need to use STDOUT_FILENO. Was using CRT handle in place of fd by mistake. [+] Linux implementation for IO yield (AuIO::IOYield() - UNIX::LinuxOverlappedYield()) [*] Fix: Linux async end of stream processing. res 0 = zero bytes consumed. <= was detecting this as an error of code 0. Should succeed with zero bytes. [+] Linux LoopQueue missing epilogue hook for the IO processor [*] Various refactors and minor bug fixes [*] Linux fix: Handle pipe EOS as zero [*] Linux fix: thread termination via a user signal of 77. Need a force terminate. [*] IPC handle: fix improper int to bool cast in the header setup within ToString [*] Linux fix: HWInfo CPU topology regression [-] Linux fix: remove SIGABRT handler [*] Missing override in compression, exit, and consoletty headers. [+] Unix Syslog logger backend
2022-08-02 04:52:17 +00:00
{
continue;
}
if (!AuTryInsert(dirs, stat->fileName))
{
return false;
}
}
return true;
2021-06-27 21:25:29 +00:00
}
static bool WriteFileEx(const AuString &path, const Memory::MemoryViewRead &blob, bool bCheck)
2021-06-27 21:25:29 +00:00
{
auto pFile = OpenWriteUnique(path);
SysCheckReturn(pFile, false);
if (bCheck)
{
if (pFile->GetLength())
{
SysPushErrorIO("File exists: {}", path);
return {};
}
}
2022-04-06 01:24:38 +00:00
AuUInt written = blob.length;
if (!pFile->Write(Memory::MemoryViewStreamRead(blob, written)))
{
SysPushErrorMem();
return false;
}
pFile->WriteEoS();
2022-04-06 01:24:38 +00:00
if (written != blob.length)
{
SysPushErrorIO();
return false;
}
return true;
}
AUKN_SYM bool WriteFile(const AuString &path, const Memory::MemoryViewRead &blob)
{
return WriteFileEx(path, blob, false);
}
AUKN_SYM bool WriteNewFile(const AuString &path, const Memory::MemoryViewRead &blob)
{
return WriteFileEx(path, blob, true);
}
2022-04-06 01:24:38 +00:00
AUKN_SYM bool ReadFile(const AuString &path, AuByteBuffer &buffer)
2021-06-27 21:25:29 +00:00
{
AuMemoryViewWrite writeView;
2022-04-06 01:24:38 +00:00
bool bIsStupidFD =
AuStartsWith(path, "/proc/") ||
AuStartsWith(path, "/sys/") ||
AuStartsWith(path, "/dev/");
auto pFile = OpenReadUnique(path, bIsStupidFD ? EFileAdvisoryLockLevel::eNoSafety : EFileAdvisoryLockLevel::eBlockWrite);
SysCheckReturn(pFile, false);
2021-09-06 10:58:08 +00:00
bool bIsZero = buffer.readPtr == buffer.base;
auto qwLength = pFile->GetLength();
2022-04-06 01:24:38 +00:00
// 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.
[+] Aurora::IO::Net::NetSocketConnectByHost [+] Aurora::IO::FS::DirDeleterEx [+] Aurora::IO::Compress [+] Aurora::IO::Decompress [*] Aurora::Memory::ByteBuffer zero-alloc fixes [*] Aurora::Memory::ByteBuffer linear read of begin/end should return (`const AuUInt8 *`)'s [*] Changed NT file CREATE flags [*] Fix linux regression [*] Update logger sink DirLogArchive ... [+] DirectoryLogger::uMaxLogsOrZeroBeforeDelete ... [+] DirectoryLogger::uMaxCumulativeFileSizeInMiBOrZeroBeforeDelete ... [+] DirectoryLogger::uMaxCumulativeFileSizeInMiBOrZeroBeforeCompress ... [+] DirectoryLogger::uMaxFileTimeInDeltaMSOrZeroBeforeCompress ... [+] DirectoryLogger::uMaxFileTimeInDeltaMSOrZeroBeforeDelete [*] FIX: BufferedLineReader was taking the wrong end head (prep) LZMACompressor [*] Updated build-script for LZMA (when i can be bothered to impl it) (prep) FSOverlappedUtilities (prep) FSDefaultOverlappedWorkerThread | default worker pool / apc dispatcher / auasync dispatcher concept for higher level overlapped ops (stub) [+] Aurora::IO::FS::OverlappedForceDelegatedIO (stub) [+] Aurora::IO::FS::OverlappedCompress (stub) [+] Aurora::IO::FS::OverlappedDecompress (stub) [+] Aurora::IO::FS::OverlappedWrite (stub) [+] Aurora::IO::FS::OverlappedRead (stub) [+] Aurora::IO::FS::OverlappedStat (stub) [+] Aurora::IO::FS::OverlappedCopy (stub) [+] Aurora::IO::FS::OverlappedRelink (stub) [+] Aurora::IO::FS::OverlappedTrustFile (stub) [+] Aurora::IO::FS::OverlappedBlockFile (stub) [+] Aurora::IO::FS::OverlappedUnblockFile (stub) [+] Aurora::IO::FS::OverlappedDelete
2023-01-26 21:43:19 +00:00
if (qwLength == 0)
2022-04-06 01:24:38 +00:00
{
if (bIsStupidFD)
2022-04-06 01:24:38 +00:00
{
qwLength = 4096 * 10;
2022-04-06 01:24:38 +00:00
}
else
{
return true;
}
}
writeView = buffer.GetOrAllocateLinearWriteable(qwLength);
if (!writeView)
2021-06-27 21:25:29 +00:00
{
return {};
2021-06-27 21:25:29 +00:00
}
AuUInt uLength { qwLength };
if (!pFile->Read(Memory::MemoryViewStreamWrite { writeView, uLength }))
{
SysPushErrorIO();
2022-04-06 01:24:38 +00:00
return false;
}
2022-04-06 01:24:38 +00:00
// 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;
2021-06-27 21:25:29 +00:00
}
static bool UnixExists(const AuString &path, bool dir)
{
struct stat s;
[+] Aurora::IO::Net::NetSocketConnectByHost [+] Aurora::IO::FS::DirDeleterEx [+] Aurora::IO::Compress [+] Aurora::IO::Decompress [*] Aurora::Memory::ByteBuffer zero-alloc fixes [*] Aurora::Memory::ByteBuffer linear read of begin/end should return (`const AuUInt8 *`)'s [*] Changed NT file CREATE flags [*] Fix linux regression [*] Update logger sink DirLogArchive ... [+] DirectoryLogger::uMaxLogsOrZeroBeforeDelete ... [+] DirectoryLogger::uMaxCumulativeFileSizeInMiBOrZeroBeforeDelete ... [+] DirectoryLogger::uMaxCumulativeFileSizeInMiBOrZeroBeforeCompress ... [+] DirectoryLogger::uMaxFileTimeInDeltaMSOrZeroBeforeCompress ... [+] DirectoryLogger::uMaxFileTimeInDeltaMSOrZeroBeforeDelete [*] FIX: BufferedLineReader was taking the wrong end head (prep) LZMACompressor [*] Updated build-script for LZMA (when i can be bothered to impl it) (prep) FSOverlappedUtilities (prep) FSDefaultOverlappedWorkerThread | default worker pool / apc dispatcher / auasync dispatcher concept for higher level overlapped ops (stub) [+] Aurora::IO::FS::OverlappedForceDelegatedIO (stub) [+] Aurora::IO::FS::OverlappedCompress (stub) [+] Aurora::IO::FS::OverlappedDecompress (stub) [+] Aurora::IO::FS::OverlappedWrite (stub) [+] Aurora::IO::FS::OverlappedRead (stub) [+] Aurora::IO::FS::OverlappedStat (stub) [+] Aurora::IO::FS::OverlappedCopy (stub) [+] Aurora::IO::FS::OverlappedRelink (stub) [+] Aurora::IO::FS::OverlappedTrustFile (stub) [+] Aurora::IO::FS::OverlappedBlockFile (stub) [+] Aurora::IO::FS::OverlappedUnblockFile (stub) [+] Aurora::IO::FS::OverlappedDelete
2023-01-26 21:43:19 +00:00
int err = ::stat(path.c_str(), &s);
2021-06-27 21:25:29 +00:00
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 AuString &path)
{
return UnixExists(NormalizePathRet(path), false);
}
AUKN_SYM bool DirExists(const AuString &path)
{
return UnixExists(NormalizePathRet(path), true);
}
AUKN_SYM bool DirMk(const AuString &path)
{
return CreateDirectories(NormalizePathRet(path), false);
}
AUKN_SYM bool Remove(const AuString &path)
{
return remove(NormalizePathRet(path).c_str()) != -1;
2021-06-27 21:25:29 +00:00
}
AUKN_SYM bool Relink(const AuString &src, const AuString &dest)
{
auto normalizedDestPath = NormalizePathRet(dest);
CreateDirectories(destPathNormalized, true);
return ::rename(NormalizePathRet(src).c_str(), destPathNormalized.c_str()) != -1;
2021-06-27 21:25:29 +00:00
}
2021-09-06 10:58:08 +00:00
#if defined(AURORA_IS_LINUX_DERIVED)
2021-06-27 21:25:29 +00:00
AUKN_SYM bool Copy(const AuString &src, const AuString &dest)
{
auto normalizedSrcPath = NormalizePathRet(src);
auto normalizedDestPath = NormalizePathRet(dest);
int input, output;
if ((input = ::open(normalizedSrcPath.c_str(), O_RDONLY | O_CLOEXEC)) == -1)
2021-06-27 21:25:29 +00:00
{
return false;
}
struct stat fileinfo = { 0 };
if (::fstat(input, &fileinfo) != 0)
2021-06-27 21:25:29 +00:00
{
::close(input);
2021-06-27 21:25:29 +00:00
return false;
}
if ((output = ::creat(normalizedDestPath.c_str(), fileinfo.st_mode)) == -1)
2021-06-27 21:25:29 +00:00
{
::close(input);
2021-06-27 21:25:29 +00:00
return false;
}
off_t bytesCopied = 0;
auto result = ::sendfile(output, input, &bytesCopied, fileinfo.st_size) != -1;
2021-06-27 21:25:29 +00:00
::close(input);
::close(output);
2021-06-27 21:25:29 +00:00
return result;
}
2021-09-06 10:58:08 +00:00
#elif defined(AURORA_IS_BSD_DERIVED)
AUKN_SYM bool Copy(const AuString &src, const AuString &dest)
{
auto normalizedSrcPath = NormalizePathRet(src);
auto normalizedDestPath = NormalizePathRet(dest);
CreateDirectories(normalizedDestPath, true);
2021-09-06 10:58:08 +00:00
int input, output;
if ((input = ::open(normalizedSrcPath.c_str(), O_RDONLY | O_CLOEXEC)) == -1)
2021-09-06 10:58:08 +00:00
{
return false;
}
struct stat fileinfo = { 0 };
if (::fstat(input, &fileinfo) != 0)
2021-09-06 10:58:08 +00:00
{
close(input)
return false;
}
2021-06-27 21:25:29 +00:00
if ((output = ::creat(normalizedDestPath.c_str(), fileinfo.st_mode)) == -1)
2021-09-06 10:58:08 +00:00
{
close(input);
return false;
}
auto result = ::fcopyfile(input, output, 0, COPYFILE_ALL) == 0;
2021-09-06 10:58:08 +00:00
::close(input);
::close(output);
2021-09-06 10:58:08 +00:00
return result;
}
#else
AUKN_SYM bool Copy(const AuString &src, const AuString &dest)
{
// TODO: not that i care
return false;
}
#endif
2021-06-27 21:25:29 +00:00
AUKN_SYM bool StatFile(const AuString &pathRel, Stat &stat)
{
stat = {};
auto path = NormalizePathRet(pathRel);
struct stat s;
auto err = ::stat(path.c_str(), &s);
2021-06-27 21:25:29 +00:00
if (err == -1)
{
if (ENOENT != errno)
2021-06-27 21:25:29 +00:00
{
SysPushErrorIO("Critical IO error while stating file (errno: {}, path: {})", errno, path);
2021-06-27 21:25:29 +00:00
}
return false;
}
stat.bExistsFile = S_ISREG(s.st_mode);
stat.bExistsDirectory = S_ISDIR(s.st_mode);
stat.bExistsSystemResource = S_ISSOCK(s.st_mode);
stat.bExists = stat.bExistsFile || stat.bExistsDirectory || stat.bExistsSystemResource;
2021-06-27 21:25:29 +00:00
if (!stat.bExists)
2021-06-27 21:25:29 +00:00
{
SysPushErrorIO("Missing attribute type in stat mode {} (of path {})", s.st_mode, path);
2021-06-27 21:25:29 +00:00
return false;
}
stat.uSize = s.st_size;
2021-06-27 21:25:29 +00:00
stat.created = Time::CTimeToMS(s.st_ctime);
stat.modified = Time::CTimeToMS(s.st_mtime);
stat.accessed = Time::CTimeToMS(s.st_atime);
2021-06-27 21:25:29 +00:00
err = lstat(path.c_str(), &s);
if (err != -1)
{
stat.bSymLink = S_ISLNK(s.st_mode);
2021-06-27 21:25:29 +00:00
}
return true;
}
}
#endif