Skip to content

Commit e23c903

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 <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent ea84594 commit e23c903

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

compat/mingw.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,24 @@ int mingw_chdir(const char *dirname)
909909
wchar_t wdirname[MAX_LONG_PATH];
910910
if (xutftowcs_long_path(wdirname, dirname) < 0)
911911
return -1;
912-
result = _wchdir(wdirname);
912+
913+
if (has_symlinks) {
914+
HANDLE hnd = CreateFileW(wdirname, 0,
915+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
916+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
917+
if (hnd == INVALID_HANDLE_VALUE) {
918+
errno = err_win_to_posix(GetLastError());
919+
return -1;
920+
}
921+
if (!GetFinalPathNameByHandleW(hnd, wdirname, ARRAY_SIZE(wdirname), 0)) {
922+
errno = err_win_to_posix(GetLastError());
923+
CloseHandle(hnd);
924+
return -1;
925+
}
926+
CloseHandle(hnd);
927+
}
928+
929+
result = _wchdir(normalize_ntpath(wdirname));
913930
current_directory_len = GetCurrentDirectoryW(0, NULL);
914931
return result;
915932
}

0 commit comments

Comments
 (0)