use rustix::cstr;
use rustix::fs::{openat, statat, AtFlags, Mode, OFlags, CWD};
use rustix::io::Errno;
use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
static WORKING: AtomicBool = AtomicBool::new(false);
static CHECKED: AtomicBool = AtomicBool::new(false);
#[inline]
pub(crate) fn beneath_supported() -> bool {
if WORKING.load(Relaxed) {
return true;
}
if CHECKED.load(Relaxed) {
return false;
}
check_beneath_supported()
}
#[cold]
fn check_beneath_supported() -> bool {
if let Ok(root) = openat(
CWD,
cstr!("/"),
OFlags::RDONLY | OFlags::CLOEXEC,
Mode::empty(),
) {
if let Err(Errno::NOTCAPABLE) = statat(root, cstr!(".."), AtFlags::RESOLVE_BENEATH) {
WORKING.store(true, Relaxed);
return true;
}
}
CHECKED.store(true, Relaxed);
false
}