[*] Moved some source code to AuProcAddresses.UNIX.cpp

This commit is contained in:
Reece Wilson 2024-03-05 18:37:44 +00:00
parent 2371794d47
commit 0a6c11d919
6 changed files with 119 additions and 108 deletions

View File

@ -26,4 +26,115 @@ namespace Aurora
{
killpg(0, SIGKILL);
}
static bool PosixLseek63(int fd, AuUInt64 offset, int whence, AuUInt64 *pOffset)
{
if (offset > std::numeric_limits<off_t>::max())
{
SysPushErrorIO("int overflow exploit?");
return false;
}
#if defined(AURORA_IS_LINUX_DERIVED)
auto ret = lseek64(fd, offset, whence);
#elif defined(AURORA_IS_64BIT)
static_assert(std::numeric_limits<off_t>::max() >= std::numeric_limits<AuInt64>::max(), "unsupported posix os");
auto ret = lseek(fd, offset, whence);
#elif defined(AURORA_FORCE_32BIT_FILESYSTEMS_ONLY)
auto ret = lseek(fd, (off_t)offset, whence);
#else
#error ancient 32-bit posix operating systems require 64-bit file awareness
#endif
if (ret < 0)
{
// Intentionally omitted (thank linuxs' mess of incomplete filesystems)
// SysPushErrorIO("PosixLseek63 IO Error: {}", ret);
return false;
}
if (pOffset)
{
*pOffset = ret;
}
return true;
}
static AuUInt64 PosixGetLength(int fd)
{
AuUInt64 ret {}, old {};
bool status {};
if (!PosixLseek63(fd, 0, SEEK_CUR, &old))
{
return 0;
}
status = PosixLseek63(fd, 0, SEEK_END, &ret);
status &= PosixLseek63(fd, old, SEEK_SET, nullptr);
return status ? ret : 0;
}
AuUInt64 PosixGetOffset(int fd)
{
AuUInt64 ret;
if (!PosixLseek63(fd, 0, SEEK_CUR, &ret))
{
return 0;
}
return ret;
}
bool PosixSetOffset(int fd, AuUInt64 offset)
{
return PosixLseek63(fd, offset, SEEK_SET, nullptr);
}
bool PosixRead(int fd, void *buf, AuUInt32 count, AuUInt32 *pRead)
{
AuSInt ret;
do
{
ret = ::read(fd, buf, count);
}
while ((ret == -1 && errno == EINTR));
if (ret < 0)
{
SysPushErrorIO("PosixRead IO Error: {}", errno);
return false;
}
if (pRead)
{
*pRead = ret;
}
return true;
}
bool PosixWrite(int fd, const void *buf, AuUInt32 count, AuUInt32 *pWritten)
{
AuSInt ret;
do
{
ret = ::write(fd, buf, count);
}
while ((ret == -1 && errno == EINTR));
if (ret < 0)
{
SysPushErrorIO("PosixWrite IO Error: {}", errno);
return false;
}
if (pWritten)
{
*pWritten = ret;
}
return true;
}
}

View File

@ -11,4 +11,10 @@ namespace Aurora
{
void PosixDoForkHooks();
void PosixTerminate();
AuUInt64 PosixGetOffset(int fd);
bool PosixSetOffset(int fd, AuUInt64 offset);
AuUInt64 PosixGetLength(int fd);
bool PosixRead (int fd, void *buf, AuUInt32 count, AuUInt32 *pRead);
bool PosixWrite (int fd, const void *buf, AuUInt32 count, AuUInt32 *pWritten);
}

View File

