Skip to content

Commit cad15a7

Browse files
committed
Fixes for Dir on macOS, FreeBSD, and WASI.
1 parent df3c3a1 commit cad15a7

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/backend/libc/fs/dir.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl Dir {
4646
}
4747

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

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

346+
// FreeBSD and macOS seem to read some directory entries before we call
347+
// `.next()`.
348+
#[cfg(any(apple, freebsdlike))]
349+
{
350+
dir.rewind();
351+
}
352+
344353
assert!(matches!(dir.next(), Some(Err(_))));
345354
assert!(matches!(dir.next(), None));
346355
}

tests/fs/dir.rs

Lines changed: 5 additions & 0 deletions
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)