Refactor code

Reduce complexity of examine_dir.

No logic changes
This commit is contained in:
andy.boot
2020-01-15 20:10:33 +00:00
parent bdc3d404ef
commit 5541df6a73
+31 -10
View File
@@ -1,3 +1,4 @@
use jwalk::DirEntry;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::HashSet; use std::collections::HashSet;
@@ -116,7 +117,7 @@ pub fn strip_end_slash(mut new_name: &str) -> &str {
fn examine_dir( fn examine_dir(
top_dir: &str, top_dir: &str,
apparent_size: bool, apparent_size: bool,
restricted_filesystems: &Option<HashSet<u64>>, filesystems: &Option<HashSet<u64>>,
inodes: &mut HashSet<(u64, u64)>, inodes: &mut HashSet<(u64, u64)>,
data: &mut HashMap<String, u64>, data: &mut HashMap<String, u64>,
file_count_no_permission: &mut u64, file_count_no_permission: &mut u64,
@@ -134,6 +135,24 @@ fn examine_dir(
match maybe_size_and_inode { match maybe_size_and_inode {
Some((size, maybe_inode)) => { Some((size, maybe_inode)) => {
if !should_ignore_file(apparent_size, filesystems, inodes, maybe_inode) {
process_file_with_size_and_inode(top_dir, data, e, size)
}
}
None => *file_count_no_permission += 1,
}
} else {
*file_count_no_permission += 1
}
}
}
fn should_ignore_file(
apparent_size: bool,
restricted_filesystems: &Option<HashSet<u64>>,
inodes: &mut HashSet<(u64, u64)>,
maybe_inode: Option<(u64, u64)>,
) -> bool {
if !apparent_size { if !apparent_size {
if let Some(inode_dev_pair) = maybe_inode { if let Some(inode_dev_pair) = maybe_inode {
// Ignore files on different devices (if flag applied) // Ignore files on different devices (if flag applied)
@@ -143,15 +162,24 @@ fn examine_dir(
.unwrap() .unwrap()
.contains(&inode_dev_pair.1) .contains(&inode_dev_pair.1)
{ {
continue; return true;
} }
// Ignore files already visited or symlinked // Ignore files already visited or symlinked
if inodes.contains(&inode_dev_pair) { if inodes.contains(&inode_dev_pair) {
continue; return true;
} }
inodes.insert(inode_dev_pair); inodes.insert(inode_dev_pair);
} }
} }
false
}
fn process_file_with_size_and_inode(
top_dir: &str,
data: &mut HashMap<String, u64>,
e: DirEntry,
size: u64,
) {
// This path and all its parent paths have their counter incremented // This path and all its parent paths have their counter incremented
for path_name in e.path().ancestors() { for path_name in e.path().ancestors() {
// This is required due to bug in Jwalk that adds '/' to all sub dir lists // This is required due to bug in Jwalk that adds '/' to all sub dir lists
@@ -166,13 +194,6 @@ fn examine_dir(
break; break;
} }
} }
}
None => *file_count_no_permission += 1,
}
} else {
*file_count_no_permission += 1
}
}
} }
pub fn sort_by_size_first_name_second(a: &(String, u64), b: &(String, u64)) -> Ordering { pub fn sort_by_size_first_name_second(a: &(String, u64), b: &(String, u64)) -> Ordering {