Skip to main content

scan_directory

Function scan_directory 

Source
pub fn scan_directory(
    dir_path: &PathBuf,
    options: &ScanOptions,
) -> Result<SecurityReport>
Expand description

Scan a directory for executable files and analyze their security features

This function recursively scans a directory for executable files and analyzes each one. The behavior can be customized using the ScanOptions parameter.

§Arguments

  • dir_path - Path to the directory to scan
  • options - Scanning options (recursive, issues_only, strict)

§Returns

Returns a SecurityReport containing analysis results for all found executables.

§Example

use execheck::{scan_directory, ScanOptions, FileFilter};
use std::path::PathBuf;

let options = ScanOptions {
    recursive: true,
    issues_only: false,
    strict: false,
    file_filter: FileFilter::All,
    one_filesystem: false,
};

let report = scan_directory(&PathBuf::from("/usr/bin"), &options)?;
println!("Found {} executable files", report.summary.total_files);
Examples found in repository?
examples/directory_scan.rs (line 23)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    // Configure scan options
11    let options = ScanOptions {
12        recursive: false,        // Only scan top level
13        issues_only: true,       // Only show files with security issues
14        strict: false,          // Don't exit on issues
15        file_filter: FileFilter::All, // All executable types
16        one_filesystem: false,   // Allow crossing filesystem boundaries
17    };
18    
19    // Scan a directory (adjust path as needed)
20    let scan_path = PathBuf::from("/usr/bin");
21    println!("Scanning directory: {} (issues only)", scan_path.display());
22    
23    match scan_directory(&scan_path, &options) {
24        Ok(report) => {
25            println!("\n=== Scan Summary ===");
26            println!("Total files analyzed: {}", report.summary.total_files);
27            println!("Secure files: {}", report.summary.secure_files);
28            println!("Files with issues: {}", report.summary.insecure_files);
29            println!("Unsupported files: {}", report.summary.unsupported_files);
30            
31            if report.summary.insecure_files > 0 {
32                println!("\n=== Files with Security Issues ===");
33                print_report(&report, &OutputFormat::Human, None)?;
34            } else {
35                println!("\nAll analyzed files have good security posture! ✓");
36            }
37        }
38        Err(e) => {
39            eprintln!("Failed to scan directory: {}", e);
40            std::process::exit(1);
41        }
42    }
43    
44    Ok(())
45}