| 1 | use std::str::FromStr; |
| 2 | |
| 3 | use crate::multi_eq_ignore_ascii_case; |
| 4 | |
| 5 | /// Known filesystems. Original list from |
| 6 | /// [heim](https://github.com/heim-rs/heim/blob/master/heim-disk/src/filesystem.rs). |
| 7 | /// |
| 8 | /// All physical filesystems should have their own enum element and all virtual |
| 9 | /// filesystems will go into the [`FileSystem::Other`] element. |
| 10 | #[derive(Debug, Eq, PartialEq, Hash, Clone)] |
| 11 | #[non_exhaustive] |
| 12 | pub enum FileSystem { |
| 13 | /// ext2 (<https://en.wikipedia.org/wiki/Ext2>) |
| 14 | Ext2, |
| 15 | |
| 16 | /// ext3 (<https://en.wikipedia.org/wiki/Ext3>) |
| 17 | Ext3, |
| 18 | |
| 19 | /// ext4 (<https://en.wikipedia.org/wiki/Ext4>) |
| 20 | Ext4, |
| 21 | |
| 22 | /// FAT (<https://en.wikipedia.org/wiki/File_Allocation_Table>) |
| 23 | VFat, |
| 24 | |
| 25 | /// exFAT (<https://en.wikipedia.org/wiki/ExFAT>) |
| 26 | ExFat, |
| 27 | |
| 28 | /// F2FS (<https://en.wikipedia.org/wiki/F2FS>) |
| 29 | F2fs, |
| 30 | |
| 31 | /// NTFS (<https://en.wikipedia.org/wiki/NTFS>) |
| 32 | Ntfs, |
| 33 | |
| 34 | /// ZFS (<https://en.wikipedia.org/wiki/ZFS>) |
| 35 | Zfs, |
| 36 | |
| 37 | /// HFS (<https://en.wikipedia.org/wiki/Hierarchical_File_System>) |
| 38 | Hfs, |
| 39 | |
| 40 | /// HFS+ (<https://en.wikipedia.org/wiki/HFS_Plus>) |
| 41 | HfsPlus, |
| 42 | |
| 43 | /// JFS (<https://en.wikipedia.org/wiki/JFS_(file_system)>) |
| 44 | Jfs, |
| 45 | |
| 46 | /// ReiserFS 3 (<https://en.wikipedia.org/wiki/ReiserFS>) |
| 47 | Reiser3, |
| 48 | |
| 49 | /// ReiserFS 4 (<https://en.wikipedia.org/wiki/Reiser4>) |
| 50 | Reiser4, |
| 51 | |
| 52 | /// Btrfs (<https://en.wikipedia.org/wiki/Btrfs>) |
| 53 | Btrfs, |
| 54 | |
| 55 | /// Bcachefs (<https://en.wikipedia.org/wiki/Bcachefs>) |
| 56 | Bcachefs, |
| 57 | |
| 58 | /// MINIX FS (<https://en.wikipedia.org/wiki/MINIX_file_system>) |
| 59 | Minix, |
| 60 | |
| 61 | /// NILFS (<https://en.wikipedia.org/wiki/NILFS>) |
| 62 | Nilfs, |
| 63 | |
| 64 | /// XFS (<https://en.wikipedia.org/wiki/XFS>) |
| 65 | Xfs, |
| 66 | |
| 67 | /// APFS (<https://en.wikipedia.org/wiki/Apple_File_System>) |
| 68 | Apfs, |
| 69 | |
| 70 | /// FUSE (<https://en.wikipedia.org/wiki/Filesystem_in_Userspace>) |
| 71 | FuseBlk, |
| 72 | |
| 73 | /// Some unspecified filesystem. |
| 74 | Other(String), |
| 75 | } |
| 76 | |
| 77 | impl FileSystem { |
| 78 | /// Checks if filesystem is used for a physical devices. |
| 79 | #[inline] |
| 80 | pub fn is_physical(&self) -> bool { |
| 81 | !self.is_virtual() |
| 82 | } |
| 83 | |
| 84 | /// Checks if filesystem is used for a virtual devices (such as `tmpfs` or |
| 85 | /// `smb` mounts). |
| 86 | #[inline] |
| 87 | pub fn is_virtual(&self) -> bool { |
| 88 | matches!(self, FileSystem::Other(..)) |
| 89 | } |
| 90 | |
| 91 | #[expect(dead_code)] |
| 92 | #[inline] |
| 93 | /// Returns a string literal identifying this filesystem. |
| 94 | pub fn as_str(&self) -> &str { |
| 95 | match self { |
| 96 | FileSystem::Ext2 => "ext2", |
| 97 | FileSystem::Ext3 => "ext3", |
| 98 | FileSystem::Ext4 => "ext4", |
| 99 | FileSystem::VFat => "vfat", |
| 100 | FileSystem::Ntfs => "ntfs", |
| 101 | FileSystem::Zfs => "zfs", |
| 102 | FileSystem::Hfs => "hfs", |
| 103 | FileSystem::Reiser3 => "reiserfs", |
| 104 | FileSystem::Reiser4 => "reiser4", |
| 105 | FileSystem::FuseBlk => "fuseblk", |
| 106 | FileSystem::ExFat => "exfat", |
| 107 | FileSystem::F2fs => "f2fs", |
| 108 | FileSystem::HfsPlus => "hfs+", |
| 109 | FileSystem::Jfs => "jfs", |
| 110 | FileSystem::Btrfs => "btrfs", |
| 111 | FileSystem::Bcachefs => "bcachefs", |
| 112 | FileSystem::Minix => "minix", |
| 113 | FileSystem::Nilfs => "nilfs", |
| 114 | FileSystem::Xfs => "xfs", |
| 115 | FileSystem::Apfs => "apfs", |
| 116 | FileSystem::Other(string) => string.as_str(), |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | impl FromStr for FileSystem { |
| 122 | type Err = anyhow::Error; |
| 123 | |
| 124 | #[inline] |
| 125 | fn from_str(s: &str) -> anyhow::Result<Self> { |
| 126 | // Done like this as `eq_ignore_ascii_case` avoids a string allocation. |
| 127 | Ok(if s.eq_ignore_ascii_case("ext2") { |
| 128 | FileSystem::Ext2 |
| 129 | } else if s.eq_ignore_ascii_case("ext3") { |
| 130 | FileSystem::Ext3 |
| 131 | } else if s.eq_ignore_ascii_case("ext4") { |
| 132 | FileSystem::Ext4 |
| 133 | } else if multi_eq_ignore_ascii_case!(s, "msdos" | "vfat") { |
| 134 | FileSystem::VFat |
| 135 | } else if multi_eq_ignore_ascii_case!(s, "ntfs3" | "ntfs") { |
| 136 | FileSystem::Ntfs |
| 137 | } else if s.eq_ignore_ascii_case("zfs") { |
| 138 | FileSystem::Zfs |
| 139 | } else if s.eq_ignore_ascii_case("hfs") { |
| 140 | FileSystem::Hfs |
| 141 | } else if s.eq_ignore_ascii_case("reiserfs") { |
| 142 | FileSystem::Reiser3 |
| 143 | } else if s.eq_ignore_ascii_case("reiser4") { |
| 144 | FileSystem::Reiser4 |
| 145 | } else if s.eq_ignore_ascii_case("exfat") { |
| 146 | FileSystem::ExFat |
| 147 | } else if s.eq_ignore_ascii_case("f2fs") { |
| 148 | FileSystem::F2fs |
| 149 | } else if s.eq_ignore_ascii_case("hfsplus") { |
| 150 | FileSystem::HfsPlus |
| 151 | } else if s.eq_ignore_ascii_case("jfs") { |
| 152 | FileSystem::Jfs |
| 153 | } else if s.eq_ignore_ascii_case("btrfs") { |
| 154 | FileSystem::Btrfs |
| 155 | } else if s.eq_ignore_ascii_case("bcachefs") { |
| 156 | FileSystem::Bcachefs |
| 157 | } else if s.eq_ignore_ascii_case("minix") { |
| 158 | FileSystem::Minix |
| 159 | } else if multi_eq_ignore_ascii_case!(s, "nilfs" | "nilfs2") { |
| 160 | FileSystem::Nilfs |
| 161 | } else if s.eq_ignore_ascii_case("xfs") { |
| 162 | FileSystem::Xfs |
| 163 | } else if s.eq_ignore_ascii_case("apfs") { |
| 164 | FileSystem::Apfs |
| 165 | } else if s.eq_ignore_ascii_case("fuseblk") { |
| 166 | FileSystem::FuseBlk |
| 167 | } else { |
| 168 | FileSystem::Other(s.to_string()) |
| 169 | }) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | #[cfg(test)] |
| 174 | mod test { |
| 175 | use std::str::FromStr; |
| 176 | |
| 177 | use super::FileSystem; |
| 178 | |
| 179 | #[test] |
| 180 | fn file_system_from_str() { |
| 181 | // Something supported |
| 182 | assert_eq!(FileSystem::from_str("ext4").unwrap(), FileSystem::Ext4); |
| 183 | assert_eq!(FileSystem::from_str("msdos").unwrap(), FileSystem::VFat); |
| 184 | assert_eq!(FileSystem::from_str("vfat").unwrap(), FileSystem::VFat); |
| 185 | |
| 186 | // Something unsupported |
| 187 | assert_eq!( |
| 188 | FileSystem::from_str("this does not exist").unwrap(), |
| 189 | FileSystem::Other("this does not exist".to_owned()) |
| 190 | ); |
| 191 | } |
| 192 | } |