use std::path::PathBuf;
pub fn home() -> PathBuf {
std::env::var_os("HOME")
.or_else(|| std::env::var_os("USERPROFILE"))
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("."))
}
pub fn state_dir() -> PathBuf {
if let Some(dir) = std::env::var_os("IPH_STATE_DIR") {
return PathBuf::from(dir);
}
if cfg!(windows) {
return std::env::var_os("LOCALAPPDATA")
.map(PathBuf::from)
.unwrap_or_else(|| home().join("AppData/Local"))
.join("iph");
}
home().join("Library/Application Support/iph")
}
pub fn config_dir() -> PathBuf {
if let Some(dir) = std::env::var_os("IPH_CONFIG_DIR") {
return PathBuf::from(dir);
}
if cfg!(windows) {
return std::env::var_os("APPDATA")
.map(PathBuf::from)
.unwrap_or_else(|| home().join("AppData/Roaming"))
.join("iph");
}
home().join(".config/iph")
}
pub fn ddi_cache() -> PathBuf {
state_dir().join("ddi")
}
pub fn socket(udid: &str) -> PathBuf {
state_dir().join(format!("{udid}.sock"))
}
pub fn daemon_pid(udid: &str) -> PathBuf {
state_dir().join(format!("{udid}.pid"))
}
pub fn daemon_log(udid: &str) -> PathBuf {
state_dir().join(format!("{udid}.log"))
}
pub fn dnd_record(udid: &str) -> PathBuf {
state_dir().join(format!("{udid}.dnd.json"))
}
pub fn config_file() -> PathBuf {
config_dir().join("config.toml")
}
pub fn personal_scripts() -> PathBuf {
config_dir().join("scripts")
}
pub fn ensure_private_dir(dir: &std::path::Path) -> std::io::Result<()> {
std::fs::create_dir_all(dir)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700))?;
}
Ok(())
}
pub fn private_file() -> std::fs::OpenOptions {
#[allow(unused_mut)]
let mut options = std::fs::OpenOptions::new();
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600);
}
options
}