Skip to content

Commit e252dc8

Browse files
skvoboodscho
authored andcommitted
mingw: fix getcwd when the parent directory cannot be queried
`GetLongPathName()` function may fail when it is unable to query the parent directory of a path component to determine the long name for that component. It happens, because of it uses `FindFirstFile()` function for each next short part of path. The `FindFirstFile()` requires `List Directory` and `Synchronize` desired access for a calling process. In case of lacking such permission for some part of path, the `GetLongPathName()` returns 0 as result and `GetLastError()` returns ERROR_ACCESS_DENIED. `GetFinalPathNameByHandle()` function can help in such cases, because it requires `Read Attributes` and `Synchronize` desired access to the target path only. The `GetFinalPathNameByHandle()` function was introduced on `Windows Server 2008/Windows Vista`. So we need to load it dynamically. `CreateFile()` parameters: `lpFileName` = path to the current directory `dwDesiredAccess` = 0 (it means `Read Attributes` and `Synchronize`) `dwShareMode` = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE (it prevents `Sharing Violation`) `lpSecurityAttributes` = NULL (default security attributes) `dwCreationDisposition` = OPEN_EXISTING (required to obtain a directory handle) `dwFlagsAndAttributes` = FILE_FLAG_BACKUP_SEMANTICS (required to obtain a directory handle) `hTemplateFile` = NULL (when opening an existing file or directory, `CreateFile` ignores this parameter) The string that is returned by `GetFinalPathNameByHandle()` function uses the \\?\ syntax. To skip the prefix and convert backslashes to slashes, the `normalize_ntpath()` mingw function will be used. Note: `GetFinalPathNameByHandle()` function returns a final path. It is the path that is returned when a path is fully resolved. For example, for a symbolic link named "C:\tmp\mydir" that points to "D:\yourdir", the final path would be "D:\yourdir". Signed-off-by: Anton Serbulov <[email protected]>
1 parent cf29621 commit e252dc8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

compat/mingw.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,11 +838,28 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
838838
char *mingw_getcwd(char *pointer, int len)
839839
{
840840
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
841+
DECLARE_PROC_ADDR(kernel32.dll, DWORD, GetFinalPathNameByHandleW,
842+
HANDLE, LPWSTR, DWORD, DWORD);
841843
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
842844

843845
if (ret < 0 || ret >= ARRAY_SIZE(cwd))
844846
return NULL;
845847
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
848+
if (!ret && GetLastError() == ERROR_ACCESS_DENIED &&
849+
INIT_PROC_ADDR(GetFinalPathNameByHandleW)) {
850+
HANDLE hnd = CreateFileW(cwd, 0,
851+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
852+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
853+
if (hnd == INVALID_HANDLE_VALUE)
854+
return NULL;
855+
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
856+
CloseHandle(hnd);
857+
if (!ret || ret >= ARRAY_SIZE(wpointer))
858+
return NULL;
859+
if (xwcstoutf(pointer, normalize_ntpath(wpointer), len) < 0)
860+
return NULL;
861+
return pointer;
862+
}
846863
if (!ret || ret >= ARRAY_SIZE(wpointer))
847864
return NULL;
848865
if (xwcstoutf(pointer, wpointer, len) < 0)

0 commit comments

Comments
 (0)