use std::path::{Path, PathBuf};
pub(crate) struct Scratch(PathBuf);
impl Scratch {
pub(crate) fn new(name: &str) -> Scratch {
let dir = Scratch::path(name);
std::fs::remove_dir_all(&dir).ok();
std::fs::create_dir_all(&dir).expect("scratch dir");
Scratch(dir)
}
pub(crate) fn path(name: &str) -> PathBuf {
std::env::temp_dir().join(format!("fats-{name}-{}", std::process::id()))
}
}
impl std::ops::Deref for Scratch {
type Target = Path;
fn deref(&self) -> &Path {
&self.0
}
}
impl Drop for Scratch {
fn drop(&mut self) {
std::fs::remove_dir_all(&self.0).ok();
}
}
pub(crate) fn auth(dir: &Path, keys: &[(&str, &str)]) {
let keys: serde_json::Map<String, serde_json::Value> =
keys.iter().map(|(n, k)| (n.to_string(), (*k).into())).collect();
std::fs::create_dir_all(dir).expect("key dir");
let file = dir.join("auth.json");
std::fs::write(&file, serde_json::json!({"version": 1, "keys": keys}).to_string()).expect("auth.json");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700)).expect("chmod key dir");
std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o600)).expect("chmod auth.json");
}
}