Skip to content

Commit 87aca61

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 <johannes.schindelin@gmx.de>
1 parent c6ca5c1 commit 87aca61

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

Diff for: compat/mingw.c

+7-11
Original file line numberDiff line numberDiff line change
@@ -1211,18 +1211,16 @@ char *mingw_getcwd(char *pointer, int len)
12111211
{
12121212
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
12131213
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1214+
HANDLE hnd;
12141215

12151216
if (!ret || ret >= ARRAY_SIZE(cwd)) {
12161217
errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
12171218
return NULL;
12181219
}
1219-
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
1220-
if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
1221-
HANDLE hnd = CreateFileW(cwd, 0,
1222-
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1223-
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1224-
if (hnd == INVALID_HANDLE_VALUE)
1225-
return NULL;
1220+
hnd = CreateFileW(cwd, 0,
1221+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1222+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1223+
if (hnd != INVALID_HANDLE_VALUE) {
12261224
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
12271225
CloseHandle(hnd);
12281226
if (!ret || ret >= ARRAY_SIZE(wpointer))
@@ -1231,13 +1229,11 @@ char *mingw_getcwd(char *pointer, int len)
12311229
return NULL;
12321230
return pointer;
12331231
}
1234-
if (!ret || ret >= ARRAY_SIZE(wpointer))
1235-
return NULL;
1236-
if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) {
1232+
if (GetFileAttributesW(cwd) == INVALID_FILE_ATTRIBUTES) {
12371233
errno = ENOENT;
12381234
return NULL;
12391235
}
1240-
if (xwcstoutf(pointer, wpointer, len) < 0)
1236+
if (xwcstoutf(pointer, cwd, len) < 0)
12411237
return NULL;
12421238
convert_slashes(pointer);
12431239
return pointer;

0 commit comments

Comments
 (0)