1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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
74
75
76
77
78
79
use heim_common::prelude::*;

use std::str::FromStr;

/// Known filesystems.
///
/// All physical filesystems should have their own enum element
/// and all virtual filesystems will go into the `Other` element.
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub enum FileSystem {
    Ext2,
    Ext3,
    Ext4,
    VFat,
    Ntfs,
    Zfs,
    Hfs,
    Reiserfs,
    FuseBlk,

    // TODO: Extend list
    // References:
    //  * https://linux.die.net/man/2/fstatfs
    //  * FAT, FAT32, NTFS, HPFS, CDFS, UDF or NWFS
    Other(String),

    #[doc(hidden)]
    __Nonexhaustive,
}

impl FileSystem {
    /// Checks if filesystem is used for a physical devices
    pub fn is_physical(&self) -> bool {
        match self {
            FileSystem::Other(..) => false,
            _ => true,
        }
    }

    /// Checks if filesystem is used for a virtual devices (such as `tmpfs` or `smb` mounts)
    pub fn is_virtual(&self) -> bool {
        !self.is_physical()
    }

    pub fn as_str(&self) -> &str {
        match self {
            FileSystem::Ext2 => "ext2",
            FileSystem::Ext3 => "ext3",
            FileSystem::Ext4 => "ext4",
            FileSystem::VFat => "vfat",
            FileSystem::Ntfs => "ntfs",
            FileSystem::Zfs => "zfs",
            FileSystem::Hfs => "hfs",
            FileSystem::Reiserfs => "reiserfs",
            FileSystem::FuseBlk => "fuseblk",
            FileSystem::Other(string) => string.as_str(),
            _ => unreachable!(),
        }
    }
}

impl FromStr for FileSystem {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        match () {
            _ if s.eq_ignore_ascii_case("ext2") => Ok(FileSystem::Ext2),
            _ if s.eq_ignore_ascii_case("ext3") => Ok(FileSystem::Ext3),
            _ if s.eq_ignore_ascii_case("ext4") => Ok(FileSystem::Ext4),
            _ if s.eq_ignore_ascii_case("vfat") => Ok(FileSystem::VFat),
            _ if s.eq_ignore_ascii_case("ntfs") => Ok(FileSystem::Ntfs),
            _ if s.eq_ignore_ascii_case("zfs") => Ok(FileSystem::Zfs),
            _ if s.eq_ignore_ascii_case("hfs") => Ok(FileSystem::Hfs),
            _ if s.eq_ignore_ascii_case("reiserfs") => Ok(FileSystem::Reiserfs),
            _ if s.eq_ignore_ascii_case("fuseblk") => Ok(FileSystem::FuseBlk),
            _ => Ok(FileSystem::Other(s.to_string())),
        }
    }
}