Skip to content

Commit cfd9f53

Browse files
dschoGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
Merge pull request #2449 from dscho/mingw-getcwd-and-symlinks
Do resolve symlinks in `getcwd()`
2 parents 2f34509 + 4ec60b2 commit cfd9f53

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

compat/mingw.c

+7-11
Original file line numberDiff line numberDiff line change
@@ -1242,18 +1242,16 @@ char *mingw_getcwd(char *pointer, int len)
12421242
{
12431243
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
12441244
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1245+
HANDLE hnd;
12451246

12461247
if (!ret || ret >= ARRAY_SIZE(cwd)) {
12471248
errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
12481249
return NULL;
12491250
}
1250-
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
1251-
if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
1252-
HANDLE hnd = CreateFileW(cwd, 0,
1253-
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1254-
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1255-
if (hnd == INVALID_HANDLE_VALUE)
1256-
return NULL;
1251+
hnd = CreateFileW(cwd, 0,
1252+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1253+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1254+
if (hnd != INVALID_HANDLE_VALUE) {
12571255
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
12581256
CloseHandle(hnd);
12591257
if (!ret || ret >= ARRAY_SIZE(wpointer))
@@ -1262,13 +1260,11 @@ char *mingw_getcwd(char *pointer, int len)
12621260
return NULL;
12631261
return pointer;
12641262
}
1265-
if (!ret || ret >= ARRAY_SIZE(wpointer))
1266-
return NULL;
1267-
if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) {
1263+
if (GetFileAttributesW(cwd) == INVALID_FILE_ATTRIBUTES) {
12681264
errno = ENOENT;
12691265
return NULL;
12701266
}
1271-
if (xwcstoutf(pointer, wpointer, len) < 0)
1267+
if (xwcstoutf(pointer, cwd, len) < 0)
12721268
return NULL;
12731269
convert_slashes(pointer);
12741270
return pointer;

0 commit comments

Comments
 (0)