core: refuse a directory that holds the home directory

Removing a directory takes what is inside it. In a whole-disk scan on
Windows, C:\Users is an ordinary directory that passes every other
guard, and so is /home on a Linux install without a separate volume:
marking either would remove the home directory the guards are there to
protect.
This commit is contained in:
Oleg Shevchuk
2026-09-25 15:09:09 +05:00
parent f3741c1a51
commit cac3d76c3f
3 changed files with 32 additions and 4 deletions
+2 -1
View File
@@ -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
+3 -1
View File
@@ -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
+27 -2
View File
@@ -242,8 +242,15 @@ fn refuse(path: &Path, root: &Path, home: Option<&Path>) -> Option<String> {
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();