Files
rust/library/std
Jonathan Brouwer d754454416 Rollup merge of #158727 - joboet:more_readdir, r=JohnTitor
std: use `readdir` on nearly all UNIX platforms

POSIX.1-2024 [formalised](https://pubs.opengroup.org/onlinepubs/9799919799/functions/readdir.html) what was already guaranteed by a lot of implementations and required `readdir` to be thread-safe as long as an individual `DIR*` is not accessed concurrently (which `ReadDir::next` ensures by taking a mutable reference). But since our `read_dir` implementation predates that standard, we currently only utilise `readdir` on the platforms that guarantee thread-safety in their documentation. On other implementations – notably macOS – we use the `readdir_r` function, which was always required to be thread-safe but is problematic because it cannot handle directory entries with names longer than `NAME_MAX`.

However, even the first POSIX issue, POSIX.1-1994, specified that the data in the returned `dirent`
> is not overwritten by another call to readdir() on a different directory stream.

and that guarantee together with the requirement that the underlying syscalls need to be thread-safe already because of `readdir_r` result in `readdir` being thread-safe on nearly all implementations, even if they predate POSIX.1-2024. Given the now formalised guarantee I think it safe to assume that currently thread-safe implementations will not be changed in a way that violates thread-safety.

CC T-libs, do you agree?
@rustbot label +I-libs-nominated

I thus looked at the `readdir` implementation of all the UNIXes currently utilising `readdir_r` to check for thread-safety:
- [x] [Apple platforms](8a5571058e/gen.subproj/readdir.c), starting with Mac OS X 10.0. While the original implementation did not use any synchronisation, it only modifies data of the `DIR*` it acts on.
- [x] [Cygwin](854594be78/winsup/cygwin/dir.cc): same thing.
- [x] [Dragonfly](2e3a87ce41/lib/libc/gen/readdir.c) uses locks.
- [x] [L4Re](b6f7495bb8/libc/musl/contrib/musl/src/dirent/readdir.c): only modifies the passed `DIR*`.
- [ ] LynxOS: closed-source, the [documentation](https://www-f9.ijs.si/~rok/detectors/doc/LynxOS-2.5.0/LynxOS_Documentation.html) says that `readdir` is not reentrant. CC @rfatykhov-lynx
- [x] [Managarm](368a00fa3a/options/posix/generic/dirent.cpp): only modifies the passed `DIR*`.
- [x] [NetBSD](3604f3d817/lib/libc/gen/readdir.c): either uses locks depending on configuration, or only modifies the passed `DIR* `.
- [x] [OpenBSD](3943361423/lib/libc/gen/readdir.c): uses locks.
- [x] [Unikraft](be744898b6/lib/nolibc/musl-imported/src/dirent/readdir.c)
- [ ] VxWorks: closed-source, the [documentation](https://archive.org/details/manualzilla-id-5786163) does not make any reference to thread-safety. CC @biabbas @hax0kartik
- [x] QNX: the [documentation](https://www.qnx.com/developers/docs/8.0/com.qnx.doc.neutrino.lib_ref/topic/r/readdir.html) gives the same guarantee as POSIX.1-2024.
- [x] [NuttX](9a4114a9d3/libs/libc/dirent/lib_readdir.c):  only modifies the passed `DIR*`.
- [x] 3DS: the underlying [directory read implementation in libctru](36fe1ada5b/libctru/source/services/fs.c (L1835-L1850)) is thread-safe
- [ ] RTEMS: I'm unable to find the implementation. CC @thesummer
- [ ] QuRT: CC @androm3da
- [ ] ESP-IDF: `readdir` is *not* thread-safe (at least on FAT) due to [caching file metadata in the VFS context without locks](fa8039b5ca/components/fatfs/vfs/vfs_fat.c (L976)). [`opendir`](fa8039b5ca/components/fatfs/vfs/vfs_fat.c (L924)) does the same thing, too?! And the cache is never invalidated, even when the file is deleted?! Honestly, this is just broken... CC @ivmarkov @MabezDev @SergioGasquez
- [x] [Emscripten](f351c42755/system/lib/libc/musl/src/dirent/readdir.c): derived from musl, only modifies the current `DIR*`.

On the implementations where I couldn't confirm thread-safety `ReadDir` will still use `readdir_r`, but I've changed the code so that this edge-case is limited to `ReadDir::next`. All other platforms now use `readdir`.
2026-08-04 18:35:26 +02:00
..
2026-06-30 09:01:03 +00:00
2026-07-29 13:01:15 -07:00