rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! What `rho/fs/look` answers: a folder resolved to its real path and
//! project, and a folder's subfolders, each with what its `.git` says.

use std::{io::ErrorKind, path::Path};

use rho_protocol::methods::{FsGitCheckout, FsListing, FsResolved, FsSubfolder};

use super::{canonical, host_path, parse_head, project_root};

/// What `look` answers for a folder to resolve: its real path and project
/// when it is a folder the daemon can enter, through any symlinks. One
/// that cannot be resolved or entered, or whose paths are not UTF-8 and so
/// cannot travel, is not a folder to the client.
pub(super) fn resolve_folder(path: &Path) -> FsResolved {
    let folder = || {
        let real = canonical(path).ok().filter(|real| can_enter(real))?;
        Some(FsResolved::Folder {
            project: host_path(&project_root(&real)).ok()?,
            real: host_path(&real).ok()?,
        })
    };
    folder().unwrap_or(FsResolved::NotFolder)
}

/// What `look` answers for a folder to list: its subfolders, through any
/// symlinks, each with what its `.git` says. The checks go as rho-ui's
/// script made them: gone, then not a folder, then unreadable.
pub(super) fn list_folder(folder: &Path) -> FsListing {
    match std::fs::metadata(folder) {
        // A broken symlink too.
        Err(error) if error.kind() == ErrorKind::NotFound => return FsListing::Gone,
        Ok(metadata) if !metadata.is_dir() => return FsListing::NotFolder,
        _ => {}
    }
    let entries = match std::fs::read_dir(folder) {
        Ok(entries) => entries,
        Err(error) => {
            return match error.kind() {
                ErrorKind::NotFound => FsListing::Gone,
                ErrorKind::PermissionDenied => FsListing::Denied,
                _ => FsListing::Failed {
                    message: error.to_string(),
                },
            }
        }
    };
    let subfolders = entries
        .flatten()
        .filter_map(|entry| {
            let path = entry.path();
            if !path.is_dir() {
                return None;
            }
            // A host call cannot name one that is not UTF-8.
            let name = entry.file_name().into_string().ok()?;
            Some(FsSubfolder::new(name, git_checkout(&path)))
        })
        .collect();
    FsListing::Listed {
        // A folder that can be listed but not entered has none, and one
        // whose real path is not UTF-8 cannot travel, so as if it could not
        // be entered.
        real: canonical(folder)
            .ok()
            .filter(|real| can_enter(real))
            .and_then(|real| host_path(&real).ok()),
        subfolders,
    }
}

/// Whether `dir` is a folder the daemon can enter, as `cd` checks, which
/// rho-ui's script ran: on Unix whether it may search it, which reaching
/// `dir/.` needs; on Windows, which lets everyone through a folder, whether
/// it can read it.
fn can_enter(dir: &Path) -> bool {
    #[cfg(unix)]
    {
        std::fs::metadata(dir.join(".")).is_ok_and(|metadata| metadata.is_dir())
    }
    #[cfg(not(unix))]
    {
        std::fs::read_dir(dir).is_ok()
    }
}

/// `dir` as the top of a git checkout, when it is one: what its `HEAD`
/// names, if that can be read.
fn git_checkout(dir: &Path) -> Option<FsGitCheckout> {
    let git_dir = crate::git::git_dir_at(dir)?;
    let head = crate::git::read_small(&git_dir.join("HEAD"));
    Some(FsGitCheckout::new(head.as_deref().and_then(parse_head)))
}