Skip to content

Commit 1dbd59d

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. Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 514aca2 commit 1dbd59d

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

compat/mingw.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,24 @@ int mingw_chdir(const char *dirname)
657657
wchar_t wdirname[MAX_LONG_PATH];
658658
if (xutftowcs_long_path(wdirname, dirname) < 0)
659659
return -1;
660-
result = _wchdir(wdirname);
660+
661+
if (has_symlinks) {
662+
HANDLE hnd = CreateFileW(wdirname, 0,
663+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
664+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
665+
if (hnd == INVALID_HANDLE_VALUE) {
666+
errno = err_win_to_posix(GetLastError());
667+
return -1;
668+
}
669+
if (!GetFinalPathNameByHandleW(hnd, wdirname, ARRAY_SIZE(wdirname), 0)) {
670+
errno = err_win_to_posix(GetLastError());
671+
CloseHandle(hnd);
672+
return -1;
673+
}
674+
CloseHandle(hnd);
675+
}
676+
677+
result = _wchdir(normalize_ntpath(wdirname));
661678
current_directory_len = GetCurrentDirectoryW(0, NULL);
662679
return result;
663680
}

0 commit comments

Comments
 (0)