use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::RwLock;
static SERVED: RwLock<Vec<(u64, Served)>> = RwLock::new(Vec::new());
static NEXT_FENCE: AtomicU64 = AtomicU64::new(0);
#[cfg_attr(not(feature = "sandbox"), allow(dead_code))]
enum Served {
Socket(PathBuf),
Port(SocketAddr),
Secret(PathBuf),
}
#[must_use = "the fence comes down when this is dropped"]
pub struct Fence(u64);
impl Drop for Fence {
fn drop(&mut self) {
let mut served = SERVED.write().unwrap_or_else(|e| e.into_inner());
served.retain(|(id, _)| *id != self.0);
}
}
fn fence(entry: Served) -> Fence {
let id = NEXT_FENCE.fetch_add(1, Ordering::Relaxed);
let mut served = SERVED.write().unwrap_or_else(|e| e.into_inner());
served.push((id, entry));
Fence(id)
}
pub fn fence_socket(path: &Path) -> Fence {
fence(Served::Socket(
path.canonicalize().unwrap_or_else(|_| path.to_path_buf()),
))
}
pub fn fence_port(addr: SocketAddr) -> Fence {
fence(Served::Port(addr))
}
pub fn fence_secret(path: &Path) -> Fence {
fence(Served::Secret(
path.canonicalize().unwrap_or_else(|_| path.to_path_buf()),
))
}
#[cfg(feature = "sandbox")]
fn fenced<T: Ord>(pick: impl Fn(&Served) -> Option<T>) -> Vec<T> {
let served = SERVED.read().unwrap_or_else(|e| e.into_inner());
let mut found: Vec<T> = served.iter().filter_map(|(_, s)| pick(s)).collect();
found.sort();
found.dedup();
found
}
#[cfg(all(feature = "sandbox", any(target_os = "macos", target_os = "linux")))]
pub(super) fn fenced_secrets() -> Vec<PathBuf> {
fenced(|s| match s {
Served::Secret(p) if p.exists() => Some(p.clone()),
_ => None,
})
}
#[cfg(all(feature = "sandbox", any(target_os = "macos", target_os = "linux")))]
pub(super) fn fenced_sockets() -> Vec<PathBuf> {
fenced(|s| match s {
Served::Socket(p) if is_socket(p) => Some(p.clone()),
_ => None,
})
}
#[cfg(all(feature = "sandbox", any(target_os = "macos", target_os = "linux")))]
pub(super) fn fenced_ports() -> Vec<SocketAddr> {
fenced(|s| match s {
Served::Port(addr) => Some(*addr),
_ => None,
})
}
#[cfg(all(feature = "sandbox", any(target_os = "macos", target_os = "linux")))]
pub(super) fn is_socket(path: &Path) -> bool {
use std::os::unix::fs::FileTypeExt;
path.symlink_metadata()
.is_ok_and(|m| m.file_type().is_socket())
}