diff --git a/crates/disktree-core/Cargo.toml b/crates/disktree-core/Cargo.toml new file mode 100644 index 0000000..21ac328 --- /dev/null +++ b/crates/disktree-core/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "disktree-core" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +description = "Filesystem scanning and treemap layout for disktree" + +[lints] +workspace = true + +[dependencies] +chrono = { version = "0.4.45", default-features = false, features = ["clock", "std"] } +rayon = { workspace = true } +rustix = { version = "1.1", features = ["fs"] } +rustc-hash = { workspace = true } + +[dev-dependencies] +tempfile = "3" diff --git a/crates/disktree-core/src/lib.rs b/crates/disktree-core/src/lib.rs new file mode 100644 index 0000000..4b6354d --- /dev/null +++ b/crates/disktree-core/src/lib.rs @@ -0,0 +1,13 @@ +//! `disktree-core`: scanning, layout, space accounting and deletion, with no +//! UI dependency. +//! +//! The split exists so the parts that must be *correct* — size accounting, +//! 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 removal; +pub mod scan; +pub mod size; +pub mod space; +pub mod tree; +pub mod treemap; diff --git a/crates/disktree-core/src/removal.rs b/crates/disktree-core/src/removal.rs new file mode 100644 index 0000000..5e4ffd0 --- /dev/null +++ b/crates/disktree-core/src/removal.rs @@ -0,0 +1,844 @@ +//! Turning marked paths into deletions, safely. +//! +//! Three things matter here, in this order: never remove something the user did +//! not point at, never descend into a different filesystem, and always be able +//! to say what happened. +//! +//! Two mechanisms are offered: +//! +//! * [`RemovalMode::Permanent`] — `rm -rf` semantics, implemented with the +//! standard library rather than by shelling out, so no path ever reaches a +//! shell and no filename can be misread as an option. +//! * [`RemovalMode::Trash`] — move to the desktop trash, using `trash-put`, +//! then `gio trash`, then a built-in XDG implementation. The backend is +//! detected once and named in the UI so the user knows what actually happens. + +use std::fs; +use std::io; +use std::path::{Component, Path, PathBuf}; +use std::process::Command; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::mpsc::{self, Receiver}; +use std::thread; + +/// One path the user asked to remove. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Target { + pub path: PathBuf, + /// Bytes measured when the path was marked; used for the projection. + pub bytes: u64, + pub is_dir: bool, + pub hidden: bool, +} + +/// How a removal should be carried out. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum RemovalMode { + /// Delete now. Not recoverable. + #[default] + Permanent, + /// Move to the desktop trash so the removal can be undone. + Trash, +} + +impl RemovalMode { + pub const fn label(self) -> &'static str { + match self { + Self::Permanent => "Delete permanently", + Self::Trash => "Move to trash", + } + } + + pub const fn detail(self) -> &'static str { + match self { + Self::Permanent => "rm -rf: unrecoverable", + Self::Trash => "recoverable until the trash is emptied", + } + } +} + +/// A target that will not be touched, and why. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Blocked { + pub path: PathBuf, + pub reason: String, +} + +/// The work a [`RemovalMode`] will actually do. +#[derive(Clone, Debug, Default)] +pub struct Plan { + /// Targets to act on, with any target contained in another removed first. + pub targets: Vec, + /// Marked paths covered by a target above; reported, not acted on. + pub covered: Vec, + /// Marked paths that must not be touched. + pub blocked: Vec, +} + +impl Plan { + pub fn bytes(&self) -> u64 { + self.targets.iter().map(|target| target.bytes).sum() + } + + pub const fn is_empty(&self) -> bool { + self.targets.is_empty() + } +} + +/// Build the plan for `targets`, which must all live under `root`. +/// +/// Paths outside `root` are blocked rather than removed: the tree the user was +/// looking at is the only thing they consented to act on. +pub fn plan(targets: &[Target], root: &Path) -> Plan { + let root = normalize(root); + let home = std::env::var_os("HOME").map(PathBuf::from); + let mut plan = Plan::default(); + let mut accepted: Vec = Vec::new(); + + for target in targets { + let path = normalize(&target.path); + if let Some(reason) = refuse(&path, &root, home.as_deref()) { + plan.blocked.push(Blocked { + path: target.path.clone(), + reason, + }); + continue; + } + accepted.push(Target { + path, + ..target.clone() + }); + } + + // A path inside another target is removed with it. Keep the outer one and + // report the inner one so the review screen can explain the nesting. + accepted.sort_by(|left, right| left.path.cmp(&right.path)); + let mut outer: Vec = Vec::new(); + for target in accepted { + if outer + .iter() + .any(|candidate| target.path.starts_with(&candidate.path)) + { + plan.covered.push(target); + } else { + outer.push(target); + } + } + plan.targets = outer; + plan +} + +/// Why this path must not be removed, if it must not. +fn refuse(path: &Path, root: &Path, home: Option<&Path>) -> Option { + if path.parent().is_none() { + return Some("the filesystem root cannot be removed".into()); + } + if path == root { + return Some("the scanned root cannot be removed".into()); + } + if home.is_some_and(|home| path == normalize(home)) { + return Some("the home directory cannot be removed".into()); + } + if !path.starts_with(root) { + return Some("outside the scanned root".into()); + } + if is_mount_point(path) { + return Some( + "a mount point: removing it would cross onto another filesystem" + .into(), + ); + } + None +} + +/// Whether `path` sits on a different device than its parent, i.e. is a +/// mount point. Descending into one would delete data the user never marked. +#[cfg(unix)] +pub fn is_mount_point(path: &Path) -> bool { + use std::os::unix::fs::MetadataExt as _; + let Ok(meta) = fs::symlink_metadata(path) else { + return false; + }; + if !meta.is_dir() { + return false; + } + let Some(parent) = path.parent() else { + return true; + }; + match fs::symlink_metadata(parent) { + Ok(parent_meta) => parent_meta.dev() != meta.dev(), + // A directory whose parent cannot be stat'ed is not worth the risk. + Err(_) => true, + } +} + +#[cfg(not(unix))] +pub fn is_mount_point(_path: &Path) -> bool { + false +} + +/// Lexically normalize a path: resolve `.` and `..` without touching the +/// filesystem, so a symlink can never redirect a guard. +/// +/// A `..` at the root has nowhere to go, so `/..` is `/` rather than the empty +/// path. A leading `..` on a relative path is preserved. +pub fn normalize(path: &Path) -> PathBuf { + let mut out = PathBuf::new(); + for component in path.components() { + match component { + Component::CurDir => {} + Component::ParentDir => { + if matches!( + out.components().next_back(), + Some(Component::Normal(_)) + ) { + out.pop(); + } else if !out.has_root() && out.as_os_str().is_empty() { + out.push(".."); + } + } + Component::Prefix(_) + | Component::RootDir + | Component::Normal(_) => { + out.push(component.as_os_str()); + } + } + } + out +} + +/// Which tool, if any, moves files to the desktop trash. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum TrashBackend { + /// `trash-put` from trash-cli. + TrashPut, + /// `gio trash`, present anywhere `GLib` is installed. + Gio, + /// The XDG trash directory, implemented here. + XdgHome, + /// No way to move files to a trash on this machine. + #[default] + Unavailable, +} + +impl TrashBackend { + pub const fn is_available(self) -> bool { + match self { + Self::TrashPut | Self::Gio | Self::XdgHome => true, + Self::Unavailable => false, + } + } + + pub const fn label(self) -> &'static str { + match self { + Self::TrashPut => "trash-put", + Self::Gio => "gio trash", + Self::XdgHome => "XDG trash", + Self::Unavailable => "no trash tool found", + } + } + + pub const fn detail(self) -> &'static str { + match self { + Self::TrashPut => { + "uses trash-cli, the same trash as your file manager" + } + Self::Gio => "uses GLib, the same trash as your file manager", + Self::XdgHome => { + "moves into ~/.local/share/Trash on the same volume" + } + Self::Unavailable => { + "install trash-cli or keep deleting permanently" + } + } + } +} + +/// Detect the best available trash backend for this machine. +pub fn detect_trash_backend() -> TrashBackend { + if which("trash-put") { + TrashBackend::TrashPut + } else if which("gio") { + TrashBackend::Gio + } else if home_trash_dir().is_some() { + TrashBackend::XdgHome + } else { + TrashBackend::Unavailable + } +} + +fn which(program: &str) -> bool { + let Some(path) = std::env::var_os("PATH") else { + return false; + }; + std::env::split_paths(&path).any(|dir| { + let candidate = dir.join(program); + fs::metadata(candidate).is_ok_and(|meta| meta.is_file()) + }) +} + +fn home_trash_dir() -> Option { + let data = std::env::var_os("XDG_DATA_HOME").map_or_else( + || { + std::env::var_os("HOME") + .map(PathBuf::from) + .map(|home| home.join(".local/share")) + }, + |value| Some(PathBuf::from(value)), + )?; + Some(data.join("Trash")) +} + +/// What a running removal reports. +#[derive(Clone, Debug)] +pub enum RemovalEvent { + Start { + total: usize, + }, + Item { + path: PathBuf, + bytes: u64, + outcome: Result<(), String>, + }, + Done { + removed: u64, + bytes: u64, + failed: usize, + }, +} + +/// A removal running on its own thread. +#[derive(Debug)] +pub struct RemovalHandle { + events: Receiver, + cancel: Arc, +} + +impl RemovalHandle { + /// Take the next event, if one has arrived. + pub fn poll(&self) -> Option { + // Empty and Disconnected mean the same thing to the caller: there is + // nothing more to do this tick. + self.events.try_recv().ok() + } + + /// Stop before the next target starts. + pub fn cancel(&self) { + self.cancel.store(true, Ordering::Relaxed); + } +} + +/// Start removing the plan's targets on a worker thread. +pub fn spawn(plan: Plan, mode: RemovalMode) -> RemovalHandle { + let (sender, events) = mpsc::channel(); + let cancel = Arc::new(AtomicBool::new(false)); + let worker_cancel = Arc::clone(&cancel); + let backend = if mode == RemovalMode::Trash { + detect_trash_backend() + } else { + TrashBackend::Unavailable + }; + + let worker = thread::Builder::new() + .name("disktree-remove".into()) + .spawn({ + let sender = sender.clone(); + move || run(&plan, mode, backend, &worker_cancel, &sender) + }); + if let Err(error) = worker { + let _ = sender.send(RemovalEvent::Item { + path: PathBuf::new(), + bytes: 0, + outcome: Err(format!( + "could not start the removal worker: {error}" + )), + }); + } + + RemovalHandle { events, cancel } +} + +fn run( + plan: &Plan, + mode: RemovalMode, + backend: TrashBackend, + cancel: &AtomicBool, + sender: &mpsc::Sender, +) { + let _ = sender.send(RemovalEvent::Start { + total: plan.targets.len(), + }); + let mut removed = 0_u64; + let mut bytes = 0_u64; + let mut failed = 0_usize; + + for target in &plan.targets { + if cancel.load(Ordering::Relaxed) { + break; + } + let outcome = match mode { + RemovalMode::Permanent => remove_permanently(&target.path), + RemovalMode::Trash => move_to_trash(&target.path, backend), + }; + if outcome.is_ok() { + removed += 1; + bytes += target.bytes; + } else { + failed += 1; + } + let _ = sender.send(RemovalEvent::Item { + path: target.path.clone(), + bytes: target.bytes, + outcome: outcome.map_err(|error| error.to_string()), + }); + } + + let _ = sender.send(RemovalEvent::Done { + removed, + bytes, + failed, + }); +} + +/// `rm -rf` semantics: a symlink is unlinked, never followed. +pub fn remove_permanently(path: &Path) -> io::Result<()> { + let meta = fs::symlink_metadata(path)?; + if meta.is_dir() { + fs::remove_dir_all(path) + } else { + fs::remove_file(path) + } +} + +/// Move one path to the desktop trash. +pub fn move_to_trash(path: &Path, backend: TrashBackend) -> io::Result<()> { + match backend { + TrashBackend::TrashPut => run_tool(Path::new("trash-put"), &[], path), + TrashBackend::Gio => run_tool(Path::new("gio"), &["trash"], path), + TrashBackend::XdgHome => trash_via_xdg(path), + TrashBackend::Unavailable => Err(io::Error::other( + "no trash tool is installed; use permanent deletion instead", + )), + } +} + +/// Run one trash tool on one path. +/// +/// `--` is passed before the path so that a file named `-rf` is read as a path +/// and never as an option. Splitting this out is also what makes the argument +/// order testable without writing to anybody's real trash. +fn run_tool(program: &Path, arguments: &[&str], path: &Path) -> io::Result<()> { + let output = Command::new(program) + .args(arguments) + .arg("--") + .arg(path) + .output()?; + if output.status.success() { + Ok(()) + } else { + let stderr = String::from_utf8_lossy(&output.stderr); + Err(io::Error::other(format!( + "{} failed: {}", + program.display(), + stderr.trim() + ))) + } +} + +/// The XDG trash layout, implemented directly so the app still has a trash on +/// machines without `trash-cli` or `GLib`. +/// +/// Only same-volume paths can be trashed this way: the specification forbids +/// copying a file into a trash directory on another filesystem. +fn trash_via_xdg(path: &Path) -> io::Result<()> { + let trash = home_trash_dir().ok_or_else(|| { + io::Error::other("neither XDG_DATA_HOME nor HOME is set") + })?; + trash_into(path, &trash) +} + +/// Move `path` into the trash directory at `trash`. +#[cfg(unix)] +pub fn trash_into(path: &Path, trash: &Path) -> io::Result<()> { + use std::os::unix::fs::MetadataExt as _; + + let files = trash.join("files"); + let info = trash.join("info"); + fs::create_dir_all(&files)?; + fs::create_dir_all(&info)?; + + let source = fs::symlink_metadata(path)?; + let trash_meta = fs::metadata(&files)?; + if source.dev() != trash_meta.dev() { + return Err(io::Error::other( + "on a different filesystem than the trash; use permanent deletion", + )); + } + + let name = path + .file_name() + .ok_or_else(|| { + io::Error::other("refusing to trash a path without a name") + })? + .to_string_lossy() + .into_owned(); + let (candidate, trashed_name) = unique_name(&files, &name); + fs::rename(path, &candidate)?; + + let info_path = info.join(format!("{trashed_name}.trashinfo")); + let contents = format!( + "[Trash Info]\nPath={}\nDeletionDate={}\n", + percent_encode(&path.to_string_lossy()), + deletion_date() + ); + if let Err(error) = fs::write(&info_path, contents) { + // Put it back rather than leaving an entry the trash UI cannot restore. + let _ = fs::rename(&candidate, path); + return Err(error); + } + Ok(()) +} + +#[cfg(not(unix))] +pub fn trash_into(_path: &Path, _trash: &Path) -> io::Result<()> { + Err(io::Error::other( + "the XDG trash is only implemented on Unix", + )) +} + +/// `name`, or `name.1`, `name.2`, … until the name is free in `dir`. +fn unique_name(dir: &Path, name: &str) -> (PathBuf, String) { + let first = dir.join(name); + if !first.exists() { + return (first, name.to_string()); + } + for index in 1..10_000 { + let candidate = format!("{name}.{index}"); + let path = dir.join(&candidate); + if !path.exists() { + return (path, candidate); + } + } + (dir.join(name), name.to_string()) +} + +/// Percent-encode a path for a `.trashinfo` file: everything outside the +/// unreserved set is escaped, so spaces and newlines survive. +pub fn percent_encode(input: &str) -> String { + let mut out = String::with_capacity(input.len()); + for byte in input.bytes() { + let unreserved = byte.is_ascii_alphanumeric() + || matches!(byte, b'/' | b'-' | b'.' | b'_' | b'~'); + if unreserved { + out.push(char::from(byte)); + } else { + use std::fmt::Write as _; + let _ = write!(out, "%{byte:02X}"); + } + } + out +} + +fn deletion_date() -> String { + // The specification wants ISO 8601 in local time; chrono is already in the + // dependency graph, so use it rather than approximating the offset. + chrono::Local::now().format("%Y-%m-%dT%H:%M:%S").to_string() +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::TempDir; + + fn target(path: &Path, bytes: u64) -> Target { + Target { + path: path.to_path_buf(), + bytes, + is_dir: path.is_dir(), + hidden: false, + } + } + + fn tree() -> TempDir { + let temp = TempDir::new().expect("tempdir"); + fs::create_dir_all(temp.path().join("a/b")).expect("mkdir"); + fs::write(temp.path().join("a/one.bin"), vec![b'x'; 10]) + .expect("write"); + fs::write(temp.path().join("a/c.bin"), vec![b'x'; 20]).expect("write"); + fs::create_dir_all(temp.path().join("other")).expect("mkdir"); + temp + } + + #[test] + fn a_plan_keeps_only_the_outermost_targets() { + let temp = tree(); + let root = temp.path(); + let inner = target(&root.join("a/b"), 5); + let outer = target(&root.join("a"), 30); + let separate = target(&root.join("other"), 0); + + let plan = + plan(&[inner.clone(), outer.clone(), separate.clone()], root); + assert_eq!(plan.targets.len(), 2); + assert!(plan.targets.contains(&outer)); + assert!(plan.targets.contains(&separate)); + assert_eq!(plan.covered.len(), 1); + assert_eq!(plan.covered[0].path, inner.path); + assert_eq!(plan.bytes(), 30); + } + + #[test] + fn the_root_and_home_are_refused() { + let temp = tree(); + let root = temp.path(); + let home = std::env::var_os("HOME").map(PathBuf::from); + let mut targets = vec![target(Path::new("/"), 0), target(root, 0)]; + if let Some(home) = &home { + targets.push(target(home, 0)); + } + + let plan = plan(&targets, root); + assert!(plan.is_empty()); + assert_eq!(plan.blocked.len(), targets.len()); + assert!( + plan.blocked + .iter() + .any(|blocked| blocked.reason.contains("home directory")) + || home.is_none() + ); + } + + #[test] + fn paths_outside_the_root_are_refused() { + let temp = tree(); + let plan = plan(&[target(Path::new("/etc/passwd"), 1)], temp.path()); + assert!(plan.is_empty()); + assert_eq!(plan.blocked[0].reason, "outside the scanned root"); + } + + #[test] + fn sibling_prefixes_are_not_treated_as_containment() { + let temp = tree(); + let root = temp.path(); + fs::create_dir_all(root.join("a-real")).expect("mkdir"); + let plan = plan( + &[target(&root.join("a"), 1), target(&root.join("a-real"), 1)], + root, + ); + assert_eq!(plan.targets.len(), 2, "a-real is not inside a"); + assert!(plan.covered.is_empty()); + } + + #[test] + fn normalize_resolves_dots_without_the_filesystem() { + assert_eq!( + normalize(Path::new("/home/tobi/./src/../src/")), + PathBuf::from("/home/tobi/src") + ); + assert_eq!(normalize(Path::new("a/../b")), PathBuf::from("b")); + assert_eq!(normalize(Path::new("/..")), PathBuf::from("/")); + assert_eq!(normalize(Path::new("../x")), PathBuf::from("../x")); + } + + #[test] + fn mount_points_are_recognized() { + let temp = tree(); + assert!(!is_mount_point(temp.path())); + // /proc is a mount point everywhere Linux runs this test. + if Path::new("/proc/self").exists() { + assert!(is_mount_point(Path::new("/proc"))); + } + } + + #[test] + fn permanent_removal_takes_directories_and_leaves_siblings() { + let temp = tree(); + let doomed = temp.path().join("a/b"); + remove_permanently(&doomed).expect("remove dir"); + assert!(!doomed.exists()); + assert!(temp.path().join("a/c.bin").exists()); + } + + #[test] + fn permanent_removal_unlinks_a_symlink_instead_of_following_it() { + let temp = tree(); + let keep = TempDir::new().expect("tempdir"); + fs::write(keep.path().join("precious.bin"), b"data").expect("write"); + let link = temp.path().join("link"); + std::os::unix::fs::symlink(keep.path(), &link).expect("symlink"); + + remove_permanently(&link).expect("remove link"); + assert!(!link.exists()); + assert!(keep.path().join("precious.bin").exists()); + } + + #[test] + fn a_missing_target_reports_not_found() { + let temp = tree(); + let error = remove_permanently(&temp.path().join("absent")) + .expect_err("missing"); + assert_eq!(error.kind(), io::ErrorKind::NotFound); + } + + #[test] + fn percent_encoding_escapes_what_the_spec_requires() { + assert_eq!(percent_encode("/home/tobi/a b"), "/home/tobi/a%20b"); + assert_eq!(percent_encode("/a/b-c.d_e~f"), "/a/b-c.d_e~f"); + assert_eq!(percent_encode("/a/néw"), "/a/n%C3%A9w"); + assert_eq!(percent_encode("/a\nb"), "/a%0Ab"); + } + + #[test] + fn trash_names_avoid_collisions() { + let temp = TempDir::new().expect("tempdir"); + let (first, first_name) = unique_name(temp.path(), "notes"); + assert_eq!(first_name, "notes"); + fs::write(&first, b"x").expect("write"); + + let (second, second_name) = unique_name(temp.path(), "notes"); + assert_eq!(second_name, "notes.1"); + assert_ne!(first, second); + } + + #[test] + fn the_xdg_trash_moves_a_file_and_records_where_it_came_from() { + // A private trash directory keeps the test out of the real trash can. + let trash = TempDir::new().expect("tempdir"); + let work = TempDir::new().expect("tempdir"); + let file = work.path().join("doomed.bin"); + fs::write(&file, b"payload").expect("write"); + + trash_into(&file, trash.path()).expect("trashed"); + + assert!(!file.exists()); + let moved = trash.path().join("files/doomed.bin"); + assert!(moved.exists(), "moved into the trash"); + assert_eq!(fs::read(&moved).expect("read"), b"payload"); + let info = + fs::read_to_string(trash.path().join("info/doomed.bin.trashinfo")) + .expect("info file"); + assert!(info.starts_with("[Trash Info]\nPath="), "{info}"); + assert!(info.contains("doomed.bin")); + assert!(info.contains("DeletionDate=")); + } + + #[test] + fn a_trash_info_path_is_restorable() { + let trash = TempDir::new().expect("tempdir"); + let work = TempDir::new().expect("tempdir"); + let file = work.path().join("with space.bin"); + fs::write(&file, b"x").expect("write"); + trash_into(&file, trash.path()).expect("trashed"); + + let info = fs::read_to_string( + trash.path().join("info/with space.bin.trashinfo"), + ) + .expect("info file"); + let recorded = info + .lines() + .find_map(|line| line.strip_prefix("Path=")) + .expect("a path"); + assert_eq!(recorded, percent_encode(&file.to_string_lossy())); + assert!(recorded.ends_with("with%20space.bin")); + } + + #[test] + fn a_removal_run_reports_every_item_and_a_total() { + let temp = tree(); + let root = temp.path(); + let plan = plan( + &[ + target(&root.join("a/b"), 0), + target(&root.join("a/c.bin"), 20), + target(&root.join("absent"), 0), + ], + root, + ); + assert_eq!(plan.targets.len(), 3); + + let handle = spawn(plan, RemovalMode::Permanent); + let mut events = Vec::new(); + for _ in 0..2000 { + while let Some(event) = handle.poll() { + events.push(event); + } + if matches!(events.last(), Some(RemovalEvent::Done { .. })) { + break; + } + thread::sleep(std::time::Duration::from_millis(1)); + } + + assert!(matches!(events[0], RemovalEvent::Start { total: 3 })); + let items = events + .iter() + .filter(|event| matches!(event, RemovalEvent::Item { .. })) + .count(); + assert_eq!(items, 3); + match events.last() { + Some(RemovalEvent::Done { + removed, + bytes, + failed, + }) => { + assert_eq!(*removed, 2); + assert_eq!(*failed, 1); + assert_eq!(*bytes, 20); + } + other => panic!("expected Done, got {other:?}"), + } + assert!(!root.join("a/b").exists()); + assert!(root.join("a/one.bin").exists()); + } + + #[test] + fn a_trash_tool_is_called_with_the_path_after_a_separator() { + use std::os::unix::fs::PermissionsExt as _; + let temp = TempDir::new().expect("tempdir"); + let recording = temp.path().join("argv"); + let script = temp.path().join("fake-trash-put"); + fs::write( + &script, + format!( + "#!/bin/sh\nprintf '%s\\n' \"$@\" > {}\n", + recording.display() + ), + ) + .expect("write the script"); + fs::set_permissions(&script, fs::Permissions::from_mode(0o755)) + .expect("chmod"); + + // A path that would be an option if the separator were missing. + let doomed = temp.path().join("-rf"); + fs::write(&doomed, b"not this time").expect("write"); + run_tool(&script, &[], &doomed).expect("the tool ran"); + + let recorded = fs::read_to_string(&recording).expect("the argv"); + let lines: Vec<&str> = recorded.lines().collect(); + assert_eq!(lines, vec!["--", doomed.to_string_lossy().as_ref()]); + assert!(doomed.exists(), "the real tool would have moved it"); + } + + #[test] + fn a_failing_trash_tool_reports_its_stderr() { + use std::os::unix::fs::PermissionsExt as _; + let temp = TempDir::new().expect("tempdir"); + let script = temp.path().join("angry-trash-put"); + fs::write(&script, "#!/bin/sh\necho 'no trash here' >&2\nexit 3\n") + .expect("write"); + fs::set_permissions(&script, fs::Permissions::from_mode(0o755)) + .expect("chmod"); + + let error = run_tool(&script, &[], temp.path()).expect_err("it failed"); + assert!(error.to_string().contains("no trash here"), "{error}"); + } + + #[test] + fn detection_prefers_a_tool_this_machine_has() { + let backend = detect_trash_backend(); + if which("trash-put") { + assert_eq!(backend, TrashBackend::TrashPut); + } + assert!(backend.is_available()); + } +} diff --git a/crates/disktree-core/src/scan.rs b/crates/disktree-core/src/scan.rs new file mode 100644 index 0000000..b75d9d3 --- /dev/null +++ b/crates/disktree-core/src/scan.rs @@ -0,0 +1,921 @@ +//! Parallel filesystem scanning. +//! +//! The shape follows dust, because that shape is the reason dust is fast: +//! +//! * one `rayon::scope` per root, so recursion depth is O(1) however deep the +//! tree is, +//! * every directory is a `PendingDir` with an atomic completion counter and a +//! `+1` sentinel, so a directory is only built once its own scan *and* all of +//! its subdirectory tasks have finished, +//! * children are handed to the parent as finished `Node`s and sizes are +//! aggregated bottom-up in one serial pass, which is also where hardlinks are +//! de-duplicated. +//! +//! What is added on top of dust's approach: live progress counters the UI can +//! poll without locking, and cooperative cancellation so a re-scan can abandon +//! a walk of a large home directory instead of queueing behind it. + +use std::fs::{self, DirEntry, Metadata}; +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::thread; + +use rayon::Scope; +use rustc_hash::FxHashSet; + +use crate::tree::{Metric, Node, NodeKind, aggregate}; + +/// Errors kept verbatim before the list is truncated; the count keeps rising. +const MAX_ERROR_DETAIL: usize = 50; + +/// How a scan measures and filters the tree. +#[derive(Clone, Debug)] +pub struct ScanOptions { + /// Measure apparent length instead of allocated blocks. Apparent size is + /// what `ls -l` shows; blocks are what the volume actually spends, which is + /// what a disk-space tool normally wants. + pub apparent_size: bool, + /// Follow symlinks. Off by default: a home directory is full of links, and + /// following them double-counts. + pub follow_links: bool, + /// 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. + 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. + pub max_depth: Option, + /// Count a hardlinked file once instead of once per link. + pub dedup_hardlinks: bool, + /// Whether children are ranked by bytes or by file count. + pub metric: Metric, +} + +impl Default for ScanOptions { + fn default() -> Self { + Self { + apparent_size: false, + follow_links: false, + include_hidden: true, + one_filesystem: false, + max_depth: None, + dedup_hardlinks: true, + metric: Metric::Bytes, + } + } +} + +/// Counters a running scan publishes for the UI. +/// +/// Plain relaxed atomics: this is a progress meter, not a synchronisation +/// point, and the UI reads a snapshot that is allowed to be a few entries +/// stale. +#[derive(Debug, Default)] +pub struct ScanProgress { + files: AtomicU64, + dirs: AtomicU64, + bytes: AtomicU64, + errors: AtomicU64, + finished: AtomicBool, + cancelled: AtomicBool, + messages: Mutex>, +} + +/// A point-in-time view of [`ScanProgress`]. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct ScanSnapshot { + pub files: u64, + pub dirs: u64, + pub bytes: u64, + pub errors: u64, + pub finished: bool, + pub cancelled: bool, + /// Up to [`MAX_ERROR_DETAIL`] unreadable paths, most recent last. + pub messages: Vec, +} + +impl ScanProgress { + fn count_file(&self, bytes: u64) { + self.files.fetch_add(1, Ordering::Relaxed); + self.bytes.fetch_add(bytes, Ordering::Relaxed); + } + + fn count_dir(&self) { + self.dirs.fetch_add(1, Ordering::Relaxed); + } + + fn record_error(&self, path: &Path, error: &io::Error) { + self.errors.fetch_add(1, Ordering::Relaxed); + let message = format!("{}: {error}", path.display()); + let mut messages = lock(&self.messages); + if messages.len() < MAX_ERROR_DETAIL { + messages.push(message); + } + drop(messages); + } + + fn finish(&self) { + self.finished.store(true, Ordering::Relaxed); + } + + /// Ask the walk to stop at the next directory boundary. + pub fn cancel(&self) { + self.cancelled.store(true, Ordering::Relaxed); + } + + /// Stop the walk now, without waiting for the worker thread. + pub fn is_cancelled(&self) -> bool { + self.cancelled.load(Ordering::Relaxed) + } + + pub fn snapshot(&self) -> ScanSnapshot { + ScanSnapshot { + files: self.files.load(Ordering::Relaxed), + dirs: self.dirs.load(Ordering::Relaxed), + bytes: self.bytes.load(Ordering::Relaxed), + errors: self.errors.load(Ordering::Relaxed), + finished: self.finished.load(Ordering::Relaxed), + cancelled: self.is_cancelled(), + messages: lock(&self.messages).clone(), + } + } +} + +/// A scan running on its own thread. +#[derive(Debug)] +pub struct ScanHandle { + pub progress: Arc, + result: Receiver>, +} + +impl ScanHandle { + /// Start walking `root` on a worker thread. + pub fn spawn(root: PathBuf, options: ScanOptions) -> Self { + let progress = Arc::new(ScanProgress::default()); + let context = Arc::new(WalkContext { + options, + progress: Arc::clone(&progress), + root_device: Mutex::new(None), + visited_dirs: Mutex::new(FxHashSet::default()), + root: Mutex::new(None), + }); + let (sender, result) = mpsc::channel(); + + // The closure owns the sender, so a failed spawn drops it and the + // receiver reports the failure. Record the real reason alongside it. + let worker = thread::Builder::new().name("disktree-scan".into()).spawn( + move || { + let outcome = scan_blocking(&root, &context); + context.progress.finish(); + let _ = sender.send(outcome); + }, + ); + if let Err(error) = worker { + progress.record_error(Path::new("scan worker"), &error); + progress.finish(); + } + + Self { progress, result } + } + + /// Take the finished tree, if the walk is done. + pub fn poll(&self) -> Option> { + match self.result.try_recv() { + Ok(outcome) => Some(outcome), + Err(TryRecvError::Empty) => None, + Err(TryRecvError::Disconnected) => Some(Err(io::Error::other( + "the scan worker stopped without a result", + ))), + } + } + + /// Abandon the walk; the worker still finishes the directory it is in. + pub fn cancel(&self) { + self.progress.cancel(); + } +} + +/// Walk `root` and return the aggregated tree. Blocking. +pub fn scan(root: &Path, options: ScanOptions) -> io::Result { + let progress = Arc::new(ScanProgress::default()); + let context = Arc::new(WalkContext { + options, + progress: Arc::clone(&progress), + root_device: Mutex::new(None), + visited_dirs: Mutex::new(FxHashSet::default()), + root: Mutex::new(None), + }); + let node = scan_blocking(root, &context)?; + progress.finish(); + Ok(node) +} + +struct WalkContext { + options: ScanOptions, + progress: Arc, + /// Device of the root, resolved once, only needed for `one_filesystem`. + root_device: Mutex>, + /// Directories already entered, so followed symlinks cannot loop. + visited_dirs: Mutex>, + /// Set by the root's `complete`, read after the scope joins. + root: Mutex>, +} + +impl WalkContext { + fn root_device(&self, path: &Path) -> Option { + let mut cached = lock(&self.root_device); + if let Some(device) = *cached { + return Some(device); + } + let device = fs::metadata(path).map(|meta| device_of(&meta)).ok(); + *cached = device; + device + } + + fn cancelled(&self) -> bool { + self.progress.is_cancelled() + } + + /// Decide what to do with an entry, and account for the work it implies. + fn classify(&self, entry: &DirEntry) -> Classified { + let path = entry.path(); + let file_type = match entry.file_type() { + Ok(file_type) => file_type, + Err(error) => { + self.progress.record_error(&path, &error); + return Classified::Skipped; + } + }; + // Non-UTF-8 names are lossy for display. The scan still measures them + // correctly; only the reported name is approximate. + let name: Box = + entry.file_name().to_string_lossy().into_owned().into(); + + if !self.options.include_hidden && name.starts_with('.') { + return Classified::Skipped; + } + + if file_type.is_symlink() { + return self.classify_symlink(&path, name); + } + + if file_type.is_dir() { + 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)) + if device != root_device => + { + return Classified::Skipped; + } + (Err(error), _) => { + self.progress.record_error(&path, &error); + return Classified::Skipped; + } + _ => {} + } + } + self.progress.count_dir(); + return Classified::Subdirectory(path); + } + + match entry.metadata() { + Ok(meta) => self.leaf(name, kind_of(&meta, file_type), &meta), + Err(error) => { + self.progress.record_error(&path, &error); + Classified::Skipped + } + } + } + + fn classify_symlink(&self, path: &Path, name: Box) -> Classified { + if !self.options.follow_links { + // Not followed: the link occupies only its target string, which + // `du` reports as a handful of bytes or nothing at all. + return match fs::symlink_metadata(path) { + Ok(meta) => { + let size = measure(&meta, self.options.apparent_size); + self.progress.count_file(size); + Classified::Entry(leaf_node( + name, + NodeKind::Symlink, + size, + &meta, + )) + } + Err(error) => { + self.progress.record_error(path, &error); + Classified::Skipped + } + }; + } + + let meta = match fs::metadata(path) { + Ok(meta) => meta, + Err(error) => { + // A dangling link is normal, not an error worth counting. + if error.kind() != io::ErrorKind::NotFound { + self.progress.record_error(path, &error); + } + return Classified::Skipped; + } + }; + + if meta.is_dir() { + if let Some(key) = file_identity(&meta) + && !lock(&self.visited_dirs).insert(key) + { + return Classified::Skipped; + } + self.progress.count_dir(); + return Classified::Subdirectory(path.to_path_buf()); + } + + self.leaf(name, kind_of(&meta, meta.file_type()), &meta) + } + + fn leaf( + &self, + name: Box, + kind: NodeKind, + meta: &Metadata, + ) -> Classified { + let size = measure(meta, self.options.apparent_size); + self.progress.count_file(size); + Classified::Entry(leaf_node(name, kind, size, meta)) + } +} + +/// What a directory entry turned out to be. +enum Classified { + /// Descend into this directory on a new task. + Subdirectory(PathBuf), + /// A leaf that contributes size. + Entry(Node), + /// Filtered out, unreadable, or a symlink we chose not to follow. + Skipped, +} + +/// One directory being walked, plus the counter that decides when it is done. +#[derive(Debug)] +struct PendingDir { + path: PathBuf, + parent: Option>, + /// Starts at 1 for the directory itself; one more per subdirectory task. + /// When it reaches zero the directory is complete. + pending: AtomicUsize, + /// Files found directly here, and finished subdirectories handed back up. + children: Mutex>, + read_error: AtomicBool, + depth: usize, +} + +impl PendingDir { + const fn new( + path: PathBuf, + parent: Option>, + depth: usize, + ) -> Self { + Self { + path, + parent, + pending: AtomicUsize::new(1), + children: Mutex::new(Vec::new()), + read_error: AtomicBool::new(false), + depth, + } + } + + /// Turn a completed directory into a node. Only called when `pending` has + /// reached zero, so every child is already in `self.children`. + /// + /// `bytes` and `own_bytes` are left at zero on purpose: the walk cannot + /// know the aggregate, and [`crate::tree::aggregate`] derives both from the + /// children once every child is present. + fn build(&self) -> Node { + Node { + name: file_name(&self.path), + kind: NodeKind::Directory, + bytes: 0, + own_bytes: 0, + files: 0, + own_files: 0, + dirs: 1, + inode: None, + read_error: self.read_error.load(Ordering::Relaxed), + children: std::mem::take(&mut *lock(&self.children)), + } + } +} + +fn scan_blocking(root: &Path, context: &Arc) -> io::Result { + let root_meta = fs::metadata(root)?; + if !root_meta.is_dir() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("{} is not a directory", root.display()), + )); + } + if context.options.one_filesystem { + *lock(&context.root_device) = Some(device_of(&root_meta)); + } + if context.options.follow_links + && let Some(key) = file_identity(&root_meta) + { + lock(&context.visited_dirs).insert(key); + } + + let root_dir = Arc::new(PendingDir::new(root.to_path_buf(), None, 0)); + rayon::scope(|scope| walk(scope, &root_dir, context)); + + let node = lock(&context.root).take(); + let node = node.ok_or_else(|| { + io::Error::other(format!("{} produced no tree", root.display())) + })?; + Ok(finish_tree(node, &context.options)) +} + +/// Read one directory, spawn a task per subdirectory, then report completion. +/// +/// Flat tasks inside the scope: the worker stack never grows with tree depth. +fn walk(scope: &Scope<'_>, dir: &Arc, context: &Arc) { + let mut subdirs: Vec> = Vec::new(); + let mut leaves: Vec = Vec::new(); + + match fs::read_dir(&dir.path) { + Ok(entries) => { + for entry in entries { + if context.cancelled() { + break; + } + match entry { + Ok(entry) => match context.classify(&entry) { + Classified::Subdirectory(path) => { + let child = Arc::new(PendingDir::new( + path, + Some(Arc::clone(dir)), + dir.depth + 1, + )); + subdirs.push(child); + } + Classified::Entry(node) => leaves.push(node), + Classified::Skipped => {} + }, + Err(error) => { + context.progress.record_error(&dir.path, &error); + } + } + } + } + Err(error) => { + context.progress.record_error(&dir.path, &error); + dir.read_error.store(true, Ordering::Relaxed); + } + } + + // A depth-limited scan still measures what is directly in the directory, + // it just does not descend further. + let descend = context + .options + .max_depth + .is_none_or(|max_depth| dir.depth < max_depth); + if descend { + for subdir in subdirs { + if context.cancelled() { + break; + } + dir.pending.fetch_add(1, Ordering::AcqRel); + let subdir = Arc::clone(&subdir); + let context = Arc::clone(context); + scope.spawn(move |scope| walk(scope, &subdir, &context)); + } + } + lock(&dir.children).extend(leaves); + + signal_done(&Arc::clone(dir), context); +} + +/// Report that one task for `dir` is done: either its own scan, or one of its +/// children. The last one to report builds the node and bubbles up. +/// +/// This is the whole reason for the `+1` sentinel: a directory is only built +/// once its own scan *and* every subdirectory task has finished, no matter +/// which of them lands last. +fn signal_done(dir: &Arc, context: &Arc) { + if dir.pending.fetch_sub(1, Ordering::AcqRel) != 1 { + return; + } + let node = dir.build(); + let Some(parent) = dir.parent.clone() else { + *lock(&context.root) = Some(node); + return; + }; + lock(&parent.children).push(node); + signal_done(&parent, context); +} + +/// Charge a hardlinked file once, then derive every aggregate from the result. +/// +/// Zeroing `own_bytes` rather than `bytes` is deliberate: +/// [`crate::tree::aggregate`] recomputes totals from the direct contents, so a +/// patched `bytes` would be overwritten. +fn finish_tree(mut node: Node, options: &ScanOptions) -> Node { + if options.dedup_hardlinks { + let mut seen = FxHashSet::default(); + mark_duplicate_hardlinks(&mut node, &mut seen); + } + aggregate(&mut node, options.metric); + node +} + +fn mark_duplicate_hardlinks(node: &mut Node, seen: &mut FxHashSet<(u64, u64)>) { + if !node.is_dir() { + if node.inode.is_some_and(|key| !seen.insert(key)) { + node.own_bytes = 0; + } + return; + } + for child in &mut node.children { + mark_duplicate_hardlinks(child, seen); + } +} + +fn leaf_node( + name: Box, + kind: NodeKind, + size: u64, + meta: &Metadata, +) -> Node { + let mut node = Node::entry(name, kind, size); + node.inode = file_identity(meta); + node +} + +fn measure(meta: &Metadata, apparent_size: bool) -> u64 { + if apparent_size { + meta.len() + } else { + allocated_bytes(meta).unwrap_or(meta.len()) + } +} + +/// Allocated bytes, `st_blocks * 512`: sparse files spend less than they claim, +/// and this is the number that matches `du`. +/// +/// Returns `Option` because only Unix reports allocated blocks; elsewhere the +/// caller falls back to the apparent length. +#[allow( + clippy::unnecessary_wraps, + reason = "the Option is the non-Unix answer, where the caller falls back" +)] +#[cfg(unix)] +fn allocated_bytes(meta: &Metadata) -> Option { + use std::os::unix::fs::MetadataExt as _; + Some(meta.blocks().saturating_mul(512)) +} + +#[cfg(not(unix))] +fn allocated_bytes(_meta: &Metadata) -> Option { + None +} + +#[cfg(unix)] +fn device_of(meta: &Metadata) -> u64 { + use std::os::unix::fs::MetadataExt as _; + meta.dev() +} + +#[cfg(not(unix))] +fn device_of(_meta: &Metadata) -> u64 { + 0 +} + +/// `(device, inode)`, or `None` where the platform does not expose them. +/// Hardlink de-duplication and symlink loop detection both depend on this, so +/// they are simply unavailable rather than silently wrong elsewhere. +#[allow( + clippy::unnecessary_wraps, + reason = "the Option is the non-Unix answer, so both arms must agree" +)] +#[cfg(unix)] +fn file_identity(meta: &Metadata) -> Option<(u64, u64)> { + use std::os::unix::fs::MetadataExt as _; + Some((meta.dev(), meta.ino())) +} + +#[cfg(not(unix))] +fn file_identity(_meta: &Metadata) -> Option<(u64, u64)> { + None +} + +fn kind_of(meta: &Metadata, file_type: fs::FileType) -> NodeKind { + if meta.is_dir() || file_type.is_dir() { + NodeKind::Directory + } else if meta.is_file() || file_type.is_file() { + NodeKind::File + } else if file_type.is_symlink() { + NodeKind::Symlink + } else { + NodeKind::Other + } +} + +/// The display name of a scanned root: its final component, or the path itself +/// for `/`. +fn file_name(path: &Path) -> Box { + path.file_name() + .map_or_else( + || path.to_string_lossy().into_owned(), + |name| name.to_string_lossy().into_owned(), + ) + .into() +} + +/// A poisoned lock means another task panicked; the data is still a valid tree +/// prefix, and refusing to read it would turn one panic into two. +fn lock(mutex: &Mutex) -> MutexGuard<'_, T> { + mutex.lock().unwrap_or_else(PoisonError::into_inner) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::tree::Metric; + use std::fs; + use tempfile::TempDir; + + /// Apparent sizes, so the assertions are about the tree rather than about + /// how the filesystem rounds a small file up to a block. + fn options() -> ScanOptions { + ScanOptions { + apparent_size: true, + ..ScanOptions::default() + } + } + + fn write(root: &Path, relative: &str, bytes: usize) -> PathBuf { + let path = root.join(relative); + fs::create_dir_all(path.parent().expect("parent")).expect("mkdir"); + fs::write(&path, vec![b'x'; bytes]).expect("write"); + path + } + + fn scan_dir(root: &Path, options: &ScanOptions) -> Node { + scan(root, options.clone()).expect("scan") + } + + fn child<'a>(node: &'a Node, name: &str) -> &'a Node { + node.children + .iter() + .find(|child| &*child.name == name) + .unwrap_or_else(|| { + panic!("no child named {name} in {:?}", node.name) + }) + } + + #[test] + fn totals_are_summed_bottom_up() { + let temp = TempDir::new().expect("tempdir"); + let root = temp.path(); + write(root, "a/one.bin", 1000); + write(root, "a/nested/two.bin", 2000); + write(root, "b/three.bin", 500); + + let tree = scan_dir(root, &options()); + assert_eq!(tree.bytes, 3500); + assert_eq!(tree.files, 3); + assert_eq!(tree.dirs, 4, "root, a, a/nested, b"); + assert_eq!(child(&tree, "a").bytes, 3000); + assert_eq!(child(child(&tree, "a"), "nested").bytes, 2000); + assert_eq!(child(&tree, "b").bytes, 500); + } + + #[test] + fn children_are_ranked_largest_first() { + let temp = TempDir::new().expect("tempdir"); + let root = temp.path(); + write(root, "small.bin", 10); + write(root, "large.bin", 1000); + write(root, "medium.bin", 100); + + let tree = scan_dir(root, &options()); + let names: Vec<&str> = tree.children.iter().map(|c| &*c.name).collect(); + assert_eq!(names, vec!["large.bin", "medium.bin", "small.bin"]); + } + + #[test] + fn a_directory_reports_its_own_bytes_separately() { + let temp = TempDir::new().expect("tempdir"); + let root = temp.path(); + write(root, "direct.bin", 700); + write(root, "sub/deep.bin", 300); + + let tree = scan_dir(root, &options()); + assert_eq!(tree.own_bytes, 700); + assert_eq!(tree.own_files, 1); + assert_eq!(tree.bytes, 1000); + assert_eq!(tree.files, 2); + } + + #[test] + fn hardlinks_are_charged_once_by_default() { + let temp = TempDir::new().expect("tempdir"); + let root = temp.path(); + let original = write(root, "original.bin", 4096); + fs::hard_link(&original, root.join("link.bin")).expect("hard link"); + + let deduped = scan_dir(root, &options()); + assert_eq!(deduped.bytes, 4096); + assert_eq!(deduped.files, 2, "both names still exist"); + + let counted = scan_dir( + root, + &ScanOptions { + dedup_hardlinks: false, + ..options() + }, + ); + assert_eq!(counted.bytes, 8192); + } + + #[test] + fn hidden_entries_are_included_by_default_and_excluded_on_request() { + let temp = TempDir::new().expect("tempdir"); + let root = temp.path(); + write(root, ".cache/blob.bin", 900); + write(root, "visible.bin", 100); + + let default = scan_dir(root, &options()); + assert_eq!(default.bytes, 1000); + assert_eq!(child(&default, ".cache").bytes, 900); + + let without = scan_dir( + root, + &ScanOptions { + include_hidden: false, + ..options() + }, + ); + assert_eq!(without.bytes, 100); + } + + #[test] + fn symlinks_are_not_followed_by_default() { + let temp = TempDir::new().expect("tempdir"); + let outside = TempDir::new().expect("tempdir"); + write(outside.path(), "elsewhere.bin", 5000); + let root = temp.path(); + write(root, "real.bin", 100); + std::os::unix::fs::symlink(outside.path(), root.join("link")) + .expect("symlink"); + + let tree = scan_dir(root, &options()); + // Only the real file's bytes; the link itself holds just its target + // string, and its 5000-byte destination is outside this tree. + assert!(tree.bytes < 200, "{}", tree.bytes); + let link = child(&tree, "link"); + assert_eq!(link.kind, NodeKind::Symlink); + assert!(link.bytes < 100, "a link holds only its target string"); + } + + #[test] + fn followed_symlink_loops_do_not_hang_the_scan() { + let temp = TempDir::new().expect("tempdir"); + let root = temp.path(); + write(root, "sub/leaf.bin", 42); + std::os::unix::fs::symlink(root, root.join("sub/loop")) + .expect("symlink"); + + let tree = scan_dir( + root, + &ScanOptions { + follow_links: true, + ..options() + }, + ); + assert_eq!(tree.bytes, 42); + } + + #[test] + fn max_depth_stops_descending_but_keeps_direct_files() { + let temp = TempDir::new().expect("tempdir"); + let root = temp.path(); + write(root, "here.bin", 10); + write(root, "a/there.bin", 20); + write(root, "a/b/far.bin", 30); + + let tree = scan_dir( + root, + &ScanOptions { + max_depth: Some(1), + ..options() + }, + ); + assert_eq!(tree.own_bytes, 10); + let a = child(&tree, "a"); + assert_eq!( + a.bytes, 20, + "direct files at the cut-off depth still count" + ); + assert!( + a.children.iter().all(|child| !child.is_dir()), + "descending stopped at depth 1" + ); + assert_eq!(a.files, 1); + } + + #[test] + fn file_count_metric_ranks_by_entries() { + let temp = TempDir::new().expect("tempdir"); + let root = temp.path(); + for index in 0..5 { + write(root, &format!("many/f{index}.bin"), 1); + } + write(root, "one/huge.bin", 100_000); + + let tree = scan_dir(root, &options()); + assert_eq!(child(&tree, "one").bytes, 100_000); + assert_eq!(tree.children[0].name.as_ref(), "one"); + + let by_files = scan_dir( + root, + &ScanOptions { + metric: Metric::Files, + ..options() + }, + ); + assert_eq!(by_files.children[0].name.as_ref(), "many"); + assert_eq!(by_files.children[0].files, 5); + } + + #[test] + fn an_unreadable_directory_is_recorded_not_fatal() { + use std::os::unix::fs::PermissionsExt as _; + let temp = TempDir::new().expect("tempdir"); + let root = temp.path(); + write(root, "readable.bin", 11); + let locked = root.join("locked"); + fs::create_dir_all(&locked).expect("mkdir"); + write(&locked, "hidden.bin", 22); + fs::set_permissions(&locked, fs::Permissions::from_mode(0o000)) + .expect("chmod"); + + // Running as root would bypass the permission bits entirely. + let readable = fs::read_dir(&locked).map(|mut it| it.next().is_some()); + let progress = Arc::new(ScanProgress::default()); + let context = Arc::new(WalkContext { + options: options(), + progress: Arc::clone(&progress), + root_device: Mutex::new(None), + visited_dirs: Mutex::new(FxHashSet::default()), + root: Mutex::new(None), + }); + let tree = scan_blocking(root, &context).expect("scan still succeeds"); + let _ = fs::set_permissions(&locked, fs::Permissions::from_mode(0o700)); + + assert_eq!(tree.bytes, 11); + let node = child(&tree, "locked"); + if matches!(readable, Ok(false)) { + assert!(node.read_error); + assert!(progress.snapshot().errors >= 1); + assert!(!progress.snapshot().messages.is_empty()); + } + } + + #[test] + fn a_file_root_is_rejected() { + let temp = TempDir::new().expect("tempdir"); + let path = write(temp.path(), "file.bin", 1); + let error = scan(&path, options()).expect_err("a file is not a tree"); + assert_eq!(error.kind(), io::ErrorKind::InvalidInput); + } + + #[test] + fn a_spawned_scan_reports_progress_and_a_result() { + let temp = TempDir::new().expect("tempdir"); + let root = temp.path(); + write(root, "a/one.bin", 1024); + write(root, "b/two.bin", 2048); + + let handle = ScanHandle::spawn(root.to_path_buf(), options()); + let mut outcome = None; + for _ in 0..2000 { + if let Some(result) = handle.poll() { + outcome = Some(result); + break; + } + thread::sleep(std::time::Duration::from_millis(1)); + } + let tree = outcome.expect("the scan finished").expect("a tree"); + assert_eq!(tree.bytes, 3072); + + let snapshot = handle.progress.snapshot(); + assert!(snapshot.finished); + assert_eq!(snapshot.files, 2); + assert_eq!(snapshot.errors, 0); + } +} diff --git a/crates/disktree-core/src/size.rs b/crates/disktree-core/src/size.rs new file mode 100644 index 0000000..74f49c6 --- /dev/null +++ b/crates/disktree-core/src/size.rs @@ -0,0 +1,135 @@ +//! Human-readable sizes and counts. +//! +//! Binary units, like `du -h` and dust. One decimal below 10, none above 100: +//! enough resolution to compare neighbours without a wall of digits. + +const BYTE_UNITS: [&str; 6] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB"]; + +/// `1.4 GiB`, `523 MiB`, `12 B`. +pub fn human_bytes(bytes: u64) -> String { + scale(bytes).format(1) +} + +/// `1.4G`, `523M`, `12B`: for tile labels, where columns are scarce. +pub fn human_bytes_short(bytes: u64) -> String { + scale(bytes).format(0) +} + +fn scale(bytes: u64) -> Scaled { + let mut value = bytes as f64; + let mut unit = 0; + while value >= 1024.0 && unit + 1 < BYTE_UNITS.len() { + value /= 1024.0; + unit += 1; + } + Scaled { value, unit } +} + +struct Scaled { + value: f64, + unit: usize, +} + +impl Scaled { + fn format(&self, gap: usize) -> String { + // Whole bytes have no fraction to show: `0 B`, not `0.0 B`. + let text = if self.unit == 0 { + format!("{:.0}", self.value) + } else if self.value < 9.95 { + format!("{:.1}", self.value) + } else { + format!("{:.0}", self.value) + }; + if gap == 0 { + format!("{text}{}", BYTE_UNITS[self.unit]) + } else { + format!("{text} {}", BYTE_UNITS[self.unit]) + } + } +} + +/// `1.2k`, `3.4M`, `812`: file counts, which grow past a million quickly. +pub fn human_count(count: u64) -> String { + let value = count as f64; + if count < 10_000 { + format!("{count}") + } else if value < 1_000_000.0 { + format!("{:.1}k", value / 1_000.0) + } else if value < 1_000_000_000.0 { + format!("{:.1}M", value / 1_000_000.0) + } else { + format!("{:.1}G", value / 1_000_000_000.0) + } +} + +/// Share of `total` as a whole percentage, saturating instead of dividing by +/// zero. +pub fn share(part: u64, total: u64) -> f64 { + if total == 0 { + 0.0 + } else { + (part as f64 / total as f64) * 100.0 + } +} + +/// Compass width for a share, e.g. `▓▓▓░░`. +pub fn share_bar(part: u64, total: u64, width: usize) -> String { + let filled = if total == 0 { + 0 + } else { + ((part as f64 / total as f64) * width as f64).round() as usize + }; + let filled = filled.min(width); + let mut bar = String::with_capacity(width); + for index in 0..width { + bar.push(if index < filled { '▓' } else { '░' }); + } + bar +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn bytes_use_binary_units_with_one_decimal_below_ten() { + assert_eq!(human_bytes(0), "0 B"); + assert_eq!(human_bytes(7), "7 B"); + assert_eq!(human_bytes(12), "12 B"); + assert_eq!(human_bytes(1024), "1.0 KiB"); + assert_eq!(human_bytes(1536), "1.5 KiB"); + assert_eq!( + human_bytes(1024 * 1024 * 1024 + 400 * 1024 * 1024), + "1.4 GiB" + ); + assert_eq!(human_bytes(512 * 1024 * 1024), "512 MiB"); + } + + #[test] + fn short_form_drops_the_space() { + assert_eq!(human_bytes_short(1536), "1.5KiB"); + assert_eq!(human_bytes_short(512 * 1024), "512KiB"); + } + + #[test] + fn counts_switch_to_metric_suffixes() { + assert_eq!(human_count(0), "0"); + assert_eq!(human_count(9_999), "9999"); + assert_eq!(human_count(12_000), "12.0k"); + assert_eq!(human_count(2_500_000), "2.5M"); + } + + #[test] + fn share_survives_a_zero_total() { + assert!((share(5, 0) - 0.0).abs() < f64::EPSILON); + assert!((share(1, 4) - 25.0).abs() < f64::EPSILON); + } + + #[test] + fn share_bar_is_always_the_requested_width() { + assert_eq!(share_bar(1, 4, 4).chars().count(), 4); + assert_eq!(share_bar(4, 4, 4), "▓▓▓▓"); + assert_eq!(share_bar(0, 4, 4), "░░░░"); + assert_eq!(share_bar(1, 0, 3).chars().count(), 3); + } +} diff --git a/crates/disktree-core/src/space.rs b/crates/disktree-core/src/space.rs new file mode 100644 index 0000000..62ecb13 --- /dev/null +++ b/crates/disktree-core/src/space.rs @@ -0,0 +1,113 @@ +//! Free space on the volume a path lives on. +//! +//! This is what makes "amount of crap I found" a real number rather than an +//! estimate: the app measures the volume before and after a removal, and shows +//! the projection while marking. + +use std::io; +use std::path::Path; + +/// A volume's capacity in bytes. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct SpaceInfo { + /// Total size of the volume. + pub total: u64, + /// Free blocks, including the reserve only root may write to. + pub free: u64, + /// Free blocks this user may actually write; what `df -h` reports. + pub available: u64, +} + +impl SpaceInfo { + /// Space in use, computed from `free` rather than `available` so the figure + /// does not jump when a reserve is opened to root. + pub const fn used(&self) -> u64 { + self.total.saturating_sub(self.free) + } + + /// Share of the volume in use, `0.0..=1.0`. + pub fn used_fraction(&self) -> f32 { + if self.total == 0 { + 0.0 + } else { + (self.used() as f64 / self.total as f64) as f32 + } + } + + /// Free space after `bytes` are removed, saturating at the volume size and + /// never counting the same byte twice. + #[must_use] + pub fn after_removing(&self, bytes: u64) -> Self { + let available = self.available.saturating_add(bytes).min(self.total); + let free = self.free.saturating_add(bytes).min(self.total); + Self { + total: self.total, + free, + available, + } + } +} + +/// Read the space on the volume containing `path`. +pub fn space_info(path: &Path) -> io::Result { + let stat = rustix::fs::statvfs(path)?; + // `f_frsize` is the fragment size the block counts are expressed in; + // `f_bsize` is only a hint for I/O. Some filesystems report zero for + // `f_frsize`, so fall back rather than claiming a zero-sized volume. + let block = if stat.f_frsize == 0 { + stat.f_bsize + } else { + stat.f_frsize + }; + Ok(SpaceInfo { + total: stat.f_blocks.saturating_mul(block), + free: stat.f_bfree.saturating_mul(block), + available: stat.f_bavail.saturating_mul(block), + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_real_volume_reports_plausible_numbers() { + let temp = std::env::temp_dir(); + let space = space_info(&temp).expect("temp dir has a volume"); + assert!(space.total > 0, "{space:?}"); + assert!(space.free <= space.total, "{space:?}"); + assert!(space.available <= space.free, "{space:?}"); + assert!((0.0..=1.0).contains(&space.used_fraction()), "{space:?}"); + } + + #[test] + fn a_missing_path_reports_the_io_error() { + let error = space_info(Path::new("/definitely/not/here")) + .expect_err("no volume"); + assert_eq!(error.kind(), io::ErrorKind::NotFound); + } + + #[test] + fn projecting_removal_cannot_exceed_the_volume() { + let space = SpaceInfo { + total: 1000, + free: 100, + available: 100, + }; + let after = space.after_removing(50); + assert_eq!(after.available, 150); + assert_eq!(after.free, 150); + assert_eq!(after.total, 1000); + + let capped = space.after_removing(10_000); + assert_eq!(capped.available, 1000); + assert_eq!(capped.used(), 0); + assert!((capped.used_fraction() - 0.0).abs() < f32::EPSILON); + } + + #[test] + fn used_fraction_handles_an_empty_volume_report() { + let space = SpaceInfo::default(); + assert!((space.used_fraction() - 0.0).abs() < f32::EPSILON); + } +} diff --git a/crates/disktree-core/src/tree.rs b/crates/disktree-core/src/tree.rs new file mode 100644 index 0000000..5d3b612 --- /dev/null +++ b/crates/disktree-core/src/tree.rs @@ -0,0 +1,387 @@ +//! The scanned tree. + +use std::path::{Path, PathBuf}; + +/// What a node represents on disk. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum NodeKind { + Directory, + File, + Symlink, + /// Sockets, fifos and devices: addressable, but not space. + Other, +} + +impl NodeKind { + pub const fn is_dir(self) -> bool { + matches!(self, Self::Directory) + } +} + +/// How a node's importance is measured. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Metric { + /// Bytes, apparent or on-disk depending on [`crate::scan::ScanOptions`]. + #[default] + Bytes, + /// Number of files at or beneath the node. + Files, +} + +impl Metric { + pub const fn label(self) -> &'static str { + match self { + Self::Bytes => "size", + Self::Files => "files", + } + } + + #[must_use] + pub const fn toggled(self) -> Self { + match self { + Self::Bytes => Self::Files, + Self::Files => Self::Bytes, + } + } +} + +/// One entry in the scanned tree. +/// +/// Totals and direct figures are both kept: `own_bytes` and `own_files` are +/// what sits directly in a directory, `bytes` and `files` are the subtree +/// totals the treemap draws. The selection line needs both, and keeping them means +/// no second traversal when one of them is displayed. +#[derive(Clone, Debug)] +pub struct Node { + pub name: Box, + pub kind: NodeKind, + /// Subtree total: direct contents plus every descendant. + pub bytes: u64, + /// Bytes of the leaf entries directly in this directory, or this file's + /// own size. Derived by [`aggregate`]. + pub own_bytes: u64, + /// Files at or beneath this node; `1` for a file. + pub files: u64, + /// Files directly in this directory; `1` for a file. Derived by + /// [`aggregate`]. + pub own_files: u64, + /// Directories at or beneath this node; `1` for a directory. + pub dirs: u64, + /// `(device, inode)` for files, used to de-duplicate hardlinks. + pub inode: Option<(u64, u64)>, + /// The directory could not be read; its contents are unknown. + pub read_error: bool, + /// Children, ordered by [`Metric`] value, descending. + pub children: Vec, +} + +impl Node { + /// A directory with no children yet. + #[allow( + clippy::missing_const_for_fn, + reason = "`impl Into>` cannot be called in a const fn" + )] + pub fn directory(name: impl Into>) -> Self { + Self { + name: name.into(), + kind: NodeKind::Directory, + bytes: 0, + own_bytes: 0, + files: 0, + own_files: 0, + dirs: 1, + inode: None, + read_error: false, + children: Vec::new(), + } + } + + /// A leaf entry. + pub fn entry( + name: impl Into>, + kind: NodeKind, + bytes: u64, + ) -> Self { + Self { + name: name.into(), + kind, + bytes, + own_bytes: bytes, + files: u64::from(kind == NodeKind::File), + own_files: u64::from(kind == NodeKind::File), + dirs: 0, + inode: None, + read_error: false, + children: Vec::new(), + } + } + + pub const fn is_dir(&self) -> bool { + self.kind.is_dir() + } + + /// The value a treemap should weight this node by. + pub const fn value(&self, metric: Metric) -> u64 { + match metric { + Metric::Bytes => self.bytes, + Metric::Files => self.files, + } + } + + pub fn child(&self, index: usize) -> Option<&Self> { + self.children.get(index) + } + + /// Follow `crumbs` from this node. Crumbs are child indices, so they stay + /// valid across re-sorting only for the tree they were produced from. + pub fn resolve(&self, crumbs: &[usize]) -> Option<&Self> { + let mut node = self; + for &index in crumbs { + node = node.children.get(index)?; + } + Some(node) + } + + /// The chain of nodes ending at `crumbs`, including this node. + pub fn resolve_chain<'a>(&'a self, crumbs: &[usize]) -> Vec<&'a Self> { + let mut chain = vec![self]; + let mut node = self; + for &index in crumbs { + match node.children.get(index) { + Some(child) => { + chain.push(child); + node = child; + } + None => break, + } + } + chain + } + + /// Index of the largest child, used to pick a useful descent target. + pub const fn largest_child(&self) -> Option { + if self.children.is_empty() { + None + } else { + Some(0) + } + } + + /// Depth of the deepest descendant. + pub fn depth(&self) -> u32 { + self.children + .iter() + .map(Self::depth) + .max() + .map_or(0, |deepest| deepest + 1) + } + + /// Breadth-first search for the first node whose name contains `needle` + /// (case-insensitive), returning its crumbs and the node. + pub fn find(&self, needle: &str) -> Option<(Vec, &Self)> { + let needle = needle.to_lowercase(); + if needle.is_empty() { + return None; + } + let mut queue = vec![(Vec::new(), self)]; + while let Some((crumbs, node)) = queue.pop() { + for (index, child) in node.children.iter().enumerate() { + if child.name.to_lowercase().contains(&needle) { + let found: Vec = crumbs + .iter() + .copied() + .chain(std::iter::once(index)) + .collect(); + return Some((found, child)); + } + if child.is_dir() && !child.children.is_empty() { + let mut next = crumbs.clone(); + next.push(index); + queue.push((next, child)); + } + } + } + None + } +} + +/// Recompute `bytes`, `files`, `dirs` and the direct totals bottom-up, then +/// order children by `metric`, largest first. +/// +/// `bytes` and `files` are the subtree totals; `own_bytes` and `own_files` are +/// the direct contents, derived from the leaf children rather than tracked +/// separately. Deriving them is what keeps the two consistent: hardlink +/// de-duplication rewrites a leaf's weight, and every total above it — +/// including its parent's "direct" figure — follows without a second pass. +pub fn aggregate(node: &mut Node, metric: Metric) { + if !node.is_dir() { + node.bytes = node.own_bytes; + node.files = node.own_files; + node.dirs = 0; + return; + } + + let mut bytes = 0; + let mut files = 0; + let mut own_bytes = 0; + let mut own_files = 0; + let mut dirs: u64 = 1; + for child in &mut node.children { + aggregate(child, metric); + bytes += child.bytes; + files += child.files; + dirs += child.dirs; + if !child.is_dir() { + own_bytes += child.bytes; + own_files += child.files; + } + } + node.bytes = bytes; + node.files = files; + node.own_bytes = own_bytes; + node.own_files = own_files; + node.dirs = dirs; + + // Largest first: a treemap lays out big tiles best, and the order is what + // makes "descend into the largest child" meaningful. + node.children.sort_by(|left, right| { + right + .value(metric) + .cmp(&left.value(metric)) + .then_with(|| left.name.cmp(&right.name)) + }); +} + +/// Absolute path of the node at `crumbs` beneath a scanned root. +pub fn path_of(root_path: &Path, root: &Node, crumbs: &[usize]) -> PathBuf { + let mut path = root_path.to_path_buf(); + let mut node = root; + for &index in crumbs { + match node.children.get(index) { + Some(child) => { + path.push(&*child.name); + node = child; + } + None => break, + } + } + path +} + +#[cfg(test)] +mod tests { + use super::*; + + fn leaf(name: &str, bytes: u64) -> Node { + Node::entry(name, NodeKind::File, bytes) + } + + #[test] + fn aggregate_derives_totals_and_orders_children() { + let mut root = Node::directory("root"); + let mut nested = Node::directory("child"); + nested.children.push(leaf("deep", 7)); + root.children.push(nested); + root.children.push(leaf("direct", 5)); + root.children.push(leaf("small", 9)); + + aggregate(&mut root, Metric::Bytes); + assert_eq!(root.own_bytes, 5 + 9, "direct leaves only"); + assert_eq!(root.bytes, 5 + 9 + 7); + assert_eq!(root.files, 3); + assert_eq!(root.own_files, 2); + assert_eq!(root.dirs, 2); + assert_eq!(child_named(&root, "child").own_bytes, 7); + assert_eq!(&*root.children[0].name, "small", "9 bytes, largest first"); + assert_eq!(&*root.children[2].name, "direct"); + + // A file's own size is what it weighs; aggregates never invent more. + let mut single = leaf("solo", 3); + aggregate(&mut single, Metric::Bytes); + assert_eq!(single.bytes, 3); + assert_eq!(single.own_bytes, 3); + assert_eq!(single.files, 1); + assert_eq!(single.dirs, 0); + } + + fn child_named<'a>(node: &'a Node, name: &str) -> &'a Node { + node.children + .iter() + .find(|child| &*child.name == name) + .unwrap_or_else(|| panic!("no child named {name}")) + } + + #[test] + fn aggregate_can_rank_by_file_count() { + let mut root = Node::directory("root"); + let mut many = Node::directory("many"); + for index in 0..5 { + many.children.push(leaf(&format!("f{index}"), 1)); + } + root.children.push(many); + root.children.push(leaf("huge", 10_000)); + + aggregate(&mut root, Metric::Bytes); + assert_eq!(&*root.children[0].name, "huge"); + aggregate(&mut root, Metric::Files); + assert_eq!(&*root.children[0].name, "many"); + assert_eq!(root.children[0].files, 5); + } + + #[test] + fn resolve_walks_child_indices() { + let mut root = Node::directory("root"); + let mut nested = Node::directory("child"); + nested.children.push(leaf("deep", 1)); + root.children.push(nested); + + assert!(root.resolve(&[]).is_some()); + assert_eq!(root.resolve(&[0, 0]).map(|n| &*n.name), Some("deep")); + assert!(root.resolve(&[0, 1]).is_none()); + assert_eq!(root.resolve_chain(&[0, 0]).len(), 3); + } + + #[test] + fn path_of_joins_names_beneath_the_root() { + let mut root = Node::directory("root"); + let mut nested = Node::directory("child"); + nested.children.push(leaf("deep", 1)); + root.children.push(nested); + + let path = path_of(Path::new("/home/tobi"), &root, &[0, 0]); + assert_eq!(path, PathBuf::from("/home/tobi/child/deep")); + } + + #[test] + fn largest_child_is_the_first_child_because_children_are_sorted() { + let mut root = Node::directory("root"); + root.children.push(leaf("big", 10)); + root.children.push(leaf("small", 1)); + assert_eq!(root.largest_child(), Some(0)); + assert_eq!(Node::directory("empty").largest_child(), None); + } + + #[test] + fn find_reports_crumbs_and_is_case_insensitive() { + let mut root = Node::directory("root"); + let mut target = Node::directory("target"); + target.children.push(leaf("needle-file", 1)); + root.children.push(target); + + let (crumbs, found) = root.find("NEEDLE").expect("found"); + assert_eq!(crumbs, vec![0, 0]); + assert_eq!(&*found.name, "needle-file"); + assert!(root.find("").is_none()); + assert!(root.find("absent").is_none()); + } + + #[test] + fn depth_counts_edges() { + let mut root = Node::directory("root"); + let mut nested = Node::directory("child"); + nested.children.push(leaf("deep", 1)); + root.children.push(nested); + assert_eq!(root.depth(), 2); + assert_eq!(leaf("x", 0).depth(), 0); + } +} diff --git a/crates/disktree-core/src/treemap.rs b/crates/disktree-core/src/treemap.rs new file mode 100644 index 0000000..c5dc2d7 --- /dev/null +++ b/crates/disktree-core/src/treemap.rs @@ -0,0 +1,617 @@ +//! Squarified treemap layout. +//! +//! Bruls, Huizing and van Wijk's squarified algorithm: grow a row of tiles +//! while the worst aspect ratio inside it keeps improving, then start a new row +//! in the remaining space. That is what produces the legible mosaic of +//! KDirStat-style explorers instead of the slivers a naive slice-and-dice +//! gives. +//! +//! Layout runs in pixels, in the coordinate space of an unzoomed viewport. The +//! view applies its own pan and zoom when painting, so re-layout is only needed +//! when the tree, the viewport size, or the requested depth changes. + +use crate::tree::{Metric, Node}; + +/// An axis-aligned rectangle in viewport pixels. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Rect { + pub x: f32, + pub y: f32, + pub w: f32, + pub h: f32, +} + +impl Rect { + pub const fn new(x: f32, y: f32, w: f32, h: f32) -> Self { + Self { x, y, w, h } + } + + pub fn right(&self) -> f32 { + self.x + self.w + } + + pub fn bottom(&self) -> f32 { + self.y + self.h + } + + pub fn area(&self) -> f32 { + (self.w.max(0.0)) * (self.h.max(0.0)) + } + + pub fn contains(&self, x: f32, y: f32) -> bool { + x >= self.x && x < self.right() && y >= self.y && y < self.bottom() + } + + /// Shrink on every side, never past empty. + #[must_use] + pub fn inset(&self, padding: f32) -> Self { + let w = padding.mul_add(-2.0, self.w).max(0.0); + let h = padding.mul_add(-2.0, self.h).max(0.0); + Self::new(self.x + padding, self.y + padding, w, h) + } + + #[must_use] + pub fn scaled(&self, scale: f32) -> Self { + Self::new( + self.x * scale, + self.y * scale, + self.w * scale, + self.h * scale, + ) + } + + #[must_use] + pub fn translated(&self, dx: f32, dy: f32) -> Self { + Self::new(self.x + dx, self.y + dy, self.w, self.h) + } +} + +/// What a tile stands for. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum TileKind { + /// A real node at `crumbs`, which are child indices from the scanned root. + Node { crumbs: Vec }, + /// The tail of a long child list, merged so its area is still accounted + /// for instead of silently dropped. + Others { crumbs: Vec, count: usize }, +} + +/// One rectangle of the mosaic. +#[derive(Clone, Debug)] +pub struct Tile { + pub kind: TileKind, + pub rect: Rect, + /// Nesting level: 0 tiles are children of the current root. + pub depth: u32, + /// The band this directory reserved for its own name, when it is + /// subdivided. Its children are laid out below it, so a parent's name + /// never sits on top of a child — and the band is part of the parent for + /// hit-testing, which makes a parent reachable without aiming at its + /// border. + pub header: Option, +} + +impl Tile { + pub fn crumbs(&self) -> &[usize] { + match &self.kind { + TileKind::Node { crumbs } | TileKind::Others { crumbs, .. } => { + crumbs + } + } + } +} + +/// How much of the tree to draw, and how finely. +#[derive(Clone, Debug, PartialEq)] +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. + pub padding: 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. + pub header: f32, +} + +impl Default for LayoutOptions { + fn default() -> Self { + Self { + max_depth: 3, + padding: 2.0, + min_tile: 5.0, + max_children: 96, + header: 17.0, + } + } +} + +/// Lay out everything beneath `root` inside `area`. +/// +/// `root_crumbs` is where `root` sits in the scanned tree. Every tile's +/// crumbs extend it, so they always address the scanned tree, never the node +/// being drawn: a caller that resolves a tile from the scanned root gets that +/// tile, at any depth. +pub fn layout( + root: &Node, + root_crumbs: &[usize], + area: Rect, + metric: Metric, + options: &LayoutOptions, +) -> Vec { + let mut tiles = Vec::new(); + let mut crumbs = root_crumbs.to_vec(); + place_children(root, area, metric, options, 0, &mut crumbs, &mut tiles); + tiles +} + +fn place_children( + node: &Node, + area: Rect, + metric: Metric, + options: &LayoutOptions, + depth: u32, + crumbs: &mut Vec, + out: &mut Vec, +) { + if node.children.is_empty() || area.w <= 0.0 || area.h <= 0.0 { + return; + } + + // Rank by importance ourselves: the tree is already sorted, but a metric + // switch or a hand-built tree must not produce a bad layout. + let mut ranked: Vec<(usize, f64)> = node + .children + .iter() + .enumerate() + .map(|(index, child)| (index, child.value(metric) as f64)) + .filter(|(_, value)| *value > 0.0) + .collect(); + if ranked.is_empty() { + return; + } + ranked.sort_by(|left, right| right.1.total_cmp(&left.1)); + + let kept = ranked.len().min(options.max_children); + let mut values: Vec = + ranked[..kept].iter().map(|(_, value)| *value).collect(); + let mut sources: Vec> = ranked[..kept] + .iter() + .map(|(index, _)| Some(*index)) + .collect(); + let tail_count = ranked.len() - kept; + if tail_count > 0 { + values.push(ranked[kept..].iter().map(|(_, value)| *value).sum()); + sources.push(None); + } + + for (slot, raw) in squarify(&values, area).iter().enumerate() { + let rect = raw.inset(options.padding); + if rect.w < options.min_tile || rect.h < options.min_tile { + continue; + } + let Some(index) = sources[slot] else { + out.push(Tile { + kind: TileKind::Others { + crumbs: crumbs.clone(), + count: tail_count, + }, + rect, + depth, + header: None, + }); + continue; + }; + + let child = &node.children[index]; + let subdividable = child.is_dir() && depth + 1 < options.max_depth; + // A directory that is about to be subdivided claims a header band for + // its own name. When there is no room for one it stays whole: a name + // drawn over its own children is worse than one level less of detail, + // and zooming in gives it the room back. + let header = subdividable + .then(|| header_band(rect, options, depth)) + .flatten(); + crumbs.push(index); + out.push(Tile { + kind: TileKind::Node { + crumbs: crumbs.clone(), + }, + rect, + depth, + header, + }); + if let Some(header) = header { + let body = Rect::new( + rect.x, + header.bottom(), + rect.w, + rect.bottom() - header.bottom(), + ); + place_children( + child, + body, + metric, + options, + depth + 1, + crumbs, + out, + ); + } + crumbs.pop(); + } +} + +/// 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. +fn header_band( + rect: Rect, + options: &LayoutOptions, + depth: u32, +) -> Option { + let height = (depth as f32) + .mul_add(-1.5, options.header) + .max(options.header * 0.6); + let body = rect.h - height; + if rect.w < 44.0 || body < options.min_tile * 3.0 { + return None; + } + Some(Rect::new(rect.x, rect.y, rect.w, height)) +} + +/// Split `area` into one rectangle per value, proportional to it. +/// +/// Returned in the order of `values`, not in layout order. +pub fn squarify(values: &[f64], area: Rect) -> Vec { + let mut rects = vec![Rect::default(); values.len()]; + let total: f64 = values.iter().filter(|value| **value > 0.0).sum(); + if total <= 0.0 || area.w <= 0.0 || area.h <= 0.0 { + return rects; + } + + let mut order: Vec = + (0..values.len()).filter(|i| values[*i] > 0.0).collect(); + order.sort_by(|left, right| values[*right].total_cmp(&values[*left])); + + let scale = f64::from(area.w) * f64::from(area.h) / total; + let areas: Vec = + order.iter().map(|index| values[*index] * scale).collect(); + + let mut free = area; + let mut start = 0; + while start < areas.len() { + let side = f64::from(free.w.min(free.h)); + let mut end = start + 1; + let mut row_sum = areas[start]; + let mut row_worst = worst_ratio(&areas[start..end], row_sum, side); + + while end < areas.len() { + let candidate_sum = row_sum + areas[end]; + let candidate_worst = + worst_ratio(&areas[start..=end], candidate_sum, side); + if candidate_worst > row_worst { + break; + } + row_sum = candidate_sum; + row_worst = candidate_worst; + end += 1; + } + + if free.w >= free.h { + // A vertical strip on the left; tiles stack top to bottom. + let strip_w = (row_sum / f64::from(free.h)) as f32; + let strip_w = strip_w.min(free.w); + let mut y = free.y; + for index in start..end { + let height = if strip_w > 0.0 { + (areas[index] / f64::from(strip_w)) as f32 + } else { + 0.0 + }; + let height = height.min(free.bottom() - y).max(0.0); + rects[order[index]] = Rect::new(free.x, y, strip_w, height); + y += height; + } + free.x += strip_w; + free.w -= strip_w; + } else { + // A horizontal strip along the top; tiles run left to right. + let strip_h = (row_sum / f64::from(free.w)) as f32; + let strip_h = strip_h.min(free.h); + let mut x = free.x; + for index in start..end { + let width = if strip_h > 0.0 { + (areas[index] / f64::from(strip_h)) as f32 + } else { + 0.0 + }; + let width = width.min(free.right() - x).max(0.0); + rects[order[index]] = Rect::new(x, free.y, width, strip_h); + x += width; + } + free.y += strip_h; + free.h -= strip_h; + } + start = end; + } + rects +} + +/// Worst (largest) aspect ratio in a row of `areas` laid along `side`. +fn worst_ratio(areas: &[f64], row_sum: f64, side: f64) -> f64 { + if row_sum <= 0.0 || side <= 0.0 { + return f64::INFINITY; + } + let thickness = row_sum / side; + areas.iter().fold(0.0_f64, |worst, area| { + if *area <= 0.0 || thickness <= 0.0 { + return worst; + } + let other = area / thickness; + let ratio = (thickness / other).max(other / thickness); + worst.max(ratio) + }) +} + +/// The deepest tile containing a point. +/// +/// Children are emitted after their parent and are inset inside it, so the last +/// match in reverse order is the most specific one. +pub fn hit(tiles: &[Tile], x: f32, y: f32) -> Option<&Tile> { + tiles.iter().rev().find(|tile| tile.rect.contains(x, y)) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::tree::NodeKind; + + fn file(name: &str, bytes: u64) -> Node { + Node::entry(name, NodeKind::File, bytes) + } + + /// A directory whose direct contents are the given children, so aggregate + /// totals are consistent before layout runs. + fn dir(name: &str, children: Vec) -> Node { + let mut node = Node::directory(name); + node.children = children; + crate::tree::aggregate(&mut node, Metric::Bytes); + node + } + + fn area() -> Rect { + Rect::new(0.0, 0.0, 800.0, 500.0) + } + + #[test] + fn squarify_fills_the_area() { + let values = [40.0, 30.0, 20.0, 5.0, 3.0, 2.0]; + let rects = squarify(&values, area()); + let covered: f32 = rects.iter().map(Rect::area).sum(); + assert!( + (covered - area().area()).abs() < 1.0, + "covered {covered} of {}", + area().area() + ); + for rect in &rects { + assert!(rect.x >= -0.01 && rect.y >= -0.01); + assert!(rect.right() <= area().right() + 0.01); + assert!(rect.bottom() <= area().bottom() + 0.01); + } + } + + #[test] + fn squarify_keeps_aspect_ratios_reasonable() { + // Same distribution as the canonical squarify example: 6, 6, 4, 3, 2, 2, + // 1 on a 6x4 canvas. + let values = [6.0, 6.0, 4.0, 3.0, 2.0, 2.0, 1.0]; + let rects = squarify(&values, Rect::new(0.0, 0.0, 600.0, 400.0)); + for rect in &rects { + let ratio = (rect.w / rect.h).max(rect.h / rect.w); + assert!(ratio <= 4.0, "aspect ratio {ratio} in {rect:?}"); + } + } + + #[test] + fn squarify_survives_degenerate_input() { + assert_eq!(squarify(&[], area()).len(), 0); + assert!( + squarify(&[0.0, 0.0], area()) + .iter() + .all(|r| r.area().abs() < f32::EPSILON) + ); + assert!( + squarify(&[1.0], Rect::new(0.0, 0.0, 0.0, 10.0))[0] + .area() + .abs() + < f32::EPSILON + ); + } + + #[test] + fn layout_nests_children_inside_their_parent() { + // Depth first, parent before its own children: that order is what lets + // the view paint in sequence and hit-test in reverse. + let root = dir( + "root", + vec![ + dir("big", vec![file("inside", 100), file("also", 50)]), + file("small", 10), + ], + ); + + let tiles = layout( + &root, + &[], + area(), + Metric::Bytes, + &LayoutOptions::default(), + ); + let crumbs: Vec<&[usize]> = tiles.iter().map(Tile::crumbs).collect(); + assert_eq!(crumbs, vec![&[0][..], &[0, 0], &[0, 1], &[1]]); + + let parent = &tiles[0]; + let child = &tiles[1]; + assert_eq!(child.depth, 1); + assert!(parent.rect.contains(child.rect.x, child.rect.y)); + assert!( + parent + .rect + .contains(child.rect.right() - 0.5, child.rect.bottom() - 0.5) + ); + } + + #[test] + fn layout_depth_one_stops_at_direct_children() { + let root = dir("root", vec![dir("big", vec![file("inside", 100)])]); + + let options = LayoutOptions { + max_depth: 1, + ..LayoutOptions::default() + }; + let tiles = layout(&root, &[], area(), Metric::Bytes, &options); + assert_eq!(tiles.len(), 1); + assert_eq!(tiles[0].crumbs(), &[0]); + } + + #[test] + fn a_subdivided_directory_keeps_a_header_for_its_own_name() { + let root = dir( + "root", + vec![dir("big", vec![file("inside", 100), file("also", 50)])], + ); + let tiles = layout( + &root, + &[], + area(), + Metric::Bytes, + &LayoutOptions::default(), + ); + + let parent = tiles + .iter() + .find(|tile| tile.crumbs() == [0]) + .expect("the parent tile"); + let header = parent.header.expect("a parent keeps a header"); + assert!(header.w > 0.0 && header.h > 0.0); + assert!( + (header.y - parent.rect.y).abs() < f32::EPSILON, + "the band sits at the top of its tile" + ); + assert!(header.bottom() <= parent.rect.bottom()); + + // Every descendant starts below the band, so no child is drawn under + // the parent's name. + for tile in tiles.iter().filter(|tile| tile.crumbs().len() > 1) { + assert!( + tile.rect.y >= header.bottom() - f32::EPSILON, + "{:?} overlaps the header", + tile.crumbs() + ); + } + + // And the band belongs to the parent for hit-testing. + let centre = (header.x + header.w / 2.0, header.y + header.h / 2.0); + let hit_tile = hit(&tiles, centre.0, centre.1).expect("a hit"); + assert_eq!(hit_tile.crumbs(), &[0]); + } + + #[test] + fn a_tile_without_room_for_a_header_stays_whole() { + let root = dir("root", vec![dir("big", vec![file("inside", 100)])]); + let options = LayoutOptions { + // A band this tall leaves no usable body under it. + header: 400.0, + min_tile: 150.0, + ..LayoutOptions::default() + }; + let tiles = layout(&root, &[], area(), Metric::Bytes, &options); + assert_eq!(tiles.len(), 1, "the child was not subdivided"); + assert!(tiles[0].header.is_none(), "and it has no band of its own"); + } + + #[test] + fn tile_crumbs_address_the_scanned_tree_not_the_drawn_node() { + // Draw the node at [3, 1] of some larger tree: its tiles must be + // [3, 1, …], or anything resolving them from the scanned root would + // find a stranger. + let node = dir("inner", vec![file("a", 10), file("b", 5)]); + let tiles = layout( + &node, + &[3, 1], + area(), + Metric::Bytes, + &LayoutOptions::default(), + ); + assert!(!tiles.is_empty()); + for tile in &tiles { + assert!(tile.crumbs().starts_with(&[3, 1]), "{:?}", tile.crumbs()); + assert_eq!(tile.crumbs().len(), 3); + } + } + + #[test] + fn a_leaf_never_claims_a_header() { + let root = dir("root", vec![file("solo", 10)]); + let tiles = layout( + &root, + &[], + area(), + Metric::Bytes, + &LayoutOptions::default(), + ); + assert!(tiles.iter().all(|tile| tile.header.is_none())); + } + + #[test] + fn layout_merges_the_child_tail_into_one_tile() { + let children: Vec = (0..10) + .map(|index| file(&format!("f{index}"), 10 - index)) + .collect(); + let root = dir("root", children); + let options = LayoutOptions { + max_children: 4, + ..LayoutOptions::default() + }; + let tiles = layout(&root, &[], area(), Metric::Bytes, &options); + let others: Vec<&Tile> = tiles + .iter() + .filter(|tile| matches!(tile.kind, TileKind::Others { .. })) + .collect(); + assert_eq!(others.len(), 1); + assert!(matches!(others[0].kind, TileKind::Others { count: 6, .. })); + } + + #[test] + fn hit_returns_the_deepest_tile() { + let root = dir( + "root", + vec![dir("big", vec![file("inside", 100)]), file("small", 1)], + ); + + let tiles = layout( + &root, + &[], + area(), + Metric::Bytes, + &LayoutOptions::default(), + ); + let child = tiles + .iter() + .find(|tile| tile.crumbs() == [0, 0]) + .expect("child tile"); + let centre = ( + child.rect.x + child.rect.w / 2.0, + child.rect.y + child.rect.h / 2.0, + ); + let found = hit(&tiles, centre.0, centre.1).expect("a hit"); + assert_eq!(found.crumbs(), &[0, 0]); + assert!(hit(&tiles, -50.0, -50.0).is_none()); + } +}