From 3c7e16167963298a4433611ec31d2789eded93f2 Mon Sep 17 00:00:00 2001 From: BMARR Date: Fri, 15 Nov 2024 15:38:51 -0500 Subject: [PATCH] chore: remove affiliation, binary, review --- hipcheck/src/analysis/mod.rs | 4 +- hipcheck/src/metric/binary_detector/mod.rs | 67 ++-------------------- 2 files changed, 5 insertions(+), 66 deletions(-) diff --git a/hipcheck/src/analysis/mod.rs b/hipcheck/src/analysis/mod.rs index 69a5c2a7..3b51579a 100644 --- a/hipcheck/src/analysis/mod.rs +++ b/hipcheck/src/analysis/mod.rs @@ -11,10 +11,8 @@ use crate::{ F64, }; use std::{ - collections::{HashMap, HashSet}, + collections::{ HashSet}, default::Default, - ops::Not, - sync::Arc, }; /// Queries about analyses diff --git a/hipcheck/src/metric/binary_detector/mod.rs b/hipcheck/src/metric/binary_detector/mod.rs index bf12f85f..88fc4bf1 100644 --- a/hipcheck/src/metric/binary_detector/mod.rs +++ b/hipcheck/src/metric/binary_detector/mod.rs @@ -5,23 +5,18 @@ mod query; pub use query::*; use crate::{ - error::{Context, Result}, - hc_error, + error::{Context}, util::fs::read_toml, }; -use content_inspector::{inspect, ContentType}; + use serde::{de::Visitor, Deserialize, Deserializer}; use std::{ fmt, fmt::Formatter, - fs::File, - io::{prelude::Read, BufReader}, path::Path, result::Result as StdResult, - sync::Arc, }; -use walkdir::{DirEntry, WalkDir}; - + #[derive(Debug, PartialEq, Eq)] pub struct BinaryFileDetector { extensions: Vec, @@ -134,58 +129,4 @@ impl<'de> Visitor<'de> for BinaryTypeVisitor { } } } - -/// Determines whether a DirEntry is a hidden file/directory. -/// -/// This is a Unix-style determination. -fn is_hidden(entry: &DirEntry) -> bool { - entry - .file_name() - .to_str() - .map(|s| s.starts_with('.')) - .unwrap_or(false) -} - -/// Fetches all files from `dir`. -fn fetch_entries(dir: &Path) -> Result> { - let walker = WalkDir::new(dir).into_iter(); - let mut entries: Vec = Vec::new(); - for entry in walker.filter_entry(|e| !is_hidden(e)) { - entries.push(entry?) - } - Ok(entries) -} - -/// Searches `dir` for any binary files and records their paths as Strings. -pub fn detect_binary_files(dir: &Path) -> Result>> { - let path_entries = fetch_entries(dir)?; - let mut possible_binary: Vec> = Vec::new(); - - // Inspect the first 4K of each file for telltale signs of binary data. - // Store a String of each Path that leads to a binary file. - const SAMPLE_SIZE: u64 = 4096; - for entry in path_entries { - // Skip directories, as they are neither text nor binary. - if entry.path().is_dir() { - continue; - } - - let working_file = File::open(entry.path())?; - let reader = BufReader::new(working_file); - let mut contents: Vec = Vec::new(); - let _bytes_read = reader.take(SAMPLE_SIZE).read_to_end(&mut contents)?; - if inspect(&contents) == ContentType::BINARY { - possible_binary.push(match entry.path().strip_prefix(dir)?.to_str() { - Some(p) => Arc::new(String::from(p)), - None => { - return Err(hc_error!( - "path was not valid unicode: '{}'", - entry.path().display() - )) - } - }); - } - } - - Ok(possible_binary) -} + \ No newline at end of file