1#![allow(clippy::cast_possible_wrap)]
7
8use crate::FsKind;
9
10#[cfg(target_os = "macos")]
11pub type Magic = [i8; 16];
12
13#[cfg(target_os = "macos")]
14type CharType = i8;
15
16#[cfg(target_os = "linux")]
17pub type Magic = rustix::fs::FsWord;
18
19#[cfg(windows)]
20pub type Magic = [u16; 16];
21
22#[cfg(windows)]
23type CharType = u16;
24
25#[cfg(any(target_os = "macos", target_os = "windows"))]
26const fn to_magic<const L: usize>(s: &'static [u8; L]) -> Magic {
27 let mut buf = [0; 16];
28
29 let mut i = 0;
30 while i < L {
31 buf[i] = s[i] as CharType;
32 i += 1;
33 }
34
35 buf
36}
37
38#[cfg(target_os = "linux")]
42const FAT32: Magic = 0x4d44;
43
44#[cfg(target_os = "macos")]
45const FAT32: Magic = to_magic(b"msdos");
46
47#[cfg(windows)]
48const FAT32: Magic = to_magic(b"FAT32");
49
50#[cfg(target_os = "linux")]
54const EXFAT: Magic = 0x2011BAB0;
55
56#[cfg(target_os = "macos")]
57const EXFAT: Magic = to_magic(b"exfat");
58
59#[cfg(windows)]
60const EXFAT: Magic = to_magic(b"exFAT");
61
62#[cfg(target_os = "macos")]
66const APFS: Magic = to_magic(b"apfs");
67
68#[cfg(target_os = "macos")]
72const HFS: Magic = to_magic(b"hfs");
73
74#[cfg(target_os = "linux")]
78const EXT2: Magic = 0xef53;
79
80#[cfg(target_os = "linux")]
84const BTRFS: Magic = 0x9123683E;
85
86#[cfg(target_os = "linux")]
90const F2FS: Magic = 0xF2F52010;
91
92#[cfg(target_os = "linux")]
96const BCACHEFS: Magic = 0xca451a4e;
97
98#[cfg(target_os = "linux")]
102const XFS: Magic = 0x58465342;
103
104#[cfg(target_os = "linux")]
108const FUSE: Magic = 0x65735546;
109
110#[cfg(target_os = "macos")]
111const FUSE: Magic = to_magic(b"fusefs");
112
113#[cfg(target_os = "linux")]
117const NTFS: Magic = 0x5346544e;
118
119#[cfg(windows)]
120const NTFS: Magic = to_magic(b"NTFS");
121
122#[cfg(target_os = "macos")]
123const NTFS: Magic = to_magic(b"ntfs");
124
125#[must_use]
126pub fn which_kind(magic: Magic) -> FsKind {
127 match magic {
128 FAT32 => FsKind::Fat32,
129 EXFAT => FsKind::ExFat,
130 NTFS => FsKind::Ntfs,
131
132 #[cfg(unix)]
133 FUSE => FsKind::Fuse,
134
135 #[cfg(target_os = "macos")]
136 APFS => FsKind::Apfs,
137 #[cfg(target_os = "macos")]
138 HFS => FsKind::Hfs,
139
140 #[cfg(target_os = "linux")]
141 EXT2 => FsKind::Ext2,
142 #[cfg(target_os = "linux")]
143 BTRFS => FsKind::Btrfs,
144 #[cfg(target_os = "linux")]
145 F2FS => FsKind::F2fs,
146 #[cfg(target_os = "linux")]
147 BCACHEFS => FsKind::Bcachefs,
148 #[cfg(target_os = "linux")]
149 XFS => FsKind::Xfs,
150
151 idk => FsKind::Unknown(idk),
152 }
153}