-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Improve ArchGetFileName for windows to return full path
#3361
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -532,24 +532,34 @@ 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) | ||
| { | ||
| // It needs to strip the path prefix as | ||
| // the path returned is DOS device path, and the | ||
| // syntax is one of: | ||
| // \\.\C:\Test\Foo.txt | ||
| // \\?\C:\Test\Foo.txt | ||
|
||
| if (result.compare(0, 4, "\\\\?\\") == 0 || | ||
| result.compare(0, 4, "\\\\.\\") == 0) | ||
| { | ||
| result.erase(0, 4); | ||
| } | ||
|
||
| } | ||
| } | ||
| free(fileNameInfo); | ||
| return result; | ||
| #else | ||
| #error Unknown system architecture | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs aren't super clear to me, but I think using MAX_PATH for the buffer size seems like it could be insufficient for Windows configurations where long paths are enabled.
GetFilePathNameByHandleW is supposed to return the required buffer size in the case where the filename would overflow the given buffer – it seems like we should be handling that somehow.
from @sunyab
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improved.