Skip to content

Commit

Permalink
Merge pull request #93 from R9295/feature/recurisve-input-dirs
Browse files Browse the repository at this point in the history
feat: add recursive option when running inputs
  • Loading branch information
vanhauser-thc committed Jun 4, 2024
2 parents dd33c83 + c0cd484 commit 6540fcd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/bin/cargo-ziggy/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ pub struct Run {
#[clap(short, long, value_name = "DIR", default_value = DEFAULT_CORPUS_DIR)]
inputs: Vec<PathBuf>,

/// Recursively run nested directories for all input directories
#[clap(short, long)]
recursive: bool,

/// Fuzzers output directory
#[clap(short, long, env="ZIGGY_OUTPUT", value_parser, value_name = "DIR", default_value=DEFAULT_OUTPUT_DIR)]
ziggy_output: PathBuf,
Expand Down Expand Up @@ -298,7 +302,7 @@ fn main() -> Result<(), anyhow::Error> {
match command {
Ziggy::Build(args) => args.build().context("Failed to build the fuzzers"),
Ziggy::Fuzz(mut args) => args.fuzz().context("Failure running fuzzers"),
Ziggy::Run(args) => args.run().context("Failure running inputs"),
Ziggy::Run(mut args) => args.run().context("Failure running inputs"),
Ziggy::Minimize(mut args) => args.minimize().context("Failure running minimization"),
Ziggy::Cover(mut args) => args
.generate_coverage()
Expand Down
41 changes: 39 additions & 2 deletions src/bin/cargo-ziggy/run.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use crate::{find_target, Run};
use anyhow::{anyhow, Context, Result};
use console::style;
use std::{env, process};
use std::{
collections::HashSet,
env, fs,
path::{Path, PathBuf},
process,
};

impl Run {
// Run inputs
pub fn run(&self) -> Result<(), anyhow::Error> {
pub fn run(&mut self) -> Result<(), anyhow::Error> {
let cargo = env::var("CARGO").unwrap_or_else(|_| String::from("cargo"));
let target = find_target(&self.target)?;

Expand All @@ -29,6 +34,21 @@ impl Run {
}

eprintln!(" {} runner", style("Finished").cyan().bold());

if self.recursive {
info!("Finding nested input directories recursively...");
let mut all_dirs = HashSet::new();
for input in &self.inputs {
all_dirs.insert(input.clone());
collect_dirs_recursively(input, &mut all_dirs)?;
}
for dir in all_dirs {
if !self.inputs.contains(&dir) {
self.inputs.push(dir);
}
}
}

info!("Running inputs");
let run_args: Vec<String> = self
.inputs
Expand All @@ -52,3 +72,20 @@ impl Run {
Ok(())
}
}

fn collect_dirs_recursively(
dir: &Path,
dir_list: &mut HashSet<PathBuf>,
) -> Result<(), anyhow::Error> {
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() && !dir_list.contains(&path) {
dir_list.insert(path.clone());
collect_dirs_recursively(&path, dir_list)?;
}
}
}
Ok(())
}

0 comments on commit 6540fcd

Please sign in to comment.