Skip to content

Commit a41b148

Browse files
committed
Merge pull request #606 from skvoboo/issue_598
Use GetFinalPathNameByHandleW instead of GetLongPathNameW to get full path
2 parents 78ce6bc + 3700315 commit a41b148

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

compat/mingw.c

+17
Original file line numberDiff line numberDiff line change
@@ -952,11 +952,28 @@ char *mingw_getcwd(char *pointer, int len)
952952
{
953953
int i;
954954
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
955+
DECLARE_PROC_ADDR(kernel32.dll, DWORD, GetFinalPathNameByHandleW,
956+
HANDLE, LPWSTR, DWORD, DWORD);
955957
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
956958

957959
if (ret < 0 || ret >= ARRAY_SIZE(cwd))
958960
return NULL;
959961
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
962+
if (!ret && GetLastError() == ERROR_ACCESS_DENIED &&
963+
INIT_PROC_ADDR(GetFinalPathNameByHandleW)) {
964+
HANDLE hnd = CreateFileW(cwd, 0,
965+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
966+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
967+
if (hnd == INVALID_HANDLE_VALUE)
968+
return NULL;
969+
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
970+
CloseHandle(hnd);
971+
if (!ret || ret >= ARRAY_SIZE(wpointer))
972+
return NULL;
973+
if (xwcstoutf(pointer, normalize_ntpath(wpointer), len) < 0)
974+
return NULL;
975+
return pointer;
976+
}
960977
if (!ret || ret >= ARRAY_SIZE(wpointer))
961978
return NULL;
962979
if (xwcstoutf(pointer, wpointer, len) < 0)

0 commit comments

Comments
 (0)