diff --git a/Source/AuProcAddresses.UNIX.cpp b/Source/AuProcAddresses.UNIX.cpp index 1109f40b..ee8e581f 100755 --- a/Source/AuProcAddresses.UNIX.cpp +++ b/Source/AuProcAddresses.UNIX.cpp @@ -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::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::max() >= std::numeric_limits::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; + } } \ No newline at end of file diff --git a/Source/AuProcAddresses.UNIX.hpp b/Source/AuProcAddresses.UNIX.hpp index 364da8c0..630f3582 100755 --- a/Source/AuProcAddresses.UNIX.hpp +++ b/Source/AuProcAddresses.UNIX.hpp @@ -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); } \ No newline at end of file diff --git a/Source/IO/AuIOHandle.Unix.cpp b/Source/IO/AuIOHandle.Unix.cpp index 3f3ede9d..39c424cf 100644 --- a/Source/IO/AuIOHandle.Unix.cpp +++ b/Source/IO/AuIOHandle.Unix.cpp @@ -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); diff --git a/Source/IO/FS/FileStream.Unix.cpp b/Source/IO/FS/FileStream.Unix.cpp index 15585b69..a3c6d960 100755 --- a/Source/IO/FS/FileStream.Unix.cpp +++ b/Source/IO/FS/FileStream.Unix.cpp @@ -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::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::max() >= std::numeric_limits::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)), diff --git a/Source/IO/FS/FileStream.Unix.hpp b/Source/IO/FS/FileStream.Unix.hpp index e10a1073..67b8468a 100644 --- a/Source/IO/FS/FileStream.Unix.hpp +++ b/Source/IO/FS/FileStream.Unix.hpp @@ -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(); diff --git a/Source/IO/IPC/AuIPCPipe.Unix.cpp b/Source/IO/IPC/AuIPCPipe.Unix.cpp index f7a8b851..14f27ae4 100644 --- a/Source/IO/IPC/AuIPCPipe.Unix.cpp +++ b/Source/IO/IPC/AuIPCPipe.Unix.cpp @@ -15,11 +15,6 @@ #include #include -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(read.ptr)[offset], blockSize, &written)) + if (!PosixWrite(handle, &reinterpret_cast(read.ptr)[offset], blockSize, &written)) { if (!nonblock) {