Skip to content

Commit 4d1e17c

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 210defc commit 4d1e17c

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
@@ -898,7 +898,24 @@ int mingw_chdir(const char *dirname)
898898
wchar_t wdirname[MAX_LONG_PATH];
899899
if (xutftowcs_long_path(wdirname, dirname) < 0)
900900
return -1;
901-
result = _wchdir(wdirname);
901+
902+
if (has_symlinks) {
903+
HANDLE hnd = CreateFileW(wdirname, 0,
904+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
905+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
906+
if (hnd == INVALID_HANDLE_VALUE) {
907+
errno = err_win_to_posix(GetLastError());
908+
return -1;
909+
}
910+
if (!GetFinalPathNameByHandleW(hnd, wdirname, ARRAY_SIZE(wdirname), 0)) {
911+
errno = err_win_to_posix(GetLastError());
912+
CloseHandle(hnd);
913+
return -1;
914+
}
915+
CloseHandle(hnd);
916+
}
917+
918+
result = _wchdir(normalize_ntpath(wdirname));
902919
current_directory_len = GetCurrentDirectoryW(0, NULL);
903920
return result;
904921
}

0 commit comments

Comments
 (0)