streamline func APIs processing target_dirs

This commit is contained in:
Jan Chren ~rindeal
2024-06-24 04:29:05 +00:00
committed by andy.boot
parent 58c9f6d509
commit 3ed95ee399
4 changed files with 29 additions and 29 deletions
+1 -1
View File
@@ -111,7 +111,7 @@ fn ignore_file(entry: &DirEntry, walk_data: &WalkData) -> bool {
let is_dot_file = entry.file_name().to_str().unwrap_or("").starts_with('.'); let is_dot_file = entry.file_name().to_str().unwrap_or("").starts_with('.');
let is_ignored_path = walk_data.ignore_directories.contains(&entry.path()); let is_ignored_path = walk_data.ignore_directories.contains(&entry.path());
let size_inode_device = get_metadata(&entry.path(), false); let size_inode_device = get_metadata(entry.path(), false);
if let Some((_size, Some((_id, dev)), (modified_time, accessed_time, changed_time))) = if let Some((_size, Some((_id, dev)), (modified_time, accessed_time, changed_time))) =
size_inode_device size_inode_device
{ {
+2 -2
View File
@@ -193,9 +193,9 @@ fn main() {
let follow_links = options.get_flag("dereference_links"); let follow_links = options.get_flag("dereference_links");
let allowed_filesystems = limit_filesystem let allowed_filesystems = limit_filesystem
.then(|| get_filesystem_devices(target_dirs.iter().cloned())) .then(|| get_filesystem_devices(&target_dirs))
.unwrap_or_default(); .unwrap_or_default();
let simplified_dirs = simplify_dir_names(target_dirs); let simplified_dirs = simplify_dir_names(&target_dirs);
let ignored_full_path: HashSet<PathBuf> = ignore_directories let ignored_full_path: HashSet<PathBuf> = ignore_directories
.into_iter() .into_iter()
+13 -12
View File
@@ -14,12 +14,12 @@ type InodeAndDevice = (u64, u64);
type FileTime = (i64, i64, i64); type FileTime = (i64, i64, i64);
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
pub fn get_metadata( pub fn get_metadata<P: AsRef<Path>>(
d: &Path, path: P,
use_apparent_size: bool, use_apparent_size: bool,
) -> Option<(u64, Option<InodeAndDevice>, FileTime)> { ) -> Option<(u64, Option<InodeAndDevice>, FileTime)> {
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
match d.metadata() { match path.as_ref().metadata() {
Ok(md) => { Ok(md) => {
if use_apparent_size { if use_apparent_size {
Some(( Some((
@@ -40,8 +40,8 @@ pub fn get_metadata(
} }
#[cfg(target_family = "windows")] #[cfg(target_family = "windows")]
pub fn get_metadata( pub fn get_metadata<P: AsRef<Path>>(
d: &Path, path: P,
use_apparent_size: bool, use_apparent_size: bool,
) -> Option<(u64, Option<InodeAndDevice>, FileTime)> { ) -> Option<(u64, Option<InodeAndDevice>, FileTime)> {
// On windows opening the file to get size, file ID and volume can be very // On windows opening the file to get size, file ID and volume can be very
@@ -82,7 +82,7 @@ pub fn get_metadata(
use std::io; use std::io;
use winapi_util::Handle; use winapi_util::Handle;
fn handle_from_path_limited<P: AsRef<Path>>(path: P) -> io::Result<Handle> { fn handle_from_path_limited(path: &Path) -> io::Result<Handle> {
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::os::windows::fs::OpenOptionsExt; use std::os::windows::fs::OpenOptionsExt;
const FILE_READ_ATTRIBUTES: u32 = 0x0080; const FILE_READ_ATTRIBUTES: u32 = 0x0080;
@@ -108,18 +108,18 @@ pub fn get_metadata(
} }
fn get_metadata_expensive( fn get_metadata_expensive(
d: &Path, path: &Path,
use_apparent_size: bool, use_apparent_size: bool,
) -> Option<(u64, Option<InodeAndDevice>, FileTime)> { ) -> Option<(u64, Option<InodeAndDevice>, FileTime)> {
use winapi_util::file::information; use winapi_util::file::information;
let h = handle_from_path_limited(d).ok()?; let h = handle_from_path_limited(path).ok()?;
let info = information(&h).ok()?; let info = information(&h).ok()?;
if use_apparent_size { if use_apparent_size {
use filesize::PathExt; use filesize::PathExt;
Some(( Some((
d.size_on_disk().ok()?, path.size_on_disk().ok()?,
Some((info.file_index(), info.volume_serial_number())), Some((info.file_index(), info.volume_serial_number())),
( (
info.last_write_time().unwrap() as i64, info.last_write_time().unwrap() as i64,
@@ -141,7 +141,8 @@ pub fn get_metadata(
} }
use std::os::windows::fs::MetadataExt; use std::os::windows::fs::MetadataExt;
match d.metadata() { let path = path.as_ref();
match path.metadata() {
Ok(ref md) => { Ok(ref md) => {
const FILE_ATTRIBUTE_ARCHIVE: u32 = 0x20; const FILE_ATTRIBUTE_ARCHIVE: u32 = 0x20;
const FILE_ATTRIBUTE_READONLY: u32 = 0x01; const FILE_ATTRIBUTE_READONLY: u32 = 0x01;
@@ -179,9 +180,9 @@ pub fn get_metadata(
), ),
)) ))
} else { } else {
get_metadata_expensive(d, use_apparent_size) get_metadata_expensive(path, use_apparent_size)
} }
} }
_ => get_metadata_expensive(d, use_apparent_size), _ => get_metadata_expensive(path, use_apparent_size),
} }
} }
+13 -14
View File
@@ -8,10 +8,10 @@ use crate::dir_walker::Operater;
use crate::platform; use crate::platform;
use regex::Regex; use regex::Regex;
pub fn simplify_dir_names<P: AsRef<Path>>(filenames: Vec<P>) -> HashSet<PathBuf> { pub fn simplify_dir_names<P: AsRef<Path>>(dirs: &[P]) -> HashSet<PathBuf> {
let mut top_level_names: HashSet<PathBuf> = HashSet::with_capacity(filenames.len()); let mut top_level_names: HashSet<PathBuf> = HashSet::with_capacity(dirs.len());
for t in filenames { for t in dirs {
let top_level_name = normalize_path(t); let top_level_name = normalize_path(t);
let mut can_add = true; let mut can_add = true;
let mut to_remove: Vec<PathBuf> = Vec::new(); let mut to_remove: Vec<PathBuf> = Vec::new();
@@ -34,12 +34,11 @@ pub fn simplify_dir_names<P: AsRef<Path>>(filenames: Vec<P>) -> HashSet<PathBuf>
top_level_names top_level_names
} }
pub fn get_filesystem_devices<'a, P: IntoIterator<Item = &'a str>>(paths: P) -> HashSet<u64> { pub fn get_filesystem_devices<P: AsRef<Path>>(paths: &[P]) -> HashSet<u64> {
// Gets the device ids for the filesystems which are used by the argument paths // Gets the device ids for the filesystems which are used by the argument paths
paths paths
.into_iter() .iter()
.map(PathBuf::from) .filter_map(|p| match get_metadata(p, false) {
.filter_map(|p| match get_metadata(&p, false) {
Some((_size, Some((_id, dev)), _time)) => Some(dev), Some((_size, Some((_id, dev)), _time)) => Some(dev),
_ => None, _ => None,
}) })
@@ -96,15 +95,15 @@ mod tests {
fn test_simplify_dir() { fn test_simplify_dir() {
let mut correct = HashSet::new(); let mut correct = HashSet::new();
correct.insert(PathBuf::from("a")); correct.insert(PathBuf::from("a"));
assert_eq!(simplify_dir_names(vec!["a"]), correct); assert_eq!(simplify_dir_names(&["a"]), correct);
} }
#[test] #[test]
fn test_simplify_dir_rm_subdir() { fn test_simplify_dir_rm_subdir() {
let mut correct = HashSet::new(); let mut correct = HashSet::new();
correct.insert(["a", "b"].iter().collect::<PathBuf>()); correct.insert(["a", "b"].iter().collect::<PathBuf>());
assert_eq!(simplify_dir_names(vec!["a/b/c", "a/b", "a/b/d/f"]), correct); assert_eq!(simplify_dir_names(&["a/b/c", "a/b", "a/b/d/f"]), correct);
assert_eq!(simplify_dir_names(vec!["a/b", "a/b/c", "a/b/d/f"]), correct); assert_eq!(simplify_dir_names(&["a/b", "a/b/c", "a/b/d/f"]), correct);
} }
#[test] #[test]
@@ -113,7 +112,7 @@ mod tests {
correct.insert(["a", "b"].iter().collect::<PathBuf>()); correct.insert(["a", "b"].iter().collect::<PathBuf>());
correct.insert(PathBuf::from("c")); correct.insert(PathBuf::from("c"));
assert_eq!( assert_eq!(
simplify_dir_names(vec![ simplify_dir_names(&[
"a/b", "a/b",
"a/b//", "a/b//",
"a/././b///", "a/././b///",
@@ -132,14 +131,14 @@ mod tests {
correct.insert(PathBuf::from("b")); correct.insert(PathBuf::from("b"));
correct.insert(["c", "a", "b"].iter().collect::<PathBuf>()); correct.insert(["c", "a", "b"].iter().collect::<PathBuf>());
correct.insert(["a", "b"].iter().collect::<PathBuf>()); correct.insert(["a", "b"].iter().collect::<PathBuf>());
assert_eq!(simplify_dir_names(vec!["a/b", "c/a/b/", "b"]), correct); assert_eq!(simplify_dir_names(&["a/b", "c/a/b/", "b"]), correct);
} }
#[test] #[test]
fn test_simplify_dir_dots() { fn test_simplify_dir_dots() {
let mut correct = HashSet::new(); let mut correct = HashSet::new();
correct.insert(PathBuf::from("src")); correct.insert(PathBuf::from("src"));
assert_eq!(simplify_dir_names(vec!["src/."]), correct); assert_eq!(simplify_dir_names(&["src/."]), correct);
} }
#[test] #[test]
@@ -147,7 +146,7 @@ mod tests {
let mut correct = HashSet::new(); let mut correct = HashSet::new();
correct.insert(PathBuf::from("src")); correct.insert(PathBuf::from("src"));
correct.insert(PathBuf::from("src_v2")); correct.insert(PathBuf::from("src_v2"));
assert_eq!(simplify_dir_names(vec!["src/", "src_v2"]), correct); assert_eq!(simplify_dir_names(&["src/", "src_v2"]), correct);
} }
#[test] #[test]