Skip to content

Commit 11901a1

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 edf2f54 commit 11901a1

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;
@@ -482,11 +510,30 @@ static int current_directory_len = 0;
482510
int mingw_chdir(const char *dirname)
483511
{
484512
int result;
513+
DECLARE_PROC_ADDR(kernel32.dll, DWORD, GetFinalPathNameByHandleW,
514+
HANDLE, LPWSTR, DWORD, DWORD);
485515
wchar_t wdirname[MAX_PATH];
486516
/* SetCurrentDirectoryW doesn't support long paths */
487517
if (xutftowcs_path(wdirname, dirname) < 0)
488518
return -1;
489-
result = _wchdir(wdirname);
519+
520+
if (has_symlinks && INIT_PROC_ADDR(GetFinalPathNameByHandleW)) {
521+
HANDLE hnd = CreateFileW(wdirname, 0,
522+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
523+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
524+
if (hnd == INVALID_HANDLE_VALUE) {
525+
errno = err_win_to_posix(GetLastError());
526+
return -1;
527+
}
528+
if (!GetFinalPathNameByHandleW(hnd, wdirname, ARRAY_SIZE(wdirname), 0)) {
529+
errno = err_win_to_posix(GetLastError());
530+
CloseHandle(hnd);
531+
return -1;
532+
}
533+
CloseHandle(hnd);
534+
}
535+
536+
result = _wchdir(normalize_ntpath(wdirname));
490537
current_directory_len = GetCurrentDirectoryW(0, NULL);
491538
return result;
492539
}

0 commit comments

Comments
 (0)