Compare commits

...

2 Commits

Author SHA1 Message Date
andy.boot
07c228fd08 Fix: depth=0 bug for multiple arguments
https://github.com/bootandy/dust/issues/282
2023-01-05 01:11:16 +00:00
andy.boot
c457a295b5 fix: bug where hard links could be double counted
When running:
dust dir_a dir_b

if a file was hard linked in both dir_a and dir_b it would be double
counted.

This fix resolves this by keeping the shared hashmap around between runs
for the second and subsequent arguments.
https://github.com/bootandy/dust/issues/282
2023-01-04 22:22:41 +00:00
4 changed files with 59 additions and 27 deletions

View File

@@ -31,12 +31,13 @@ pub struct WalkData<'a> {
pub fn walk_it(dirs: HashSet<PathBuf>, walk_data: WalkData) -> (Vec<Node>, bool) { pub fn walk_it(dirs: HashSet<PathBuf>, walk_data: WalkData) -> (Vec<Node>, bool) {
let permissions_flag = AtomicBool::new(false); let permissions_flag = AtomicBool::new(false);
let mut inodes = HashSet::new();
let top_level_nodes: Vec<_> = dirs let top_level_nodes: Vec<_> = dirs
.into_iter() .into_iter()
.filter_map(|d| { .filter_map(|d| {
clean_inodes( clean_inodes(
walk(d, &permissions_flag, &walk_data, 0)?, walk(d, &permissions_flag, &walk_data, 0)?,
&mut HashSet::new(), &mut inodes,
walk_data.use_apparent_size, walk_data.use_apparent_size,
) )
}) })

View File

@@ -20,13 +20,20 @@ pub fn get_biggest(
let mut heap = BinaryHeap::new(); let mut heap = BinaryHeap::new();
let number_top_level_nodes = top_level_nodes.len(); let number_top_level_nodes = top_level_nodes.len();
let root = get_new_root(top_level_nodes); let root = get_new_root(top_level_nodes);
let mut allowed_nodes = HashSet::new(); let mut allowed_nodes = HashSet::new();
allowed_nodes.insert(root.name.as_path()); allowed_nodes.insert(root.name.as_path());
heap = add_children(using_a_filter, min_size, only_dir, &root, depth, heap);
for _ in number_top_level_nodes..n { if number_top_level_nodes > 1 {
heap = add_children(using_a_filter, min_size, only_dir, &root, usize::MAX, heap);
} else {
heap = add_children(using_a_filter, min_size, only_dir, &root, depth, heap);
}
let number_of_lines_in_output = n - number_top_level_nodes;
for _ in 0..number_of_lines_in_output {
let line = heap.pop(); let line = heap.pop();
match line { match line {
Some(line) => { Some(line) => {

View File

@@ -73,6 +73,7 @@ pub fn test_ignore_dir() {
let output = build_command(vec!["-c", "-X", "dir_substring", "tests/test_dir2/"]); let output = build_command(vec!["-c", "-X", "dir_substring", "tests/test_dir2/"]);
assert!(!output.contains("dir_substring")); assert!(!output.contains("dir_substring"));
} }
// Add test for multiple dirs - with -d 0 and maybe -d 1 check the
#[test] #[test]
pub fn test_with_bad_param() { pub fn test_with_bad_param() {

View File

@@ -17,6 +17,18 @@ fn build_temp_file(dir: &TempDir) -> PathBuf {
file_path file_path
} }
fn link_it(link_path: PathBuf, file_path_s: &str, is_soft: bool) -> String {
let link_name_s = link_path.to_str().unwrap();
let mut c = Command::new("ln");
if is_soft {
c.arg("-s");
}
c.arg(file_path_s);
c.arg(link_name_s);
assert!(c.output().is_ok());
return link_name_s.into();
}
#[cfg_attr(target_os = "windows", ignore)] #[cfg_attr(target_os = "windows", ignore)]
#[test] #[test]
pub fn test_soft_sym_link() { pub fn test_soft_sym_link() {
@@ -26,13 +38,7 @@ pub fn test_soft_sym_link() {
let file_path_s = file.to_str().unwrap(); let file_path_s = file.to_str().unwrap();
let link_name = dir.path().join("the_link"); let link_name = dir.path().join("the_link");
let link_name_s = link_name.to_str().unwrap(); let link_name_s = link_it(link_name, file_path_s, true);
let c = Command::new("ln")
.arg("-s")
.arg(file_path_s)
.arg(link_name_s)
.output();
assert!(c.is_ok());
let c = format!(" ├── {}", link_name_s); let c = format!(" ├── {}", link_name_s);
let b = format!(" ┌── {}", file_path_s); let b = format!(" ┌── {}", file_path_s);
@@ -41,7 +47,7 @@ pub fn test_soft_sym_link() {
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
// Mac test runners create long filenames in tmp directories // Mac test runners create long filenames in tmp directories
let output = cmd let output = cmd
.args(["-p", "-c", "-s", "-w 999", dir_s]) .args(["-p", "-c", "-s", "-w", "999", dir_s])
.unwrap() .unwrap()
.stdout; .stdout;
@@ -61,19 +67,14 @@ pub fn test_hard_sym_link() {
let file_path_s = file.to_str().unwrap(); let file_path_s = file.to_str().unwrap();
let link_name = dir.path().join("the_link"); let link_name = dir.path().join("the_link");
let link_name_s = link_name.to_str().unwrap(); link_it(link_name, file_path_s, false);
let c = Command::new("ln")
.arg(file_path_s)
.arg(link_name_s)
.output();
assert!(c.is_ok());
let file_output = format!(" ┌── {}", file_path_s); let file_output = format!(" ┌── {}", file_path_s);
let dirs_output = format!("─┴ {}", dir_s); let dirs_output = format!("─┴ {}", dir_s);
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
// Mac test runners create long filenames in tmp directories // Mac test runners create long filenames in tmp directories
let output = cmd.args(["-p", "-c", "-w 999", dir_s]).unwrap().stdout; let output = cmd.args(["-p", "-c", "-w", "999", dir_s]).unwrap().stdout;
// The link should not appear in the output because multiple inodes are now ordered // The link should not appear in the output because multiple inodes are now ordered
// then filtered. // then filtered.
@@ -82,6 +83,34 @@ pub fn test_hard_sym_link() {
assert!(output.contains(file_output.as_str())); assert!(output.contains(file_output.as_str()));
} }
#[cfg_attr(target_os = "windows", ignore)]
#[test]
pub fn test_hard_sym_link_no_dup_multi_arg() {
let dir = Builder::new().tempdir().unwrap();
let dir_link = Builder::new().tempdir().unwrap();
let file = build_temp_file(&dir);
let dir_s = dir.path().to_str().unwrap();
let dir_link_s = dir_link.path().to_str().unwrap();
let file_path_s = file.to_str().unwrap();
let link_name = dir_link.path().join("the_link");
let link_name_s = link_it(link_name, file_path_s, false);
let mut cmd = Command::cargo_bin("dust").unwrap();
// Mac test runners create long filenames in tmp directories
let output = cmd
.args(["-p", "-c", "-w", "999", "-b", dir_link_s, dir_s])
.unwrap()
.stdout;
// The link or the file should appear but not both
let output = str::from_utf8(&output).unwrap();
let has_file_only = output.contains(file_path_s) && !output.contains(&link_name_s);
let has_link_only = !output.contains(file_path_s) && output.contains(&link_name_s);
assert!(has_file_only || has_link_only);
}
#[cfg_attr(target_os = "windows", ignore)] #[cfg_attr(target_os = "windows", ignore)]
#[test] #[test]
pub fn test_recursive_sym_link() { pub fn test_recursive_sym_link() {
@@ -89,14 +118,7 @@ pub fn test_recursive_sym_link() {
let dir_s = dir.path().to_str().unwrap(); let dir_s = dir.path().to_str().unwrap();
let link_name = dir.path().join("the_link"); let link_name = dir.path().join("the_link");
let link_name_s = link_name.to_str().unwrap(); let link_name_s = link_it(link_name, dir_s, true);
let c = Command::new("ln")
.arg("-s")
.arg(dir_s)
.arg(link_name_s)
.output();
assert!(c.is_ok());
let a = format!("─┬ {}", dir_s); let a = format!("─┬ {}", dir_s);
let b = format!(" └── {}", link_name_s); let b = format!(" └── {}", link_name_s);
@@ -107,7 +129,8 @@ pub fn test_recursive_sym_link() {
.arg("-c") .arg("-c")
.arg("-r") .arg("-r")
.arg("-s") .arg("-s")
.arg("-w 999") .arg("-w")
.arg("999")
.arg(dir_s) .arg(dir_s)
.unwrap() .unwrap()
.stdout; .stdout;