Skip to content

Commit

Permalink
Update dependencies and apply clippy suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
cundd committed Apr 27, 2022
1 parent 03abee7 commit ffd6134
Show file tree
Hide file tree
Showing 25 changed files with 463 additions and 406 deletions.
588 changes: 338 additions & 250 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 6 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hackscanner"
version = "0.4.0-dev"
version = "0.5.0-dev"
authors = ["Daniel Corn <[email protected]>"]
edition = "2018"

Expand Down Expand Up @@ -29,16 +29,12 @@ simplelog = "^0.6.0"
clap = "^2.33.0"
ansi_term = "^0.12.0"
term = "^0.6.0"
libc = "0.2.60"
lazy_static = "1.3.0"
serde = "1.0.98"
serde_derive = "1.0.98"
libc = "^0.2.60"
lazy_static = "^1.4.0"
serde = { version = "^1.0", features = ["derive"] }

serde_yaml = { version = "0.8.9", optional = true }
serde_json = { version = "1.0.40", optional = true }
serde_yaml = { version = "^0.8.23", optional = true }
serde_json = { version = "^1.0.79", optional = true }

[target.'cfg(not(target_env = "msvc"))'.dependencies]
jemallocator = { version="0.3.2", optional = true }

[profile.release]
debug = true
9 changes: 5 additions & 4 deletions benches/bench_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate test;

use hackscanner_lib::file_finder::FileFinderTrait;
use hackscanner_lib::*;
use std::str::FromStr;
use test::Bencher;

fn get_test_dir() -> String {
Expand All @@ -19,10 +20,10 @@ where
let rules = vec![Rule::new(
"2",
Severity::NOTICE,
"\\.tx_mocfilemanager",
true,
RawPath::from_str("\\.tx_mocfilemanager").unwrap(),
None,
)];
)
.unwrap()];
b.iter(|| ff.find(get_test_dir(), &rules));
}

Expand All @@ -31,5 +32,5 @@ where
D: DirEntryTrait,
F: FileFinderTrait<DirEntry = D>,
{
b.iter(|| ff.find(get_test_dir(), &vec![]));
b.iter(|| ff.find(get_test_dir(), &[]));
}
35 changes: 15 additions & 20 deletions src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use clap::App;
use clap::Arg;
use clap::ArgMatches;
use hackscanner_lib::*;
use simplelog;

