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 (permissions, nodes) = get_dir_tree(
&simplified_dirs,
ignore_directories,
&ignore_directories,
use_apparent_size,
limit_filesystem,
threads,

View File

@@ -70,9 +70,9 @@ pub fn simplify_dir_names<P: AsRef<Path>>(filenames: Vec<P>) -> HashSet<PathBuf>
top_level_names
}
pub fn get_dir_tree(
top_level_names: &HashSet<PathBuf>,
ignore_directories: Option<Vec<PathBuf>>,
pub fn get_dir_tree<P: AsRef<Path>>(
top_level_names: &HashSet<P>,
ignore_directories: &Option<Vec<PathBuf>>,
apparent_size: bool,
limit_filesystem: bool,
threads: Option<usize>,
@@ -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<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();
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<PathBuf>) -> Option<HashSet
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 ...
// 1. removing repeated separators
// 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>()
}
fn examine_dir(
top_dir: &PathBuf,
fn examine_dir<P: AsRef<Path>>(
top_dir: P,
apparent_size: bool,
filesystems: &Option<HashSet<u64>>,
ignore_directories: &Option<Vec<PathBuf>>,
@@ -128,6 +128,7 @@ fn examine_dir(
file_count_no_permission: &mut u64,
threads: Option<usize>,
) {
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<P: AsRef<Path>>(
top_dir: P,
data: &mut HashMap<PathBuf, u64>,
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

View File

@@ -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<u64, io::Error> {
pub fn get_filesystem<P: AsRef<Path>>(file_path: P) -> Result<u64, io::Error> {
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<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::Handle;