Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add recursive option when running inputs #93

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}
Loading