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

This is the ideal FileType on Windows. You may not like it, but this is what peak performance looks like. #47956

Merged
merged 6 commits into from
Feb 17, 2018
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
18 changes: 18 additions & 0 deletions src/libstd/sys/windows/ext/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,24 @@ impl MetadataExt for Metadata {
fn file_size(&self) -> u64 { self.as_inner().size() }
}

/// Add support for the Windows specific fact that a symbolic link knows whether it is a file
/// or directory.
#[unstable(feature = "windows_file_type_ext", issue = "0")]
pub trait FileTypeExt {
/// Returns whether this file type is a symbolic link that is also a directory.
#[unstable(feature = "windows_file_type_ext", issue = "0")]
fn is_symlink_dir(&self) -> bool;
/// Returns whether this file type is a symbolic link that is also a file.
#[unstable(feature = "windows_file_type_ext", issue = "0")]
fn is_symlink_file(&self) -> bool;
}

#[unstable(feature = "windows_file_type_ext", issue = "0")]
impl FileTypeExt for fs::FileType {
fn is_symlink_dir(&self) -> bool { self.as_inner().is_symlink_dir() }
fn is_symlink_file(&self) -> bool { self.as_inner().is_symlink_file() }
}

/// Creates a new file symbolic link on the filesystem.
///
/// The `dst` path will be a file symbolic link pointing to the `src`
Expand Down
47 changes: 26 additions & 21 deletions src/libstd/sys/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ pub struct FileAttr {
}

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum FileType {
Dir, File, SymlinkFile, SymlinkDir, ReparsePoint, MountPoint,
pub struct FileType {
attributes: c::DWORD,
reparse_tag: c::DWORD,
}

pub struct ReadDir {
Expand Down Expand Up @@ -516,30 +517,34 @@ impl FilePermissions {

impl FileType {
fn new(attrs: c::DWORD, reparse_tag: c::DWORD) -> FileType {
match (attrs & c::FILE_ATTRIBUTE_DIRECTORY != 0,
attrs & c::FILE_ATTRIBUTE_REPARSE_POINT != 0,
reparse_tag) {
(false, false, _) => FileType::File,
(true, false, _) => FileType::Dir,
(false, true, c::IO_REPARSE_TAG_SYMLINK) => FileType::SymlinkFile,
(true, true, c::IO_REPARSE_TAG_SYMLINK) => FileType::SymlinkDir,
(true, true, c::IO_REPARSE_TAG_MOUNT_POINT) => FileType::MountPoint,
(_, true, _) => FileType::ReparsePoint,
// Note: if a _file_ has a reparse tag of the type IO_REPARSE_TAG_MOUNT_POINT it is
// invalid, as junctions always have to be dirs. We set the filetype to ReparsePoint
// to indicate it is something symlink-like, but not something you can follow.
FileType {
attributes: attrs,
reparse_tag: reparse_tag,
}
}

pub fn is_dir(&self) -> bool { *self == FileType::Dir }
pub fn is_file(&self) -> bool { *self == FileType::File }
pub fn is_dir(&self) -> bool {
!self.is_symlink() && self.is_directory()
}
pub fn is_file(&self) -> bool {
!self.is_symlink() && !self.is_directory()
}
pub fn is_symlink(&self) -> bool {
*self == FileType::SymlinkFile ||
*self == FileType::SymlinkDir ||
*self == FileType::MountPoint
self.is_reparse_point() && self.is_reparse_tag_name_surrogate()
}
pub fn is_symlink_dir(&self) -> bool {
*self == FileType::SymlinkDir || *self == FileType::MountPoint
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
}
}

Expand Down