use std::collections::VecDeque;
const MAX_LINKS: usize = 40;
const MAX_ENTRIES: usize = 4096;
const MAX_MATCHES: usize = 256;
const SYSTEM_LINKS: [&str; 3] = ["/private/etc", "/private/tmp", "/private/var"];
const DATA_VOLUME: &str = "/System/Volumes/Data";
#[derive(Debug, PartialEq, Eq)]
enum Outcome {
Plain,
Followed(String),
}
struct Budget {
links: usize,
matches: usize,
}
pub(super) fn resolve(raw: &str, follow_last: bool) -> Option<String> {
if !raw.starts_with('/') {
return None;
}
let follow_last = follow_last || raw.ends_with('/') || raw.ends_with("/.");
let mut budget = Budget { links: 0, matches: 0 };
match walk(Vec::new(), parts(raw, true), follow_last, &mut budget) {
Outcome::Plain => None,
Outcome::Followed(real) => (real != super::lexical::lexical_join("/", raw)).then_some(real),
}
}
pub(super) fn root(root: &str) -> String {
resolve(root, true).unwrap_or_else(|| root.to_string())
}
fn parts(path: &str, from_shell: bool) -> VecDeque<(String, bool)> {
path.split('/')
.filter(|s| !s.is_empty() && *s != ".")
.map(|s| (s.to_string(), from_shell))
.collect()
}
fn joined(done: &[String]) -> String {
format!("/{}", done.join("/"))
}
fn unresolved() -> Outcome {
Outcome::Followed(super::UNRESOLVED_CWD.to_string())
}
fn system_spelling(path: String) -> String {
if let Some(rest) = path.strip_prefix(DATA_VOLUME).filter(|rest| rest.starts_with('/')) {
return system_spelling(rest.to_string());
}
for long in SYSTEM_LINKS {
if path == long || path.starts_with(&format!("{long}/")) {
return path["/private".len()..].to_string();
}
}
path
}
fn walk(mut done: Vec<String>, mut todo: VecDeque<(String, bool)>, follow_last: bool, budget: &mut Budget) -> Outcome {
let mut followed = false;
let mut missing: Option<usize> = None;
while let Some((part, from_shell)) = todo.pop_front() {
if part == ".." {
if done.pop().is_some_and(|p| p.contains(crate::cst::eval::TAGGED_PREFIX)) {
return unresolved();
}
if missing.is_some_and(|at| done.len() <= at) {
missing = None;
}
continue;
}
if missing.is_some() || (todo.is_empty() && !follow_last) {
done.push(part);
continue;
}
if from_shell && part.contains(['*', '?', '[']) {
match glob(&done, &part, &todo, follow_last, budget) {
Some(outcome) => return outcome,
None => {
missing = Some(done.len());
done.push(part);
continue;
}
}
}
let candidate = format!("{}/{part}", joined(&done).trim_end_matches('/'));
match std::fs::symlink_metadata(&candidate) {
Ok(meta) if meta.file_type().is_symlink() => {
budget.links += 1;
if budget.links > MAX_LINKS {
return unresolved();
}
let Some(target) = std::fs::read_link(&candidate).ok().and_then(|t| t.to_str().map(str::to_string)) else {
return unresolved();
};
if target.starts_with('/') {
done.clear();
}
for item in parts(&target, false).into_iter().rev() {
todo.push_front(item);
}
followed = true;
}
Ok(_) => done.push(part),
Err(_) => {
missing = Some(done.len());
done.push(part);
}
}
}
if followed { Outcome::Followed(system_spelling(joined(&done))) } else { Outcome::Plain }
}
fn glob(done: &[String], pattern: &str, rest: &VecDeque<(String, bool)>, follow_last: bool, budget: &mut Budget) -> Option<Outcome> {
let dir = joined(done);
let entries = std::fs::read_dir(&dir).ok()?;
let mut names = Vec::new();
for entry in entries.take(MAX_ENTRIES + 1) {
let Some(name) = entry.ok().and_then(|e| e.file_name().into_string().ok()) else {
return Some(unresolved());
};
names.push(name);
}
if names.len() > MAX_ENTRIES {
return Some(unresolved());
}
let mut reached: Vec<String> = Vec::new();
let mut plain = 0usize;
for name in names {
if name.starts_with('.') && !pattern.starts_with('.') {
continue;
}
match crate::cst::opaque::glob::matches(pattern.as_bytes(), name.as_bytes()) {
Some(true) => {}
Some(false) => continue,
None => return Some(unresolved()),
}
budget.matches += 1;
if budget.matches > MAX_MATCHES {
return Some(unresolved());
}
let mut next = rest.clone();
next.push_front((name, false));
match walk(done.to_vec(), next, follow_last, budget) {
Outcome::Followed(real) => reached.push(real),
Outcome::Plain => plain += 1,
}
}
reached.sort();
reached.dedup();
match reached.len() {
0 => None,
1 if plain == 0 => reached.pop().map(Outcome::Followed),
_ if reached.iter().all(|real| stays_unprotected(real)) => None,
_ => Some(unresolved()),
}
}
fn stays_unprotected(real: &str) -> bool {
let Some(root) = super::root().filter(|r| r.starts_with('/')).map(|r| root(&r)) else {
return false;
};
let inside = real == root || real.starts_with(&format!("{}/", root.trim_end_matches('/')));
inside && !real.contains(['*', '?', '[']) && crate::engine::resolve::write_target_verdict(real) != crate::verdict::Verdict::Denied
}
#[cfg(test)]
#[path = "physical_tests.rs"]
mod tests;