On Thursday 15 January 2026 11:21:05 LIU Hao wrote:
> 在 2025-12-20 00:38, Pali Rohár 写道:
> > msvcrt _fstat function is broken and for directory fd it returns S_ISREG
> > type. 32-bit pre-msvcr110 _fstat does not properly signal size overflow.
> > UCRT seems to work correctly.
> >
> > Add mingw-w64 fstat wrappers around msvcrt _fstat functions which change
> > S_IFMT to S_IFDIR if winapi filehandle has FILE_ATTRIBUTE_DIRECTORY
> > attribute set. Provide wrapper for all 4 fstat size_t/time_t variants.
> > For 32-bit pre-msvcr110 builds, adds similar workaround like for existing
> > mingw-w64 stat functions.
> >
>
> How would you obtain an FD of a directory? `_open("C:/Windows", _O_RDONLY)`
> fails with EPERM.
I know two options:
- for msvcr110, msvcr120 and UCRT builds via _open() with _O_OBTAIN_DIR flag:
https://godbolt.org/z/9fon7zY1q
- for any build via WinAPI CreateFile() with FILE_FLAG_BACKUP_SEMANTICS flag
followed by the CRT _open_osfhandle() call:
https://godbolt.org/z/b68rnssYP
Seems that mingw-w64 does not define _O_OBTAIN_DIR flag in header files.
But as can be seen on the godbolt, it works with msvc. Second option
works also with mingw-w64.
_O_OBTAIN_DIR is not mentioned in the _open documentation:
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/open-wopen
But FILE_FLAG_BACKUP_SEMANTICS is documented as a way how to obtain a handle to
a directory:
https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public