Skip to content
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

Inconsistent behavior in FileType comparisons on Windows #138668

Closed
mikerooni opened this issue Mar 18, 2025 · 4 comments · Fixed by #138671
Closed

Inconsistent behavior in FileType comparisons on Windows #138668

mikerooni opened this issue Mar 18, 2025 · 4 comments · Fixed by #138671
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. O-windows Operating system: Windows T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@mikerooni
Copy link

The PartialEq/Eq implementation of FileType on Windows is currently comparing more than just the actual file type. It seems to be using all file attributes for the comparison instead.

Steps to reproduce:

  1. Create two files, e.g. a.txt and b.txt
  2. Using Windows Explorer:
    • Right click -> Properties -> open the "Advanced" file attributes dialog
    • Enable "Allow this file to have contents indexed in addition to file properties" for only one of these files, but not the other

The following test will now fail:

let type_a = fs::metadata("a.txt").unwrap().file_type();
let type_b = fs::metadata("b.txt").unwrap().file_type();
assert_eq!(type_a, type_b);

Expected behavior:

I would expect the comparison between two FileTypes to only compare the actual file TYPE (e.g. file, dir, symlink, any platform-specific extensions), but not file attributes.

Another issue in this case is that it isn't obvious why the comparison fails, because FileType's Debug implementation only shows is_file, is_dir, and is_symlink:

assertion `left == right` failed
  left: FileType { is_file: true, is_dir: false, is_symlink: false, .. }
 right: FileType { is_file: true, is_dir: false, is_symlink: false, .. }

I'm not sure whether this is something that can be easily fixed - I'd argue it should be considered an actual bug occurring in an edge case, but fixing it would technically introduce a breaking change.

rustc --version --verbose:

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-pc-windows-msvc
release: 1.85.0
LLVM version: 19.1.7
@mikerooni mikerooni added the C-bug Category: This is a bug. label Mar 18, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 18, 2025
@Kyuuhachi
Copy link
Contributor

When this was brought up on Discord, I also noticed that impl From<c::WIN32_FIND_DATAW> for FileAttr checks for FILE_ATTRIBUTE_REPARSE_POINT, but DirEntry::file_type does not, and thus reads reserved data.

reparse_tag: if wfd.dwFileAttributes & c::FILE_ATTRIBUTE_REPARSE_POINT != 0 {
// reserved unless this is a reparse point
wfd.dwReserved0
} else {
0
},

Ok(FileType::new(
self.data.dwFileAttributes,
/* reparse_tag = */ self.data.dwReserved0,
))

@lolbinarycat lolbinarycat added O-windows Operating system: Windows T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 18, 2025
@ChrisDenton
Copy link
Member

When this was brought up on Discord, I also noticed that impl From<c::WIN32_FIND_DATAW> for FileAttr checks for FILE_ATTRIBUTE_REPARSE_POINT, but DirEntry::file_type does not, and thus reads reserved data.

While not ideal, that part is technically "fine" because dwReserved0 must be initialised (because WIN32_FIND_DATAW is stored by value, so must be fully initialised). And FileType does not use reparse_tag unless the file attributes contain FILE_ATTRIBUTE_REPARSE_POINT. So it may store a nonsense value but it doesn't affect behaviour as far as I can tell.

@Kyuuhachi
Copy link
Contributor

Except for equality. I haven't tested, but I think the FileType returned from a DirEntry and from a Metadata might not compare equal, even for the same file.

@ChrisDenton
Copy link
Member

I submitted a fix (see above) that only compares the relevant information, so let's see what the libs-api team says.

@bors bors closed this as completed in 8e30df7 Mar 24, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 24, 2025
Rollup merge of rust-lang#138671 - ChrisDenton:filetype, r=joshtriplett

Fix `FileType` `PartialEq` implementation on Windows

Fixes rust-lang#138668

On Windows the [`FileType`](https://doc.rust-lang.org/stable/std/fs/struct.FileType.html) struct was deriving `PartialEq` which in turn means it was doing a bit-for-bit comparison on the file attributes and reparse point. This is wrong because `attributes` may contain many things unrelated to file type.

`FileType` on Windows allows for four possible combinations (see also [`FileTypeExt`](https://doc.rust-lang.org/stable/std/os/windows/fs/trait.FileTypeExt.html)): `file`, `dir`, `symlink_file` and `symlink_dir`. So the new implementation makes sure both symlink and directory information match (and only those things).

This could be considered just a bug fix but it is a behaviour change so someone from libs-api might want to FCP this (or might not)...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. O-windows Operating system: Windows T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants