Skip to content
Open
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
1 change: 0 additions & 1 deletion library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2802,7 +2802,6 @@ impl ReadDir {
/// handle used by `ReadDir`. However, this may fall back to opening a new handle based on the
/// path, which is subject to race conditions if the file system has changed since the `ReadDir`
/// was created.
// FIXME: before stabilization, ensure that we have a race-free implementation on Windows!
#[unstable(feature = "dirfd", issue = "120426")]
pub fn dir(&self) -> io::Result<Dir> {
self.0.dir().map(|d| Dir { inner: d })
Expand Down
45 changes: 43 additions & 2 deletions library/std/src/fs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ pub(crate) fn got_symlink_permission(tmpdir: &TempDir) -> bool {
}
let link = tmpdir.join("some_hopefully_unique_link_name");

match symlink_file(r"nonexisting_target", link) {
let res = match symlink_file(r"nonexisting_target", &link) {
// ERROR_PRIVILEGE_NOT_HELD = 1314
Err(ref err) if err.raw_os_error() == Some(1314) => false,
Ok(_) | Err(_) => true,
}
};
fs::remove_file(link).ok(); // do not leave behind junk
res
}

#[test]
Expand Down Expand Up @@ -2186,12 +2188,23 @@ fn realpath_works_tricky() {

#[test]
fn dir_entry_methods() {
#[cfg(windows)]
use crate::os::windows::fs::FileTypeExt;

let tmpdir = tmpdir();
let mut num_files = 0;

fs::create_dir_all(&tmpdir.join("a")).unwrap();
File::create(&tmpdir.join("b")).unwrap();
num_files += 2;
if got_symlink_permission(&tmpdir) {

@ChrisDenton ChrisDenton Oct 20, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This either shouldn't use the same tmpdir as is used for the read_dir tests or else got_symlink_permission should cleanup any files it creates.

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh I see, good point. I made got_symlink_permission clean up after itself.

symlink_dir("a", &tmpdir.join("c")).unwrap();
symlink_file("b", &tmpdir.join("d")).unwrap();
num_files += 2;
}

for file in tmpdir.path().read_dir().unwrap().map(|f| f.unwrap()) {
num_files -= 1;
let fname = file.file_name();
match fname.to_str() {
Some("a") => {
Expand All @@ -2202,9 +2215,28 @@ fn dir_entry_methods() {
assert!(file.file_type().unwrap().is_file());
assert!(file.metadata().unwrap().is_file());
}
Some("c") => {
assert!(file.file_type().unwrap().is_symlink());
assert!(file.metadata().unwrap().is_symlink());
#[cfg(windows)]
{
assert!(file.file_type().unwrap().is_symlink_dir());
assert!(file.metadata().unwrap().file_type().is_symlink_dir());
}
}
Some("d") => {
assert!(file.file_type().unwrap().is_symlink());
assert!(file.metadata().unwrap().is_symlink());
#[cfg(windows)]
{
assert!(file.file_type().unwrap().is_symlink_file());
assert!(file.metadata().unwrap().file_type().is_symlink_file());
}
}
f => panic!("unknown file name: {f:?}"),
}
}
assert_eq!(num_files, 0);
}

#[test]
Expand All @@ -2224,6 +2256,15 @@ fn read_dir_not_found() {
assert_eq!(res.err().unwrap().kind(), ErrorKind::NotFound);
}

#[test]
fn read_dir_not_directory() {
let tmpdir = tmpdir();
let filename = tmpdir.join("file");
File::create(&filename).unwrap();
let res = fs::read_dir(&filename);
assert_eq!(res.err().unwrap().kind(), ErrorKind::NotADirectory);
}

#[test]
fn file_open_not_found() {
let res = File::open("/path/that/does/not/exist");
Expand Down
Loading
Loading