Skip to content

Commit 1aaa22f

Browse files
kbleesdscho
authored andcommitted
Win32: mingw_chdir: change to symlink-resolved directory
If symlinks are enabled, resolve all symlinks when changing directories, as required by POSIX. Note: Git's real_path() function bases its link resolution algorithm on this property of chdir(). Unfortunately, the current directory on Windows is limited to only MAX_PATH (260) characters. Therefore using symlinks and long paths in combination may be problematic. Note: GetFinalPathNameByHandleW() was introduced with symlink support in Windows Vista. Thus, for compatibility with Windows XP, we need to load it dynamically and behave gracefully if it isnt's available. Signed-off-by: Karsten Blees <[email protected]>
1 parent 5f67ac2 commit 1aaa22f

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

compat/mingw.c

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,34 @@ int mingw_core_config(const char *var, const char *value, void *cb)
269269
return 0;
270270
}
271271

272+
/* Normalizes NT paths as returned by some low-level APIs. */
273+
static wchar_t *normalize_ntpath(wchar_t *wbuf)
274+
{
275+
int i;
276+
/* fix absolute path prefixes */
277+
if (wbuf[0] == '\\') {
278+
/* strip NT namespace prefixes */
279+
if (!wcsncmp(wbuf, L"\\??\\", 4) ||
280+
!wcsncmp(wbuf, L"\\\\?\\", 4))
281+
wbuf += 4;
282+
else if (!wcsnicmp(wbuf, L"\\DosDevices\\", 12))
283+
wbuf += 12;
284+
/* replace remaining '...UNC\' with '\\' */
285+
if (!wcsnicmp(wbuf, L"UNC\\", 4)) {
286+
wbuf += 2;
287+
*wbuf = '\\';
288+
}
289+
}
290+
/* convert backslashes to slashes */
291+
for (i = 0; wbuf[i]; i++)
292+
if (wbuf[i] == '\\')
293+
wbuf[i] = '/';
294+
/* remove potential trailing slashes */
295+
while (i && wbuf[i - 1] == '/')
296+
wbuf[--i] = 0;
297+
return wbuf;
298+
}
299+
272300
int mingw_unlink(const char *pathname)
273301
{
274302
int tries = 0;
@@ -561,10 +589,29 @@ static int current_directory_len = 0;
561589
int mingw_chdir(const char *dirname)
562590
{
563591
int result;
592+
DECLARE_PROC_ADDR(kernel32.dll, DWORD, GetFinalPathNameByHandleW,
593+
HANDLE, LPWSTR, DWORD, DWORD);
564594
wchar_t wdirname[MAX_LONG_PATH];
565595
if (xutftowcs_long_path(wdirname, dirname) < 0)
566596
return -1;
567-
result = _wchdir(wdirname);
597+
598+
if (has_symlinks && INIT_PROC_ADDR(GetFinalPathNameByHandleW)) {
599+
HANDLE hnd = CreateFileW(wdirname, 0,
600+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
601+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
602+
if (hnd == INVALID_HANDLE_VALUE) {
603+
errno = err_win_to_posix(GetLastError());
604+
return -1;
605+
}
606+
if (!GetFinalPathNameByHandleW(hnd, wdirname, ARRAY_SIZE(wdirname), 0)) {
607+
errno = err_win_to_posix(GetLastError());
608+
CloseHandle(hnd);
609+
return -1;
610+
}
611+
CloseHandle(hnd);
612+
}
613+
614+
result = _wchdir(normalize_ntpath(wdirname));
568615
current_directory_len = GetCurrentDirectoryW(0, NULL);
569616
return result;
570617
}

0 commit comments

Comments
 (0)