Skip to content

Commit

Permalink
If not path to corpus was specified, try to load it from CWD or from …
Browse files Browse the repository at this point in the history
…the binarie's folder
  • Loading branch information
trou committed Mar 4, 2024
1 parent b2d81fb commit 9861e84
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
mod corpus;
use crate::corpus::{load_corpus, CorpusStats};
use anyhow::{Context, Error, Result};
use anyhow::{bail, Context, Error, Result};
use clap::{arg, Arg, ArgAction};
use log::{debug, info};
use std::cmp::min;
Expand Down Expand Up @@ -202,7 +202,7 @@ fn main() -> Result<()> {
.propagate_version(true)
.author("Raphaël Rigo <[email protected]>")
.about("Identifies CPU architectures in binaries")
.arg(arg!(--corpus <corpus_dir>).default_value("cpu_rec_corpus"))
.arg(arg!(--corpus <corpus_dir>))
.arg(arg!(-d - -debug))
.arg(arg!(-v - -verbose))
.arg(
Expand All @@ -223,17 +223,40 @@ fn main() -> Result<()> {
};
simple_logger::init_with_level(level)?;

let corpus_dir = args.get_one::<String>("corpus").unwrap().to_owned();
let corpus_dir_res = args.get_one::<String>("corpus");

/* Use the given path to load the corpus from, else try
* to find it in the current directory or in the exec
* folder */
let corpus_dir = match corpus_dir_res {
Some(c) => c.to_owned(),
None => if Path::new("cpu_rec_corpus").is_dir() {
"cpu_rec_corpus".to_string()
} else {
let exe_path = std::env::current_exe().with_context(|| "Could not get exe filename")?;
let parent_path = exe_path.parent().unwrap();
if parent_path.join("cpu_rec_corpus").is_dir() {
// Found it in the exe path
parent_path
.join("cpu_rec_corpus")
.to_str()
.unwrap()
.to_string()
} else {
bail!("Could not find \"cpu_rec_corpus\", please specify it using --corpus");
}
}
.to_owned(),
};

if !Path::new(&corpus_dir).is_dir() {
return Err(Error::msg(format!(
"{} is not a valid directory",
corpus_dir
)));
bail!("{} is not a valid directory", corpus_dir);
}
let corpus_files: String = args.get_one::<String>("corpus").unwrap().to_owned() + "/*.corpus";
println!("Loading corpus from {}", corpus_files);

let corpus_stats = load_corpus(&corpus_files)?;
let corpus_files = Path::new(&corpus_dir).join("*.corpus");
println!("Loading corpus from {:?}", corpus_files);

let corpus_stats = load_corpus(&corpus_files.to_str().unwrap())?;

info!("Corpus size: {}", corpus_stats.len());

Expand Down

0 comments on commit 9861e84

Please sign in to comment.