diff --git a/Cargo.toml b/Cargo.toml index 97913d2..e0b2fb7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "hackscanner" version = "0.3.0" authors = ["Daniel Corn "] +edition = "2018" [lib] name = "hackscanner_lib" diff --git a/src/bin.rs b/src/bin.rs index 6406d23..619899f 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -132,7 +132,7 @@ fn configure_logging(matches: &ArgMatches) -> Result<(), Error> { _ => simplelog::LevelFilter::Warn, }; - let mut loggers: Vec> = vec![]; + let mut loggers: Vec> = vec![]; let mut config = simplelog::Config::default(); config.time_format = Some("%H:%M:%S%.3f"); diff --git a/src/classifier/content_classifier.rs b/src/classifier/content_classifier.rs index 6e58a6c..c716321 100644 --- a/src/classifier/content_classifier.rs +++ b/src/classifier/content_classifier.rs @@ -1,10 +1,10 @@ -use DirEntryTrait; +use crate::DirEntryTrait; use super::Classification; use super::ClassifierTrait; use super::Violation; -use rule::*; -use matcher::Matcher; -use errors::Result; +use crate::rule::*; +use crate::matcher::Matcher; +use crate::errors::Result; use std::fs::File; use std::io::prelude::*; use std::path::PathBuf; diff --git a/src/classifier/mod.rs b/src/classifier/mod.rs index 4aa85a1..6f8fe73 100644 --- a/src/classifier/mod.rs +++ b/src/classifier/mod.rs @@ -3,8 +3,8 @@ mod classification; mod content_classifier; mod path_classifier; -use dir_entry::*; -use rule::*; +use crate::dir_entry::*; +use crate::rule::*; pub use self::violation::Violation; use self::classification::*; @@ -100,9 +100,9 @@ fn panic_empty() -> ! { #[cfg(test)] mod test { use super::*; - use severity::Severity; + use crate::severity::Severity; use std::convert::TryInto; - use fs::StandaloneFileType; + use crate::fs::StandaloneFileType; fn test_classify_entry(entry: &D, rule: &Rule) -> Classification { let mut path_classifier = path_classifier::PathClassifier::new(entry); diff --git a/src/classifier/path_classifier.rs b/src/classifier/path_classifier.rs index 2bfb95a..77d3448 100644 --- a/src/classifier/path_classifier.rs +++ b/src/classifier/path_classifier.rs @@ -1,9 +1,9 @@ -use rule::*; +use crate::rule::*; use super::Classification; use super::ClassifierTrait; use super::Violation; -use DirEntryTrait; -use matcher::Matcher; +use crate::DirEntryTrait; +use crate::matcher::Matcher; pub struct PathClassifier {} diff --git a/src/classifier/violation.rs b/src/classifier/violation.rs index 446ef41..6c7c776 100644 --- a/src/classifier/violation.rs +++ b/src/classifier/violation.rs @@ -1,6 +1,6 @@ -use ::{Rule, Severity}; +use crate::{Rule, Severity}; use std::error::Error as StdError; -use PatternRule; +use crate::PatternRule; #[derive(Debug, Clone)] pub struct Violation { @@ -39,14 +39,14 @@ impl Violation { } } -impl From<&StdError> for Violation { - fn from(error: &StdError) -> Self { +impl From<&dyn StdError> for Violation { + fn from(error: &dyn StdError) -> Self { Self::with_name_and_severity(error.description().to_owned(), Severity::NOTICE) } } -impl From<&::errors::Error> for Violation { - fn from(error: &::errors::Error) -> Self { +impl From<&crate::errors::Error> for Violation { + fn from(error: &crate::errors::Error) -> Self { Self::with_name_and_severity(error.description().to_owned(), Severity::NOTICE) } } diff --git a/src/dir_entry/mod.rs b/src/dir_entry/mod.rs index cfcc0f1..b9a3b6e 100644 --- a/src/dir_entry/mod.rs +++ b/src/dir_entry/mod.rs @@ -3,7 +3,7 @@ use std::path::Path; use std::ffi::OsStr; use std::io; use std::fmt::Debug; -use fs::FileTypeTrait; +use crate::fs::FileTypeTrait; pub trait DirEntryTrait: Debug { /// The full path that this entry represents. @@ -25,7 +25,7 @@ pub trait DirEntryTrait: Debug { /// Return the file type for the file that this entry points to. /// /// See [`walkdir::DirEntry::file_type`] for more details - fn file_type(&self) -> Box; + fn file_type(&self) -> Box; /// Return the file name of this entry. /// diff --git a/src/dir_entry/standalone.rs b/src/dir_entry/standalone.rs index d7e58b7..4138991 100644 --- a/src/dir_entry/standalone.rs +++ b/src/dir_entry/standalone.rs @@ -3,8 +3,8 @@ use std::io; use std::path::Path; use std::path::PathBuf; use std::fs; -use fs::FileTypeTrait; -use fs::StandaloneFileType; +use crate::fs::FileTypeTrait; +use crate::fs::StandaloneFileType; use super::DirEntryTrait; #[derive(Debug, Clone)] @@ -56,7 +56,7 @@ impl DirEntryTrait for DirEntry { /// Return the file type for the file that this entry points to. /// /// See [`walkdir::DirEntry::file_type`] for more details - fn file_type(&self) -> Box { + fn file_type(&self) -> Box { Box::new(self.file_type.clone()) } diff --git a/src/dir_entry/walkdir.rs b/src/dir_entry/walkdir.rs index 0e9835e..da395e8 100644 --- a/src/dir_entry/walkdir.rs +++ b/src/dir_entry/walkdir.rs @@ -3,7 +3,7 @@ use std::io; use std::path::Path; use std::ffi::OsStr; use walkdir; -use fs::FileTypeTrait; +use crate::fs::FileTypeTrait; use super::DirEntryTrait; #[derive(Debug, Clone)] @@ -59,7 +59,7 @@ impl DirEntryTrait for DirEntry { /// Return the file type for the file that this entry points to. /// /// See [`walkdir::DirEntry::file_type`] for more details - fn file_type(&self) -> Box { + fn file_type(&self) -> Box { Box::new(self.raw.file_type()) } diff --git a/src/file_finder/ftw.rs.off b/src/file_finder/ftw.rs.off deleted file mode 100644 index 60319b1..0000000 --- a/src/file_finder/ftw.rs.off +++ /dev/null @@ -1,143 +0,0 @@ -extern crate libc; - -use std::ffi::CStr; -use std::ffi::CString; -use self::libc::c_char; -use self::libc::c_int; -use self::libc::stat; - -use std::path::Path; -use std::path::PathBuf; -use std::fmt::Debug; -//use std::sync::Arc; -use std::sync::{Arc, Mutex, MutexGuard}; -use std::sync::RwLock; - -use std::vec::Vec; - -use super::FileFinderTrait; -use dir_entry::StandaloneDirEntry; - - -static LOCK: RwLock = RwLock::new(true); -static mut FOUND_PATHS: Arc>> = Arc::new(None); - - -//static mut LOCK: Arc> = Arc::new(Mutex::new(0)); -//static mut FOUND_PATHS: Arc>>> = Arc::new(Mutex::new(None)); - - -//let mut tmp: *mut c_char = 0 as *mut c_char; -//pub type WalkFn = extern fn(fpath:*mut c_char, const struct stat *sb, typeflag:c_int) -type WalkFn = extern fn(fpath: *const c_char, sb: *const stat, typeflag: c_int) -> c_int; - - -//static mut ARC: Arc> = Arc::new(vec![]); - -// int ftw( -// const char *dirpath, -// int (*fn) (const char *fpath, const struct stat *sb, int typeflag), -// int nopenfd -// ); -extern { - fn ftw(dirpath: *const c_char, f: WalkFn, nopenfd: c_int) -> c_int; -} - - -extern fn x(fpath: *const c_char, sb: *const stat, typeflag: c_int) -> c_int { - unsafe { - let path_string = CStr::from_ptr(fpath); -// let paths: Option> = *FOUND_PATHS; - - match *FOUND_PATHS { - Some(ref mut p) => p.push(path_string.to_string_lossy().into_owned()), - None => panic!("FOUND_PATHS is None") - } - } - - 0 -} - - -pub struct FileFinder {} - -impl FileFinderTrait for FileFinder { - type DirEntry = StandaloneDirEntry; - fn walk_dir + Debug + Clone, F>(root: P, filter: F) -> Vec - where F: FnMut(&Self::DirEntry) -> bool { - let entries = collect_dir_entries(root); - - entries.into_iter().filter(filter).collect() - } -} - -fn collect_dir_entries + Debug + Clone>(root: P) -> Vec { - unsafe { - match *FOUND_PATHS { - None => FOUND_PATHS = Arc::new(Some(vec![])), - Some(_) => {} - } - } - let path_as_string = root.as_ref().to_string_lossy().into_owned(); - unsafe { - ftw(CString::new(path_as_string).unwrap().as_ptr(), x, 20); - } - - unsafe { - match *FOUND_PATHS { - Some(ref mut p) => p.iter(). - filter_map(|path| { - StandaloneDirEntry::from_path(PathBuf::from(path)).ok() - }).collect(), - None => panic!("FOUND_PATHS is None") - } - - -// match FOUND_PATHS { -// Some(ref mut paths) => paths. -// iter(). -// filter_map(|path| { -// StandaloneDirEntry::from_path(PathBuf::from(path)).ok() -// }).collect(), -// None => vec![] -// } - } -} - -#[cfg(test)] -mod test { - use super::*; - - - #[test] - fn walk_dir_test() { - -// let w :WalkFn = |fpath: *mut c_char, sb: *const stat, typeflag: c_int| {}; -// let tmp: *mut libc::c_char = std::ptr::null(); - let r = FileFinder::walk_dir("/Users/daniel/Sites/Projects/hackscanner", |_| true); -// println!("{:#?}", r); - } - - #[test] - fn find_test() { - -// let w :WalkFn = |fpath: *mut c_char, sb: *const stat, typeflag: c_int| {}; -// let tmp: *mut libc::c_char = std::ptr::null(); - let r = FileFinder::walk_dir("/Users/daniel/Sites/Projects/hackscanner", |_| true); -// println!("{:#?}", r); - } -} - -//int ftw(const char *dirpath, -//int (*fn) (const char *fpath, const struct stat *sb, -//int typeflag), -//int nopenfd); - -//fn signal(signal: c_int, sighandler_t: SignalHandlerFn) -> SignalHandlerFn; -//} -// -//pub fn register(signal_code: Signal, callback: SignalHandlerFn) { -//unsafe { -//signal(signal_code as c_int, callback); -//} -//} diff --git a/src/file_finder/ftw/mod.rs b/src/file_finder/ftw/mod.rs index 703ad02..ffeaf41 100644 --- a/src/file_finder/ftw/mod.rs +++ b/src/file_finder/ftw/mod.rs @@ -10,11 +10,11 @@ use std::fmt::Debug; use std::vec::Vec; use std::cell::RefCell; -use fs::constants::*; +use crate::fs::constants::*; use super::FileFinderTrait; -use dir_entry::StandaloneDirEntry; -use fs::StandaloneFileType; +use crate::dir_entry::StandaloneDirEntry; +use crate::fs::StandaloneFileType; #[repr(C)] struct FTW { diff --git a/src/file_finder/mod.rs b/src/file_finder/mod.rs index 5f9ade6..fc6ab3a 100644 --- a/src/file_finder/mod.rs +++ b/src/file_finder/mod.rs @@ -6,10 +6,10 @@ pub mod ftw; pub mod fts; pub mod walkdir; -use rule::*; -use dir_entry::*; -use matcher::Matcher; -use Severity; +use crate::rule::*; +use crate::dir_entry::*; +use crate::matcher::Matcher; +use crate::Severity; pub trait FileFinderTrait { type DirEntry: DirEntryTrait; diff --git a/src/file_finder/walkdir.rs b/src/file_finder/walkdir.rs index a9c1caa..edb7b3d 100644 --- a/src/file_finder/walkdir.rs +++ b/src/file_finder/walkdir.rs @@ -1,4 +1,4 @@ -use dir_entry::WalkdirDirEntry; +use crate::dir_entry::WalkdirDirEntry; use std::fmt::Debug; use std::path::Path; use super::FileFinderTrait; diff --git a/src/join.rs b/src/join.rs index 9a34164..c7c0286 100644 --- a/src/join.rs +++ b/src/join.rs @@ -1,4 +1,4 @@ -use classifier::Violation; +use crate::classifier::Violation; pub fn join_violations(violations: &Vec) -> String { violations.iter().map(|v| v.name().to_owned()).collect::>().join(", ") diff --git a/src/lib.rs b/src/lib.rs index c645e55..d0fec2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,10 +30,10 @@ pub mod rating; pub mod file_finder; pub mod classifier; -pub use errors::*; -pub use dir_entry::*; -pub use rule::*; -pub use rating::*; -pub use file_finder::find_files; -pub use severity::Severity; -pub use join::join_violations; +pub use crate::errors::*; +pub use crate::dir_entry::*; +pub use crate::rule::*; +pub use crate::rating::*; +pub use crate::file_finder::find_files; +pub use crate::severity::Severity; +pub use crate::join::join_violations; diff --git a/src/matcher.rs b/src/matcher.rs index 30deedf..535c2fd 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -1,6 +1,6 @@ -use rule::RuleTrait; -use rule::PatternRule; -use dir_entry::*; +use crate::rule::RuleTrait; +use crate::rule::PatternRule; +use crate::dir_entry::*; pub struct Matcher {} diff --git a/src/rating/mod.rs b/src/rating/mod.rs index 33e81fa..5e88e57 100644 --- a/src/rating/mod.rs +++ b/src/rating/mod.rs @@ -2,11 +2,11 @@ mod summary; pub use self::summary::Summary; use std::fmt; -use dir_entry::DirEntryTrait; -use severity::Severity; -use classifier::{Violation, classify_entry}; -use join::join_violations; -use ::PatternRule; +use crate::dir_entry::DirEntryTrait; +use crate::severity::Severity; +use crate::classifier::{Violation, classify_entry}; +use crate::join::join_violations; +use crate::PatternRule; pub fn rate_entries<'a, 'b, D: DirEntryTrait>(entries: &'a Vec, rules: &'a Vec) -> Vec> { debug!("Will rate entries"); @@ -28,13 +28,13 @@ pub fn sort_ratings<'a>(ratings: &[Rating<'a>]) -> Vec> { #[derive(Debug, Clone)] pub struct Rating<'a> { - entry: &'a DirEntryTrait, + entry: &'a dyn DirEntryTrait, rating: isize, violations: Vec, } impl<'a> Rating<'a> { - pub fn new(entry: &'a DirEntryTrait, rating: isize, violations: Vec) -> Self { + pub fn new(entry: &'a dyn DirEntryTrait, rating: isize, violations: Vec) -> Self { Rating { entry, rating, @@ -42,7 +42,7 @@ impl<'a> Rating<'a> { } } - pub fn entry(&self) -> &DirEntryTrait { + pub fn entry(&self) -> &dyn DirEntryTrait { self.entry } @@ -102,10 +102,10 @@ fn rate_entry<'a, 'b, D: DirEntryTrait>(entry: &'a D, rules: &'a Vec StandaloneDirEntry { StandaloneDirEntry::from_path( diff --git a/src/rating/summary.rs b/src/rating/summary.rs index 6c0eacb..84fc972 100644 --- a/src/rating/summary.rs +++ b/src/rating/summary.rs @@ -1,4 +1,4 @@ -use ::{Rating, Severity}; +use crate::{Rating, Severity}; pub struct Summary { critical: isize, diff --git a/src/rule/builtin.rs b/src/rule/builtin.rs index fe4ef84..3894cef 100644 --- a/src/rule/builtin.rs +++ b/src/rule/builtin.rs @@ -1,5 +1,5 @@ -use severity::Severity; -use Rule; +use crate::severity::Severity; +use crate::Rule; pub fn get_builtin_rules() -> Vec { let mut collection = vec![]; diff --git a/src/rule/mod.rs b/src/rule/mod.rs index 1fb2665..93a6f2b 100644 --- a/src/rule/mod.rs +++ b/src/rule/mod.rs @@ -16,8 +16,8 @@ mod builtin; mod reader; use std::path::Path; -use errors::*; -use severity::Severity; +use crate::errors::*; +use crate::severity::Severity; pub use self::raw_rule::RawRule; pub use self::pattern_rule::PatternRule; use self::builtin::get_builtin_rules; diff --git a/src/rule/pattern_rule.rs b/src/rule/pattern_rule.rs index e57a148..42d4bba 100644 --- a/src/rule/pattern_rule.rs +++ b/src/rule/pattern_rule.rs @@ -1,10 +1,10 @@ use std::convert::TryFrom; use regex::Regex; -use errors::*; -use severity::Severity; +use crate::errors::*; +use crate::severity::Severity; use super::RuleTrait; use super::raw_rule::RawRule; -use Rule; +use crate::Rule; /// Rule with compiled regular expression members #[derive(Debug, Clone)] diff --git a/src/rule/raw_rule.rs b/src/rule/raw_rule.rs index 6df884e..1e86bd8 100644 --- a/src/rule/raw_rule.rs +++ b/src/rule/raw_rule.rs @@ -1,4 +1,4 @@ -use severity::Severity; +use crate::severity::Severity; use super::RuleTrait; /// "raw" Rule diff --git a/src/rule/reader.rs b/src/rule/reader.rs index 735c83d..105f5e4 100644 --- a/src/rule/reader.rs +++ b/src/rule/reader.rs @@ -1,9 +1,10 @@ -use rule::raw_rule::RawRule; +use crate::rule::raw_rule::RawRule; use std::path::Path; -use errors::*; +use crate::errors::*; use std::fs::File; use std::io::BufReader; -use Rule; +use crate::Rule; +use std::error::Error as StdError; pub struct Reader {} @@ -61,7 +62,7 @@ fn build_file_type_error(path: &Path) -> Error { }.into() } -fn build_deserialize_error(path: &Path, error: &::std::error::Error) -> Error { +fn build_deserialize_error(path: &Path, error: &dyn StdError) -> Error { match path.to_str() { None => ErrorKind::ReaderError(format!("Could not deserialize file: {}", error)), Some(f) => ErrorKind::ReaderError(format!("Could not deserialize the file '{}': {}", f, error)), @@ -86,8 +87,8 @@ fn get_file_reader(path: &Path) -> Result, Error> { #[cfg(test)] mod test { use super::*; - use rule::RuleTrait; - use severity::Severity; + use crate::rule::RuleTrait; + use crate::severity::Severity; fn path(rule: &Rule) -> String { match rule { diff --git a/src/severity.rs b/src/severity.rs index 0824313..88eb5f3 100644 --- a/src/severity.rs +++ b/src/severity.rs @@ -1,7 +1,7 @@ use std::fmt; use std::fmt::Display; use std::str::FromStr; -use errors::Error; +use crate::errors::Error; #[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Deserialize)] pub enum Severity { diff --git a/tests/file_finder_test.rs b/tests/file_finder_test.rs index 2fee893..9a8453d 100644 --- a/tests/file_finder_test.rs +++ b/tests/file_finder_test.rs @@ -3,7 +3,7 @@ extern crate hackscanner_lib; mod test_helpers; mod file_finder_test_suite { - use test_helpers::*; + use crate::test_helpers::*; use hackscanner_lib::*; #[test] diff --git a/tests/fts_test.rs b/tests/fts_test.rs index d37efa4..712074b 100644 --- a/tests/fts_test.rs +++ b/tests/fts_test.rs @@ -1,11 +1,11 @@ extern crate hackscanner_lib; -mod test_helpers; +pub mod test_helpers; // FTS support is disable by default #[cfg(feature = "fts")] mod file_finder_test_suite_fts { - use test_helpers::*; + use crate::test_helpers::*; use hackscanner_lib::file_finder::FileFinderTrait; use hackscanner_lib::file_finder::fts::FileFinder; diff --git a/tests/ftw_test.rs b/tests/ftw_test.rs index bd94984..7ee5b8f 100644 --- a/tests/ftw_test.rs +++ b/tests/ftw_test.rs @@ -3,7 +3,7 @@ extern crate hackscanner_lib; mod test_helpers; mod file_finder_test_suite_ftw { - use test_helpers::*; + use crate::test_helpers::*; use hackscanner_lib::file_finder::FileFinderTrait; use hackscanner_lib::file_finder::ftw::FileFinder; diff --git a/tests/run_test.rs b/tests/run_test.rs index 1d6da12..5f69c87 100644 --- a/tests/run_test.rs +++ b/tests/run_test.rs @@ -47,7 +47,7 @@ fn assert_not_contains_entry_with_score(ratings: &Vec, score: isize, pat } fn configure_logging(log_level_filter: simplelog::LevelFilter) { - let mut loggers: Vec> = vec![]; + let mut loggers: Vec> = vec![]; let mut config = simplelog::Config::default(); config.time_format = Some("%H:%M:%S%.3f"); diff --git a/tests/walkdir_test.rs b/tests/walkdir_test.rs index 274079a..a636e7f 100644 --- a/tests/walkdir_test.rs +++ b/tests/walkdir_test.rs @@ -3,7 +3,7 @@ extern crate hackscanner_lib; mod test_helpers; mod file_finder_test_suite_walkdir { - use test_helpers::*; + use crate::test_helpers::*; use hackscanner_lib::file_finder::FileFinderTrait; use hackscanner_lib::file_finder::walkdir::FileFinder;