summaryrefslogtreecommitdiffstats
path: root/src/app/data_harvester/disks/unix/file_systems.rs
blob: ba6c568979294427ec45dca1e93c2a710e8e47db (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use std::str::FromStr;

/// Known filesystems. Original list from
/// [heim](https://github.com/heim-rs/heim/blob/master/heim-disk/src/filesystem.rs).
///
/// All physical filesystems should have their own enum element and all virtual filesystems will go into
/// the [`FileSystem::Other`] element.
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
#[non_exhaustive]
pub enum FileSystem {
    /// ext2 (https://en.wikipedia.org/wiki/Ext2)
    Ext2,

    /// ext3 (https://en.wikipedia.org/wiki/Ext3)
    Ext3,

    /// ext4 (https://en.wikipedia.org/wiki/Ext4)
    Ext4,

    /// FAT (https://en.wikipedia.org/wiki/File_Allocation_Table)
    VFat,

    /// exFAT (https://en.wikipedia.org/wiki/ExFAT)
    ExFat,

    /// F2FS (https://en.wikipedia.org/wiki/F2FS)
    F2fs,

    /// NTFS (https://en.wikipedia.org/wiki/NTFS)
    Ntfs,

    /// ZFS (https://en.wikipedia.org/wiki/ZFS)
    Zfs,

    /// HFS (https://en.wikipedia.org/wiki/Hierarchical_File_System)
    Hfs,

    /// HFS+ (https://en.wikipedia.org/wiki/HFS_Plus)
    HfsPlus,

    /// JFS (https://en.wikipedia.org/wiki/JFS_(file_system))
    Jfs,

    /// ReiserFS 3 (https://en.wikipedia.org/wiki/ReiserFS)
    Reiser3,

    /// ReiserFS 4 (https://en.wikipedia.org/wiki/Reiser4)
    Reiser4,

    /// Btrfs (https://en.wikipedia.org/wiki/Btrfs)
    Btrfs,

    /// MINIX FS (https://en.wikipedia.org/wiki/MINIX_file_system)
    Minix,

    /// NILFS (https://en.wikipedia.org/wiki/NILFS)
    Nilfs,

    /// XFS (https://en.wikipedia.org/wiki/XFS)
    Xfs,

    /// APFS (https://en.wikipedia.org/wiki/Apple_File_System)
    Apfs,

    /// FUSE (https://en.wikipedia.org/wiki/Filesystem_in_Userspace)
    FuseBlk,

    /// Some unspecified filesystem.
    Other(String),
}

impl FileSystem {
    /// Checks if filesystem is used for a physical devices.
    #[inline]
    pub fn is_physical(&self) -> bool {
        !self.is_virtual()
    }

    /// Checks if filesystem is used for a virtual devices (such as `tmpfs` or `smb` mounts).
    #[inline]
    pub fn is_virtual(&self) -> bool {
        matches!(self, FileSystem::Other(..))
    }

    #[allow(dead_code)]
    #[inline]
    /// Returns a string literal identifying this filesystem.
    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::Reiser3 => "reiserfs",
            FileSystem::Reiser4 => "reiser4",
            FileSystem::FuseBlk => "fuseblk",
            FileSystem::ExFat => "exfat",
            FileSystem::F2fs => "f2fs",
            FileSystem::HfsPlus => "hfs+",
            FileSystem::Jfs => "jfs",
            FileSystem::Btrfs => "btrfs",
            FileSystem::Minix => "minix",
            FileSystem::Nilfs => "nilfs",
            FileSystem::Xfs => "xfs",
            FileSystem::Apfs => "apfs",
            FileSystem::Other(string) => string.as_str(),
        }
    }
}

impl FromStr for FileSystem {
    type Err = anyhow::Error;

    #[inline]

    fn from_str(s: &str) -> anyhow::Result<Self> {
        // Done like this as `eq_ignore_ascii_case` avoids a string allocation.
        Ok(if s.eq_ignore_ascii_case("ext2") {
            FileSystem::Ext2
        } else if s.eq_ignore_ascii_case("ext3") {
            FileSystem::Ext3
        } else if s.eq_ignore_ascii_case("ext4") {
            FileSystem::Ext4
        } else if s.eq_ignore_ascii_case("msdos") || s.eq_ignore_ascii_case("vfat") {
            FileSystem::VFat
        } else if s.eq_ignore_ascii_case("ntfs3") || s.eq_ignore_ascii_case("ntfs") {
            FileSystem::Ntfs
        } else if s.eq_ignore_ascii_case("zfs") {
            FileSystem::Zfs
        } else if s.eq_ignore_ascii_case("hfs") {
            FileSystem::Hfs
        } else if s.eq_ignore_ascii_case("reiserfs") {
            FileSystem::Reiser3
        } else if s.eq_ignore_ascii_case("reiser4") {
            FileSystem::Reiser4
        } else if s.eq_ignore_ascii_case("exfat") {
            FileSystem::ExFat
        } else if s.eq_ignore_ascii_case("f2fs") {
            FileSystem::F2fs
        } else if s.eq_ignore_ascii_case("hfsplus") {
            FileSystem::HfsPlus
        } else if s.eq_ignore_ascii_case("jfs") {
            FileSystem::Jfs
        } else if s.eq_ignore_ascii_case("btrfs") {
            FileSystem::Btrfs
        } else if s.eq_ignore_ascii_case("minix") {
            FileSystem::Minix
        } else if s.eq_ignore_ascii_case("nilfs") {
            FileSystem::Nilfs
        } else if s.eq_ignore_ascii_case("xfs") {
            FileSystem::Xfs
        } else if s.eq_ignore_ascii_case("apfs") {
            FileSystem::Apfs
        } else if s.eq_ignore_ascii_case("fuseblk") {
            FileSystem::FuseBlk
        } else {
            FileSystem::Other(s.to_string())
        })
    }
}

#[cfg(test)]
mod test {
    use super::FileSystem;
    use std::str::FromStr;

    #[test]
    fn file_system_from_str() {
        // Something supported
        assert_eq!(FileSystem::from_str("ext4").unwrap(), FileSystem::Ext4);
        assert_eq!(FileSystem::from_str("msdos").unwrap(), FileSystem::VFat);
        assert_eq!(FileSystem::from_str("vfat").unwrap(), FileSystem::VFat);

        // Something unsupported
        assert_eq!(
            FileSystem::from_str("this does not exist").unwrap(),
            FileSystem::Other("this does not exist".to_owned())
        );
    }
}