aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2021-05-11 18:28:31 +0200
committerStefan Boberg <[email protected]>2021-05-11 18:28:31 +0200
commit3d7740818ec5fe3b078ab2b6b3a41044ae903bc9 (patch)
tree3ba385593526728ddd2e1ca13a6938a214e71a44
parentAdding zenservice code (diff)
downloadzen-3d7740818ec5fe3b078ab2b6b3a41044ae903bc9.tar.xz
zen-3d7740818ec5fe3b078ab2b6b3a41044ae903bc9.zip
Added PathFromHandle helper function
Primarily to be used to add context to error messages in places where we only have a handle available
-rw-r--r--zencore/filesystem.cpp18
-rw-r--r--zencore/include/zencore/filesystem.h4
2 files changed, 22 insertions, 0 deletions
diff --git a/zencore/filesystem.cpp b/zencore/filesystem.cpp
index 663a626a1..578b28277 100644
--- a/zencore/filesystem.cpp
+++ b/zencore/filesystem.cpp
@@ -589,4 +589,22 @@ FileSystemTraversal::TraverseFileSystem(const std::filesystem::path& RootDir, Tr
}
}
+std::filesystem::path
+PathFromHandle(void* NativeHandle)
+{
+ if (NativeHandle == nullptr || NativeHandle == INVALID_HANDLE_VALUE)
+ {
+ return std::filesystem::path();
+ }
+
+ const DWORD RequiredLengthIncludingNul = GetFinalPathNameByHandleW(NativeHandle, nullptr, 0, FILE_NAME_OPENED);
+
+ std::wstring FullPath;
+ FullPath.resize(RequiredLengthIncludingNul - 1);
+
+ const DWORD FinalLength = GetFinalPathNameByHandleW(NativeHandle, FullPath.data(), RequiredLengthIncludingNul, FILE_NAME_OPENED);
+
+ return FullPath;
+}
+
} // namespace zen
diff --git a/zencore/include/zencore/filesystem.h b/zencore/include/zencore/filesystem.h
index b20a1e7c6..ba8588c5c 100644
--- a/zencore/include/zencore/filesystem.h
+++ b/zencore/include/zencore/filesystem.h
@@ -29,6 +29,10 @@ ZENCORE_API bool CreateDirectories(const std::filesystem::path& dir);
ZENCORE_API bool CleanDirectory(const wchar_t* dir);
ZENCORE_API bool CleanDirectory(const std::filesystem::path& dir);
+/** Map native file handle to a path
+ */
+ZENCORE_API std::filesystem::path PathFromHandle(void* NativeHandle);
+
struct FileContents
{
std::vector<IoBuffer> Data;