@ -256,7 +256,7 @@ namespace Aurora::IO
if (create.bFailIfNonEmptyFile)
{
if (FS::PosixGetLength(iFileDescriptor))
if (PosixGetLength(iFileDescriptor))
{
SysPushErrorResourceExists("File {} already exists", create.path);
::close(iFileDescriptor);

View File

@ -22,102 +22,6 @@
namespace Aurora::IO::FS
{
static const AuUInt64 kFileCopyBlock = 0x4000; // 16KiB
static bool PosixLseek63(int fd, AuUInt64 offset, int whence, AuUInt64 *pOffset)
{
if (offset > std::numeric_limits<off_t>::max())
{
SysPushErrorIO("int overflow exploit?");
return false;
}
#if defined(AURORA_IS_LINUX_DERIVED)
auto ret = lseek64(fd, offset, whence);
#elif defined(AURORA_IS_64BIT)
static_assert(std::numeric_limits<off_t>::max() >= std::numeric_limits<AuInt64>::max(), "unsupported posix os");
auto ret = lseek(fd, offset, whence);
#else
#error accient 32-bit posix operating systems require 64-bit file awareness
#endif
if (ret < 0)
{
//SysPushErrorIO("PosixLseek63 IO Error: %i", ret);
return false;
}
if (pOffset)
{
*pOffset = ret;
}
return true;
}
static AuUInt64 PosixGetOffset(int fd)
{
AuUInt64 ret;
if (!PosixLseek63(fd, 0, SEEK_CUR, &ret))
{
return 0;
}
return ret;
}
bool PosixSetOffset(int fd, AuUInt64 offset)
{
return PosixLseek63(fd, offset, SEEK_SET, nullptr);
}
static AuUInt64 PosixGetLength(int fd)
{
AuUInt64 ret {}, old {};
bool status {};
if (!PosixLseek63(fd, 0, SEEK_CUR, &old))
{
return 0;
}
status = PosixLseek63(fd, 0, SEEK_END, &ret);
status &= PosixLseek63(fd, old, SEEK_SET, nullptr);
return status ? ret : 0;
}
bool PosixRead(int fd, void *buf, AuUInt32 count, AuUInt32 *pRead)
{
auto ret = read(fd, buf, count);
if (ret < 0)
{
SysPushErrorIO("PosixRead IO Error: %i", ret);
return false;
}
if (pRead)
{
*pRead = ret;
}
return true;
}
bool PosixWrite(int fd, const void *buf, AuUInt32 count, AuUInt32 *pWritten)
{
auto ret = write(fd, buf, count);
if (ret < 0)
{
SysPushErrorIO("PosixWrite IO Error: %i", ret);
return false;
}
if (pWritten)
{
*pWritten = ret;
}
return true;
}
PosixFileStream::PosixFileStream() :
reader_(AuUnsafeRaiiToShared(this)),

View File

@ -12,11 +12,6 @@
namespace Aurora::IO::FS
{
// Code-sharing for async API fallbacks...
bool PosixSetOffset(int fd, AuUInt64 offset);
AuUInt64 PosixGetLength(int fd);
bool PosixRead(int fd, void *buf, AuUInt32 count, AuUInt32 *pRead);
bool PosixWrite(int fd, const void *buf, AuUInt32 count, AuUInt32 *pWritten);
struct PosixFileStream : IFileStream
{
PosixFileStream();

View File

@ -15,11 +15,6 @@
#include <termios.h>
#include <Source/IO/AuIOHandle.hpp>
namespace Aurora::IO::FS
{
bool PosixWrite(int fd, const void *buf, AuUInt32 count, AuUInt32 *pWritten);
}
namespace Aurora::IO::IPC
{
static const AuUInt64 kFileCopyBlock = 0x4000; // 16KiB
@ -321,7 +316,7 @@ namespace Aurora::IO::IPC
int blockSize = AuMin(AuUInt(kFileCopyBlock), length);
if (!FS::PosixWrite(handle, &reinterpret_cast<const char *>(read.ptr)[offset], blockSize, &written))
if (!PosixWrite(handle, &reinterpret_cast<const char *>(read.ptr)[offset], blockSize, &written))
{
if (!nonblock)
{