Skip to content

Commit

Permalink
Fixes for Dir on macOS, FreeBSD, and WASI.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode committed Oct 12, 2023
1 parent 8365c00 commit 8a29e65
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/backend/libc/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl Dir {
}

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

Expand All @@ -85,6 +86,7 @@ impl Dir {
let flags = fcntl_getfl(&fd)?;
let fd_for_dir = match openat(&fd, cstr!("."), flags | OFlags::CLOEXEC, Mode::empty()) {
Ok(fd) => fd,
#[cfg(not(target_os = "wasi"))]
Err(io::Errno::NOENT) => {
// If "." doesn't exist, it means the directory was removed.
// We treat that as iterating through a directory with no
Expand Down Expand Up @@ -540,6 +542,13 @@ fn dir_iterator_handles_io_errors() {
core::mem::forget(owned_fd);
}

// FreeBSD and macOS seem to read some directory entries before we call
// `.next()`.
#[cfg(any(apple, freebsdlike))]
{
dir.rewind();
}

assert!(matches!(dir.next(), Some(Err(_))));
assert!(matches!(dir.next(), None));
}
5 changes: 5 additions & 0 deletions tests/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ fn test_dir() {
assert!(saw_cargo_toml);
}

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

// Like `dir_iterator_handles_dir_removal`, but close the directory after
// `Dir::read_from`.
#[cfg_attr(any(apple, freebsdlike), ignore)]
#[test]
fn dir_iterator_handles_dir_removal_after_open() {
// create a dir, keep the FD, then delete the dir
Expand Down

0 comments on commit 8a29e65

Please sign in to comment.