Skip to content

Commit a3d2459

Browse files
dschoGit for Windows Build Agent
authored andcommitted
Merge pull request #2449 from dscho/mingw-getcwd-and-symlinks
Do resolve symlinks in `getcwd()`
2 parents 65a41de + 3eb9ba5 commit a3d2459

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
@@ -1245,18 +1245,16 @@ char *mingw_getcwd(char *pointer, int len)
12451245
{
12461246
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
12471247
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1248+
HANDLE hnd;
12481249

12491250
if (!ret || ret >= ARRAY_SIZE(cwd)) {
12501251
errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
12511252
return NULL;
12521253
}
1253-
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
1254-
if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
1255-
HANDLE hnd = CreateFileW(cwd, 0,
1256-
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1257-
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1258-
if (hnd == INVALID_HANDLE_VALUE)
1259-
return NULL;
1254+
hnd = CreateFileW(cwd, 0,
1255+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1256+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1257+
if (hnd != INVALID_HANDLE_VALUE) {
12601258
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
12611259
CloseHandle(hnd);
12621260
if (!ret || ret >= ARRAY_SIZE(wpointer))
@@ -1265,13 +1263,11 @@ char *mingw_getcwd(char *pointer, int len)
12651263
return NULL;
12661264
return pointer;
12671265
}
1268-
if (!ret || ret >= ARRAY_SIZE(wpointer))
1269-
return NULL;
1270-
if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) {
1266+
if (GetFileAttributesW(cwd) == INVALID_FILE_ATTRIBUTES) {
12711267
errno = ENOENT;
12721268
return NULL;
12731269
}
1274-
if (xwcstoutf(pointer, wpointer, len) < 0)
1270+
if (xwcstoutf(pointer, cwd, len) < 0)
12751271
return NULL;
12761272
convert_slashes(pointer);
12771273
return pointer;

0 commit comments

Comments
 (0)