Skip to content

Commit 8afef17

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 2249a76 commit 8afef17

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

compat/mingw.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,18 +1260,16 @@ char *mingw_getcwd(char *pointer, int len)
12601260
{
12611261
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
12621262
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1263+
HANDLE hnd;
12631264

12641265
if (!ret || ret >= ARRAY_SIZE(cwd)) {
12651266
errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
12661267
return NULL;
12671268
}
1268-
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
1269-
if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
1270-
HANDLE hnd = CreateFileW(cwd, 0,
1271-
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1272-
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1273-
if (hnd == INVALID_HANDLE_VALUE)
1274-
return NULL;
1269+
hnd = CreateFileW(cwd, 0,
1270+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1271+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1272+
if (hnd != INVALID_HANDLE_VALUE) {
12751273
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
12761274
CloseHandle(hnd);
12771275
if (!ret || ret >= ARRAY_SIZE(wpointer))
@@ -1280,9 +1278,7 @@ char *mingw_getcwd(char *pointer, int len)
12801278
return NULL;
12811279
return pointer;
12821280
}
1283-
if (!ret || ret >= ARRAY_SIZE(wpointer))
1284-
return NULL;
1285-
if (xwcstoutf(pointer, wpointer, len) < 0)
1281+
if (xwcstoutf(pointer, cwd, len) < 0)
12861282
return NULL;
12871283
convert_slashes(pointer);
12881284
return pointer;

0 commit comments

Comments
 (0)