From 727335878d316f6301780d182ea14ec4fb32531d Mon Sep 17 00:00:00 2001 From: Alex Saveau Date: Fri, 14 Oct 2022 20:51:54 -0700 Subject: [PATCH] Support DirEntry metadata calls in miri Signed-off-by: Alex Saveau --- library/std/src/sys/unix/fs.rs | 10 ++++++++-- src/tools/miri/tests/pass-dep/shims/fs.rs | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs index 3ac01e6a27d16..780f46f8c11eb 100644 --- a/library/std/src/sys/unix/fs.rs +++ b/library/std/src/sys/unix/fs.rs @@ -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 { let fd = cvt(unsafe { dirfd(self.dir.dirp.0) })?; let name = self.name_cstr().as_ptr(); @@ -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 { lstat(&self.path()) } diff --git a/src/tools/miri/tests/pass-dep/shims/fs.rs b/src/tools/miri/tests/pass-dep/shims/fs.rs index 9faced0291612..e2a1d14f7404f 100644 --- a/src/tools/miri/tests/pass-dep/shims/fs.rs +++ b/src/tools/miri/tests/pass-dep/shims/fs.rs @@ -404,6 +404,14 @@ fn test_directory() { let mut file_names = dir_iter.map(|e| e.unwrap().file_name()).collect::>(); 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], + &*read_dir(&dir_path) + .unwrap() + .map(|e| e.unwrap().metadata().unwrap().is_file()) + .collect::>() + ); // 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