Skip to content

Commit 5ef2049

Browse files
committed
Fix: capture Windows errors in call_fstatat
1 parent 6be62fd commit 5ef2049

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

  • graalpython/python-libposix/src

‎graalpython/python-libposix/src/posix.c‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,41 @@ static void stat_path_handle_to_longs(const wchar_t *path, int32_t followSymlink
799799
}
800800
}
801801

802+
static void stat_attributes_to_longs(DWORD attributes, DWORD sizeHigh, DWORD sizeLow, FILETIME accessTime, FILETIME writeTime, FILETIME creationTime, int64_t *out) {
803+
memset(out, 0, 13 * sizeof(*out));
804+
if (attributes & FILE_ATTRIBUTE_DIRECTORY) {
805+
out[0] = _S_IFDIR | 0111;
806+
} else {
807+
out[0] = _S_IFREG;
808+
}
809+
out[0] |= attributes & FILE_ATTRIBUTE_READONLY ? 0444 : 0666;
810+
out[3] = 1;
811+
out[6] = ((int64_t) sizeHigh << 32) | sizeLow;
812+
out[7] = filetime_to_unix_time(accessTime);
813+
out[8] = filetime_to_unix_time(writeTime);
814+
out[9] = filetime_to_unix_time(creationTime);
815+
}
816+
817+
static int stat_path_attributes_to_longs(const wchar_t *path, int64_t *out) {
818+
WIN32_FILE_ATTRIBUTE_DATA info;
819+
if (GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
820+
stat_attributes_to_longs(info.dwFileAttributes, info.nFileSizeHigh, info.nFileSizeLow,
821+
info.ftLastAccessTime, info.ftLastWriteTime, info.ftCreationTime, out);
822+
return 0;
823+
}
824+
825+
WIN32_FIND_DATAW findInfo;
826+
HANDLE findHandle = FindFirstFileW(path, &findInfo);
827+
if (findHandle == INVALID_HANDLE_VALUE) {
828+
set_win_errno(GetLastError());
829+
return -1;
830+
}
831+
stat_attributes_to_longs(findInfo.dwFileAttributes, findInfo.nFileSizeHigh, findInfo.nFileSizeLow,
832+
findInfo.ftLastAccessTime, findInfo.ftLastWriteTime, findInfo.ftCreationTime, out);
833+
FindClose(findHandle);
834+
return 0;
835+
}
836+
802837
GP_EXPORT int64_t call_getpid(void) {
803838
return _getpid();
804839
}
@@ -1103,7 +1138,6 @@ GP_EXPORT int32_t get_terminal_size(int32_t fd, int32_t *size) {
11031138
}
11041139

11051140
GP_EXPORT int32_t call_fstatat(int32_t dirFd, const char *path, int32_t followSymlinks, int64_t *out) {
1106-
(void) followSymlinks;
11071141
if (!is_default_dir_fd(dirFd)) {
11081142
return unsupported();
11091143
}
@@ -1116,9 +1150,10 @@ GP_EXPORT int32_t call_fstatat(int32_t dirFd, const char *path, int32_t followSy
11161150
if (result == 0) {
11171151
stat_struct_to_longs(&st, out);
11181152
stat_path_handle_to_longs(wide_path, followSymlinks, out);
1119-
} else {
1120-
capture_errno();
1153+
free(wide_path);
1154+
return 0;
11211155
}
1156+
result = stat_path_attributes_to_longs(wide_path, out);
11221157
free(wide_path);
11231158
return result;
11241159
}

0 commit comments

Comments
 (0)