pub enum FileType {
BlockDevice,
CharDevice,
Directory,
Pipe,
Symlink,
RegularFile,
Socket,
Unknown,
}Expand description
Represents the type of a file in the filesystem
This enum provides a cross-platform abstraction for file types with
specialised support for Unix filesystem semantics. It can be constructed
from various sources including dirent d_type values, stat mode bits,
and path-based lookups.
§Examples
use fdf::FileType;
use libc::DT_DIR;
// Create from dirent d_type
let dir_type = FileType::from_dtype(DT_DIR);
assert!(dir_type.is_dir());
// Check if a file type is traversible
assert!(dir_type.is_traversible());Variants§
BlockDevice
Block special device file (e.g., /dev/sda)
CharDevice
Character special device file (e.g., /dev/tty)
Directory
Directory
Pipe
FIFO (named pipe)
Symlink
Symbolic link
RegularFile
Regular file
Socket
Socket file
Unknown
Unknown file type (should be rare on supported filesystems)
Implementations§
Source§impl FileType
impl FileType
Sourcepub const fn from_dtype(d_type: u8) -> Self
pub const fn from_dtype(d_type: u8) -> Self
Converts a libc dirent d_type value to a FileType
This is the preferred method when d_type is available, as it avoids
expensive filesystem lookups. However, note that some filesystems
may not support d_type or may set it to DT_UNKNOWN.
§Parameters
d_type: The file type from adirentstructure
§Examples
use fdf::FileType;
use libc::{DT_DIR, DT_REG};
assert!(FileType::from_dtype(DT_DIR).is_dir());
assert!(FileType::from_dtype(DT_REG).is_regular_file());Sourcepub const fn is_dir(&self) -> bool
pub const fn is_dir(&self) -> bool
Returns true if this represents a directory (cost free check)
Sourcepub const fn is_regular_file(&self) -> bool
pub const fn is_regular_file(&self) -> bool
Returns true if this represents a regular file (cost free check)
Sourcepub const fn is_symlink(&self) -> bool
pub const fn is_symlink(&self) -> bool
Returns true if this represents a symbolic link (cost free check)
Sourcepub const fn is_block_device(&self) -> bool
pub const fn is_block_device(&self) -> bool
Returns true if this represents a block device (cost free check)
Sourcepub const fn is_char_device(&self) -> bool
pub const fn is_char_device(&self) -> bool
Returns true if this represents a character device (cost free check)
Sourcepub const fn is_pipe(&self) -> bool
pub const fn is_pipe(&self) -> bool
Returns true if this represents a FIFO (named pipe) (cost free check)
Sourcepub const fn is_socket(&self) -> bool
pub const fn is_socket(&self) -> bool
Returns true if this represents a socket (cost free check)
Sourcepub const fn is_unknown(&self) -> bool
pub const fn is_unknown(&self) -> bool
Returns true if this represents an unknown file type (cost free check)
Sourcepub const fn is_traversible(&self) -> bool
pub const fn is_traversible(&self) -> bool
Returns true if the file type is traversible (directory or symlink)
This is useful for determining whether a directory entry can be explored further during filesystem traversal.
Sourcepub fn from_dtype_fallback(d_type: u8, file_path: &[u8]) -> Self
pub fn from_dtype_fallback(d_type: u8, file_path: &[u8]) -> Self
Fallback method to determine file type when d_type is unavailable or DT_UNKNOWN
This method first checks the d_type value, and if it’s DT_UNKNOWN,
falls back to a more expensive lstat-based lookup using the file path.
§Parameters
d_type: The file type from a dirent structurefile_path: The path to the file for fallback lookup
§Notes
While ext4 and BTRFS (and others, not entirely tested!) typically provide reliable d_type values,
other filesystems like NTFS, XFS, or FUSE-based filesystems
may require the fallback path.
Sourcepub fn from_bytes(file_path: &[u8]) -> Self
pub fn from_bytes(file_path: &[u8]) -> Self
Determines file type using an lstat call on the provided path
This is more expensive but more reliable than relying on d_type,
especially on filesystems that don’t fully support dirent d_type.
§Parameters
file_path: The path to the file to stat (must be a valid filepath)
Sourcepub const fn from_mode(mode: mode_t) -> Self
pub const fn from_mode(mode: mode_t) -> Self
Converts Unix mode bits to a FileType
This extracts the file type from the st_mode field of a stat structure.
§Parameters
mode: The mode bits from a stat structure
Sourcepub fn from_path<P: AsRef<Path>>(path_start: P) -> Self
pub fn from_path<P: AsRef<Path>>(path_start: P) -> Self
Determines file type using the standard library’s metadata lookup
This method is primarily useful for verification and testing purposes, not for use within performance-critical iteration code paths.
§Parameters
path_start: The path to examine
Trait Implementations§
Source§impl Ord for FileType
impl Ord for FileType
Source§impl PartialOrd for FileType
impl PartialOrd for FileType
impl Copy for FileType
impl Eq for FileType
impl StructuralPartialEq for FileType
Auto Trait Implementations§
impl Freeze for FileType
impl RefUnwindSafe for FileType
impl Send for FileType
impl Sync for FileType
impl Unpin for FileType
impl UnwindSafe for FileType
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> CloneUnsized for Twhere
T: Clone,
impl<T> CloneUnsized for Twhere
T: Clone,
Source§fn unsized_clone_from(&mut self, source: &T)
fn unsized_clone_from(&mut self, source: &T)
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more