Skip to content

Commit dfdbfc5

Browse files
miss-islingtonaisk
andauthored
gh-111856: Fix os.fstat on windows with FAT32 and exFAT filesystem (GH-112038)
(cherry picked from commit 29af736) Co-authored-by: AN Long <[email protected]>
1 parent c6aea46 commit dfdbfc5

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixes :func:`~os.fstat` on file systems that do not support file ID
2+
requests. This includes FAT32 and exFAT.

Python/fileutils.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,7 @@ _Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
12361236
BY_HANDLE_FILE_INFORMATION info;
12371237
FILE_BASIC_INFO basicInfo;
12381238
FILE_ID_INFO idInfo;
1239+
FILE_ID_INFO *pIdInfo = &idInfo;
12391240
HANDLE h;
12401241
int type;
12411242

@@ -1268,15 +1269,19 @@ _Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
12681269
}
12691270

12701271
if (!GetFileInformationByHandle(h, &info) ||
1271-
!GetFileInformationByHandleEx(h, FileBasicInfo, &basicInfo, sizeof(basicInfo)) ||
1272-
!GetFileInformationByHandleEx(h, FileIdInfo, &idInfo, sizeof(idInfo))) {
1272+
!GetFileInformationByHandleEx(h, FileBasicInfo, &basicInfo, sizeof(basicInfo))) {
12731273
/* The Win32 error is already set, but we also set errno for
12741274
callers who expect it */
12751275
errno = winerror_to_errno(GetLastError());
12761276
return -1;
12771277
}
12781278

1279-
_Py_attribute_data_to_stat(&info, 0, &basicInfo, &idInfo, status);
1279+
if (!GetFileInformationByHandleEx(h, FileIdInfo, &idInfo, sizeof(idInfo))) {
1280+
/* Failed to get FileIdInfo, so do not pass it along */
1281+
pIdInfo = NULL;
1282+
}
1283+
1284+
_Py_attribute_data_to_stat(&info, 0, &basicInfo, pIdInfo, status);
12801285
return 0;
12811286
#else
12821287
return fstat(fd, status);

0 commit comments

Comments
 (0)