Skip to content

Commit

Permalink
fix ignore dirs wip
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrwach committed Dec 27, 2023
1 parent 196f0d6 commite2d5a34
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
83 changes: 79 additions & 4 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
usecrate::crossdev;
usecrate::traverse::{EntryData,Tree,TreeIndex};
usebyte_unit::{n_gb_bytes,n_gib_bytes,n_mb_bytes,n_mib_bytes,ByteUnit};
uselog::info;
usestd::fs;
usestd::path::PathBuf;
usestd::sync::atomic::{AtomicBool,Ordering};
usestd::sync::Arc;
Expand Down Expand Up @@ -178,7 +178,7 @@ type WalkDir = jwalk::WalkDirGeneric<((), Option<Result<std::fs::Metadata, jwalk

implWalkOptions{
pub(crate)fniter_from_path(&self,root:&Path,root_device_id:u64)->WalkDir{
info!("root path={:?}",root);
letignore_dirs =self.ignore_dirs.clone();
WalkDir::new(root)
.follow_links(false)
.sort(matchself.sorting{
Expand All @@ -187,7 +187,6 @@ impl WalkOptions {
})
.skip_hidden(false)
.process_read_dir({
letignore_dirs =self.ignore_dirs.clone();
letcross_filesystems =self.cross_filesystems;
move|_,_,_,dir_entry_results|{
dir_entry_results.iter_mut().for_each(|dir_entry_result|{
Expand All @@ -200,7 +199,7 @@ impl WalkOptions {
.as_ref()
.map(|m| crossdev::is_same_device(root_device_id,m))
.unwrap_or(true);
if!ok_for_fs ||ignore_dirs.contains(&dir_entry.path()){
if!ok_for_fs ||ignore_directory(&dir_entry.path(),&ignore_dirs){
dir_entry.read_children_path=None;
}
}
Expand Down Expand Up @@ -241,3 +240,79 @@ impl WalkResult {
i32::from(self.num_errors>0)
}
}

pubfncanonicalize_ignore_dirs(ignore_dirs:&[PathBuf])->Vec<PathBuf>{
ignore_dirs
.iter()
.map(fs::canonicalize)
.filter_map(Result::ok)
.collect()
}

fnignore_directory(path:&Path,ignore_dirs:&[PathBuf])->bool{
ifignore_dirs.is_empty(){
returnfalse;
}
letpath = fs::canonicalize(path);
path.map(|path| ignore_dirs.contains(&path))
.unwrap_or(false)
}

#[cfg(test)]
modtests{
usesuper::*;

#[test]
fntest_ignore_directories(){
#[cfg(unix)]
letmutparameters =vec![
("/usr",vec!["/usr"],true),
("/usr/local",vec!["/usr"],false),
("/smth",vec!["/usr"],false),
("/usr/local/..",vec!["/usr/local/.."],true),
("/usr",vec!["/usr/local/.."],true),
("/usr/local/share/../..",vec!["/usr"],true),
];

#[cfg(windows)]
letmutparameters =vec![
("C:\\Windows ",vec!["C:\\Windows "],true),
("C:\\Windows\\System ",vec!["C:\\Windows "],false),
("C:\\Smth ",vec!["C:\\Windows "],false),
(
"C:\\Windows\\System\\.. ",
vec!["C:\\Windows\\System\\.. "],
true,
),
("C:\\Windows ",vec!["C:\\Windows\\System\\.. "],true),
(
"C:\\Windows\\System\\Speech\\..\\.. ",
vec!["C:\\Windows "],
true,
),
];

parameters.append(&mutvec![
("src",vec!["src"],true),
("src/interactive",vec!["src"],false),
("src/interactive/..",vec!["src"],true),
]);

for(path,ignore_dirs,expected_result)inparameters{
letignore_dirs =canonicalize_ignore_dirs(
&ignore_dirs
.into_iter()
.map(|p|PathBuf::from(p))
.collect::<Vec<PathBuf>>(),
);
assert_eq!(
ignore_directory(&PathBuf::from(path),&ignore_dirs),
expected_result,
"result='{}' for path='{}' and ignore_dir='{:?}'",
expected_result,
path,
ignore_dirs
);
}
}
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![forbid(unsafe_code,rust_2018_idioms,unsafe_code)]
useanyhow::Result;
useclap::Parser;
usedua::TraversalSorting;
usedua::{canonicalize_ignore_dirs,TraversalSorting};
uselog::info;
usesimplelog::{Config,LevelFilter,WriteLogger};
usestd::fs::OpenOptions;
Expand Down Expand Up @@ -46,7 +46,7 @@ fn main() -> Result<()> {
count_hard_links:opt.count_hard_links,
sorting:TraversalSorting::None,
cross_filesystems:!opt.stay_on_filesystem,
ignore_dirs:opt.ignore_dirs,
ignore_dirs:canonicalize_ignore_dirs(&opt.ignore_dirs),
};
letres =matchopt.command{
#[cfg(feature ="tui-crossplatform")]
Expand Down

0 comments on commite2d5a34

Please sign into comment.