use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use crate::error::CliCoreError;
fn env_path(key: &str) -> Option<PathBuf> {
std::env::var(key)
.ok()
.filter(|v| !v.is_empty())
.map(PathBuf::from)
}
fn home_config_dir() -> Option<PathBuf> {
env_path("HOME").map(|home| home.join(".config"))
}
#[must_use]
pub fn config_base_dir() -> Option<PathBuf> {
env_path("XDG_CONFIG_HOME")
.or_else(|| {
if cfg!(windows) {
env_path("APPDATA").or_else(home_config_dir)
} else {
home_config_dir().or_else(|| env_path("APPDATA"))
}
})
.filter(|p| p.is_absolute())
}
#[must_use]
pub fn home_dir() -> Option<PathBuf> {
if cfg!(windows) {
env_path("USERPROFILE").or_else(|| env_path("HOME"))
} else {
env_path("HOME")
}
.filter(|p| p.is_absolute())
}
#[must_use]
pub fn is_safe_path_component(s: &str) -> bool {
const FORBIDDEN: &[char] = &['/', '\\', ':', '*', '?', '"', '<', '>', '|'];
if s.contains(FORBIDDEN) || s.bytes().any(|b| b < 0x20 || b == 0x7F) {
return false;
}
if s.starts_with(' ') || s.ends_with('.') || s.ends_with(' ') {
return false;
}
const RESERVED: &[&str] = &[
"CON", "PRN", "AUX", "NUL", "COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7",
"COM8", "COM9", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8",
"LPT9",
];
let stem = Path::new(s)
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or(s);
if RESERVED.iter().any(|r| stem.eq_ignore_ascii_case(r)) {
return false;
}
let mut components = Path::new(s).components();
matches!(components.next(), Some(std::path::Component::Normal(_)))
&& components.next().is_none()
}
pub fn write_string_atomic(path: &Path, contents: &str) -> crate::Result<()> {
if let Some(parent) = path.parent() {
let parent_existed = parent.is_dir();
std::fs::create_dir_all(parent)
.map_err(|e| CliCoreError::message(format!("failed to create directory: {e}")))?;
#[cfg(unix)]
if !parent_existed {
use std::os::unix::fs::PermissionsExt as _;
if let Err(e) = std::fs::set_permissions(parent, std::fs::Permissions::from_mode(0o700))
{
tracing::debug!(
path = %parent.display(),
error = %e,
"could not restrict directory permissions"
);
}
}
}
static TMP_COUNTER: AtomicU64 = AtomicU64::new(0);
let unique = TMP_COUNTER.fetch_add(1, Ordering::Relaxed);
let pid = std::process::id();
let tmp_path = path.with_file_name(format!(
"{}.{pid:x}.{unique:x}.tmp",
path.file_name().and_then(|s| s.to_str()).unwrap_or("tmp"),
));
write_tmp_file(&tmp_path, contents)?;
if let Err(e) = std::fs::rename(&tmp_path, path) {
std::fs::remove_file(&tmp_path).ok();
return Err(CliCoreError::message(format!(
"failed to finalize {}: {e}",
path.display()
)));
}
Ok(())
}
fn write_tmp_file(tmp_path: &Path, contents: &str) -> crate::Result<()> {
use std::io::Write as _;
let mut opts = std::fs::OpenOptions::new();
opts.write(true).create_new(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt as _;
opts.mode(0o600);
}
let mut file = opts.open(tmp_path).map_err(|e| {
CliCoreError::message(format!("failed to write {}: {e}", tmp_path.display()))
})?;
file.write_all(contents.as_bytes())
.map_err(|e| CliCoreError::message(format!("failed to write {}: {e}", tmp_path.display())))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::test_env::{EnvVarGuard, lock, with_xdg_config_home};
fn with_home<F: FnOnce() -> R, R>(value: &Path, f: F) -> R {
let _lock = lock();
let _restore = EnvVarGuard::set("HOME", Some(value));
f()
}
#[test]
fn safe_path_component_basic() {
assert!(is_safe_path_component("godaddy"));
assert!(!is_safe_path_component(".."));
assert!(!is_safe_path_component(""));
assert!(!is_safe_path_component("a/b"));
assert!(!is_safe_path_component("NUL"));
}
#[test]
fn safe_path_component_rejects_windows_reserved_names() {
for name in &[
"CON", "con", "NUL", "nul", "COM1", "LPT9", "CON.txt", "NUL.json",
] {
assert!(
!is_safe_path_component(name),
"{name:?} should be rejected as a Windows reserved name"
);
}
}
#[test]
fn safe_path_component_rejects_control_and_space_edges() {
assert!(!is_safe_path_component(" prod"), "leading space");
assert!(!is_safe_path_component("prod\x7f"), "DEL byte");
assert!(!is_safe_path_component("prod."), "trailing dot");
assert!(!is_safe_path_component("prod "), "trailing space");
}
#[test]
fn safe_path_component_accepts_normal_values() {
for name in &["dev", "prod", "staging", "my-app", "my_app", "app.v2"] {
assert!(is_safe_path_component(name), "{name:?} should be accepted");
}
}
#[test]
fn config_base_dir_rejects_relative_xdg() {
with_xdg_config_home(Path::new("."), || {
assert!(
config_base_dir().is_none(),
"relative XDG_CONFIG_HOME should be rejected"
);
});
}
#[test]
fn config_base_dir_honors_xdg() {
let dir = std::env::temp_dir().join("cli-engine-fs-base-test");
with_xdg_config_home(&dir, || {
assert_eq!(config_base_dir(), Some(dir.clone()));
});
}
#[test]
fn home_dir_honors_home_env() {
let dir = std::env::temp_dir().join("cli-engine-fs-home-test");
with_home(&dir, || {
assert_eq!(home_dir(), Some(dir.clone()));
});
}
#[test]
fn home_dir_rejects_relative() {
with_home(Path::new("."), || {
assert!(home_dir().is_none(), "relative HOME should be rejected");
});
}
#[tokio::test]
async fn write_string_atomic_round_trip_creates_dirs() {
let tmp = tempfile::tempdir().expect("tempdir");
let path = tmp.path().join("nested").join("file.txt");
write_string_atomic(&path, "hello").expect("write");
assert_eq!(std::fs::read_to_string(&path).expect("read"), "hello");
write_string_atomic(&path, "world").expect("rewrite");
assert_eq!(std::fs::read_to_string(&path).expect("read"), "world");
let strays: Vec<_> = std::fs::read_dir(path.parent().expect("parent"))
.expect("read_dir")
.filter_map(|e| e.ok())
.filter(|e| e.file_name().to_string_lossy().ends_with(".tmp"))
.collect();
assert!(strays.is_empty(), "temp files should be renamed away");
}
#[cfg(unix)]
#[tokio::test]
async fn write_string_atomic_sets_owner_only_mode() {
use std::os::unix::fs::PermissionsExt as _;
let tmp = tempfile::tempdir().expect("tempdir");
let path = tmp.path().join("secret.txt");
write_string_atomic(&path, "s3cr3t").expect("write");
let mode = std::fs::metadata(&path).expect("meta").permissions().mode() & 0o777;
assert_eq!(mode, 0o600, "file should be owner read/write only");
}
}