diff options
| -rw-r--r-- | zencore/except.cpp | 6 | ||||
| -rw-r--r-- | zencore/filesystem.cpp | 68 | ||||
| -rw-r--r-- | zencore/include/zencore/except.h | 6 | ||||
| -rw-r--r-- | zencore/include/zencore/filesystem.h | 4 | ||||
| -rw-r--r-- | zencore/iobuffer.cpp | 5 | ||||
| -rw-r--r-- | zencore/zencore.cpp | 3 | ||||
| -rw-r--r-- | zenhttp/httpsys.cpp | 6 |
7 files changed, 89 insertions, 9 deletions
diff --git a/zencore/except.cpp b/zencore/except.cpp index 834585522..75d0c8dd1 100644 --- a/zencore/except.cpp +++ b/zencore/except.cpp @@ -31,13 +31,13 @@ ThrowLastError(std::string_view Message) std::string GetLastErrorAsString() { - return GetWindowsErrorAsString(zen::GetLastError()); + return GetErrorAsString(zen::GetLastError()); } std::string -GetWindowsErrorAsString(uint32_t Win32ErrorCode) +GetErrorAsString(uint32_t ErrorCode) { - return std::error_code(Win32ErrorCode, std::system_category()).message(); + return std::error_code(ErrorCode, std::system_category()).message(); } #if __cpp_lib_source_location diff --git a/zencore/filesystem.cpp b/zencore/filesystem.cpp index 59300b7ad..2942910ed 100644 --- a/zencore/filesystem.cpp +++ b/zencore/filesystem.cpp @@ -15,6 +15,7 @@ #include <winnt.h> #include <filesystem> +#include <doctest/doctest.h> #include <gsl/gsl-lite.hpp> namespace zen { @@ -605,6 +606,7 @@ FileSystemTraversal::TraverseFileSystem(const std::filesystem::path& RootDir, Tr std::filesystem::path PathFromHandle(void* NativeHandle) { +#if ZEN_PLATFORM_WINDOWS if (NativeHandle == nullptr || NativeHandle == INVALID_HANDLE_VALUE) { return std::filesystem::path(); @@ -620,15 +622,81 @@ PathFromHandle(void* NativeHandle) ZEN_UNUSED(FinalLength); return FullPath; +#elif ZEN_PLATFORM_LINUX + char Buffer[256]; + sprintf(Buffer, "/proc/%d/fd/%d", getpid(), int(uintptr_t(NativeHandle))); + ssize_t BytesRead = readlink(Buffer, Buffer, sizeof(Buffer) - 1); + if (BytesRead <= 0) + return std::filesystem::path(); + + Buffer[BytesRead] = '\0'; + return Buffer; +#else +# error Unimplemented platform +#endif // ZEN_PLATFORM_WINDOWS } std::filesystem::path GetRunningExecutablePath() { +#if ZEN_PLATFORM_WINDOWS TCHAR ExePath[MAX_PATH]; DWORD PathLength = GetModuleFileName(NULL, ExePath, ZEN_ARRAY_COUNT(ExePath)); return {std::wstring_view(ExePath, PathLength)}; +#elif ZEN_PLATFORM_LINUX + char Buffer[256]; + sprintf(Buffer, "/proc/%d/exe", getpid()); + ssize_t BytesRead = readlink(Buffer, Buffer, sizeof(Buffer) - 1); + if (BytesRead < 0) + return {}; + + Buffer[BytesRead] = '\0'; + return Buffer; +#else +# error Unimplemented platform +#endif // ZEN_PLATFORM_WINDOWS +} + + + +////////////////////////////////////////////////////////////////////////// +// +// Testing related code follows... +// + +void +filesystem_forcelink() +{ +} + +TEST_CASE("filesystem") +{ + using namespace std::filesystem; + + path BinPath = GetRunningExecutablePath(); + CHECK(BinPath.stem() == "zencore-test"); + CHECK(is_regular_file(BinPath)); + + void* Handle; +#if ZEN_PLATFORM_WINDOWS + Handle = CreateFileW(BinPath.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, + OPEN_EXISTING, 0, nullptr); + CHECK(Handle != INVALID_HANDLE_VALUE); +#else + int Fd = open(BinPath.c_str(), O_RDONLY); + CHECK(Fd >= 0); + Handle = (void*)uintptr_t(Fd); +#endif + + auto FromHandle = PathFromHandle((void*)uintptr_t(Handle)); + CHECK(equivalent(FromHandle, BinPath)); + +#if ZEN_PLATFORM_WINDOWS + CloseHandle(Handle); +#else + close(int(uintptr_t(Handle))); +#endif } } // namespace zen diff --git a/zencore/include/zencore/except.h b/zencore/include/zencore/except.h index 36cca895f..90f7d45db 100644 --- a/zencore/include/zencore/except.h +++ b/zencore/include/zencore/except.h @@ -60,7 +60,7 @@ ZENCORE_API void ThrowLastError(std::string_view Message, const std::source_loca #endif ZENCORE_API std::string GetLastErrorAsString(); -ZENCORE_API std::string GetWindowsErrorAsString(uint32_t Win32ErrorCode); +ZENCORE_API std::string GetErrorAsString(uint32_t ErrorCode); inline void ThrowSystemException(const char* Message) @@ -83,9 +83,9 @@ GetLastError() } inline std::error_code -MakeWin32ErrorCode(uint32_t Win32ErrorCode) noexcept +MakeErrorCode(uint32_t ErrorCode) noexcept { - return std::error_code(Win32ErrorCode, std::system_category()); + return std::error_code(ErrorCode, std::system_category()); } inline std::error_code diff --git a/zencore/include/zencore/filesystem.h b/zencore/include/zencore/filesystem.h index a2d368d6f..a5e4dcf80 100644 --- a/zencore/include/zencore/filesystem.h +++ b/zencore/include/zencore/filesystem.h @@ -78,4 +78,8 @@ public: void TraverseFileSystem(const std::filesystem::path& RootDir, TreeVisitor& Visitor); }; +////////////////////////////////////////////////////////////////////////// + +void filesystem_forcelink(); // internal + } // namespace zen diff --git a/zencore/iobuffer.cpp b/zencore/iobuffer.cpp index 758cf539c..cf167a7c6 100644 --- a/zencore/iobuffer.cpp +++ b/zencore/iobuffer.cpp @@ -4,6 +4,7 @@ #include <doctest/doctest.h> #include <memory.h> +#include <zencore/except.h> #include <zencore/filesystem.h> #include <zencore/fmtutils.h> #include <zencore/logging.h> @@ -21,12 +22,14 @@ namespace zen { void* IoBufferCore::AllocateBuffer(size_t InSize, size_t Alignment) { +#if ZEN_PLATFORM_WINDOWS if (((InSize & 0xffFF) == 0) && (Alignment == 0x10000)) { m_Flags |= kLowLevelAlloc; return VirtualAlloc(nullptr, InSize, MEM_COMMIT, PAGE_READWRITE); } else +#endif // ZEN_PLATFORM_WINDOWS { return Memory::Alloc(InSize, Alignment); } @@ -35,11 +38,13 @@ IoBufferCore::AllocateBuffer(size_t InSize, size_t Alignment) void IoBufferCore::FreeBuffer() { +#if ZEN_PLATFORM_WINDOWS if (m_Flags & kLowLevelAlloc) { VirtualFree(const_cast<void*>(m_DataPtr), 0, MEM_DECOMMIT); } else +#endif // ZEN_PLATFORM_WINDOWS { return Memory::Free(const_cast<void*>(m_DataPtr)); } diff --git a/zencore/zencore.cpp b/zencore/zencore.cpp index c53fd218f..56bdd2ae8 100644 --- a/zencore/zencore.cpp +++ b/zencore/zencore.cpp @@ -16,6 +16,8 @@ #include <zencore/compactbinarypackage.h> #include <zencore/compositebuffer.h> #include <zencore/compress.h> +#include <zencore/filesystem.h> +#include <zencore/intmath.h> #include <zencore/iobuffer.h> #include <zencore/memory.h> #include <zencore/refcount.h> @@ -85,6 +87,7 @@ zencore_forcelinktests() zen::blake3_forcelink(); zen::compositebuffer_forcelink(); zen::compress_forcelink(); + zen::filesystem_forcelink(); zen::intmath_forcelink(); zen::iobuffer_forcelink(); zen::memory_forcelink(); diff --git a/zenhttp/httpsys.cpp b/zenhttp/httpsys.cpp index 4536d0ed9..d4c58bffd 100644 --- a/zenhttp/httpsys.cpp +++ b/zenhttp/httpsys.cpp @@ -418,7 +418,7 @@ HttpMessageResponseRequest::HandleCompletion(ULONG IoResult, ULONG_PTR NumberOfB if (IoResult) { - ZEN_WARN("response aborted due to error: '{}'", GetWindowsErrorAsString(IoResult)); + ZEN_WARN("response aborted due to error: '{}'", GetErrorAsString(IoResult)); // if one transmit failed there's really no need to go on return nullptr; @@ -556,7 +556,7 @@ HttpMessageResponseRequest::IssueRequest(std::error_code& ErrorCode) ZEN_ERROR("failed to send HTTP response (error: '{}'), request URL: {}", SendResult, HttpReq->pRawUrl); - ErrorCode = MakeWin32ErrorCode(SendResult); + ErrorCode = MakeErrorCode(SendResult); } else { @@ -1237,7 +1237,7 @@ InitialRequestHandler::IssueRequest(std::error_code& ErrorCode) // CleanupHttpIoRequest(pIoRequest); - ErrorCode = MakeWin32ErrorCode(HttpApiResult); + ErrorCode = MakeErrorCode(HttpApiResult); ZEN_ERROR("HttpReceiveHttpRequest failed, error {}", ErrorCode.message()); |