Skip to content

Commit 81f9b7b

Browse files
committed
mingw: do resolve symlinks in getcwd()
As pointed out in #1676, the `git rev-parse --is-inside-work-tree` command currently fails when the current directory's path contains symbolic links. The underlying reason for this bug is that `getcwd()` is supposed to resolve symbolic links, but our `mingw_getcwd()` implementation did not. We do have all the building blocks for that, though: the `GetFinalPathByHandleW()` function will resolve symbolic links. However, we only called that function if `GetLongPathNameW()` failed, for historical reasons: the latter function was supported for a long time, but the former API function was introduced only with Windows Vista, and we used to support also Windows XP. With that support having been dropped, we are free to call the symbolic link-resolving function right away. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent fd2ade2 commit 81f9b7b

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

compat/mingw.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,18 +1129,16 @@ char *mingw_getcwd(char *pointer, int len)
11291129
{
11301130
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
11311131
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1132+
HANDLE hnd;
11321133

11331134
if (!ret || ret >= ARRAY_SIZE(cwd)) {
11341135
errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
11351136
return NULL;
11361137
}
1137-
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
1138-
if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
1139-
HANDLE hnd = CreateFileW(cwd, 0,
1140-
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1141-
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1142-
if (hnd == INVALID_HANDLE_VALUE)
1143-
return NULL;
1138+
hnd = CreateFileW(cwd, 0,
1139+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1140+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1141+
if (hnd != INVALID_HANDLE_VALUE) {
11441142
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
11451143
CloseHandle(hnd);
11461144
if (!ret || ret >= ARRAY_SIZE(wpointer))
@@ -1149,13 +1147,11 @@ char *mingw_getcwd(char *pointer, int len)
11491147
return NULL;
11501148
return pointer;
11511149
}
1152-
if (!ret || ret >= ARRAY_SIZE(wpointer))
1153-
return NULL;
1154-
if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) {
1150+
if (GetFileAttributesW(cwd) == INVALID_FILE_ATTRIBUTES) {
11551151
errno = ENOENT;
11561152
return NULL;
11571153
}
1158-
if (xwcstoutf(pointer, wpointer, len) < 0)
1154+
if (xwcstoutf(pointer, cwd, len) < 0)
11591155
return NULL;
11601156
convert_slashes(pointer);
11611157
return pointer;

0 commit comments

Comments
 (0)