standard_lib/fs/
file_type.rs

1// TODO: Ensure that only Files are supported? Preferably while keeping Metadata lazy - Metadata
2// has a size of 120 bytes.
3#[derive(Debug, Clone)]
4pub enum FileType {
5    BlockDevice,
6    CharDevice,
7    Directory,
8    Fifo,
9    Symlink,
10    Regular,
11    Socket,
12    Other,
13}
14
15use FileType::*;
16
17impl FileType {
18    #[inline(always)]
19    pub(crate) const fn from_stat_mode(st_mode: u32) -> FileType {
20        match st_mode & libc::S_IFMT {
21            libc::S_IFBLK => BlockDevice,
22            libc::S_IFCHR => CharDevice,
23            libc::S_IFDIR => Directory,
24            libc::S_IFIFO => Fifo,
25            libc::S_IFLNK => Symlink,
26            libc::S_IFREG => Regular,
27            libc::S_IFSOCK => Socket,
28            _ => Other,
29        }
30    }
31
32    pub(crate) fn from_dirent_type(d_type: u8) -> Option<FileType> {
33        Some(match d_type {
34            libc::DT_BLK => BlockDevice,
35            libc::DT_CHR => CharDevice,
36            libc::DT_DIR => Directory,
37            libc::DT_FIFO => Fifo,
38            libc::DT_LNK => Symlink,
39            libc::DT_REG => Regular,
40            libc::DT_SOCK => Socket,
41            libc::DT_UNKNOWN => None?,
42            _ => Other,
43        })
44    }
45}