Skip to content

A treemap for finding and removing what fills your disk, for Omarchy. Rust + GPUI.

Rust

core: kinds, findings, and scanning a whole disk

Tobi Lutke5b7361b015d9
diff --git a/crates/disktree-core/src/classify.rs b/crates/disktree-core/src/classify.rs
new file mode 100644
index 0000000..2fb4fe6
--- /dev/null
+++ b/crates/disktree-core/src/classify.rs
@@ -0,0 +1,437 @@
+//! What a directory *is*, and whether its space can be had back.
+//!
+//! Colour in the treemap means a kind of data, and a hatch means reclaimable
+//! space, so the two questions a cleanup tool exists to answer — "what is it"
+//! and "can I delete it" — can be read off a tile at a glance.
+//!
+//! Both come from names. A short lookup of well-known directory names covers
+//! most of a home directory; anything unmatched takes its parent's kind, and a
+//! top-level directory with an unknown name takes the kind of its largest
+//! recognisable child (`~/world` is mostly `.git`, so it is git). Reclaimable
+//! space is the same idea, plus a sibling check where a name alone is too
+//! common to trust: `target` is only a build directory beside a `Cargo.toml`.
+
+use crate::tree::Node;
+
+/// A kind of data, for colour.
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
+pub enum Category {
+ /// Source code and checkouts.
+ Code,
+ /// Space agents write into: worktrees, sandboxes, experiments.
+ AgentScratch,
+ /// Compilers, package managers and their installs.
+ Toolchain,
+ /// Folders a sync client owns.
+ Synced,
+ /// Version-control object stores.
+ Git,
+ /// Pictures, music, video, games and models.
+ Media,
+ /// Documents, downloads and the desktop.
+ Documents,
+ /// Caches and other regenerable state.
+ Cache,
+ /// Nothing recognisable.
+ #[default]
+ Other,
+}
+
+impl Category {
+ /// The categories the legend lists, in its order.
+ pub const LEGEND: [Self; 8] = [
+ Self::Code,
+ Self::AgentScratch,
+ Self::Toolchain,
+ Self::Synced,
+ Self::Git,
+ Self::Media,
+ Self::Documents,
+ Self::Cache,
+ ];
+
+ pub const fn label(self) -> &'static str {
+ match self {
+ Self::Code => "Code",
+ Self::AgentScratch => "Agent scratch",
+ Self::Toolchain => "Toolchains",
+ Self::Synced => "Synced",
+ Self::Git => "Git",
+ Self::Media => "Media",
+ Self::Documents => "Documents",
+ Self::Cache => "Cache",
+ Self::Other => "Other",
+ }
+ }
+}
+
+/// Why a directory's space can be had back.
+#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
+pub enum Reclaim {
+ /// A cache: whatever wrote it will write it again.
+ Regenerable,
+ /// A sync client's old versions of files.
+ SyncHistory,
+ /// A package manager's content store.
+ PackageStore,
+ /// Compiler or bundler output beside its sources.
+ BuildOutput,
+ /// Installed dependencies beside their manifest.
+ Reinstallable,
+ /// Container or sandbox image layers.
+ SandboxLayers,
+ /// Sandbox or VM snapshots.
+ Snapshots,
+ /// Already deleted, still on disk.
+ Trash,
+ /// Scratch space meant to be thrown away.
+ Temporary,
+}
+
+impl Reclaim {
+ /// The reason, as the "Worth a look" list says it.
+ pub const fn label(self) -> &'static str {
+ match self {
+ Self::Regenerable => "regenerable",
+ Self::SyncHistory => "sync history",
+ Self::PackageStore => "package store",
+ Self::BuildOutput => "build output",
+ Self::Reinstallable => "reinstallable",
+ Self::SandboxLayers => "sandbox layers",
+ Self::Snapshots => "snapshots",
+ Self::Trash => "trash",
+ Self::Temporary => "temporary",
+ }
+ }
+}
+
+/// The kind a directory name announces on its own, if any.
+pub fn category_of_name(name: &str) -> Option<Category> {
+ let lower = name.to_ascii_lowercase();
+ let category = match lower.as_str() {
+ "src" | "code" | "projects" | "repos" | "dev" | "work"
+ | "workspace" | "workspaces" | "github.com" | "gitlab.com"
+ | "sites" | "development" => Category::Code,
+ ".codex" | ".claude" | ".herdr" | ".pi" | ".cursor" | ".aider"
+ | ".gemini" | ".continue" | ".windsurf" | ".microsandbox" | ".omp"
+ | ".agents" | ".openai" | "tries" | "worktrees" | "experiments"
+ | "scratch" | "playground" => Category::AgentScratch,
+ ".cargo" | ".rustup" | ".local" | ".npm" | ".pnpm-store" | "pnpm"
+ | ".bun" | ".deno" | "go" | ".gradle" | ".m2" | ".platformio"
+ | "mise" | ".mise" | ".pyenv" | ".nvm" | ".gem" | "gem" | ".rbenv"
+ | ".espressif" | ".arduino15" | ".config" | ".vscode" | ".zig"
+ | ".rye" | ".conda" | "anaconda3" | "miniconda3" | ".opam"
+ | ".ghcup" | ".stack" | ".julia" | ".dotnet" | ".android"
+ | ".sdkman" | ".volta" | ".yarn" | ".java" => Category::Toolchain,
+ "sync" | "dropbox" | "nextcloud" | "google drive" | "onedrive"
+ | "pclouddrive" | "mega" | ".stversions" => Category::Synced,
+ ".git" => Category::Git,
+ "pictures" | "photos" | "music" | "videos" | "movies" | "steam"
+ | "models" | ".ollama" | ".lmstudio" | "games" | "wineprefix" => {
+ Category::Media
+ }
+ "documents" | "desktop" | "downloads" | "books" | "notes"
+ | "obsidian" | "public" | "templates" => Category::Documents,
+ ".cache" | "cache" | "caches" | ".ccache" | ".sccache" | "_cacache"
+ | "__pycache__" | "node_modules" | "trash" | ".trash" | "tmp"
+ | ".tmp" => Category::Cache,
+ _ => return None,
+ };
+ Some(category)
+}
+
+/// Whether a directory's space can be had back, judged from its name, the
+/// kind of the directory holding it, and its siblings' names.
+pub fn reclaim_of(
+ name: &str,
+ parent: Category,
+ has_sibling: impl Fn(&str) -> bool,
+) -> Option<Reclaim> {
+ let lower = name.to_ascii_lowercase();
+ let reclaim = match lower.as_str() {
+ ".cache" | "cache" | "caches" | ".ccache" | ".sccache" | "_cacache" => {
+ Reclaim::Regenerable
+ }
+ ".stversions" => Reclaim::SyncHistory,
+ ".pnpm-store" | "pnpm" => Reclaim::PackageStore,
+ "__pycache__" | ".pytest_cache" | ".mypy_cache" | ".ruff_cache"
+ | ".next" | ".turbo" | ".parcel-cache" => Reclaim::BuildOutput,
+ // Too common to trust alone: only a build directory beside a manifest.
+ "target" if has_sibling("Cargo.toml") => Reclaim::BuildOutput,
+ "node_modules" if has_sibling("package.json") => Reclaim::Reinstallable,
+ // Layers and snapshots are only disposable inside sandbox state.
+ "layers" if parent == Category::AgentScratch => Reclaim::SandboxLayers,
+ "snapshots" if parent == Category::AgentScratch => Reclaim::Snapshots,
+ "trash" | ".trash" => Reclaim::Trash,
+ "tmp" | ".tmp" => Reclaim::Temporary,
+ _ => return None,
+ };
+ Some(reclaim)
+}
+
+/// Assign a category and a reclaim reason to every node beneath `root`.
+///
+/// Top-down: a node's own name wins, otherwise it inherits. Reclaimable
+/// space is inherited too, so everything under a cache is hatched.
+pub fn classify(root: &mut Node) {
+ root.category = Category::Other;
+ root.reclaim = None;
+ let children = std::mem::take(&mut root.children);
+ let names: Vec<Box<str>> =
+ children.iter().map(|child| child.name.clone()).collect();
+ root.children = children;
+ for index in 0..root.children.len() {
+ let has_sibling =
+ |wanted: &str| names.iter().any(|name| &**name == wanted);
+ let child = &mut root.children[index];
+ // A top-level directory with an unknown name takes the kind of its
+ // largest recognisable child: `~/world` is mostly `.git`.
+ let category = category_of_name(&child.name)
+ .or_else(|| is_git_store(child).then_some(Category::Git))
+ .or_else(|| dominant_child_category(child))
+ .unwrap_or(Category::Other);
+ let reclaim = child
+ .is_dir()
+ .then(|| reclaim_of(&child.name, Category::Other, has_sibling))
+ .flatten();
+ classify_below(child, category, reclaim);
+ }
+}
+
+fn classify_below(
+ node: &mut Node,
+ category: Category,
+ reclaim: Option<Reclaim>,
+) {
+ node.category = category;
+ node.reclaim = reclaim;
+ if node.children.is_empty() {
+ return;
+ }
+ let names: Vec<Box<str>> = node
+ .children
+ .iter()
+ .map(|child| child.name.clone())
+ .collect();
+ for child in &mut node.children {
+ let has_sibling =
+ |wanted: &str| names.iter().any(|name| &**name == wanted);
+ let child_category = if child.is_dir() {
+ category_of_name(&child.name)
+ .or_else(|| is_git_store(child).then_some(Category::Git))
+ .unwrap_or(category)
+ } else {
+ category
+ };
+ let child_reclaim = reclaim.or_else(|| {
+ child
+ .is_dir()
+ .then(|| reclaim_of(&child.name, category, has_sibling))
+ .flatten()
+ });
+ classify_below(child, child_category, child_reclaim);
+ }
+}
+
+/// The kind of an unknown directory, from what fills it: the first
+/// recognisable name down the largest children, a few levels deep.
+fn dominant_child_category(node: &Node) -> Option<Category> {
+ let mut node = node;
+ for _ in 0..3 {
+ if let Some(category) = node
+ .children
+ .iter()
+ .filter(|child| child.is_dir())
+ .find_map(|child| {
+ category_of_name(&child.name)
+ .or_else(|| is_git_store(child).then_some(Category::Git))
+ })
+ {
+ return Some(category);
+ }
+ node = node.children.iter().find(|child| child.is_dir())?;
+ }
+ None
+}
+
+/// A git object store by its shape, whatever it is called: a bare
+/// repository, or a `.git` directory, has `objects`, `refs` and `HEAD`.
+pub fn is_git_store(node: &Node) -> bool {
+ let has =
+ |wanted: &str| node.children.iter().any(|child| &*child.name == wanted);
+ node.is_dir() && has("objects") && has("refs") && has("HEAD")
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::tree::{Metric, NodeKind, aggregate};
+
+ fn file(name: &str, bytes: u64) -> Node {
+ Node::entry(name, NodeKind::File, bytes)
+ }
+
+ fn dir(name: &str, children: Vec<Node>) -> Node {
+ let mut node = Node::directory(name);
+ node.children = children;
+ node
+ }
+
+ fn home() -> Node {
+ let mut home = dir(
+ "tobi",
+ vec![
+ dir(
+ "src",
+ vec![dir(
+ "tries",
+ vec![dir("2026-09-01", vec![file("a", 1)])],
+ )],
+ ),
+ dir(
+ ".cache",
+ vec![dir("kache", vec![dir("store", vec![file("b", 1)])])],
+ ),
+ dir(
+ "world",
+ vec![
+ dir(".git", vec![dir("objects", vec![file("c", 9)])]),
+ file("README", 1),
+ ],
+ ),
+ dir(
+ "rust-thing",
+ vec![
+ file("Cargo.toml", 1),
+ dir("target", vec![file("d", 5)]),
+ ],
+ ),
+ dir("js-thing", vec![dir("target", vec![file("e", 5)])]),
+ dir(
+ ".microsandbox",
+ vec![
+ dir("cache", vec![dir("layers", vec![file("f", 1)])]),
+ dir("snapshots", vec![file("g", 1)]),
+ ],
+ ),
+ dir("Sync", vec![dir(".stversions", vec![file("h", 1)])]),
+ dir("mystery", vec![file("i", 1)]),
+ dir(
+ "monorepo",
+ vec![dir(
+ "git",
+ vec![
+ file("HEAD", 1),
+ dir("refs", vec![]),
+ dir("objects", vec![file("pack", 50)]),
+ ],
+ )],
+ ),
+ ],
+ );
+ aggregate(&mut home, Metric::Bytes);
+ classify(&mut home);
+ home
+ }
+
+ fn named<'a>(node: &'a Node, path: &[&str]) -> &'a Node {
+ let mut node = node;
+ for part in path {
+ node = node
+ .children
+ .iter()
+ .find(|child| &*child.name == *part)
+ .unwrap_or_else(|| panic!("no {part}"));
+ }
+ node
+ }
+
+ #[test]
+ fn names_announce_their_kind_and_children_inherit_it() {
+ let home = home();
+ assert_eq!(named(&home, &["src"]).category, Category::Code);
+ // `tries` is agent scratch even inside code: it is what agents write.
+ assert_eq!(
+ named(&home, &["src", "tries"]).category,
+ Category::AgentScratch
+ );
+ assert_eq!(
+ named(&home, &["src", "tries", "2026-09-01"]).category,
+ Category::AgentScratch,
+ "an unknown name inherits"
+ );
+ assert_eq!(
+ named(&home, &["src", "tries", "2026-09-01", "a"]).category,
+ Category::AgentScratch,
+ "files inherit too"
+ );
+ }
+
+ #[test]
+ fn an_unknown_top_level_directory_takes_its_largest_known_child() {
+ let home = home();
+ assert_eq!(named(&home, &["world"]).category, Category::Git);
+ assert_eq!(named(&home, &["mystery"]).category, Category::Other);
+ // A bare repository is git by its shape, whatever it is called.
+ assert_eq!(named(&home, &["monorepo"]).category, Category::Git);
+ assert_eq!(named(&home, &["monorepo", "git"]).category, Category::Git);
+ }
+
+ #[test]
+ fn caches_are_reclaimable_all_the_way_down() {
+ let home = home();
+ assert_eq!(
+ named(&home, &[".cache"]).reclaim,
+ Some(Reclaim::Regenerable)
+ );
+ assert_eq!(
+ named(&home, &[".cache", "kache", "store"]).reclaim,
+ Some(Reclaim::Regenerable)
+ );
+ assert_eq!(
+ named(&home, &["Sync", ".stversions"]).reclaim,
+ Some(Reclaim::SyncHistory)
+ );
+ assert_eq!(named(&home, &["src"]).reclaim, None);
+ }
+
+ #[test]
+ fn target_is_build_output_only_beside_a_cargo_manifest() {
+ let home = home();
+ assert_eq!(
+ named(&home, &["rust-thing", "target"]).reclaim,
+ Some(Reclaim::BuildOutput)
+ );
+ assert_eq!(named(&home, &["js-thing", "target"]).reclaim, None);
+ }
+
+ #[test]
+ fn sandbox_layers_and_snapshots_are_reclaimable_only_in_sandbox_state() {
+ let home = home();
+ // .microsandbox/cache is already a regenerable cache, so its layers
+ // inherit that; the snapshots are judged on their own.
+ assert!(
+ named(&home, &[".microsandbox", "cache", "layers"])
+ .reclaim
+ .is_some()
+ );
+ assert_eq!(
+ named(&home, &[".microsandbox", "snapshots"]).reclaim,
+ Some(Reclaim::Snapshots)
+ );
+ assert_eq!(
+ reclaim_of("snapshots", Category::Documents, |_| false),
+ None
+ );
+ }
+
+ #[test]
+ fn the_legend_lists_every_named_category_once() {
+ let mut seen = std::collections::HashSet::new();
+ for category in Category::LEGEND {
+ assert!(seen.insert(category));
+ assert_ne!(category, Category::Other);
+ assert!(!category.label().is_empty());
+ }
+ }
+}
diff --git a/crates/disktree-core/src/insights.rs b/crates/disktree-core/src/insights.rs
new file mode 100644
index 0000000..c038800
--- /dev/null
+++ b/crates/disktree-core/src/insights.rs
@@ -0,0 +1,242 @@
+//! "Worth a look": the biggest things in a scan that could plausibly go.
+//!
+//! Three kinds of finding, each one a directory a person can judge in a
+//! second: reclaimable space ([`crate::classify::Reclaim`]), agent worktrees,
+//! and experiments nobody has written to in a month. Findings never nest, so
+//! their total is space that really exists once.
+
+use crate::classify::{Category, Reclaim};
+use crate::tree::Node;
+
+/// Seconds in a day.
+const DAY: i64 = 86_400;
+
+/// An experiment untouched this long is worth a look.
+pub const STALE_DAYS: i64 = 30;
+
+/// Smaller than this is not worth a line.
+const MIN_BYTES: u64 = 64 * 1024 * 1024;
+
+/// Why a directory is on the list.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub enum Finding {
+ /// Its space can be had back, for this reason.
+ Reclaimable(Reclaim),
+ /// A directory of agent worktrees: how many, and the oldest one's age.
+ Worktrees { count: usize, oldest_days: i64 },
+ /// Experiments untouched for [`STALE_DAYS`]: only those are counted.
+ StaleExperiments { count: usize },
+}
+
+/// One line of the list.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct Candidate {
+ /// Where it is, from the scanned root.
+ pub crumbs: Vec<usize>,
+ /// What clearing it frees. For stale experiments, only the stale ones.
+ pub bytes: u64,
+ pub finding: Finding,
+}
+
+/// The `limit` largest findings beneath `root`, largest first. `now` is Unix
+/// seconds, passed in so the answer is testable.
+pub fn worth_a_look(root: &Node, now: i64, limit: usize) -> Vec<Candidate> {
+ let mut found = Vec::new();
+ let mut crumbs = Vec::new();
+ for (index, child) in root.children.iter().enumerate() {
+ crumbs.push(index);
+ visit(child, &mut crumbs, now, &mut found);
+ crumbs.pop();
+ }
+ found.retain(|candidate| candidate.bytes >= MIN_BYTES);
+ found.sort_by_key(|candidate| std::cmp::Reverse(candidate.bytes));
+ found.truncate(limit);
+ found
+}
+
+fn visit(
+ node: &Node,
+ crumbs: &mut Vec<usize>,
+ now: i64,
+ found: &mut Vec<Candidate>,
+) {
+ if !node.is_dir() {
+ return;
+ }
+ // Topmost only: everything beneath a reclaimable directory goes with it.
+ if let Some(reason) = node.reclaim {
+ found.push(Candidate {
+ crumbs: crumbs.clone(),
+ bytes: node.bytes,
+ finding: Finding::Reclaimable(reason),
+ });
+ return;
+ }
+ let name = node.name.to_ascii_lowercase();
+ let scratch = node.category == Category::AgentScratch;
+ if scratch && name == "worktrees" {
+ let trees: Vec<&Node> = node
+ .children
+ .iter()
+ .filter(|child| child.is_dir())
+ .collect();
+ if !trees.is_empty() {
+ let oldest = trees
+ .iter()
+ .map(|tree| tree.modified)
+ .filter(|&time| time > 0)
+ .min()
+ .unwrap_or(now);
+ found.push(Candidate {
+ crumbs: crumbs.clone(),
+ bytes: node.bytes,
+ finding: Finding::Worktrees {
+ count: trees.len(),
+ oldest_days: (now - oldest).max(0) / DAY,
+ },
+ });
+ return;
+ }
+ }
+ let experiments =
+ scratch && matches!(name.as_str(), "tries" | "experiments");
+ let mut stale = (0_usize, 0_u64);
+ for (index, child) in node.children.iter().enumerate() {
+ let is_stale = experiments
+ && child.is_dir()
+ && child.modified > 0
+ && now - child.modified > STALE_DAYS * DAY;
+ if is_stale {
+ stale.0 += 1;
+ stale.1 += child.bytes;
+ // A stale experiment is judged whole; its caches go with it.
+ continue;
+ }
+ crumbs.push(index);
+ visit(child, crumbs, now, found);
+ crumbs.pop();
+ }
+ if stale.0 > 0 {
+ found.push(Candidate {
+ crumbs: crumbs.clone(),
+ bytes: stale.1,
+ finding: Finding::StaleExperiments { count: stale.0 },
+ });
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::classify::classify;
+ use crate::tree::{Metric, NodeKind, aggregate};
+
+ const GIB: u64 = 1024 * 1024 * 1024;
+ const NOW: i64 = 1_800_000_000;
+
+ fn file(name: &str, bytes: u64, days_old: i64) -> Node {
+ let mut node = Node::entry(name, NodeKind::File, bytes);
+ node.modified = NOW - days_old * DAY;
+ node
+ }
+
+ fn dir(name: &str, children: Vec<Node>) -> Node {
+ let mut node = Node::directory(name);
+ node.children = children;
+ node
+ }
+
+ fn scan(mut root: Node) -> Node {
+ aggregate(&mut root, Metric::Bytes);
+ classify(&mut root);
+ root
+ }
+
+ fn home() -> Node {
+ scan(dir(
+ "tobi",
+ vec![
+ dir(
+ ".cache",
+ vec![dir("kache", vec![file("blob", 5 * GIB, 1)])],
+ ),
+ dir(
+ ".codex",
+ vec![dir(
+ "worktrees",
+ vec![
+ dir("a1", vec![file("x", 3 * GIB, 41)]),
+ dir("b2", vec![file("y", 2 * GIB, 3)]),
+ ],
+ )],
+ ),
+ dir(
+ "src",
+ vec![dir(
+ "tries",
+ vec![
+ dir("old", vec![file("z", 4 * GIB, 90)]),
+ dir(
+ "fresh",
+ vec![
+ file("Cargo.toml", 1, 1),
+ dir("target", vec![file("o", GIB, 1)]),
+ ],
+ ),
+ ],
+ )],
+ ),
+ dir("Documents", vec![file("tax.pdf", 9 * GIB, 400)]),
+ dir("tiny", vec![dir(".cache", vec![file("t", 1024, 1)])]),
+ ],
+ ))
+ }
+
+ #[test]
+ fn ranks_findings_largest_first_and_skips_the_tiny() {
+ let found = worth_a_look(&home(), NOW, 10);
+ let kinds: Vec<&Finding> = found.iter().map(|c| &c.finding).collect();
+ assert_eq!(
+ kinds,
+ vec![
+ &Finding::Reclaimable(Reclaim::Regenerable),
+ &Finding::Worktrees {
+ count: 2,
+ oldest_days: 41
+ },
+ &Finding::StaleExperiments { count: 1 },
+ &Finding::Reclaimable(Reclaim::BuildOutput),
+ ]
+ );
+ assert_eq!(found[0].bytes, 5 * GIB);
+ assert_eq!(found[2].bytes, 4 * GIB, "only the stale experiment counts");
+ }
+
+ #[test]
+ fn documents_are_never_suggested() {
+ let found = worth_a_look(&home(), NOW, 10);
+ assert!(found.iter().all(|c| c.crumbs != vec![3]));
+ }
+
+ #[test]
+ fn crumbs_address_the_finding_from_the_root() {
+ let root = home();
+ for candidate in worth_a_look(&root, NOW, 10) {
+ let node = root.resolve(&candidate.crumbs).expect("resolves");
+ assert!(node.is_dir());
+ }
+ }
+
+ #[test]
+ fn the_limit_keeps_the_largest() {
+ let found = worth_a_look(&home(), NOW, 2);
+ assert_eq!(found.len(), 2);
+ assert_eq!(
+ found[1].finding,
+ Finding::Worktrees {
+ count: 2,
+ oldest_days: 41
+ }
+ );
+ }
+}
diff --git a/crates/disktree-core/src/lib.rs b/crates/disktree-core/src/lib.rs
index 4b6354d..27a8b55 100644
--- a/crates/disktree-core/src/lib.rs
+++ b/crates/disktree-core/src/lib.rs
@@ -5,6 +5,8 @@
//! hardlink de-duplication, aspect-ratio layout, and what may be deleted — can
//! be built and tested without GPUI, a display, or a GPU.
+pub mod classify;
+pub mod insights;
pub mod removal;
pub mod scan;
pub mod size;
diff --git a/crates/disktree-core/src/removal.rs b/crates/disktree-core/src/removal.rs
index 5e4ffd0..c15ad04 100644
--- a/crates/disktree-core/src/removal.rs
+++ b/crates/disktree-core/src/removal.rs
@@ -130,6 +130,40 @@ pub fn plan(targets: &[Target], root: &Path) -> Plan {
}
/// Why this path must not be removed, if it must not.
+/// Trees the operating system owns. A whole-disk scan shows them, because
+/// they are part of what fills the disk, but files there belong to packages
+/// and removing them by hand breaks the system; pacman, paccache and
+/// `journalctl --vacuum` are the right tools. Refused even where
+/// permissions would allow it, and even inside them.
+const SYSTEM_TREES: [&str; 14] = [
+ "/bin",
+ "/boot",
+ "/dev",
+ "/etc",
+ "/lib",
+ "/lib64",
+ "/nix/store",
+ "/proc",
+ "/run",
+ "/sbin",
+ "/sys",
+ "/usr",
+ "/var/lib",
+ "/efi",
+];
+
+/// The system tree `path` is in, if any. The home directory is never
+/// system, wherever it lives.
+fn system_tree(path: &Path, home: Option<&Path>) -> Option<&'static str> {
+ if home.is_some_and(|home| path.starts_with(normalize(home))) {
+ return None;
+ }
+ SYSTEM_TREES
+ .iter()
+ .find(|tree| path.starts_with(tree))
+ .copied()
+}
+
fn refuse(path: &Path, root: &Path, home: Option<&Path>) -> Option<String> {
if path.parent().is_none() {
return Some("the filesystem root cannot be removed".into());
@@ -143,6 +177,11 @@ fn refuse(path: &Path, root: &Path, home: Option<&Path>) -> Option<String> {
if !path.starts_with(root) {
return Some("outside the scanned root".into());
}
+ if let Some(system) = system_tree(path, home) {
+ return Some(format!(
+ "part of the system under {system}: use the package manager"
+ ));
+ }
if is_mount_point(path) {
return Some(
"a mount point: removing it would cross onto another filesystem"
@@ -841,4 +880,35 @@ mod tests {
}
assert!(backend.is_available());
}
+
+ #[test]
+ fn system_trees_are_refused_in_a_whole_disk_scan() {
+ let home = Path::new("/home/tobi");
+ assert_eq!(
+ system_tree(Path::new("/usr/lib/libfoo.so"), Some(home)),
+ Some("/usr")
+ );
+ assert_eq!(
+ system_tree(Path::new("/var/lib/pacman"), Some(home)),
+ Some("/var/lib")
+ );
+ assert_eq!(
+ system_tree(Path::new("/var/cache/pacman/pkg"), Some(home)),
+ None
+ );
+ assert_eq!(system_tree(Path::new("/opt/thing"), Some(home)), None);
+ assert_eq!(
+ system_tree(Path::new("/usrlocal"), Some(home)),
+ None,
+ "components, not prefixes"
+ );
+ let odd_home = Path::new("/usr/home/tobi");
+ assert_eq!(
+ system_tree(Path::new("/usr/home/tobi/.cache"), Some(odd_home)),
+ None
+ );
+ let reason =
+ refuse(Path::new("/etc/hosts"), Path::new("/"), Some(home));
+ assert!(reason.is_some_and(|reason| reason.contains("/etc")));
+ }
}
diff --git a/crates/disktree-core/src/scan.rs b/crates/disktree-core/src/scan.rs
index b75d9d3..8cdec56 100644
--- a/crates/disktree-core/src/scan.rs
+++ b/crates/disktree-core/src/scan.rs
@@ -20,7 +20,7 @@ use std::io;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
use std::sync::mpsc::{self, Receiver, TryRecvError};
-use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
+use std::sync::{Arc, Mutex, MutexGuard, OnceLock, PoisonError};
use std::thread;
use rayon::Scope;
@@ -44,7 +44,11 @@ pub struct ScanOptions {
/// Include dotfiles and dot-directories. On by default, like dust and `du`:
/// `~/.cache` is frequently the largest directory in a home directory.
pub include_hidden: bool,
- /// Do not cross filesystem boundaries below the root.
+ /// Stay on the root's volume: skip other disks, pseudo filesystems,
+ /// network shares and automount points, but keep subvolumes of the same
+ /// disk. See [`crate::space::foreign_mounts`]. On by default: a disk
+ /// tool measures a disk, and its free-space meter only means anything
+ /// for one volume.
pub one_filesystem: bool,
/// Stop descending past this depth. Totals below that depth are then
/// unknown, which makes an overview scan of a huge tree cheap.
@@ -61,7 +65,7 @@ impl Default for ScanOptions {
apparent_size: false,
follow_links: false,
include_hidden: true,
- one_filesystem: false,
+ one_filesystem: true,
max_depth: None,
dedup_hardlinks: true,
metric: Metric::Bytes,
@@ -160,6 +164,7 @@ impl ScanHandle {
options,
progress: Arc::clone(&progress),
root_device: Mutex::new(None),
+ foreign_mounts: OnceLock::new(),
visited_dirs: Mutex::new(FxHashSet::default()),
root: Mutex::new(None),
});
@@ -206,6 +211,7 @@ pub fn scan(root: &Path, options: ScanOptions) -> io::Result<Node> {
options,
progress: Arc::clone(&progress),
root_device: Mutex::new(None),
+ foreign_mounts: OnceLock::new(),
visited_dirs: Mutex::new(FxHashSet::default()),
root: Mutex::new(None),
});
@@ -217,8 +223,12 @@ pub fn scan(root: &Path, options: ScanOptions) -> io::Result<Node> {
struct WalkContext {
options: ScanOptions,
progress: Arc<ScanProgress>,
- /// Device of the root, resolved once, only needed for `one_filesystem`.
+ /// Device of the root, resolved once: the `one_filesystem` fallback
+ /// when the mount table cannot be read.
root_device: Mutex<Option<u64>>,
+ /// Mount points `one_filesystem` keeps out, from the mount table. Unset
+ /// when the table cannot be read, and devices are compared instead.
+ foreign_mounts: OnceLock<FxHashSet<PathBuf>>,
/// Directories already entered, so followed symlinks cannot loop.
visited_dirs: Mutex<FxHashSet<(u64, u64)>>,
/// Set by the root's `complete`, read after the scope joins.
@@ -264,7 +274,15 @@ impl WalkContext {
}
if file_type.is_dir() {
- if self.options.one_filesystem {
+ if self.options.one_filesystem
+ && let Some(foreign) = self.foreign_mounts.get()
+ {
+ // Checked by path before anything reads the directory, so an
+ // automount point is never triggered.
+ if foreign.contains(&path) {
+ return Classified::Skipped;
+ }
+ } else if self.options.one_filesystem {
let device = entry.metadata().map(|meta| device_of(&meta));
match (device, self.root_device(&path)) {
(Ok(device), Some(root_device))
@@ -407,6 +425,9 @@ impl PendingDir {
dirs: 1,
inode: None,
read_error: self.read_error.load(Ordering::Relaxed),
+ modified: 0,
+ category: crate::classify::Category::Other,
+ reclaim: None,
children: std::mem::take(&mut *lock(&self.children)),
}
}
@@ -422,6 +443,10 @@ fn scan_blocking(root: &Path, context: &Arc<WalkContext>) -> io::Result<Node> {
}
if context.options.one_filesystem {
*lock(&context.root_device) = Some(device_of(&root_meta));
+ let root = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
+ if let Some(foreign) = crate::space::foreign_mounts_for(&root) {
+ let _ = context.foreign_mounts.set(foreign.into_iter().collect());
+ }
}
if context.options.follow_links
&& let Some(key) = file_identity(&root_meta)
@@ -529,6 +554,7 @@ fn finish_tree(mut node: Node, options: &ScanOptions) -> Node {
mark_duplicate_hardlinks(&mut node, &mut seen);
}
aggregate(&mut node, options.metric);
+ crate::classify::classify(&mut node);
node
}
@@ -552,9 +578,21 @@ fn leaf_node(
) -> Node {
let mut node = Node::entry(name, kind, size);
node.inode = file_identity(meta);
+ node.modified = modified_seconds(meta);
node
}
+/// Last write time in Unix seconds, from the metadata the walk already has:
+/// age costs no extra system call.
+fn modified_seconds(meta: &Metadata) -> i64 {
+ meta.modified()
+ .ok()
+ .and_then(|time| time.duration_since(std::time::UNIX_EPOCH).ok())
+ .map_or(0, |since| {
+ i64::try_from(since.as_secs()).unwrap_or(i64::MAX)
+ })
+}
+
fn measure(meta: &Metadata, apparent_size: bool) -> u64 {
if apparent_size {
meta.len()
@@ -871,6 +909,7 @@ mod tests {
options: options(),
progress: Arc::clone(&progress),
root_device: Mutex::new(None),
+ foreign_mounts: OnceLock::new(),
visited_dirs: Mutex::new(FxHashSet::default()),
root: Mutex::new(None),
});
@@ -918,4 +957,32 @@ mod tests {
assert_eq!(snapshot.files, 2);
assert_eq!(snapshot.errors, 0);
}
+
+ /// A real whole-disk scan, run by hand: `cargo test -p disktree-core
+ /// -- --ignored --nocapture whole_disk`. Prints what it found, so the
+ /// volume rules can be checked against this machine's mounts.
+ #[test]
+ #[ignore = "walks the whole disk"]
+ fn whole_disk_smoke() {
+ let home = std::env::var_os("HOME").map(PathBuf::from).expect("HOME");
+ let root = crate::space::volume_root_for(&home).expect("a volume root");
+ let started = std::time::Instant::now();
+ let tree = scan(&root, ScanOptions::default()).expect("scan");
+ println!("root {} in {:.1?}", root.display(), started.elapsed());
+ println!(
+ "total {} files {}",
+ crate::size::human_bytes(tree.bytes),
+ tree.files
+ );
+ for child in tree.children.iter().take(14) {
+ println!(
+ " {:>10} {}",
+ crate::size::human_bytes(child.bytes),
+ child.name
+ );
+ }
+ let skipped =
+ crate::space::foreign_mounts_for(&root).unwrap_or_default();
+ println!("left out: {skipped:?}");
+ }
}
diff --git a/crates/disktree-core/src/space.rs b/crates/disktree-core/src/space.rs
index 62ecb13..008eec3 100644
--- a/crates/disktree-core/src/space.rs
+++ b/crates/disktree-core/src/space.rs
@@ -5,7 +5,7 @@
//! the projection while marking.
use std::io;
-use std::path::Path;
+use std::path::{Path, PathBuf};
/// A volume's capacity in bytes.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
@@ -66,10 +66,211 @@ pub fn space_info(path: &Path) -> io::Result<SpaceInfo> {
})
}
+/// The device a path's filesystem is mounted from, such as
+/// `/dev/nvme0n1p2`: the mount with the longest prefix of `path` in
+/// `/proc/self/mounts`. `None` where that table cannot be read.
+pub fn device_for(path: &Path) -> Option<String> {
+ let table = std::fs::read_to_string("/proc/self/mounts").ok()?;
+ let path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
+ device_in(&table, &path)
+}
+
+/// [`device_for`] over a given mount table, for testing.
+pub fn device_in(table: &str, path: &Path) -> Option<String> {
+ table
+ .lines()
+ .filter_map(|line| {
+ let mut fields = line.split_whitespace();
+ let device = fields.next()?;
+ // Spaces in mount points are escaped as \040.
+ let mount = fields.next()?.replace("\\040", " ");
+ path.starts_with(&mount)
+ .then(|| (mount.len(), device.to_string()))
+ })
+ .max_by_key(|(length, _)| *length)
+ .map(|(_, device)| device)
+}
+
+/// One line of the mount table.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct Mount {
+ /// What is mounted: a device, or a name like `tmpfs` or `systemd-1`.
+ pub source: String,
+ pub point: PathBuf,
+ pub fstype: String,
+ pub options: String,
+}
+
+/// Parse `/proc/self/mounts`.
+pub fn parse_mounts(table: &str) -> Vec<Mount> {
+ table
+ .lines()
+ .filter_map(|line| {
+ let mut fields = line.split_whitespace();
+ let source = fields.next()?.to_string();
+ // Spaces in mount points are escaped as \040.
+ let point = PathBuf::from(fields.next()?.replace("\\040", " "));
+ let fstype = fields.next()?.to_string();
+ let options = fields.next().unwrap_or_default().to_string();
+ Some(Mount {
+ source,
+ point,
+ fstype,
+ options,
+ })
+ })
+ .collect()
+}
+
+/// Mount points below `root` that are not part of `root`'s volume, which a
+/// scan of that volume must not enter.
+///
+/// "The volume" is the mount *source*, not the device number: btrfs gives
+/// every subvolume its own `st_dev`, so `/home` on Omarchy looks like a
+/// different filesystem from `/` though it is the same disk. Everything
+/// else is left out: pseudo filesystems (`/proc`, `/sys`), tmpfs, other
+/// disks, network shares, and automount points — entering one of those
+/// would mount a NAS just to measure it. Snapshot subvolumes are left out
+/// too, because every file in them shares its blocks with the live one and
+/// counting them would count the disk twice.
+pub fn foreign_mounts(mounts: &[Mount], root: &Path) -> Vec<PathBuf> {
+ let Some(own) = mounts
+ .iter()
+ .filter(|mount| root.starts_with(&mount.point))
+ .max_by_key(|mount| mount.point.as_os_str().len())
+ else {
+ return Vec::new();
+ };
+ mounts
+ .iter()
+ .filter(|mount| mount.point != root && mount.point.starts_with(root))
+ .filter(|mount| {
+ mount.source != own.source
+ || mount.fstype != own.fstype
+ || is_snapshot(mount)
+ })
+ .map(|mount| mount.point.clone())
+ .collect()
+}
+
+fn is_snapshot(mount: &Mount) -> bool {
+ let named = mount
+ .point
+ .file_name()
+ .is_some_and(|name| name == ".snapshots");
+ named
+ || mount.options.split(',').any(|option| {
+ option.starts_with("subvol=") && option.contains("snapshots")
+ })
+}
+
+/// The top of the disk `path` lives on.
+///
+/// The shortest mount point above it with the same source. On Omarchy the home directory is the `@home`
+/// subvolume at `/home`, and the disk is `/`; with a separate home disk it
+/// would be `/home`.
+pub fn volume_root(mounts: &[Mount], path: &Path) -> Option<PathBuf> {
+ let own = mounts
+ .iter()
+ .filter(|mount| path.starts_with(&mount.point))
+ .max_by_key(|mount| mount.point.as_os_str().len())?;
+ mounts
+ .iter()
+ .filter(|mount| {
+ mount.source == own.source
+ && mount.fstype == own.fstype
+ && path.starts_with(&mount.point)
+ })
+ .min_by_key(|mount| mount.point.as_os_str().len())
+ .map(|mount| mount.point.clone())
+}
+
+/// [`volume_root`] for this machine.
+pub fn volume_root_for(path: &Path) -> Option<PathBuf> {
+ let table = std::fs::read_to_string("/proc/self/mounts").ok()?;
+ let path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
+ volume_root(&parse_mounts(&table), &path)
+}
+
+/// [`foreign_mounts`] for this machine; `None` when the mount table cannot
+/// be read, so the caller can fall back to comparing devices.
+pub fn foreign_mounts_for(root: &Path) -> Option<Vec<PathBuf>> {
+ let table = std::fs::read_to_string("/proc/self/mounts").ok()?;
+ Some(foreign_mounts(&parse_mounts(&table), root))
+}
+
#[cfg(test)]
mod tests {
use super::*;
+ const OMARCHY: &str = "\
+sys /sys sysfs rw 0 0
+run /run tmpfs rw 0 0
+/dev/mapper/root / btrfs rw,subvolid=256,subvol=/@ 0 0
+/dev/mapper/root /home btrfs rw,subvolid=257,subvol=/@home 0 0
+/dev/mapper/root /var/log btrfs rw,subvolid=258,subvol=/@log 0 0
+/dev/mapper/root /.snapshots btrfs rw,subvolid=260,subvol=/@snapshots 0 0
+/dev/nvme0n1p1 /boot vfat rw 0 0
+systemd-1 /mnt/nas-home autofs rw,direct 0 0
+tmpfs /tmp tmpfs rw 0 0
+portal /run/user/1000/doc fuse.portal rw 0 0
+";
+
+ #[test]
+ fn a_volume_includes_its_subvolumes_and_nothing_else() {
+ let mounts = parse_mounts(OMARCHY);
+ let mut foreign = foreign_mounts(&mounts, Path::new("/"));
+ foreign.sort();
+ let expected: Vec<PathBuf> = [
+ "/.snapshots",
+ "/boot",
+ "/mnt/nas-home",
+ "/run",
+ "/run/user/1000/doc",
+ "/sys",
+ "/tmp",
+ ]
+ .iter()
+ .map(PathBuf::from)
+ .collect();
+ assert_eq!(foreign, expected, "/home and /var/log are the same disk");
+ }
+
+ #[test]
+ fn the_whole_disk_is_the_top_of_the_home_volume() {
+ let mounts = parse_mounts(OMARCHY);
+ let root = volume_root(&mounts, Path::new("/home/tobi"));
+ assert_eq!(root, Some(PathBuf::from("/")), "@home is on the root disk");
+ let separate = parse_mounts(
+ "/dev/sda1 / ext4 rw 0 0\n/dev/sdb1 /home ext4 rw 0 0\n",
+ );
+ let root = volume_root(&separate, Path::new("/home/tobi"));
+ assert_eq!(root, Some(PathBuf::from("/home")), "a separate home disk");
+ }
+
+ #[test]
+ fn a_home_scan_has_no_foreign_mounts_here() {
+ let mounts = parse_mounts(OMARCHY);
+ assert!(foreign_mounts(&mounts, Path::new("/home/tobi")).is_empty());
+ }
+
+ #[test]
+ fn the_device_is_the_longest_matching_mount() {
+ let table = "\
+/dev/nvme0n1p2 / btrfs rw 0 0
+tmpfs /tmp tmpfs rw 0 0
+/dev/sda1 /home/tobi/big\\040disk ext4 rw 0 0
+";
+ let device = |path: &str| device_in(table, Path::new(path));
+ assert_eq!(device("/home/tobi").as_deref(), Some("/dev/nvme0n1p2"));
+ assert_eq!(device("/tmp/x").as_deref(), Some("tmpfs"));
+ assert_eq!(
+ device("/home/tobi/big disk/a").as_deref(),
+ Some("/dev/sda1"),
+ "escaped spaces in mount points"
+ );
+ }
+
#[test]
fn a_real_volume_reports_plausible_numbers() {
let temp = std::env::temp_dir();
diff --git a/crates/disktree-core/src/tree.rs b/crates/disktree-core/src/tree.rs
index 5d3b612..1ad9ae2 100644
--- a/crates/disktree-core/src/tree.rs
+++ b/crates/disktree-core/src/tree.rs
@@ -2,6 +2,8 @@
use std::path::{Path, PathBuf};
+use crate::classify::{Category, Reclaim};
+
/// What a node represents on disk.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NodeKind {
@@ -71,6 +73,14 @@ pub struct Node {
pub inode: Option<(u64, u64)>,
/// The directory could not be read; its contents are unknown.
pub read_error: bool,
+ /// Newest write time at or beneath this node, in Unix seconds; `0` when
+ /// unknown. Derived for directories by [`aggregate`].
+ pub modified: i64,
+ /// What kind of data this is, for colour. Set by [`crate::classify`].
+ pub category: Category,
+ /// Why this space can be had back, if it can. Set by
+ /// [`crate::classify`]; inherited by everything beneath.
+ pub reclaim: Option<Reclaim>,
/// Children, ordered by [`Metric`] value, descending.
pub children: Vec<Self>,
}
@@ -92,6 +102,9 @@ impl Node {
dirs: 1,
inode: None,
read_error: false,
+ modified: 0,
+ category: Category::Other,
+ reclaim: None,
children: Vec::new(),
}
}
@@ -112,6 +125,9 @@ impl Node {
dirs: 0,
inode: None,
read_error: false,
+ modified: 0,
+ category: Category::Other,
+ reclaim: None,
children: Vec::new(),
}
}
@@ -226,8 +242,10 @@ pub fn aggregate(node: &mut Node, metric: Metric) {
let mut own_bytes = 0;
let mut own_files = 0;
let mut dirs: u64 = 1;
+ let mut modified = 0;
for child in &mut node.children {
aggregate(child, metric);
+ modified = modified.max(child.modified);
bytes += child.bytes;
files += child.files;
dirs += child.dirs;
@@ -241,6 +259,7 @@ pub fn aggregate(node: &mut Node, metric: Metric) {
node.own_bytes = own_bytes;
node.own_files = own_files;
node.dirs = dirs;
+ node.modified = modified;
// Largest first: a treemap lays out big tiles best, and the order is what
// makes "descend into the largest child" meaningful.
diff --git a/crates/disktree-core/src/treemap.rs b/crates/disktree-core/src/treemap.rs
index c5dc2d7..6dfa020 100644
--- a/crates/disktree-core/src/treemap.rs
+++ b/crates/disktree-core/src/treemap.rs
@@ -106,25 +106,33 @@ impl Tile {
pub struct LayoutOptions {
/// Nesting levels drawn at once; 1 draws only the root's children.
pub max_depth: u32,
- /// Gap between siblings and inset into a parent: what makes nesting
- /// visible without a colour legend.
+ /// Gap between siblings inside a directory, and inset into it.
pub padding: f32,
+ /// Gap between the top-level directories: wider than `padding`, so the
+ /// first level of structure reads before the detail inside it.
+ pub padding_outer: f32,
/// Tiles below this many pixels are dropped; they cannot be read or hit.
pub min_tile: f32,
/// Children kept per directory. The tail merges into one `Others` tile.
pub max_children: usize,
- /// Height of the band a subdivided directory keeps for its own name.
+ /// Height of the band a top-level directory keeps for its name.
pub header: f32,
+ /// Height of the slimmer band a deeper directory keeps when it is drawn
+ /// open. A directory drawn closed has no band: its label sits in its
+ /// corner, over nothing but its own fill.
+ pub header_inner: f32,
}
impl Default for LayoutOptions {
fn default() -> Self {
Self {
max_depth: 3,
- padding: 2.0,
+ padding: 1.0,
min_tile: 5.0,
max_children: 96,
- header: 17.0,
+ padding_outer: 3.0,
+ header: 20.0,
+ header_inner: 15.0,
}
}
}
@@ -189,7 +197,11 @@ fn place_children(
}
for (slot, raw) in squarify(&values, area).iter().enumerate() {
- let rect = raw.inset(options.padding);
+ let rect = raw.inset(if depth == 0 {
+ options.padding_outer
+ } else {
+ options.padding
+ });
if rect.w < options.min_tile || rect.h < options.min_tile {
continue;
}
@@ -248,16 +260,17 @@ fn place_children(
/// The band a directory keeps for its name, or `None` when the tile is too
/// small to leave its children a usable area below it.
///
-/// Deeper levels get a slimmer band: their names are shorter and their tiles
-/// are smaller.
+/// The top level gets the full header; deeper directories a slimmer band.
fn header_band(
rect: Rect,
options: &LayoutOptions,
depth: u32,
) -> Option<Rect> {
- let height = (depth as f32)
- .mul_add(-1.5, options.header)
- .max(options.header * 0.6);
+ let height = if depth == 0 {
+ options.header
+ } else {
+ options.header_inner
+ };
let body = rect.h - height;
if rect.w < 44.0 || body < options.min_tile * 3.0 {
return None;