use std::env;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Clone)]
pub struct OverlayFiles {
pub lower: PathBuf,
pub upper: PathBuf,
pub mount_point: PathBuf,
}
impl OverlayFiles {
pub fn new(lower: PathBuf) -> Self {
let name = lower.file_name().unwrap_or_default().to_string_lossy();
let parent = lower.parent().unwrap_or(Path::new("."));
let pid = std::process::id();
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default();
let session_id = format!("{:x}", pid ^ now.subsec_micros());
let full_ts = now.as_nanos();
let mount_name = format!("mount_{}_{}_{}", name, session_id, full_ts);
Self {
lower: lower.clone(),
upper: parent.join(format!("{}_upper", name)),
mount_point: env::temp_dir().join(mount_name),
}
}
pub fn mountpoint_as_home(&mut self) {
let name = self.lower.file_name().unwrap_or_default().to_string_lossy();
let home_cache = env::var("XDG_CACHE_HOME")
.map(PathBuf::from)
.ok()
.or_else(|| env::var("HOME").map(|h| PathBuf::from(h).join(".cache")).ok());
if let Some(cache_path) = home_cache {
let _ = std::fs::create_dir_all(&cache_path);
let new_mount = cache_path.join(format!("mount_{}", name));
self.mount_point = new_mount;
}
}
}