use crate::disk::AsarError;
use std::path::{Path, PathBuf};
pub fn ensure_within(container: &Path, file_path: &str) -> Result<PathBuf, AsarError> {
let resolved_container = container
.canonicalize()
.unwrap_or_else(|_| container.to_path_buf());
let candidate = resolved_container.join(file_path);
let resolved_path = candidate
.canonicalize()
.unwrap_or_else(|_| candidate.clone());
if !resolved_path.starts_with(&resolved_container)
&& resolved_path != resolved_container
{
return Err(AsarError::PathTraversal(format!(
"Path \"{}\" resolves to \"{}\" which is outside \"{}\"",
file_path,
resolved_path.display(),
resolved_container.display()
)));
}
Ok(resolved_path)
}