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

250 lines
6.0 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>
#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
{
return mkdir(path.c_str(), 0760) == 0;
}
AUKN_SYM bool FilesInDirectory(const AuString &string, AuList<AuString> &files)
{
return IterateDirEntriesSTL(string, true, files);
}
AUKN_SYM bool DirsInDirectory(const AuString &string, AuList<AuString> &dirs)
{
return IterateDirEntriesSTL(string, false, dirs);
}
2021-09-06 10:58:08 +00:00
AUKN_SYM bool WriteFile(const AuString &path, const void *data, AuUInt length)
2021-06-27 21:25:29 +00:00
{
2021-09-06 10:58:08 +00:00
auto file = OpenWriteUnique(path);
SysCheckReturn(file, false);
AuUInt written = length;
if (!file->Write(Memory::MemoryViewStreamRead(data, written)))
{
SysPushErrorMem();
return false;
}
if (written != length)
{
SysPushErrorIO();
return false;
}
return false;
}
2021-06-27 21:25:29 +00:00
AUKN_SYM bool ReadFile(const AuString &path, AuList<uint8_t> &buffer)
{
2021-09-06 10:58:08 +00:00
auto file = OpenReadUnique(path);
SysCheckReturn(file, false);
auto len = file->GetLength();
if (!AuTryResize(buffer, len))
2021-06-27 21:25:29 +00:00
{
2021-09-06 10:58:08 +00:00
SysPushErrorMem();
2021-06-27 21:25:29 +00:00
return false;
}
2021-09-06 10:58:08 +00:00
AuUInt read;
if (!file->Read(Memory::MemoryViewStreamWrite {buffer, read}))
{
SysPushErrorIO();
return false;
}
if (read != buffer.size())
{
SysPushErrorIO();
return false;
}
return false;
2021-06-27 21:25:29 +00:00
}
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 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)
{
return rename(NormalizePathRet(src).c_str(), NormalizePathRet(dest).c_str()) != -1;
}
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)) == -1)
{
return false;
}
struct stat fileinfo = { 0 };
if (fstat(input, &fileinfo) != 0)
{
close(input);
2021-06-27 21:25:29 +00:00
return false;
}
if ((output = creat(normalizedDestPath.c_str(), fileinfo.st_mode)) == -1)
{
close(input);
return false;
}
off_t bytesCopied = 0;
auto result = sendfile(output, input, &bytesCopied, fileinfo.st_size) != -1;
close(input);
close(output);
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);
int input, output;
if ((input = open(normalizedSrcPath.c_str(), O_RDONLY)) == -1)
{
return false;
}
struct stat fileinfo = { 0 };
if (fstat(input, &fileinfo) != 0)
{
close(input)
return false;
}
2021-06-27 21:25:29 +00:00
2021-09-06 10:58:08 +00:00
if ((output = creat(normalizedDestPath.c_str(), fileinfo.st_mode)) == -1)
{
close(input);
return false;
}
auto result = fcopyfile(input, output, 0, COPYFILE_ALL) == 0;
close(input);
close(output);
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
{
LogWarn("Critical IO error while stating file (errno: {}, path: {})", errno, path);
2021-06-27 21:25:29 +00:00
}
return false;
}
stat.existsFile = S_ISREG(s.st_mode);
stat.existsDirectory = S_ISDIR(s.st_mode);
stat.existsSystemResource = S_ISSOCK(s.st_mode);
stat.exists = stat.existsFile || stat.existsDirectory || stat.existsSystemResource;
if (!stat.exists)
{
LogWarn("Missing attribute type in stat mode {} (of path {})", s.st_mode, path);
2021-06-27 21:25:29 +00:00
return false;
}
stat.size = s.st_size;
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.symLink = S_ISLNK(s.st_mode);
}
return true;
}
}
#endif