Skip to content

Commit 4b43330

Browse files
kbleesGit for Windows Build Agent
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 4c2333f commit 4b43330

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
@@ -924,7 +924,24 @@ int mingw_chdir(const char *dirname)
924924
wchar_t wdirname[MAX_LONG_PATH];
925925
if (xutftowcs_long_path(wdirname, dirname) < 0)
926926
return -1;
927-
result = _wchdir(wdirname);
927+
928+
if (has_symlinks) {
929+
HANDLE hnd = CreateFileW(wdirname, 0,
930+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
931+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
932+
if (hnd == INVALID_HANDLE_VALUE) {
933+
errno = err_win_to_posix(GetLastError());
934+
return -1;
935+
}
936+
if (!GetFinalPathNameByHandleW(hnd, wdirname, ARRAY_SIZE(wdirname), 0)) {
937+
errno = err_win_to_posix(GetLastError());
938+
CloseHandle(hnd);
939+
return -1;
940+
}
941+
CloseHandle(hnd);
942+
}
943+
944+
result = _wchdir(normalize_ntpath(wdirname));
928945
current_directory_len = GetCurrentDirectoryW(0, NULL);
929946
return result;
930947
}

0 commit comments

Comments
 (0)