use std::path::{Path, PathBuf};
use anyhow::Result;
use super::ToolContext;
pub(super) fn strip_verbatim(p: &Path) -> PathBuf {
let s = p.display().to_string();
match s.strip_prefix(r"\\?\") {
Some(rest) => PathBuf::from(rest),
None => p.to_path_buf(),
}
}
pub(super) fn app_dirs(ctx: &ToolContext) -> Vec<PathBuf> {
ctx.storage
.json()
.app_dirs()
.iter()
.filter_map(|d| d.canonicalize().ok())
.map(|d| strip_verbatim(&d))
.collect()
}
pub(super) fn refuse_app_dirs(ctx: &ToolContext, path: &Path) -> Result<()> {
let path = strip_verbatim(path);
if app_dirs(ctx).iter().any(|dir| path.starts_with(dir)) {
anyhow::bail!(ctx.loc.t("tool.fs.err.app_dir").to_string());
}
Ok(())
}
pub(super) fn refuse_dangling_link(path: &Path, loc: &crate::shared::i18n::Locale) -> Result<()> {
if path
.symlink_metadata()
.is_ok_and(|m| m.file_type().is_symlink())
{
anyhow::bail!(loc.t("tool.fs.err.dangling_link").to_string());
}
Ok(())
}