Skip to content

Commit

Permalink
Use gix-path for more control and performance.
Browse files Browse the repository at this point in the history
Also add more debugging information to the log, and allow `debug`
logging to show up in there.

While at it, assure that performance won't be affected too strongly
when a lot of directories are used by using a set instead.
  • Loading branch information
Byron committed Jan 3, 2024
1 parent 7905b48 commit 93f0f61
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
36 changes: 24 additions & 12 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::crossdev;
use crate::traverse::{EntryData, Tree, TreeIndex};
use byte_unit::{n_gb_bytes, n_gib_bytes, n_mb_bytes, n_mib_bytes, ByteUnit};
use std::fs;
use std::collections::BTreeSet;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -171,14 +171,15 @@ pub struct WalkOptions {
pub apparent_size: bool,
pub sorting: TraversalSorting,
pub cross_filesystems: bool,
pub ignore_dirs: Vec<PathBuf>,
pub ignore_dirs: BTreeSet<PathBuf>,
}

type WalkDir = jwalk::WalkDirGeneric<((), Option<Result<std::fs::Metadata, jwalk::Error>>)>;

impl WalkOptions {
pub(crate) fn iter_from_path(&self, root: &Path, root_device_id: u64) -> WalkDir {
let ignore_dirs = self.ignore_dirs.clone();
let cwd = std::env::current_dir().unwrap_or_else(|_| root.to_owned());
WalkDir::new(root)
.follow_links(false)
.sort(match self.sorting {
Expand All @@ -199,7 +200,9 @@ impl WalkOptions {
.as_ref()
.map(|m| crossdev::is_same_device(root_device_id, m))
.unwrap_or(true);
if !ok_for_fs || ignore_directory(&dir_entry.path(), &ignore_dirs) {
if !ok_for_fs
|| ignore_directory(&dir_entry.path(), &ignore_dirs, &cwd)
{
dir_entry.read_children_path = None;
}
}
Expand Down Expand Up @@ -241,21 +244,29 @@ impl WalkResult {
}
}

pub fn canonicalize_ignore_dirs(ignore_dirs: &[PathBuf]) -> Vec<PathBuf> {
ignore_dirs
pub fn canonicalize_ignore_dirs(ignore_dirs: &[PathBuf]) -> BTreeSet<PathBuf> {
let dirs = ignore_dirs
.iter()
.map(fs::canonicalize)
.map(gix_path::realpath)
.filter_map(Result::ok)
.collect()
.collect();
log::info!("Ignoring canonicalized {dirs:?}");
dirs
}

fn ignore_directory(path: &Path, ignore_dirs: &[PathBuf]) -> bool {
fn ignore_directory(path: &Path, ignore_dirs: &BTreeSet<PathBuf>, cwd: &Path) -> bool {
if ignore_dirs.is_empty() {
return false;
}
let path = fs::canonicalize(path);
path.map(|path| ignore_dirs.contains(&path))
.unwrap_or(false)
let path = gix_path::realpath_opts(path, cwd, 32);
path.map(|path| {
let ignored = ignore_dirs.contains(&path);
if ignored {
log::debug!("Ignored {path:?}");
}
ignored
})
.unwrap_or(false)
}

#[cfg(test)]
Expand All @@ -264,6 +275,7 @@ mod tests {

#[test]
fn test_ignore_directories() {
let cwd = std::env::current_dir().unwrap();
#[cfg(unix)]
let mut parameters = vec![
("/usr", vec!["/usr"], true),
Expand Down Expand Up @@ -303,7 +315,7 @@ mod tests {
&ignore_dirs.into_iter().map(Into::into).collect::<Vec<_>>(),
);
assert_eq!(
ignore_directory(path.as_ref(), &ignore_dirs),
ignore_directory(path.as_ref(), &ignore_dirs, &cwd),
expected_result,
"result='{expected_result}' for path='{path}' and ignore_dir='{ignore_dirs:?}' "
);
Expand Down
2 changes: 1 addition & 1 deletion src/interactive/app/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub fn initialized_app_and_terminal_with_closure(
count_hard_links: false,
sorting: TraversalSorting::AlphabeticalByFileName,
cross_filesystems: false,
ignore_dirs: Vec::new(),
ignore_dirs: Default::default(),
},
input_paths,
Interaction::None,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() -> Result<()> {
if let Some(log_file) = &opt.log_file {
log_panics::init();
WriteLogger::init(
LevelFilter::Info,
LevelFilter::Debug,
Config::default(),
OpenOptions::new()
.write(true)
Expand Down

0 comments on commit 93f0f61

Please sign in to comment.