Skip to content

Fix FileType PartialEq implementation on Windows #138671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 24, 2025
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
17 changes: 17 additions & 0 deletions library/std/src/fs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,23 @@ fn test_eq_direntry_metadata() {
}
}

/// Test that windows file type equality is not affected by attributes unrelated
/// to the file type.
#[test]
#[cfg(target_os = "windows")]
fn test_eq_windows_file_type() {
let tmpdir = tmpdir();
let file1 = File::create(tmpdir.join("file1")).unwrap();
let file2 = File::create(tmpdir.join("file2")).unwrap();
assert_eq!(file1.metadata().unwrap().file_type(), file2.metadata().unwrap().file_type());

// Change the readonly attribute of one file.
let mut perms = file1.metadata().unwrap().permissions();
perms.set_readonly(true);
file1.set_permissions(perms).unwrap();
assert_eq!(file1.metadata().unwrap().file_type(), file2.metadata().unwrap().file_type());
}

/// Regression test for https://github.com/rust-lang/rust/issues/50619.
#[test]
#[cfg(target_os = "linux")]
Expand Down
33 changes: 15 additions & 18 deletions library/std/src/sys/fs/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub struct FileAttr {

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct FileType {
attributes: u32,
reparse_tag: u32,
is_directory: bool,
is_symlink: bool,
}

pub struct ReadDir {
Expand Down Expand Up @@ -1111,32 +1111,29 @@ impl FileTimes {
}

impl FileType {
fn new(attrs: u32, reparse_tag: u32) -> FileType {
FileType { attributes: attrs, reparse_tag }
fn new(attributes: u32, reparse_tag: u32) -> FileType {
let is_directory = attributes & c::FILE_ATTRIBUTE_DIRECTORY != 0;
let is_symlink = {
let is_reparse_point = attributes & c::FILE_ATTRIBUTE_REPARSE_POINT != 0;
let is_reparse_tag_name_surrogate = reparse_tag & 0x20000000 != 0;
is_reparse_point && is_reparse_tag_name_surrogate
};
FileType { is_directory, is_symlink }
}
pub fn is_dir(&self) -> bool {
!self.is_symlink() && self.is_directory()
!self.is_symlink && self.is_directory
}
pub fn is_file(&self) -> bool {
!self.is_symlink() && !self.is_directory()
!self.is_symlink && !self.is_directory
}
pub fn is_symlink(&self) -> bool {
self.is_reparse_point() && self.is_reparse_tag_name_surrogate()
self.is_symlink
}
pub fn is_symlink_dir(&self) -> bool {
self.is_symlink() && self.is_directory()
self.is_symlink && self.is_directory
}
pub fn is_symlink_file(&self) -> bool {
self.is_symlink() && !self.is_directory()
}
fn is_directory(&self) -> bool {
self.attributes & c::FILE_ATTRIBUTE_DIRECTORY != 0
}
fn is_reparse_point(&self) -> bool {
self.attributes & c::FILE_ATTRIBUTE_REPARSE_POINT != 0
}
fn is_reparse_tag_name_surrogate(&self) -> bool {
self.reparse_tag & 0x20000000 != 0
self.is_symlink && !self.is_directory
}
}

Expand Down
Loading