use std::path::Path;
#[cfg(target_os = "linux")]
const TMPFS_MAGIC: u32 = 0x0102_1994;
#[cfg(target_os = "linux")]
const RAMFS_MAGIC: u32 = 0x8584_58f6;
#[cfg(target_os = "linux")]
pub fn is_ram_backed(path: &Path) -> Option<bool> {
use std::ffi::CString;
use std::os::unix::ffi::OsStrExt;
let c_path = CString::new(path.as_os_str().as_bytes()).ok()?;
let mut buf = unsafe { std::mem::zeroed::<libc::statfs>() };
let rc = unsafe { libc::statfs(c_path.as_ptr(), &mut buf) };
if rc != 0 {
return None;
}
let fs_type = buf.f_type as u32;
Some(fs_type == TMPFS_MAGIC || fs_type == RAMFS_MAGIC)
}
#[cfg(not(target_os = "linux"))]
pub fn is_ram_backed(_path: &Path) -> Option<bool> {
None
}
#[cfg(all(test, target_os = "linux"))]
mod tests {
use super::*;
#[test]
fn detects_tmpfs_dev_shm() {
let shm = Path::new("/dev/shm");
if !shm.exists() {
eprintln!("skipping detects_tmpfs_dev_shm: /dev/shm absent");
return;
}
assert_eq!(
is_ram_backed(shm),
Some(true),
"/dev/shm must be detected as RAM-backed"
);
}
#[test]
fn real_disk_not_flagged() {
let cwd = std::env::current_dir().expect("cwd");
assert_ne!(
is_ram_backed(&cwd),
Some(true),
"a real-disk working directory must not be flagged RAM-backed"
);
}
#[test]
fn missing_path_is_undetectable() {
assert_eq!(
is_ram_backed(Path::new("/nonexistent/tylertoo-fs-probe-273")),
None,
"an unresolvable path must yield None, not a warning"
);
}
}