use crate::error::{Error, Result};
use std::fs;
use std::path::PathBuf;
pub fn upgrade_cache_dir() -> Result<PathBuf> {
let project_dirs = directories::ProjectDirs::from("", "", "ant").ok_or_else(|| {
Error::Upgrade("Cannot determine platform data directory for upgrade cache".to_string())
})?;
let cache_dir = project_dirs.data_dir().join("upgrades");
fs::create_dir_all(&cache_dir)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Ok(meta) = fs::metadata(&cache_dir) {
let mut perms = meta.permissions();
perms.set_mode(0o700);
if let Err(e) = fs::set_permissions(&cache_dir, perms) {
crate::logging::warn!(
"Could not tighten upgrade cache dir permissions to 0700 ({e}); \
ML-DSA re-verification still protects cached archives"
);
}
}
}
Ok(cache_dir)
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn test_upgrade_cache_dir_returns_path() {
let dir = upgrade_cache_dir().unwrap();
assert!(dir.exists());
assert!(dir.ends_with("upgrades"));
}
}