Skip to content

Commit 55cbe88

Browse files
committed
Fixes for Dir on macOS, FreeBSD, and WASI.
1 parent 31fd98c commit 55cbe88

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/backend/libc/fs/dir.rs

+9
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ impl Dir {
4747
}
4848

4949
#[inline]
50+
#[allow(unused_mut)]
5051
fn _read_from(fd: BorrowedFd<'_>) -> io::Result<Self> {
5152
let mut any_errors = false;
5253

@@ -58,6 +59,7 @@ impl Dir {
5859
let flags = fcntl_getfl(fd)?;
5960
let fd_for_dir = match openat(fd, cstr!("."), flags | OFlags::CLOEXEC, Mode::empty()) {
6061
Ok(fd) => fd,
62+
#[cfg(not(target_os = "wasi"))]
6163
Err(io::Errno::NOENT) => {
6264
// If "." doesn't exist, it means the directory was removed.
6365
// We treat that as iterating through a directory with no
@@ -352,6 +354,13 @@ fn dir_iterator_handles_io_errors() {
352354
core::mem::forget(owned_fd);
353355
}
354356

357+
// FreeBSD and macOS seem to read some directory entries before we call
358+
// `.next()`.
359+
#[cfg(any(apple, freebsdlike))]
360+
{
361+
dir.rewind();
362+
}
363+
355364
assert!(matches!(dir.next(), Some(Err(_))));
356365
assert!(matches!(dir.next(), None));
357366
}

tests/fs/dir.rs

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ fn test_dir() {
6262
assert!(saw_cargo_toml);
6363
}
6464

65+
// Test that `Dir` silently stops iterating if the directory has been removed.
66+
//
67+
// Except on FreeBSD and macOS, where apparently `readdir` just keeps reading.
68+
#[cfg_attr(any(apple, freebsdlike), ignore)]
6569
#[test]
6670
fn dir_iterator_handles_dir_removal() {
6771
// create a dir, keep the FD, then delete the dir
@@ -83,6 +87,7 @@ fn dir_iterator_handles_dir_removal() {
8387

8488
// Like `dir_iterator_handles_dir_removal`, but close the directory after
8589
// `Dir::read_from`.
90+
#[cfg_attr(any(apple, freebsdlike), ignore)]
8691
#[test]
8792
fn dir_iterator_handles_dir_removal_after_open() {
8893
// create a dir, keep the FD, then delete the dir

0 commit comments

Comments
 (0)