use super::frozen_walk::{Skip, Walk, is_frozen, reach_below};
use crate::verdict::Verdict;
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Reach {
Found(String),
Unlisted(String),
}
impl Reach {
pub(crate) fn path(&self) -> &str {
match self {
Reach::Found(p) | Reach::Unlisted(p) => p,
}
}
}
pub(super) fn with_reached(spec: &crate::registry::types::BehaviorSpec, operands: &[&str], tokens: &[crate::parse::Token]) -> Vec<String> {
let mut all: Vec<String> = operands.iter().map(|p| (*p).to_string()).collect();
if spec.unbounded_flags.iter().any(|f| super::behavior_flag_present(tokens, f)) {
let removes = spec.operation == crate::engine::facet::Operation::Destroy;
let reached = operands.iter().filter_map(|p| frozen_below(p, true));
all.extend(reached.filter(|r| !removes || matches!(r, Reach::Found(_))).map(|r| r.path().to_string()));
}
all
}
pub(crate) fn landed(source: &str, dest: &str, follow: bool) -> Vec<String> {
let dest = dest.trim_end_matches('/');
let own = source.rsplit('/').next().filter(|name| !source.ends_with('/') && is_frozen(name));
if let Some(name) = own {
return vec![format!("{dest}/{name}/f")];
}
let walk = if follow { Walk::follow() } else { Walk::plain(true) };
reach_below(source, walk)
.iter()
.map(|reached| match reached.path().strip_prefix(source.trim_end_matches('/')) {
Some(below) if below.starts_with('/') => format!("{dest}{below}"),
_ => reached.path().to_string(),
})
.collect()
}
pub(crate) fn read_through(source: &str) -> Vec<String> {
reach_below(source, Walk::follow()).iter().map(|r| r.path().to_string()).collect()
}
pub(crate) fn lands_frozen(source: &str, operands: &[&str], follow: bool) -> bool {
let Some(dest) = operands.last() else { return false };
let denied = |at: &String| super::write_target_verdict(at) == Verdict::Denied;
landed(source, dest, follow).iter().any(denied)
|| (follow && read_through(source).iter().any(|at| super::read_tree_verdict(at) == Verdict::Denied))
}
pub(crate) fn copy_reaches(p: &str, operands: &[&str], links: super::link_mode::Links, (tree, dest): (bool, bool)) -> bool {
(tree && lands_frozen(p, operands, links.follow))
|| (dest && links.dest == super::link_mode::DestLinks::WrittenThrough && writes_through(p))
}
pub(crate) fn writes_through(dest: &str) -> bool {
reach_below(dest, Walk::follow())
.iter()
.any(|r| super::write_target_verdict(r.path()) == Verdict::Denied)
}
pub(crate) fn judged_over(base: &str, walk: Walk, judge: impl Fn(&str) -> Verdict) -> Verdict {
let plain = super::locus::traversal_item(base);
let verdict = judge(&plain);
if verdict == Verdict::Denied || plain == super::locus::UNKNOWABLE_ITEM {
return verdict;
}
if reach_below(base, walk).iter().any(|reach| judge(reach.path()) == Verdict::Denied) { Verdict::Denied } else { verdict }
}
pub(crate) fn stage_item(name: &str, base: &str, args: &[&str]) -> String {
let plain = super::locus::traversal_item(base);
if plain == super::locus::UNKNOWABLE_ITEM {
return plain;
}
let find = name == "find";
let skip = if find { crate::handlers::coreutils::find_skip::skip_of(args) } else { Skip::default() };
let hidden = find || fd_lists_hidden(args);
let follow = if find { find_follows(args) } else { fd_follows(args) };
let walk = Walk { skip: &skip, ..Walk::items(hidden, follow) };
match reach_below(base, walk).as_slice() {
[] => plain,
[one] => one.path().to_string(),
_ => super::locus::UNKNOWABLE_ITEM.to_string(),
}
}
pub(crate) fn fd_lists_hidden(args: &[&str]) -> bool {
args.iter().any(|a| match a.strip_prefix("--") {
Some(long) => matches!(long, "hidden" | "unrestricted"),
None => a.starts_with('-') && a[1..].contains(['H', 'u']),
})
}
pub(crate) fn fd_follows(args: &[&str]) -> bool {
args.iter().any(|a| match a.strip_prefix("--") {
Some(long) => long.len() >= 3 && "follow".starts_with(long),
None => a.starts_with('-') && a[1..].contains('L'),
})
}
pub(crate) fn find_follows(args: &[&str]) -> bool {
args.iter().any(|a| matches!(*a, "-L" | "-H" | "-follow"))
}
pub(crate) fn frozen_below(base: &str, hidden: bool) -> Option<Reach> {
reach_below(base, Walk::plain(hidden)).into_iter().next()
}
#[cfg(test)]
#[path = "frozen_reach_tests.rs"]
mod tests;