use simplelog::TerminalMode;
use std::env;
use std::path::Path;
Expand Down Expand Up @@ -59,7 +59,7 @@ fn run() -> Result<(), Error> {
;

#[cfg(any(feature = "json", feature = "yaml"))]
let app = app.arg(
let app = app.arg(
Arg::with_name("configuration")
.help("File with additional rules")
.short("c")
Expand All @@ -72,9 +72,9 @@ fn run() -> Result<(), Error> {
configure_logging(&matches).unwrap();

#[cfg(not(any(feature = "json", feature = "yaml")))]
let rules = get_merged_rules(None)?;
let rules = get_merged_rules(None)?;
#[cfg(any(feature = "json", feature = "yaml"))]
let rules = get_merged_rules(match matches.value_of("configuration") {
let rules = get_merged_rules(match matches.value_of("configuration") {
Some(c) => Some(Path::new(c)),
None => None,
})?;
Expand All @@ -85,7 +85,6 @@ fn run() -> Result<(), Error> {
}
}


// Trace is only supported on debug-builds
#[cfg(debug_assertions)]
fn get_verbosity_help() -> &'static str {
Expand All @@ -97,12 +96,9 @@ fn get_verbosity_help() -> &'static str {
"Sets the level of verbosity (-v = Info, -vv = Debug)"
}

fn scan(
matches: &ArgMatches,
rules: Vec<Rule>,
) -> Result<(), Error> {
let min_severity = get_minimum_severity(&matches);
let root = get_root(&matches);
fn scan(matches: &ArgMatches, rules: Vec<Rule>) -> Result<(), Error> {
let min_severity = get_minimum_severity(matches);
let root = get_root(matches);
let quiet = matches.is_present("quiet");

let files = file_finder::find_files(root, &rules);
Expand All @@ -117,11 +113,7 @@ fn scan(
Ok(())
}

fn validate(
matches: &ArgMatches,
rules: Vec<Rule>,
test_path: &str,
) -> Result<(), Error> {
fn validate(matches: &ArgMatches, rules: Vec<Rule>, test_path: &str) -> Result<(), Error> {
let entry = ValidationDirEntry::from_path_str(test_path);
if !entry.path().exists() {
bail!(format!("File {} does not exist", entry.path().display()))
Expand Down Expand Up @@ -155,7 +147,6 @@ fn get_minimum_severity(matches: &ArgMatches<'_>) -> Severity {
}
}


/// Read the `Rule`s from the given path and merge them with the builtin rules
fn get_merged_rules(path: Option<&Path>) -> Result<Vec<Rule>, Error> {
match path {
Expand All @@ -173,11 +164,15 @@ fn configure_logging(matches: &ArgMatches<'_>) -> Result<(), Error> {
};

let mut loggers: Vec<Box<dyn simplelog::SharedLogger>> = vec![];
let mut config = simplelog::Config::default();
config.time_format = Some("%H:%M:%S%.3f");
// let mut config = simplelog::Config::default();
// config.time_format = Some("%H:%M:%S%.3f");
let config = simplelog::Config {
time_format: Some("%H:%M:%S%.3f"),
..Default::default()
};

if let Some(core_logger) =
simplelog::TermLogger::new(log_level_filter, config, TerminalMode::Mixed)
simplelog::TermLogger::new(log_level_filter, config, TerminalMode::Mixed)
{
loggers.push(core_logger);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/classifier/content_classifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ pub struct ContentClassifier {
}

impl ContentClassifier {
fn get_file_content<'f, D: DirEntryTrait>(
fn get_file_content<D: DirEntryTrait>(
&mut self,
entry: &D,
) -> Result<&str, ContentClassificationError> {
if !(entry.path() == self.path.as_path()) {
if entry.path() != self.path.as_path() {
unreachable!(
"Entry path does not match path stored in struct ContentClassifier. \n{:?} != \n{:?}",
entry.path(),
Expand Down
10 changes: 2 additions & 8 deletions src/classifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use self::content_classifier::ContentClassifier;
use self::path_classifier::PathClassifier;
pub use self::violation::Violation;

pub fn classify_entries<'a, 'b, D: DirEntryTrait>(
entries: &'a Vec<D>,
rules: &'a Vec<Rule>,
) -> Vec<Vec<Violation>> {
pub fn classify_entries<D: DirEntryTrait>(entries: &[D], rules: &[Rule]) -> Vec<Vec<Violation>> {
debug!("Will classify entries");
let result = entries
.iter()
Expand All @@ -26,10 +23,7 @@ pub fn classify_entries<'a, 'b, D: DirEntryTrait>(
result
}

pub fn classify_entry<'a, 'b, D: DirEntryTrait>(
entry: &'a D,
rules: &'a Vec<Rule>,
) -> Vec<Violation> {
pub fn classify_entry<D: DirEntryTrait>(entry: &D, rules: &[Rule]) -> Vec<Violation> {
let mut path_classifier = path_classifier::PathClassifier::new(entry);
let mut content_classifier = content_classifier::ContentClassifier::new(entry);
rules
Expand Down
4 changes: 2 additions & 2 deletions src/dir_entry/validation_dir_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl DirEntry {
}
}

fn detect_file_type(path_buf: &PathBuf) -> StandaloneFileType {
if let Ok(metadata) = path_buf.metadata() {
fn detect_file_type(path: &Path) -> StandaloneFileType {
if let Ok(metadata) = path.metadata() {
let file_type = metadata.file_type();

StandaloneFileType::from_file_type(&file_type)
Expand Down
2 changes: 1 addition & 1 deletion src/dir_entry/walkdir_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl DirEntryTrait for DirEntry {
///
/// See [`walkdir::DirEntry::path`] for more details
fn path(&self) -> &Path {
&self.raw.path()
self.raw.path()
}

/// Return `true` if and only if this entry was created from a symbolic
Expand Down
6 changes: 3 additions & 3 deletions src/file_finder/ftw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::dir_entry::StandaloneDirEntry;
use crate::fs::StandaloneFileType;

#[repr(C)]
struct FTW {
struct Ftw {
base: c_int,
level: c_int,
}
Expand All @@ -30,7 +30,7 @@ type NftwFn = extern "C" fn(
fpath: *const c_char,
sb: *const stat,
typeflag: c_int,
ftwbuf: *const FTW,
ftwbuf: *const Ftw,
) -> c_int;

extern "C" {
Expand Down Expand Up @@ -80,7 +80,7 @@ extern "C" fn nftw_collector(
fpath: *const c_char,
_sb: *const stat,
typeflag: c_int,
_ftwbuf: *const FTW,
_ftwbuf: *const Ftw,
) -> c_int {
unsafe {
let path_string = CStr::from_ptr(fpath);
Expand Down
13 changes: 3 additions & 10 deletions src/file_finder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ pub trait FileFinderTrait {
type DirEntry: DirEntryTrait;

/// Return all [`DirEntry`s] that match at least one of the [`Rule`s] starting at `root`
fn find<P: AsRef<Path> + Debug + Clone>(
&self,
root: P,
rules: &Vec<Rule>,
) -> Vec<Self::DirEntry> {
fn find<P: AsRef<Path> + Debug + Clone>(&self, root: P, rules: &[Rule]) -> Vec<Self::DirEntry> {
self.walk_dir(root, |entry: &Self::DirEntry| {
if entry.file_type().is_dir() {
return false;
Expand All @@ -29,7 +25,7 @@ pub trait FileFinderTrait {
let mut store_entry = false;
for rule in rules {
// Check if the `Rule`'s path matches the current entry
if Matcher::match_path_str(rule, &path_as_string) {
if Matcher::match_path_str(rule, path_as_string.as_ref()) {
// If the `Rule`'s path matches and the `Rule` is a whitelist-rule exit the loop
// and ignore the entry
if rule.severity() == Severity::WHITELIST {
Expand All @@ -53,9 +49,6 @@ pub trait FileFinderTrait {
F: Fn(&Self::DirEntry) -> bool;
}

pub fn find_files<P: AsRef<Path> + Debug + Clone>(
root: P,
rules: &Vec<Rule>,
) -> Vec<WalkdirDirEntry> {
pub fn find_files<P: AsRef<Path> + Debug + Clone>(root: P, rules: &[Rule]) -> Vec<WalkdirDirEntry> {
self::walkdir::FileFinder::find(&self::walkdir::FileFinder::new(), root, rules)
}
30 changes: 15 additions & 15 deletions src/fs/file_type/standalone_file_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,29 @@ impl FileType {

impl FileTypeTrait for FileType {
fn is_dir(&self) -> bool {
match self {
&FileType::File => false,
&FileType::Directory => true,
&FileType::Symlink => false,
&FileType::Unknown => false,
match *self {
FileType::File => false,
FileType::Directory => true,
FileType::Symlink => false,
FileType::Unknown => false,
}
}

fn is_file(&self) -> bool {
match self {
&FileType::File => true,
&FileType::Directory => false,
&FileType::Symlink => false,
&FileType::Unknown => false,
match *self {
FileType::File => true,
FileType::Directory => false,
FileType::Symlink => false,
FileType::Unknown => false,
}
}

fn is_symlink(&self) -> bool {
match self {
&FileType::File => false,
&FileType::Directory => false,
&FileType::Symlink => true,
&FileType::Unknown => false,
match *self {
FileType::File => false,
FileType::Directory => false,
FileType::Symlink => true,
FileType::Unknown => false,
}
}
}
2 changes: 1 addition & 1 deletion src/join.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::classifier::Violation;

pub fn join_violations(violations: &Vec<Violation>) -> String {
pub fn join_violations(violations: &[Violation]) -> String {
violations
.iter()
.map(|v| v.name().to_owned())
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#[macro_use]
extern crate error_chain;

#[macro_use]
extern crate serde_derive;
// #[macro_use]
// extern crate serde_derive;

#[macro_use]
extern crate log;
Expand Down
4 changes: 2 additions & 2 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl Matcher {
pub fn match_entry_path<C, P: RuleTrait<C>, D: DirEntryTrait>(rule: &P, entry: &D) -> bool {
let path_as_string = entry.path().to_string_lossy();

Matcher::match_path_str(rule, &path_as_string)
Matcher::match_path_str(rule, path_as_string.as_ref())
}

/// Check if the given path matches the given rule
Expand All @@ -36,7 +36,7 @@ impl Matcher {
r,
path_as_string
);
r.is_match(&path_as_string)
r.is_match(path_as_string)
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/rating/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ use crate::dir_entry::DirEntryTrait;
use crate::join::join_violations;
use crate::severity::Severity;
use crate::Rule;
use std::cmp::Reverse;
use std::fmt;

pub fn rate_entries<'a, D: DirEntryTrait>(
entries: &'a Vec<D>,
rules: &'a Vec<Rule>,
) -> Vec<Rating<'a>> {
pub fn rate_entries<'a, D: DirEntryTrait>(entries: &'a [D], rules: &'a [Rule]) -> Vec<Rating<'a>> {
debug!("Will rate entries");
let result = entries
.iter()
Expand All @@ -22,7 +20,7 @@ pub fn rate_entries<'a, D: DirEntryTrait>(
result
}

pub fn rate_entry<'a, D: DirEntryTrait>(entry: &'a D, rules: &'a Vec<Rule>) -> Rating<'a> {
pub fn rate_entry<'a, D: DirEntryTrait>(entry: &'a D, rules: &[Rule]) -> Rating<'a> {
info!("Will rate entry {:?}", entry);
let violations: Vec<Violation> = classify_entry(entry, rules);

Expand All @@ -43,7 +41,7 @@ pub fn rate_entry<'a, D: DirEntryTrait>(entry: &'a D, rules: &'a Vec<Rule>) -> R
pub fn sort_ratings<'a>(ratings: &[Rating<'a>]) -> Vec<Rating<'a>> {
let mut copy = ratings.to_vec();

copy.sort_unstable_by(|a, b| b.rating().cmp(&a.rating()));
copy.sort_unstable_by_key(|b| Reverse(b.rating()));

copy
}
Expand Down
2 changes: 1 addition & 1 deletion src/rating/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Summary {

impl Summary {
/// Build a summary of the overall Ratings
pub fn build(ratings: &Vec<Rating<'_>>) -> Self {
pub fn build(ratings: &[Rating<'_>]) -> Self {
let mut summary = Summary {
critical: 0,
major: 0,
Expand Down
Loading

0 comments on commit ffd6134

Please sign in to comment.