use std::path::{Path, PathBuf};
#[must_use]
pub fn socket_path(root: &Path) -> PathBuf {
let canonical = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
let hash = format!(
"{:016x}",
xxhash_rust::xxh64::xxh64(canonical.to_string_lossy().as_bytes(), 0,)
);
if let Ok(xdg) = std::env::var("XDG_RUNTIME_DIR") {
let dir = PathBuf::from(xdg).join("ixd");
return dir.join(format!("{hash}.sock"));
}
if let Ok(home) = std::env::var("HOME") {
let dir = PathBuf::from(home).join(".local/run/ixd");
return dir.join(format!("{hash}.sock"));
}
#[cfg(unix)]
let uid = unsafe { libc::getuid() };
#[cfg(not(unix))]
let uid = 0u32;
PathBuf::from(format!("/tmp/ixd-{uid}-{hash}.sock"))
}
pub(super) fn ensure_socket_dir(path: &Path) -> std::io::Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
Ok(())
}