Skip to content

Commit cfe1b4f

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 a6b6059 commit cfe1b4f

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

0 commit comments

Comments
 (0)