Refactor ~ use AsRef<Path> where possible

This commit is contained in:
Roy Ivy III
2020-01-29 22:25:35 -06:00
parent 9d4531d48b
commit a3d8fc00e1
3 changed files with 17 additions and 15 deletions

View File

@@ -146,7 +146,7 @@ fn main() {
let simplified_dirs = simplify_dir_names(target_dirs); let simplified_dirs = simplify_dir_names(target_dirs);
let (permissions, nodes) = get_dir_tree( let (permissions, nodes) = get_dir_tree(
&simplified_dirs, &simplified_dirs,
ignore_directories, &ignore_directories,
use_apparent_size, use_apparent_size,
limit_filesystem, limit_filesystem,
threads, threads,

View File

@@ -70,9 +70,9 @@ pub fn simplify_dir_names<P: AsRef<Path>>(filenames: Vec<P>) -> HashSet<PathBuf>
top_level_names top_level_names
} }
pub fn get_dir_tree( pub fn get_dir_tree<P: AsRef<Path>>(
top_level_names: &HashSet<PathBuf>, top_level_names: &HashSet<P>,
ignore_directories: Option<Vec<PathBuf>>, ignore_directories: &Option<Vec<PathBuf>>,
apparent_size: bool, apparent_size: bool,
limit_filesystem: bool, limit_filesystem: bool,
threads: Option<usize>, threads: Option<usize>,
@@ -90,7 +90,7 @@ pub fn get_dir_tree(
b, b,
apparent_size, apparent_size,
&restricted_filesystems, &restricted_filesystems,
&ignore_directories, ignore_directories,
&mut data, &mut data,
&mut permissions, &mut permissions,
threads, threads,
@@ -99,7 +99,7 @@ pub fn get_dir_tree(
(permissions == 0, data) (permissions == 0, data)
} }
fn get_allowed_filesystems(top_level_names: &HashSet<PathBuf>) -> Option<HashSet<u64>> { fn get_allowed_filesystems<P: AsRef<Path>>(top_level_names: &HashSet<P>) -> Option<HashSet<u64>> {
let mut limit_filesystems: HashSet<u64> = HashSet::new(); let mut limit_filesystems: HashSet<u64> = HashSet::new();
for file_name in top_level_names.iter() { for file_name in top_level_names.iter() {
if let Ok(a) = get_filesystem(file_name) { if let Ok(a) = get_filesystem(file_name) {
@@ -109,7 +109,7 @@ fn get_allowed_filesystems(top_level_names: &HashSet<PathBuf>) -> Option<HashSet
Some(limit_filesystems) Some(limit_filesystems)
} }
pub fn normalize_path<P: AsRef<std::path::Path>>(path: P) -> PathBuf { pub fn normalize_path<P: AsRef<Path>>(path: P) -> PathBuf {
// normalize path ... // normalize path ...
// 1. removing repeated separators // 1. removing repeated separators
// 2. removing interior '.' ("current directory") path segments // 2. removing interior '.' ("current directory") path segments
@@ -119,8 +119,8 @@ pub fn normalize_path<P: AsRef<std::path::Path>>(path: P) -> PathBuf {
path.as_ref().components().collect::<PathBuf>() path.as_ref().components().collect::<PathBuf>()
} }
fn examine_dir( fn examine_dir<P: AsRef<Path>>(
top_dir: &PathBuf, top_dir: P,
apparent_size: bool, apparent_size: bool,
filesystems: &Option<HashSet<u64>>, filesystems: &Option<HashSet<u64>>,
ignore_directories: &Option<Vec<PathBuf>>, ignore_directories: &Option<Vec<PathBuf>>,
@@ -128,6 +128,7 @@ fn examine_dir(
file_count_no_permission: &mut u64, file_count_no_permission: &mut u64,
threads: Option<usize>, threads: Option<usize>,
) { ) {
let top_dir = top_dir.as_ref();
let mut inodes: HashSet<(u64, u64)> = HashSet::new(); let mut inodes: HashSet<(u64, u64)> = HashSet::new();
let mut iter = WalkDir::new(top_dir) let mut iter = WalkDir::new(top_dir)
.preload_metadata(true) .preload_metadata(true)
@@ -155,7 +156,7 @@ 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, &mut inodes, 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, None => *file_count_no_permission += 1,
@@ -193,12 +194,13 @@ fn should_ignore_file(
false false
} }
fn process_file_with_size_and_inode( fn process_file_with_size_and_inode<P: AsRef<Path>>(
top_dir: &PathBuf, top_dir: P,
data: &mut HashMap<PathBuf, u64>, data: &mut HashMap<PathBuf, u64>,
e: DirEntry, e: DirEntry,
size: u64, size: u64,
) { ) {
let top_dir = top_dir.as_ref();
// This path and all its parent paths have their counter incremented // This path and all its parent paths have their counter incremented
for path in e.path().ancestors() { for path 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

View File

@@ -2,7 +2,7 @@ use jwalk::DirEntry;
#[allow(unused_imports)] #[allow(unused_imports)]
use std::fs; use std::fs;
use std::io; use std::io;
use std::path::PathBuf; use std::path::Path;
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
fn get_block_size() -> u64 { 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")] #[cfg(target_family = "unix")]
pub fn get_filesystem(file_path: &PathBuf) -> Result<u64, io::Error> { pub fn get_filesystem<P: AsRef<Path>>(file_path: P) -> Result<u64, io::Error> {
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
let metadata = fs::metadata(file_path)?; let metadata = fs::metadata(file_path)?;
Ok(metadata.dev()) Ok(metadata.dev())
} }
#[cfg(target_family = "windows")] #[cfg(target_family = "windows")]
pub fn get_filesystem(file_path: &PathBuf) -> Result<u64, io::Error> { pub fn get_filesystem<P: AsRef<Path>>(file_path: P) -> Result<u64, io::Error> {
use winapi_util::file::information; use winapi_util::file::information;
use winapi_util::Handle; use winapi_util::Handle;