Skip to content

Commit

Permalink
fix: Filter directories from scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-d-zhang committed Aug 17, 2024
1 parent a3291e2 commit adf0c9a
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ struct Distribution {
impl Distribution {
fn scan(&mut self, rules: &Rules) -> Result<DistributionScanResults> {
let mut file_scan_results: Vec<FileScanResult> = Vec::new();
for entry in WalkDir::new(self.dir.path()) {
let entry = entry?;
for entry in WalkDir::new(self.dir.path())
.into_iter()
.filter_map(|dirent| dirent.into_iter().find(|de| de.file_type().is_file()))
{
let file_scan_result = self.scan_file(entry.path(), rules)?;
file_scan_results.push(file_scan_result);
}
Expand Down Expand Up @@ -249,7 +251,7 @@ mod tests {
ScanResultSerializer, SubmitJobResultsError, SubmitJobResultsSuccess,
};
use crate::test::make_file_scan_result;
use tempfile::tempdir;
use tempfile::{tempdir, tempdir_in};

#[test]
fn test_scan_result_success_serialization() {
Expand Down Expand Up @@ -473,4 +475,36 @@ mod tests {

assert_eq!(expected_path, result);
}

#[test]
fn scan_skips_directories() {
let rules = r#"
rule contains_rust {
meta:
weight = 5
strings:
$rust = "rust" nocase
condition:
$rust
}
"#;

let compiler = Compiler::new().unwrap().add_rules_str(rules).unwrap();

let rules = compiler.compile_rules().unwrap();
let tempdir = tempdir().unwrap();
let _subtempdir = tempdir_in(tempdir.path()).unwrap();
let mut tempfile = tempfile::NamedTempFile::new_in(tempdir.path()).unwrap();
writeln!(&mut tempfile, "rust").unwrap();

let mut distro = super::Distribution {
dir: tempdir,
download_url: "https://example.com".parse().unwrap(),
inspector_url: "https://example.com".parse().unwrap(),
};

let results = distro.scan(&rules).unwrap();

assert_eq!(results.distro_scan_results.files.len(), 1);
}
}

0 comments on commit adf0c9a

Please sign in to comment.