aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2023-05-09 17:19:57 +0200
committerGitHub <[email protected]>2023-05-09 17:19:57 +0200
commit835f14403d5f6e04d65b761857ee1271a4e7fe98 (patch)
tree4a5b69325e7b468e1fdabc2bfd296f4e0b647175 /src
parentfixed merge error (diff)
downloadzen-835f14403d5f6e04d65b761857ee1271a4e7fe98.tar.xz
zen-835f14403d5f6e04d65b761857ee1271a4e7fe98.zip
add context to MapViewOfFile errors (#282)
* added FileSizeFromHandle function * added file size to error message when MapViewOfFile fails
Diffstat (limited to 'src')
-rw-r--r--src/zencore/filesystem.cpp21
-rw-r--r--src/zencore/include/zencore/filesystem.h5
-rw-r--r--src/zencore/iobuffer.cpp9
3 files changed, 32 insertions, 3 deletions
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()
{
diff --git a/src/zencore/include/zencore/filesystem.h b/src/zencore/include/zencore/filesystem.h
index fa5f94170..230d8d1c2 100644
--- a/src/zencore/include/zencore/filesystem.h
+++ b/src/zencore/include/zencore/filesystem.h
@@ -1,4 +1,5 @@
// Copyright Epic Games, Inc. All Rights Reserved.
+// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
@@ -32,6 +33,10 @@ ZENCORE_API bool CleanDirectory(const std::filesystem::path& dir);
*/
ZENCORE_API std::filesystem::path PathFromHandle(void* NativeHandle);
+/** Query file size from native file handle
+ */
+ZENCORE_API uint64_t FileSizeFromHandle(void* NativeHandle);
+
ZENCORE_API std::filesystem::path GetRunningExecutablePath();
/** Set the max open file handle count to max allowed for the current process on Linux and MacOS
diff --git a/src/zencore/iobuffer.cpp b/src/zencore/iobuffer.cpp
index 1d7d47695..e28bf11b2 100644
--- a/src/zencore/iobuffer.cpp
+++ b/src/zencore/iobuffer.cpp
@@ -356,16 +356,19 @@ IoBufferExtendedCore::Materialize() const
#if ZEN_PLATFORM_WINDOWS
CloseHandle(NewMmapHandle);
#endif // ZEN_PLATFORM_WINDOWS
- ZEN_ERROR("MapViewOfFile failed (offset {:#x}, size {:#x}) file: '{}', {}",
+
+ ZEN_ERROR("MapViewOfFile failed (offset {:#x}, size {:#x}) file: '{}' (size {:#x}), {}",
MapOffset,
MapSize,
zen::PathFromHandle(m_FileHandle),
+ zen::FileSizeFromHandle(m_FileHandle),
GetSystemErrorAsString(Error));
throw std::system_error(std::error_code(Error, std::system_category()),
- fmt::format("MapViewOfFile failed (offset {:#x}, size {:#x}) file: '{}'",
+ fmt::format("MapViewOfFile failed (offset {:#x}, size {:#x}) file: '{}' (size {:#x})",
MapOffset,
MapSize,
- zen::PathFromHandle(m_FileHandle)));
+ zen::PathFromHandle(m_FileHandle),
+ zen::FileSizeFromHandle(m_FileHandle)));
}
m_MappedPointer = MappedBase;