Skip to content

Commit 4745fee

Browse files
skvoboogitster
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 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]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 937974f commit 4745fee

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

compat/mingw.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,31 @@ static int ask_yes_no_if_possible(const char *format, ...)
202202
}
203203
}
204204

205+
/* Normalizes NT paths as returned by some low-level APIs. */
206+
static wchar_t *normalize_ntpath(wchar_t *wbuf)
207+
{
208+
int i;
209+
/* fix absolute path prefixes */
210+
if (wbuf[0] == '\\') {
211+
/* strip NT namespace prefixes */
212+
if (!wcsncmp(wbuf, L"\\??\\", 4) ||
213+
!wcsncmp(wbuf, L"\\\\?\\", 4))
214+
wbuf += 4;
215+
else if (!wcsnicmp(wbuf, L"\\DosDevices\\", 12))
216+
wbuf += 12;
217+
/* replace remaining '...UNC\' with '\\' */
218+
if (!wcsnicmp(wbuf, L"UNC\\", 4)) {
219+
wbuf += 2;
220+
*wbuf = '\\';
221+
}
222+
}
223+
/* convert backslashes to slashes */
224+
for (i = 0; wbuf[i]; i++)
225+
if (wbuf[i] == '\\')
226+
wbuf[i] = '/';
227+
return wbuf;
228+
}
229+
205230
int mingw_unlink(const char *pathname)
206231
{
207232
int ret, tries = 0;
@@ -925,6 +950,20 @@ char *mingw_getcwd(char *pointer, int len)
925950
return NULL;
926951
}
927952
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
953+
if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
954+
HANDLE hnd = CreateFileW(cwd, 0,
955+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
956+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
957+
if (hnd == INVALID_HANDLE_VALUE)
958+
return NULL;
959+
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
960+
CloseHandle(hnd);
961+
if (!ret || ret >= ARRAY_SIZE(wpointer))
962+
return NULL;
963+
if (xwcstoutf(pointer, normalize_ntpath(wpointer), len) < 0)
964+
return NULL;
965+
return pointer;
966+
}
928967
if (!ret || ret >= ARRAY_SIZE(wpointer))
929968
return NULL;
930969
if (xwcstoutf(pointer, wpointer, len) < 0)

0 commit comments

Comments
 (0)