Skip to content

Commit a8a1ec7

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 49d7abf commit a8a1ec7

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
@@ -726,7 +726,24 @@ int mingw_chdir(const char *dirname)
726726
wchar_t wdirname[MAX_LONG_PATH];
727727
if (xutftowcs_long_path(wdirname, dirname) < 0)
728728
return -1;
729-
result = _wchdir(wdirname);
729+
730+
if (has_symlinks) {
731+
HANDLE hnd = CreateFileW(wdirname, 0,
732+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
733+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
734+
if (hnd == INVALID_HANDLE_VALUE) {
735+
errno = err_win_to_posix(GetLastError());
736+
return -1;
737+
}
738+
if (!GetFinalPathNameByHandleW(hnd, wdirname, ARRAY_SIZE(wdirname), 0)) {
739+
errno = err_win_to_posix(GetLastError());
740+
CloseHandle(hnd);
741+
return -1;
742+
}
743+
CloseHandle(hnd);
744+
}
745+
746+
result = _wchdir(normalize_ntpath(wdirname));
730747
current_directory_len = GetCurrentDirectoryW(0, NULL);
731748
return result;
732749
}

0 commit comments

Comments
 (0)