Skip to content

Commit e8565ba

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 187653a commit e8565ba

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
@@ -830,7 +830,24 @@ int mingw_chdir(const char *dirname)
830830
wchar_t wdirname[MAX_LONG_PATH];
831831
if (xutftowcs_long_path(wdirname, dirname) < 0)
832832
return -1;
833-
result = _wchdir(wdirname);
833+
834+
if (has_symlinks) {
835+
HANDLE hnd = CreateFileW(wdirname, 0,
836+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
837+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
838+
if (hnd == INVALID_HANDLE_VALUE) {
839+
errno = err_win_to_posix(GetLastError());
840+
return -1;
841+
}
842+
if (!GetFinalPathNameByHandleW(hnd, wdirname, ARRAY_SIZE(wdirname), 0)) {
843+
errno = err_win_to_posix(GetLastError());
844+
CloseHandle(hnd);
845+
return -1;
846+
}
847+
CloseHandle(hnd);
848+
}
849+
850+
result = _wchdir(normalize_ntpath(wdirname));
834851
current_directory_len = GetCurrentDirectoryW(0, NULL);
835852
return result;
836853
}

0 commit comments

Comments
 (0)