From a3d8fc00e1946388c3e77ad7d26c0872c358bd53 Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Wed, 29 Jan 2020 22:25:35 -0600 Subject: [PATCH] Refactor ~ use `AsRef` where possible --- src/main.rs | 2 +- src/utils/mod.rs | 24 +++++++++++++----------- src/utils/platform.rs | 6 +++--- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index bb25603..aec5656 100644 --- a/src/main.rs +++ b/src/main.rs @@ -146,7 +146,7 @@ fn main() { let simplified_dirs = simplify_dir_names(target_dirs); let (permissions, nodes) = get_dir_tree( &simplified_dirs, - ignore_directories, + &ignore_directories, use_apparent_size, limit_filesystem, threads, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index a73325e..5586e29 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -70,9 +70,9 @@ pub fn simplify_dir_names>(filenames: Vec

) -> HashSet top_level_names } -pub fn get_dir_tree( - top_level_names: &HashSet, - ignore_directories: Option>, +pub fn get_dir_tree>( + top_level_names: &HashSet

, + ignore_directories: &Option>, apparent_size: bool, limit_filesystem: bool, threads: Option, @@ -90,7 +90,7 @@ pub fn get_dir_tree( b, apparent_size, &restricted_filesystems, - &ignore_directories, + ignore_directories, &mut data, &mut permissions, threads, @@ -99,7 +99,7 @@ pub fn get_dir_tree( (permissions == 0, data) } -fn get_allowed_filesystems(top_level_names: &HashSet) -> Option> { +fn get_allowed_filesystems>(top_level_names: &HashSet

) -> Option> { let mut limit_filesystems: HashSet = HashSet::new(); for file_name in top_level_names.iter() { if let Ok(a) = get_filesystem(file_name) { @@ -109,7 +109,7 @@ fn get_allowed_filesystems(top_level_names: &HashSet) -> Option>(path: P) -> PathBuf { +pub fn normalize_path>(path: P) -> PathBuf { // normalize path ... // 1. removing repeated separators // 2. removing interior '.' ("current directory") path segments @@ -119,8 +119,8 @@ pub fn normalize_path>(path: P) -> PathBuf { path.as_ref().components().collect::() } -fn examine_dir( - top_dir: &PathBuf, +fn examine_dir>( + top_dir: P, apparent_size: bool, filesystems: &Option>, ignore_directories: &Option>, @@ -128,6 +128,7 @@ fn examine_dir( file_count_no_permission: &mut u64, threads: Option, ) { + let top_dir = top_dir.as_ref(); let mut inodes: HashSet<(u64, u64)> = HashSet::new(); let mut iter = WalkDir::new(top_dir) .preload_metadata(true) @@ -155,7 +156,7 @@ fn examine_dir( match maybe_size_and_inode { Some((size, maybe_inode)) => { if !should_ignore_file(apparent_size, filesystems, &mut inodes, maybe_inode) { - process_file_with_size_and_inode(&top_dir, data, e, size) + process_file_with_size_and_inode(top_dir, data, e, size) } } None => *file_count_no_permission += 1, @@ -193,12 +194,13 @@ fn should_ignore_file( false } -fn process_file_with_size_and_inode( - top_dir: &PathBuf, +fn process_file_with_size_and_inode>( + top_dir: P, data: &mut HashMap, e: DirEntry, size: u64, ) { + let top_dir = top_dir.as_ref(); // This path and all its parent paths have their counter incremented for path in e.path().ancestors() { // This is required due to bug in Jwalk that adds '/' to all sub dir lists diff --git a/src/utils/platform.rs b/src/utils/platform.rs index b35687e..d2b1720 100644 --- a/src/utils/platform.rs +++ b/src/utils/platform.rs @@ -2,7 +2,7 @@ use jwalk::DirEntry; #[allow(unused_imports)] use std::fs; use std::io; -use std::path::PathBuf; +use std::path::Path; #[cfg(target_family = "unix")] fn get_block_size() -> u64 { @@ -49,14 +49,14 @@ pub fn get_metadata(d: &DirEntry, _apparent: bool) -> Option<(u64, Option<(u64, } #[cfg(target_family = "unix")] -pub fn get_filesystem(file_path: &PathBuf) -> Result { +pub fn get_filesystem>(file_path: P) -> Result { use std::os::unix::fs::MetadataExt; let metadata = fs::metadata(file_path)?; Ok(metadata.dev()) } #[cfg(target_family = "windows")] -pub fn get_filesystem(file_path: &PathBuf) -> Result { +pub fn get_filesystem>(file_path: P) -> Result { use winapi_util::file::information; use winapi_util::Handle;