INNER CODE UNIT · Rust

canonicalize_for_sandbox

ouch-org/ouch · src/sandbox/mod.rs:88

pub fn canonicalize_for_sandbox(path: &Path) -> PathBuf {
    if let Ok(c) = std::fs::canonicalize(path) {
        return c;
    }
    if let Some(parent) = path.parent()
        && let Ok(c) = std::fs::canonicalize(parent)
    {
        return c.join(path.file_name().unwrap_or_default());
    }
    path.to_path_buf()
}

/// Returns true if `target` is $HOME itself or an ancestor of it.
pub fn is_home_or_ancestor(target: &Path) -> bool {
    let Some(home) = std::env::var_os("HOME").map(PathBuf::from) else {
        return false;
    };
    let Ok(home) = std::fs::canonicalize(&home) else {

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…