Skip to content

Commit b298607

Browse files
committed
Auto merge of #47956 - retep998:is-nibbles, r=BurntSushi
This is the ideal FileType on Windows. You may not like it, but this is what peak performance looks like. Theoretically this would fix #46484 The current iteration of this PR should not cause existing code to break, but instead merely improves handling around reparse points. Specifically... * Reparse points are considered to be symbolic links if they have the name surrogate bit set. Name surrogates are reparse points that effectively act like symbolic links, redirecting you to a different directory/file. By checking for this bit instead of specific tags, we become much more general in our handling of reparse points, including those added by third parties. * If something is a reparse point but does not have the name surrogate bit set, then we ignore the fact that it is a reparse point because it is actually a file or directory directly there, despite having additional handling by drivers due to the reparse point. * For everything which is not a symbolic link (including non-surrogate reparse points) we report whether it is a directory or a file based on the presence of the directory attribute bit. * Notably this still preserves invariant that when `is_symlink` returns `true`, both `is_dir` and `is_file` will return `false`. The potential for breakage was far too high. * Adds an unstable `FileTypeExt` to allow users to determine whether a symbolic link is a directory or a file, since `FileType` by design is incapable of reporting this information.
2 parents b85bd51 + 9269e83 commit b298607

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

src/libstd/sys/windows/ext/fs.rs

+18
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,24 @@ impl MetadataExt for Metadata {
445445
fn file_size(&self) -> u64 { self.as_inner().size() }
446446
}
447447

448+
/// Add support for the Windows specific fact that a symbolic link knows whether it is a file
449+
/// or directory.
450+
#[unstable(feature = "windows_file_type_ext", issue = "0")]
451+
pub trait FileTypeExt {
452+
/// Returns whether this file type is a symbolic link that is also a directory.
453+
#[unstable(feature = "windows_file_type_ext", issue = "0")]
454+
fn is_symlink_dir(&self) -> bool;
455+
/// Returns whether this file type is a symbolic link that is also a file.
456+
#[unstable(feature = "windows_file_type_ext", issue = "0")]
457+
fn is_symlink_file(&self) -> bool;
458+
}
459+
460+
#[unstable(feature = "windows_file_type_ext", issue = "0")]
461+
impl FileTypeExt for fs::FileType {
462+
fn is_symlink_dir(&self) -> bool { self.as_inner().is_symlink_dir() }
463+
fn is_symlink_file(&self) -> bool { self.as_inner().is_symlink_file() }
464+
}
465+
448466
/// Creates a new file symbolic link on the filesystem.
449467
///
450468
/// The `dst` path will be a file symbolic link pointing to the `src`

src/libstd/sys/windows/fs.rs

+26-21
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ pub struct FileAttr {
3838
}
3939

4040
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
41-
pub enum FileType {
42-
Dir, File, SymlinkFile, SymlinkDir, ReparsePoint, MountPoint,
41+
pub struct FileType {
42+
attributes: c::DWORD,
43+
reparse_tag: c::DWORD,
4344
}
4445

4546
pub struct ReadDir {
@@ -516,30 +517,34 @@ impl FilePermissions {
516517

517518
impl FileType {
518519
fn new(attrs: c::DWORD, reparse_tag: c::DWORD) -> FileType {
519-
match (attrs & c::FILE_ATTRIBUTE_DIRECTORY != 0,
520-
attrs & c::FILE_ATTRIBUTE_REPARSE_POINT != 0,
521-
reparse_tag) {
522-
(false, false, _) => FileType::File,
523-
(true, false, _) => FileType::Dir,
524-
(false, true, c::IO_REPARSE_TAG_SYMLINK) => FileType::SymlinkFile,
525-
(true, true, c::IO_REPARSE_TAG_SYMLINK) => FileType::SymlinkDir,
526-
(true, true, c::IO_REPARSE_TAG_MOUNT_POINT) => FileType::MountPoint,
527-
(_, true, _) => FileType::ReparsePoint,
528-
// Note: if a _file_ has a reparse tag of the type IO_REPARSE_TAG_MOUNT_POINT it is
529-
// invalid, as junctions always have to be dirs. We set the filetype to ReparsePoint
530-
// to indicate it is something symlink-like, but not something you can follow.
520+
FileType {
521+
attributes: attrs,
522+
reparse_tag: reparse_tag,
531523
}
532524
}
533-
534-
pub fn is_dir(&self) -> bool { *self == FileType::Dir }
535-
pub fn is_file(&self) -> bool { *self == FileType::File }
525+
pub fn is_dir(&self) -> bool {
526+
!self.is_symlink() && self.is_directory()
527+
}
528+
pub fn is_file(&self) -> bool {
529+
!self.is_symlink() && !self.is_directory()
530+
}
536531
pub fn is_symlink(&self) -> bool {
537-
*self == FileType::SymlinkFile ||
538-
*self == FileType::SymlinkDir ||
539-
*self == FileType::MountPoint
532+
self.is_reparse_point() && self.is_reparse_tag_name_surrogate()
540533
}
541534
pub fn is_symlink_dir(&self) -> bool {
542-
*self == FileType::SymlinkDir || *self == FileType::MountPoint
535+
self.is_symlink() && self.is_directory()
536+
}
537+
pub fn is_symlink_file(&self) -> bool {
538+
self.is_symlink() && !self.is_directory()
539+
}
540+
fn is_directory(&self) -> bool {
541+
self.attributes & c::FILE_ATTRIBUTE_DIRECTORY != 0
542+
}
543+
fn is_reparse_point(&self) -> bool {
544+
self.attributes & c::FILE_ATTRIBUTE_REPARSE_POINT != 0
545+
}
546+
fn is_reparse_tag_name_surrogate(&self) -> bool {
547+
self.reparse_tag & 0x20000000 != 0
543548
}
544549
}
545550

0 commit comments

Comments
 (0)