Skip to content

Commit 2cf2faa

Browse files
Work around std::filesystem::canonical bug on UWP (#198)
* Work around std::filesystem::canonical bug on UWP * Fix legacy win32 filesystem code path
1 parent af531e9 commit 2cf2faa

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace usage of std::filesystem::canonical with PathCchCanonicalize on Windows platform to work around bug on UWP platforms. This also replaces PathCanonicalize with PathCchCanonicalize and adds the appropriate library for linking in.

src/common/filesystem_utils.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
#include <dirent.h>
8888
#endif
8989

90+
#if defined(XR_USE_PLATFORM_WIN32)
91+
#include <PathCch.h>
92+
#endif
93+
9094
#if defined(XR_USE_PLATFORM_WIN32)
9195
#define PATH_SEPARATOR ';'
9296
#define DIRECTORY_SYMBOL '\\'
@@ -128,7 +132,18 @@ bool FileSysUtilsGetAbsolutePath(const std::string& path, std::string& absolute)
128132
}
129133

130134
bool FileSysUtilsGetCanonicalPath(const std::string& path, std::string& canonical) {
135+
#if defined(XR_USE_PLATFORM_WIN32)
136+
// std::filesystem::canonical fails on UWP and must be avoided. This alternative will not
137+
// follow symbolic links but symbolic links are not needed on Windows since the loader uses
138+
// the registry as a form of indirection instead.
139+
wchar_t canonical_wide_path[MAX_PATH];
140+
if (FAILED(PathCchCanonicalize(canonical_wide_path, MAX_PATH, utf8_to_wide(path).c_str()))) {
141+
return false;
142+
}
143+
canonical = wide_to_utf8(canonical_wide_path);
144+
#else
131145
canonical = FS_PREFIX::canonical(path).string();
146+
#endif
132147
return true;
133148
}
134149

@@ -216,7 +231,7 @@ bool FileSysUtilsGetAbsolutePath(const std::string& path, std::string& absolute)
216231

217232
bool FileSysUtilsGetCanonicalPath(const std::string& path, std::string& absolute) {
218233
wchar_t tmp_path[MAX_PATH];
219-
if (0 != PathCanonicalizeW(utf8_to_wide(path).c_str(), tmp_path)) {
234+
if (SUCCEEDED(PathCchCanonicalize(tmp_path, MAX_PATH, utf8_to_wide(path).c_str()))) {
220235
absolute = wide_to_utf8(tmp_path);
221236
return true;
222237
}

src/loader/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ elseif(WIN32)
183183
target_compile_options(openxr_loader PRIVATE /wd6386)
184184
endif()
185185

186-
target_link_libraries(openxr_loader PRIVATE advapi32)
186+
target_link_libraries(openxr_loader PUBLIC advapi32 pathcch)
187187

188188
# Need to copy DLL to client directories so clients can easily load it.
189189
if(DYNAMIC_LOADER AND (CMAKE_GENERATOR MATCHES "^Visual Studio.*"))
@@ -281,4 +281,4 @@ install(
281281
FILES ${CMAKE_CURRENT_BINARY_DIR}/OpenXRConfig.cmake
282282
${CMAKE_CURRENT_BINARY_DIR}/OpenXRConfigVersion.cmake
283283
DESTINATION ${TARGET_DESTINATION}
284-
)
284+
)

0 commit comments

Comments
 (0)