fast-shlibdeps 0.2.1

A fast ELF shared library dependency analyzer for Debian-based systems
use anyhow::Result;
use std::collections::HashMap;
use std::process::Command;

/// Get the contents of the ld cache by running `/sbin/ldconfig -p`. Returns a mapping of library names to their paths.
pub(crate) fn get_ld_cache() -> Result<HashMap<String, String>> {
    let output = Command::new("/sbin/ldconfig").arg("-p").output()?;

    if !output.status.success() {
        anyhow::bail!("ldconfig -p failed with status: {}", output.status);
    }

    let stdout = String::from_utf8(output.stdout)?;
    let mut cache = HashMap::new();

    for line in stdout.lines().skip(1) {
        // Skip the header line
        let line = line.trim();
        if let Some(arrow_pos) = line.find(" => ") {
            let lib_name = line[..arrow_pos].trim();
            let lib_path = line[arrow_pos + 4..].trim();

            // Extract just the library name (e.g., "libssl.so.3" from "libssl.so.3 (libc6,x86-64)")
            if let Some(paren_pos) = lib_name.find(" (") {
                let clean_name = &lib_name[..paren_pos];
                cache.insert(clean_name.to_string(), lib_path.to_string());
            } else {
                cache.insert(lib_name.to_string(), lib_path.to_string());
            }
        }
    }

    Ok(cache)
}