Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ArchGetFileName for windows to return full path #3361

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions pxr/base/arch/fileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,24 +532,29 @@ ArchGetFileName(FILE *file)
}
return result;
#elif defined (ARCH_OS_WINDOWS)
static constexpr DWORD bufSize =
sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * 4096;
HANDLE hfile = _FileToWinHANDLE(file);
auto fileNameInfo = reinterpret_cast<PFILE_NAME_INFO>(malloc(bufSize));
string result;
if (GetFileInformationByHandleEx(
hfile, FileNameInfo, static_cast<void *>(fileNameInfo), bufSize)) {
WCHAR filePath[MAX_PATH];
HANDLE hfile = _FileToWinHANDLE(file);
if (GetFinalPathNameByHandleW(hfile, filePath, MAX_PATH, VOLUME_NAME_DOS)) {
size_t outSize = WideCharToMultiByte(
CP_UTF8, 0, fileNameInfo->FileName,
fileNameInfo->FileNameLength/sizeof(WCHAR),
CP_UTF8, 0, filePath,
wcslen(filePath),
NULL, 0, NULL, NULL);
result.resize(outSize);
WideCharToMultiByte(
CP_UTF8, 0, fileNameInfo->FileName,
fileNameInfo->FileNameLength/sizeof(WCHAR),
CP_UTF8, 0, filePath,
-1,
&result.front(), outSize, NULL, NULL);

if (result.length() > 4)
{
std::string prefix = result.substr(0, 4);
if (prefix == "\\\\?\\" || prefix == "\\\\.\\")
{
result = result.substr(4);
}
}
}
free(fileNameInfo);
return result;
#else
#error Unknown system architecture
Expand Down
6 changes: 6 additions & 0 deletions pxr/base/arch/testenv/testFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ int main()
fclose(firstFile);
ARCH_AXIOM(ArchGetFileLength(firstName.c_str()) == strlen(testContent));

// Open a file, check that the file path from FILE* handle is matched.
ARCH_AXIOM((firstFile = ArchOpenFile(firstName.c_str(), "rb")) != NULL);
std::string filePath = ArchGetFileName(firstFile);
ARCH_AXIOM(ArchNormPath(filePath) == ArchNormPath(firstName));
fclose(firstFile);

// Map the file and assert the bytes are what we expect they are.
ARCH_AXIOM((firstFile = ArchOpenFile(firstName.c_str(), "rb")) != NULL);
ArchConstFileMapping cfm = ArchMapFileReadOnly(firstFile);
Expand Down