use std::fs;
use std::path::Path;
use crate::core::path_util::{path_contains, same_path};
pub(crate) fn entry_name(entry: &fs::DirEntry) -> String {
entry.file_name().to_string_lossy().into_owned()
}
pub(crate) fn agent_root_exists(agent_dir: &Path) -> bool {
agent_dir.parent().map(|p| p.exists()).unwrap_or(false)
}
pub(crate) fn points_to(link: &Path, target: &Path) -> bool {
match fs::read_link(link) {
Ok(raw) => {
let resolved = if raw.is_absolute() {
raw
} else {
link.parent().unwrap_or(Path::new(".")).join(raw)
};
same_path(&resolved, target)
}
Err(_) => false,
}
}
pub(crate) fn is_legacy_link(entry: &fs::DirEntry, canonical: &Path) -> bool {
let path = entry.path();
let is_symlink = fs::symlink_metadata(&path)
.map(|m| m.file_type().is_symlink())
.unwrap_or(false);
if !is_symlink {
return false;
}
match fs::read_link(&path) {
Ok(raw) => {
let resolved = if raw.is_absolute() {
raw
} else {
path.parent().unwrap_or(Path::new(".")).join(raw)
};
path_contains(canonical, &resolved)
}
Err(_) => false,
}
}
pub(crate) fn classify(entries: &[fs::DirEntry], canonical: &Path) -> (Vec<String>, Vec<String>) {
let mut skills = Vec::new();
let mut others = Vec::new();
for entry in entries {
if is_legacy_link(entry, canonical) {
continue;
}
let name = entry_name(entry);
if fs::metadata(entry.path())
.map(|m| m.is_dir())
.unwrap_or(false)
{
skills.push(name);
} else {
others.push(name);
}
}
(skills, others)
}