From 835f14403d5f6e04d65b761857ee1271a4e7fe98 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Tue, 9 May 2023 17:19:57 +0200 Subject: add context to MapViewOfFile errors (#282) * added FileSizeFromHandle function * added file size to error message when MapViewOfFile fails --- src/zencore/filesystem.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/zencore/filesystem.cpp') diff --git a/src/zencore/filesystem.cpp b/src/zencore/filesystem.cpp index a17773024..8a6a9869c 100644 --- a/src/zencore/filesystem.cpp +++ b/src/zencore/filesystem.cpp @@ -1022,6 +1022,27 @@ PathFromHandle(void* NativeHandle) #endif // ZEN_PLATFORM_WINDOWS } +uint64_t +FileSizeFromHandle(void* NativeHandle) +{ + uint64_t FileSize = ~0ull; + +#if ZEN_PLATFORM_WINDOWS + BY_HANDLE_FILE_INFORMATION Bhfh = {}; + if (GetFileInformationByHandle(NativeHandle, &Bhfh)) + { + FileSize = uint64_t(Bhfh.nFileSizeHigh) << 32 | Bhfh.nFileSizeLow; + } +#else + int Fd = int(intptr_t(NativeHandle)); + struct stat Stat; + fstat(Fd, &Stat); + FileSize = size_t(Stat.st_size); +#endif + + return FileSize; +} + std::filesystem::path GetRunningExecutablePath() { -- cgit v1.2.3