1use std::collections::HashMap;
8use std::path::PathBuf;
9use std::sync::OnceLock;
10
11#[cfg(target_os = "linux")]
12mod linux;
13#[cfg(target_os = "macos")]
14mod macos;
15
16#[cfg(target_os = "linux")]
17use linux::mounts;
18#[cfg(target_os = "macos")]
19use macos::mounts;
20
21#[derive(Clone)]
23pub struct MountedFs {
24 pub dest: PathBuf,
25 pub fstype: String,
26 pub source: String,
27}
28
29#[derive(Debug)]
30#[cfg(any(target_os = "macos", target_os = "linux"))]
31pub enum Error {
32 #[cfg(target_os = "macos")]
33 GetFSStatError(i32),
34 #[cfg(target_os = "linux")]
35 IOError(std::io::Error),
36}
37
38#[cfg(any(target_os = "macos", target_os = "linux"))]
39impl std::error::Error for Error {}
40
41#[cfg(any(target_os = "macos", target_os = "linux"))]
42impl std::fmt::Display for Error {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 match self {
46 #[cfg(target_os = "macos")]
47 Error::GetFSStatError(err) => write!(f, "getfsstat failed: {err}"),
48 #[cfg(target_os = "linux")]
49 Error::IOError(err) => write!(f, "failed to read /proc/mounts: {err}"),
50 }
51 }
52}
53
54pub(super) fn all_mounts() -> &'static HashMap<PathBuf, MountedFs> {
65 static ALL_MOUNTS: OnceLock<HashMap<PathBuf, MountedFs>> = OnceLock::new();
66
67 ALL_MOUNTS.get_or_init(|| {
68 #[allow(unused_mut)]
70 let mut mount_map: HashMap<PathBuf, MountedFs> = HashMap::new();
71
72 #[cfg(any(target_os = "linux", target_os = "macos"))]
73 if let Ok(mounts) = mounts() {
74 for mount in mounts {
75 mount_map.insert(mount.dest.clone(), mount);
76 }
77 }
78
79 mount_map
80 })
81}