Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions library/std/src/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,10 @@ impl DirEntry {
self.file_name_os_str().to_os_string()
}

#[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "android"))]
#[cfg(all(
any(target_os = "linux", target_os = "emscripten", target_os = "android"),
not(miri)
))]
pub fn metadata(&self) -> io::Result<FileAttr> {
let fd = cvt(unsafe { dirfd(self.dir.dirp.0) })?;
let name = self.name_cstr().as_ptr();
Expand All @@ -695,7 +698,10 @@ impl DirEntry {
Ok(FileAttr::from_stat64(stat))
}

#[cfg(not(any(target_os = "linux", target_os = "emscripten", target_os = "android")))]
#[cfg(any(
not(any(target_os = "linux", target_os = "emscripten", target_os = "android")),
miri
))]
pub fn metadata(&self) -> io::Result<FileAttr> {
lstat(&self.path())
}
Expand Down
8 changes: 8 additions & 0 deletions src/tools/miri/tests/pass-dep/shims/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,14 @@ fn test_directory() {
let mut file_names = dir_iter.map(|e| e.unwrap().file_name()).collect::<Vec<_>>();
file_names.sort_unstable();
assert_eq!(file_names, vec!["test_file_1", "test_file_2"]);
// Test that read_dir metadata calls succeed
assert_eq!(
&[true, true],
Copy link
Member

@RalfJung RalfJung Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we'd also make sure it returns false on directories.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, created #103258

&*read_dir(&dir_path)
.unwrap()
.map(|e| e.unwrap().metadata().unwrap().is_file())
.collect::<Vec<_>>()
);
// Deleting the directory should fail, since it is not empty.
assert_eq!(ErrorKind::DirectoryNotEmpty, remove_dir(&dir_path).unwrap_err().kind());
// Clean up the files in the directory
Expand Down