Skip to content

Commit 3b41ea1

Browse files
Improve ArchGetFileName to return full path for Windows platform
1 parent 4a8c0d2 commit 3b41ea1

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

pxr/base/arch/fileSystem.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -532,24 +532,34 @@ ArchGetFileName(FILE *file)
532532
}
533533
return result;
534534
#elif defined (ARCH_OS_WINDOWS)
535-
static constexpr DWORD bufSize =
536-
sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * 4096;
537-
HANDLE hfile = _FileToWinHANDLE(file);
538-
auto fileNameInfo = reinterpret_cast<PFILE_NAME_INFO>(malloc(bufSize));
539535
string result;
540-
if (GetFileInformationByHandleEx(
541-
hfile, FileNameInfo, static_cast<void *>(fileNameInfo), bufSize)) {
536+
WCHAR filePath[MAX_PATH];
537+
HANDLE hfile = _FileToWinHANDLE(file);
538+
if (GetFinalPathNameByHandleW(hfile, filePath, MAX_PATH, VOLUME_NAME_DOS)) {
542539
size_t outSize = WideCharToMultiByte(
543-
CP_UTF8, 0, fileNameInfo->FileName,
544-
fileNameInfo->FileNameLength/sizeof(WCHAR),
540+
CP_UTF8, 0, filePath,
541+
wcslen(filePath),
545542
NULL, 0, NULL, NULL);
546543
result.resize(outSize);
547544
WideCharToMultiByte(
548-
CP_UTF8, 0, fileNameInfo->FileName,
549-
fileNameInfo->FileNameLength/sizeof(WCHAR),
545+
CP_UTF8, 0, filePath,
546+
-1,
550547
&result.front(), outSize, NULL, NULL);
548+
549+
if (result.length() > 4)
550+
{
551+
// It needs to strip the path prefix as
552+
// the path returned is DOS device path, and the
553+
// syntax is one of:
554+
// \\.\C:\Test\Foo.txt
555+
// \\?\C:\Test\Foo.txt
556+
if (result.compare(0, 4, "\\\\?\\") == 0 ||
557+
result.compare(0, 4, "\\\\.\\") == 0)
558+
{
559+
result.erase(0, 4);
560+
}
561+
}
551562
}
552-
free(fileNameInfo);
553563
return result;
554564
#else
555565
#error Unknown system architecture

pxr/base/arch/testenv/testFileSystem.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <cstdio>
1515
#include <cstdlib>
1616
#include <cstring>
17+
#include <filesystem>
1718

1819
PXR_NAMESPACE_USING_DIRECTIVE
1920

@@ -92,6 +93,12 @@ int main()
9293
fclose(firstFile);
9394
ARCH_AXIOM(ArchGetFileLength(firstName.c_str()) == strlen(testContent));
9495

96+
// Open a file, check that the file path from FILE* handle is matched.
97+
ARCH_AXIOM((firstFile = ArchOpenFile(firstName.c_str(), "rb")) != NULL);
98+
std::string filePath = ArchGetFileName(firstFile);
99+
ARCH_AXIOM(std::filesystem::equivalent(filePath, firstName));
100+
fclose(firstFile);
101+
95102
// Map the file and assert the bytes are what we expect they are.
96103
ARCH_AXIOM((firstFile = ArchOpenFile(firstName.c_str(), "rb")) != NULL);
97104
ArchConstFileMapping cfm = ArchMapFileReadOnly(firstFile);

0 commit comments

Comments
 (0)