Skip to content

Commit 0b77572

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 3088b34 commit 0b77572

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
@@ -1142,18 +1142,16 @@ char *mingw_getcwd(char *pointer, int len)
11421142
{
11431143
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
11441144
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1145+
HANDLE hnd;
11451146

11461147
if (!ret || ret >= ARRAY_SIZE(cwd)) {
11471148
errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
11481149
return NULL;
11491150
}
1150-
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
1151-
if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
1152-
HANDLE hnd = CreateFileW(cwd, 0,
1153-
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1154-
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1155-
if (hnd == INVALID_HANDLE_VALUE)
1156-
return NULL;
1151+
hnd = CreateFileW(cwd, 0,
1152+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1153+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1154+
if (hnd != INVALID_HANDLE_VALUE) {
11571155
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
11581156
CloseHandle(hnd);
11591157
if (!ret || ret >= ARRAY_SIZE(wpointer))
@@ -1162,13 +1160,11 @@ char *mingw_getcwd(char *pointer, int len)
11621160
return NULL;
11631161
return pointer;
11641162
}
1165-
if (!ret || ret >= ARRAY_SIZE(wpointer))
1166-
return NULL;
1167-
if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) {
1163+
if (GetFileAttributesW(cwd) == INVALID_FILE_ATTRIBUTES) {
11681164
errno = ENOENT;
11691165
return NULL;
11701166
}
1171-
if (xwcstoutf(pointer, wpointer, len) < 0)
1167+
if (xwcstoutf(pointer, cwd, len) < 0)
11721168
return NULL;
11731169
convert_slashes(pointer);
11741170
return pointer;

0 commit comments

Comments
 (0)