heim_disk/
filesystem.rs

1use heim_common::prelude::*;
2
3use std::str::FromStr;
4
5/// Known filesystems.
6///
7/// All physical filesystems should have their own enum element
8/// and all virtual filesystems will go into the `Other` element.
9#[derive(Debug, Eq, PartialEq, Hash, Clone)]
10pub enum FileSystem {
11    /// ext2 (https://en.wikipedia.org/wiki/Ext2)
12    Ext2,
13
14    /// ext3 (https://en.wikipedia.org/wiki/Ext3)
15    Ext3,
16
17    /// ext4 (https://en.wikipedia.org/wiki/Ext4)
18    Ext4,
19
20    /// FAT (https://en.wikipedia.org/wiki/File_Allocation_Table)
21    VFat,
22
23    /// exFAT (https://en.wikipedia.org/wiki/ExFAT)
24    ExFat,
25
26    /// F2FS (https://en.wikipedia.org/wiki/F2FS)
27    F2fs,
28
29    /// NTFS (https://en.wikipedia.org/wiki/NTFS)
30    Ntfs,
31
32    /// ZFS (https://en.wikipedia.org/wiki/ZFS)
33    Zfs,
34
35    /// HFS (https://en.wikipedia.org/wiki/Hierarchical_File_System)
36    Hfs,
37
38    /// HFS+ (https://en.wikipedia.org/wiki/HFS_Plus)
39    HfsPlus,
40
41    /// JFS (https://en.wikipedia.org/wiki/JFS_(file_system))
42    Jfs,
43
44    /// ReiserFS 3 (https://en.wikipedia.org/wiki/ReiserFS)
45    Reiser3,
46
47    /// ReiserFS 4 (https://en.wikipedia.org/wiki/Reiser4)
48    Reiser4,
49
50    /// Btrfs (https://en.wikipedia.org/wiki/Btrfs)
51    Btrfs,
52
53    /// MINIX FS (https://en.wikipedia.org/wiki/MINIX_file_system)
54    Minix,
55
56    /// NILFS (https://en.wikipedia.org/wiki/NILFS)
57    Nilfs,
58
59    /// XFS (https://en.wikipedia.org/wiki/XFS)
60    Xfs,
61
62    /// APFS (https://en.wikipedia.org/wiki/Apple_File_System)
63    Apfs,
64
65    // TODO: Should it be considered as a physical FS?
66    /// FUSE (https://en.wikipedia.org/wiki/Filesystem_in_Userspace)
67    FuseBlk,
68
69    // TODO: Extend list
70    /// Some unspecified filesystem.
71    Other(String),
72
73    #[doc(hidden)]
74    __Nonexhaustive,
75}
76
77impl FileSystem {
78    /// Checks if filesystem is used for a physical devices
79    pub fn is_physical(&self) -> bool {
80        match self {
81            FileSystem::Other(..) => false,
82            _ => true,
83        }
84    }
85
86    /// Checks if filesystem is used for a virtual devices (such as `tmpfs` or `smb` mounts)
87    pub fn is_virtual(&self) -> bool {
88        !self.is_physical()
89    }
90
91    /// Returns a string identifying this filesystem.
92    pub fn as_str(&self) -> &str {
93        match self {
94            FileSystem::Ext2 => "ext2",
95            FileSystem::Ext3 => "ext3",
96            FileSystem::Ext4 => "ext4",
97            FileSystem::VFat => "vfat",
98            FileSystem::Ntfs => "ntfs",
99            FileSystem::Zfs => "zfs",
100            FileSystem::Hfs => "hfs",
101            FileSystem::Reiser3 => "reiserfs",
102            FileSystem::Reiser4 => "reiser4",
103            FileSystem::FuseBlk => "fuseblk",
104            FileSystem::ExFat => "exfat",
105            FileSystem::F2fs => "f2fs",
106            FileSystem::HfsPlus => "hfs+",
107            FileSystem::Jfs => "jfs",
108            FileSystem::Btrfs => "btrfs",
109            FileSystem::Minix => "minix",
110            FileSystem::Nilfs => "nilfs",
111            FileSystem::Xfs => "xfs",
112            FileSystem::Apfs => "apfs",
113            FileSystem::Other(string) => string.as_str(),
114            _ => unimplemented!(),
115        }
116    }
117}
118
119impl FromStr for FileSystem {
120    type Err = Error;
121
122    fn from_str(s: &str) -> Result<Self> {
123        match () {
124            _ if s.eq_ignore_ascii_case("ext2") => Ok(FileSystem::Ext2),
125            _ if s.eq_ignore_ascii_case("ext3") => Ok(FileSystem::Ext3),
126            _ if s.eq_ignore_ascii_case("ext4") => Ok(FileSystem::Ext4),
127            _ if s.eq_ignore_ascii_case("vfat") => Ok(FileSystem::VFat),
128            _ if s.eq_ignore_ascii_case("ntfs") => Ok(FileSystem::Ntfs),
129            _ if s.eq_ignore_ascii_case("zfs") => Ok(FileSystem::Zfs),
130            _ if s.eq_ignore_ascii_case("hfs") => Ok(FileSystem::Hfs),
131            _ if s.eq_ignore_ascii_case("reiserfs") => Ok(FileSystem::Reiser3),
132            _ if s.eq_ignore_ascii_case("reiser4") => Ok(FileSystem::Reiser4),
133            _ if s.eq_ignore_ascii_case("exfat") => Ok(FileSystem::ExFat),
134            _ if s.eq_ignore_ascii_case("f2fs") => Ok(FileSystem::F2fs),
135            _ if s.eq_ignore_ascii_case("hfsplus") => Ok(FileSystem::HfsPlus),
136            _ if s.eq_ignore_ascii_case("jfs") => Ok(FileSystem::Jfs),
137            _ if s.eq_ignore_ascii_case("btrfs") => Ok(FileSystem::Btrfs),
138            _ if s.eq_ignore_ascii_case("minix") => Ok(FileSystem::Minix),
139            _ if s.eq_ignore_ascii_case("nilfs") => Ok(FileSystem::Nilfs),
140            _ if s.eq_ignore_ascii_case("xfs") => Ok(FileSystem::Xfs),
141            _ if s.eq_ignore_ascii_case("apfs") => Ok(FileSystem::Apfs),
142
143            _ if s.eq_ignore_ascii_case("fuseblk") => Ok(FileSystem::FuseBlk),
144            _ => Ok(FileSystem::Other(s.to_string())),
145        }
146    }
147}