Struct cap_primitives::fs::FileType
source · #[repr(transparent)]pub struct FileType(_);
Expand description
A structure representing a type of file with accessors for each file type.
This corresponds to std::fs::FileType
.
We need to define our own version because the libstd `FileType` doesn't
have a public constructor that we can use.
Implementations§
source§impl FileType
impl FileType
sourcepub const fn dir() -> Self
pub const fn dir() -> Self
Creates a FileType
for which is_dir()
returns true
.
Examples found in repository?
src/windows/fs/file_type_ext.rs (line 54)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
pub(crate) fn from_std(std: fs::FileType) -> FileType {
if std.is_file() {
return FileType::file();
}
if std.is_dir() {
return FileType::dir();
}
#[cfg(windows_file_type_ext)]
{
use std::os::windows::fs::FileTypeExt;
if std.is_symlink_file() {
return FileType::ext(Self::SymlinkFile);
}
if std.is_symlink_dir() {
return FileType::ext(Self::SymlinkDir);
}
}
if std.is_symlink() {
return FileType::ext(Self::SymlinkUnknown);
}
FileType::unknown()
}
sourcepub const fn file() -> Self
pub const fn file() -> Self
Creates a FileType
for which is_file()
returns true
.
Examples found in repository?
src/windows/fs/file_type_ext.rs (line 51)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
pub(crate) fn from_std(std: fs::FileType) -> FileType {
if std.is_file() {
return FileType::file();
}
if std.is_dir() {
return FileType::dir();
}
#[cfg(windows_file_type_ext)]
{
use std::os::windows::fs::FileTypeExt;
if std.is_symlink_file() {
return FileType::ext(Self::SymlinkFile);
}
if std.is_symlink_dir() {
return FileType::ext(Self::SymlinkDir);
}
}
if std.is_symlink() {
return FileType::ext(Self::SymlinkUnknown);
}
FileType::unknown()
}
sourcepub const fn unknown() -> Self
pub const fn unknown() -> Self
Creates a FileType
for which is_unknown()
returns true
.
Examples found in repository?
src/windows/fs/file_type_ext.rs (line 22)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
pub(crate) fn from(file: &fs::File, metadata: &fs::Metadata) -> io::Result<FileType> {
// Check for the things we can do with just metadata.
let file_type = Self::from_just_metadata(metadata);
if file_type != FileType::unknown() {
return Ok(file_type);
}
// Use the open file to check for one of the exotic file types.
let file_type = winx::winapi_util::file::typ(file)?;
if file_type.is_char() {
return Ok(FileType::ext(ImplFileTypeExt::CharacterDevice));
}
if file_type.is_pipe() {
return Ok(FileType::ext(ImplFileTypeExt::Fifo));
}
Ok(FileType::unknown())
}
/// Constructs a new instance of `Self` from the given
/// [`std::fs::Metadata`].
#[inline]
pub(crate) fn from_just_metadata(metadata: &fs::Metadata) -> FileType {
let std = metadata.file_type();
Self::from_std(std)
}
/// Constructs a new instance of `Self` from the given
/// [`std::fs::FileType`].
#[inline]
pub(crate) fn from_std(std: fs::FileType) -> FileType {
if std.is_file() {
return FileType::file();
}
if std.is_dir() {
return FileType::dir();
}
#[cfg(windows_file_type_ext)]
{
use std::os::windows::fs::FileTypeExt;
if std.is_symlink_file() {
return FileType::ext(Self::SymlinkFile);
}
if std.is_symlink_dir() {
return FileType::ext(Self::SymlinkDir);
}
}
if std.is_symlink() {
return FileType::ext(Self::SymlinkUnknown);
}
FileType::unknown()
}
sourcepub fn is_dir(&self) -> bool
pub fn is_dir(&self) -> bool
Tests whether this file type represents a directory.
This corresponds to std::fs::FileType::is_dir
.
sourcepub fn is_file(&self) -> bool
pub fn is_file(&self) -> bool
Tests whether this file type represents a regular file.
This corresponds to std::fs::FileType::is_file
.
sourcepub fn is_symlink(&self) -> bool
pub fn is_symlink(&self) -> bool
Tests whether this file type represents a symbolic link.
This corresponds to std::fs::FileType::is_symlink
.
Examples found in repository?
More examples
src/fs/manually/open.rs (line 481)
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
pub(crate) fn stat(start: &fs::File, path: &Path, follow: FollowSymlinks) -> io::Result<Metadata> {
// POSIX returns `ENOENT` on an empty path. TODO: On Windows, we should
// be compatible with what Windows does instead.
if path.as_os_str().is_empty() {
return Err(errors::no_such_file_or_directory());
}
let mut options = OpenOptions::new();
options.follow(follow);
let mut symlink_count = 0;
let mut ctx = Context::new(MaybeOwnedFile::borrowed(start), path, &options, None);
assert!(!ctx.dir_precluded);
while let Some(c) = ctx.components.pop() {
match c {
CowComponent::PrefixOrRootDir => return Err(errors::escape_attempt()),
CowComponent::CurDir => ctx.cur_dir()?,
CowComponent::ParentDir => ctx.parent_dir()?,
CowComponent::Normal(one) => {
if ctx.components.is_empty() {
// If this is the last component, do a non-following
// `stat_unchecked` on it.
let stat = stat_unchecked(&ctx.base, one.as_ref(), FollowSymlinks::No)?;
// If we weren't asked to follow symlinks, or it wasn't a
// symlink, we're done.
if options.follow == FollowSymlinks::No || !stat.file_type().is_symlink() {
if stat.is_dir() {
if ctx.dir_precluded {
return Err(errors::is_directory());
}
} else if ctx.dir_required {
return Err(errors::is_not_directory());
}
return Ok(stat);
}
// On Windows, symlinks know whether they are a file or
// directory.
#[cfg(windows)]
if stat.file_attributes() & FILE_ATTRIBUTE_DIRECTORY != 0 {
ctx.dir_required = true;
} else {
ctx.dir_precluded = true;
}
// If it was a symlink and we're asked to follow symlinks,
// dereference it.
ctx.symlink(&one, &mut symlink_count)?
} else {
// Otherwise open the path component normally.
ctx.normal(&one, &options, &mut symlink_count)?
}
}
}
}
// If the path ended in `.` (explicit or implied) or `..`, we may have
// opened the directory with eg. `O_PATH` on Linux, or we may have skipped
// checking for search access to `.`, so re-check it.
if ctx.follow_with_dot {
if ctx.dir_precluded {
return Err(errors::is_directory());
}
ctx.check_dot_access()?;
}
// If the path ended in `.` or `..`, we already have it open, so just do
// `.metadata()` on it.
Metadata::from_file(&*ctx.base)
}