diff --git a/AGENTS.md b/AGENTS.md index 30a5414..3dd73b0 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -63,7 +63,8 @@ done, and it must not fix anything: a red local run is the same signal CI gives. has finished.** That is the `+1` sentinel in `PendingDir::pending`. Building early silently drops whole subtrees — it has happened once. 4. **Only paths under the scanned root may be removed**, and mount points, the - root, the home directory and symlink targets are refused. + root, the home directory, any directory holding it, and symlink targets are + refused. 5. **Marks are keyed by absolute path**, not tree position, so they survive a re-scan; `Marks::refresh` re-reads their sizes and drops what is gone. 6. **The treemap is painted, not composed of elements.** Thousands of diff --git a/README.md b/README.md index e1c1580..5c2b37c 100644 --- a/README.md +++ b/README.md @@ -190,7 +190,9 @@ The removal rules live in `crates/disktree-core/src/removal.rs`, and each one is tested: - only paths under the scanned root can be removed; -- the filesystem root, the scanned root and your home directory are refused; +- the filesystem root, the scanned root and your home directory are refused, + and so is any directory holding your home directory, which would take it + along; - a mount point is refused, since removing it would reach into another filesystem; - system trees (`/usr`, `/etc`, `/boot`, `/var/lib`, `/nix/store`, …) are diff --git a/crates/disktree-core/src/removal.rs b/crates/disktree-core/src/removal.rs index 08b4b58..f2c2bb2 100644 --- a/crates/disktree-core/src/removal.rs +++ b/crates/disktree-core/src/removal.rs @@ -242,8 +242,15 @@ fn refuse(path: &Path, root: &Path, home: Option<&Path>) -> Option { if same(path, root) { return Some("the scanned root cannot be removed".into()); } - if home.is_some_and(|home| same(path, &normalize(home))) { - return Some("the home directory cannot be removed".into()); + if let Some(home) = home.map(normalize) { + if same(path, &home) { + return Some("the home directory cannot be removed".into()); + } + // A directory goes with everything in it, so `/home` or `C:\Users` + // would take the home directory along. + if within(&home, path) { + return Some("it contains the home directory".into()); + } } if !within(path, root) { return Some("outside the scanned root".into()); @@ -783,6 +790,24 @@ mod tests { ); } + /// Removing a directory takes what is inside it, so the directories + /// above home are refused too: in a whole-disk scan, `C:\Users` is an + /// ordinary directory, and so is `/home` without a separate volume. + #[test] + fn a_directory_holding_the_home_directory_is_refused() { + let home = Path::new("/home/tobi"); + let reason = refuse(Path::new("/home"), Path::new("/"), Some(home)); + assert!( + reason.is_some_and(|reason| reason.contains("home directory")), + "the home directory is inside it" + ); + assert_eq!( + refuse(Path::new("/home/other"), Path::new("/"), Some(home)), + None, + "a sibling of home is not" + ); + } + #[test] fn paths_outside_the_root_are_refused() { let temp = tree();