#[allow(clippy::module_inception)]
pub(crate) mod files;
use std::path::{Component, Path, PathBuf};
use crate::error::{Result, ToolError};
fn resolve_path(tool: &str, path_str: &str, base_dir: &Option<PathBuf>) -> Result<PathBuf> {
let requested = Path::new(path_str);
let resolved = if let Some(base) = base_dir {
let normalized_base = normalize_path(base);
let normalized = if requested.is_absolute() {
normalize_path(requested)
} else {
normalize_path(&normalized_base.join(requested))
};
if !normalized.starts_with(&normalized_base) {
return Err(ToolError::ExecutionFailed {
tool: tool.to_string(),
message: format!("路径 '{}' 超出允许的目录范围", path_str),
}
.into());
}
normalized
} else {
normalize_path(requested)
};
Ok(resolved)
}
fn normalize_path(path: &Path) -> PathBuf {
let mut components = Vec::new();
for component in path.components() {
match component {
Component::ParentDir => {
if let Some(Component::Normal(_)) = components.last() {
components.pop();
}
}
Component::CurDir => {}
c => components.push(c),
}
}
components.iter().collect()
}