From b934445e04c418bb30c93d5f552b3e662e716dc1 Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Mon, 23 Dec 2019 15:48:38 +0000 Subject: [PATCH 1/4] Pin jwalk to dependency version https://github.com/jessegrosjean/jwalk/issues/13 Having raised the above issue I don't want it fixed in a minor version as I have added a work around --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 10597f6..c44644c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ path = "src/main.rs" [dependencies] ansi_term = "=0.11" clap = "=2.33" -jwalk = "0.4" +jwalk = "0.4.0" [dev-dependencies] assert_cli = "=0.5" From ef66fb393852ac505488c6b73c46e6671766b4c4 Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Mon, 23 Dec 2019 15:49:26 +0000 Subject: [PATCH 2/4] Remove useless sort The data coming in is already sorted. We should not need to sort it a second time --- src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 90a6030..ce51d69 100644 --- a/src/main.rs +++ b/src/main.rs @@ -133,7 +133,6 @@ fn build_tree(biggest_ones: Vec<(String, u64)>, depth: Option) -> Node { children: Vec::default(), }; recursively_build_tree(&mut top_parent, n, depth); - top_parent.children.sort_unstable() } top_parent } From 311bc4538857dfbd72207ec273a1bacb22504116 Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Mon, 23 Dec 2019 15:50:01 +0000 Subject: [PATCH 3/4] Verify that '/' is parent of everything Bug could occur when run with '/' as it wasn't considered the parent of '/usr' due to having the same number of '/'s in the name --- src/utils/mod.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index cfe84ce..a404c25 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -37,7 +37,7 @@ impl PartialEq for Node { } pub fn is_a_parent_of(parent: &str, child: &str) -> bool { - child.starts_with(parent) && child.chars().nth(parent.chars().count()) == Some('/') + (child.starts_with(parent) && child.chars().nth(parent.chars().count()) == Some('/')) || parent == "/" } pub fn simplify_dir_names(filenames: Vec<&str>) -> HashSet { @@ -232,4 +232,18 @@ mod tests { correct.insert("src_v2".to_string()); assert_eq!(simplify_dir_names(vec!["src/", "src_v2"]), correct); } + + #[test] + fn test_is_a_parent_of() { + assert!(is_a_parent_of("/usr", "/usr/andy")); + assert!(is_a_parent_of("/usr", "/usr/andy/i/am/descendant")); + assert!(!is_a_parent_of("/usr/andy", "/usr")); + assert!(!is_a_parent_of("/usr/andy", "/usr/sibling")); + } + + #[test] + fn test_is_a_parent_of_root() { + assert!(is_a_parent_of("/", "/usr/andy")); + assert!(is_a_parent_of("/", "/usr")); + } } From 356d14ac0fcb65959369eab9fb7f5c1503a2f872 Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Mon, 23 Dec 2019 15:51:12 +0000 Subject: [PATCH 4/4] Hack fix around JWalk behaviour. Jwalk returns '/' as a child of the current node which breaks things. https://github.com/jessegrosjean/jwalk/issues/13 --- src/utils/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index a404c25..0d337ea 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -125,6 +125,11 @@ fn examine_dir( } // This path and all its parent paths have their counter incremented for path_name in e.path().ancestors() { + // This is required due to bug in Jwalk that adds '/' to all sub dir lists + // see: https://github.com/jessegrosjean/jwalk/issues/13 + if path_name.to_string_lossy() == "/" && top_dir != "/" { + continue + } let path_name = path_name.to_string_lossy(); let s = data.entry(path_name.to_string()).or_insert(0); *s += size;