Skip to content

Commit 72648c6

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 15741c8 commit 72648c6

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
@@ -229,6 +229,34 @@ static int retry_ask_yes_no(int *tries, const char *format, ...)
229229
return result;
230230
}
231231

232+
/* Normalizes NT paths as returned by some low-level APIs. */
233+
static wchar_t *normalize_ntpath(wchar_t *wbuf)
234+
{
235+
int i;
236+
/* fix absolute path prefixes */
237+
if (wbuf[0] == '\\') {
238+
/* strip NT namespace prefixes */
239+
if (!wcsncmp(wbuf, L"\\??\\", 4) ||
240+
!wcsncmp(wbuf, L"\\\\?\\", 4))
241+
wbuf += 4;
242+
else if (!wcsnicmp(wbuf, L"\\DosDevices\\", 12))
243+
wbuf += 12;
244+
/* replace remaining '...UNC\' with '\\' */
245+
if (!wcsnicmp(wbuf, L"UNC\\", 4)) {
246+
wbuf += 2;
247+
*wbuf = '\\';
248+
}
249+
}
250+
/* convert backslashes to slashes */
251+
for (i = 0; wbuf[i]; i++)
252+
if (wbuf[i] == '\\')
253+
wbuf[i] = '/';
254+
/* remove potential trailing slashes */
255+
while (i && wbuf[i - 1] == '/')
256+
wbuf[--i] = 0;
257+
return wbuf;
258+
}
259+
232260
int mingw_unlink(const char *pathname)
233261
{
234262
int tries = 0;
@@ -519,10 +547,29 @@ static int current_directory_len = 0;
519547
int mingw_chdir(const char *dirname)
520548
{
521549
int result;
550+
DECLARE_PROC_ADDR(kernel32.dll, DWORD, GetFinalPathNameByHandleW,
551+
HANDLE, LPWSTR, DWORD, DWORD);
522552
wchar_t wdirname[MAX_LONG_PATH];
523553
if (xutftowcs_long_path(wdirname, dirname) < 0)
524554
return -1;
525-
result = _wchdir(wdirname);
555+
556+
if (has_symlinks && INIT_PROC_ADDR(GetFinalPathNameByHandleW)) {
557+
HANDLE hnd = CreateFileW(wdirname, 0,
558+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
559+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
560+
if (hnd == INVALID_HANDLE_VALUE) {
561+
errno = err_win_to_posix(GetLastError());
562+
return -1;
563+
}
564+
if (!GetFinalPathNameByHandleW(hnd, wdirname, ARRAY_SIZE(wdirname), 0)) {
565+
errno = err_win_to_posix(GetLastError());
566+
CloseHandle(hnd);
567+
return -1;
568+
}
569+
CloseHandle(hnd);
570+
}
571+
572+
result = _wchdir(normalize_ntpath(wdirname));
526573
current_directory_len = GetCurrentDirectoryW(0, NULL);
527574
return result;
528575
}

0 commit comments

Comments
 (0)