Skip to content

Commit 1ad9b7f

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 bae32fd + 2ca0854 commit 1ad9b7f

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
@@ -1225,18 +1225,16 @@ char *mingw_getcwd(char *pointer, int len)
12251225
{
12261226
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
12271227
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1228+
HANDLE hnd;
12281229

12291230
if (!ret || ret >= ARRAY_SIZE(cwd)) {
12301231
errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
12311232
return NULL;
12321233
}
1233-
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
1234-
if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
1235-
HANDLE hnd = CreateFileW(cwd, 0,
1236-
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1237-
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1238-
if (hnd == INVALID_HANDLE_VALUE)
1239-
return NULL;
1234+
hnd = CreateFileW(cwd, 0,
1235+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1236+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1237+
if (hnd != INVALID_HANDLE_VALUE) {
12401238
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
12411239
CloseHandle(hnd);
12421240
if (!ret || ret >= ARRAY_SIZE(wpointer))
@@ -1245,13 +1243,11 @@ char *mingw_getcwd(char *pointer, int len)
12451243
return NULL;
12461244
return pointer;
12471245
}
1248-
if (!ret || ret >= ARRAY_SIZE(wpointer))
1249-
return NULL;
1250-
if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) {
1246+
if (GetFileAttributesW(cwd) == INVALID_FILE_ATTRIBUTES) {
12511247
errno = ENOENT;
12521248
return NULL;
12531249
}
1254-
if (xwcstoutf(pointer, wpointer, len) < 0)
1250+
if (xwcstoutf(pointer, cwd, len) < 0)
12551251
return NULL;
12561252
convert_slashes(pointer);
12571253
return pointer;

0 commit comments

Comments
 (0)