use crate::args::Args;
use crate::get_fd_limit;
use std::collections::HashSet;
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
#[derive(Clone)]
pub struct Config {
pub num_threads: usize,
pub num_entries: usize,
pub batch_size: usize,
pub root_path: PathBuf,
pub skip_dirs: HashSet<String>,
pub max_open_files: usize,
pub verbose: bool,
}
impl Config {
pub fn build(args: &Args) -> Result<Config, Box<dyn Error>> {
let num_threads = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1);
println!("Preparing to scan using {} threads", num_threads);
let max_open_files = get_fd_limit();
println!("Limiting open file handles to {}", max_open_files);
let num_entries = args.num_entries;
let batch_size = args.batch_size;
let verbose = args.verbose;
let root_path = if let Some(target_dir) = &args.target_dir {
PathBuf::from(target_dir)
} else {
env::current_dir()?
};
let mut skip_dirs: HashSet<String> = HashSet::new();
if let Some(exclusion_file) = &args.exclusion_file {
let file = File::open(exclusion_file)
.expect("A path to an excluded directories file was provided but the file could not be read");
let reader = BufReader::new(file);
reader.lines().for_each(|line| match line {
Ok(dir) => {
skip_dirs.insert(dir);
}
Err(e) => log::error!("Error reading line: {}", e),
});
}
Ok(Config {
num_threads,
num_entries,
batch_size,
root_path,
skip_dirs,
max_open_files,
verbose
})
}
}