Skip to content

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

Rust

fix(space): exclude foreign-volume marks from free-space projection

Mohammed Munzirdfd1fdc649a8
diff --git a/AGENTS.md b/AGENTS.md
index 0dec4c7..2312ad7 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -95,7 +95,11 @@ and `cargo build --release` directly; CI runs the gate on both systems.
8. **The view transform is the only thing zoom changes.** Layout runs in
base-space pixels and is cached; `screen = (base - origin) * scale`.
9. **The status bar never claims a saving it cannot measure.** Projections come
- from marked bytes; the final number comes from `statvfs` before and after.
+ from marked bytes *on the volume the meter measures*, which is the scan's own
+ notion of a volume — the mount *source* — so a mark on a disk mounted inside
+ the tree, which `-X` and the media exception allow, is reported and never
+ added to this volume's free space; see `space::attribution`. The final number
+ comes from `statvfs` before and after.
## Where changes belong
diff --git a/README.md b/README.md
index 0ca0d18..09579f6 100644
--- a/README.md
+++ b/README.md
@@ -253,6 +253,14 @@ escaped in the prompt, so it cannot pass for another path.
- **Hardlinks once.** Two names for one inode cost one file.
- **Hidden entries included**, because `~/.cache` is often the biggest thing in
a home directory. Symlinks are not followed.
+- **The projection counts one volume.** The meter shows the scanned volume's
+ free space, so it adds up only the marks whose bytes come back to that
+ volume, by the same rule the scan bounds the volume with. With `-X`, a mark
+ on a disk mounted inside the tree, on a network share or in a snapshot
+ subvolume is left out of the projection — removing it gives its space back
+ somewhere else, or, in a snapshot, shares its blocks with the live files —
+ and the panel and the review screen say what was left out. A mark whose
+ volume cannot be told is not claimed either.
The scan follows [dust](https://github.com/bootandy/dust)'s approach: one rayon
scope per root, a completion counter per directory so no directory is built
@@ -289,7 +297,8 @@ device number: btrfs gives each subvolume its own `st_dev`, so `/home`,
points are left out (checked by path, so an automounted NAS is never
mounted just to be measured). Snapshot subvolumes are left out too: their
files share blocks with the live ones, and counting them would count the disk
-twice. `-X` crosses into everything.
+twice. `-X` crosses into everything: a mark that lands on another volume is
+still removed, but it is not added to the metered volume's free space.
Without root, some system directories cannot be read; they are counted as
unreadable in the top bar rather than guessed at.
diff --git a/crates/disktree-app/src/views.rs b/crates/disktree-app/src/views.rs
index b3b4b61..ec08e08 100644
--- a/crates/disktree-app/src/views.rs
+++ b/crates/disktree-app/src/views.rs
@@ -6,7 +6,7 @@
use disktree_core::classify::Category;
use disktree_core::insights::{Candidate, Finding, STALE_DAYS};
-use disktree_core::removal::{RemovalMode, Target};
+use disktree_core::removal::{Plan, RemovalMode, Target};
use disktree_core::size::human_bytes;
use disktree_core::tree::Metric;
use gpui_kit::base::CheckboxState;
@@ -1836,7 +1836,10 @@ fn disk_section(
.child("Free space is not available here"),
);
};
- let reclaiming = app.plan().bytes();
+ let plan = app.plan();
+ // The meter measures this volume, so it projects only the marks whose
+ // bytes come back to it; the rest is said, never added.
+ let reclaiming = plan.reclaim();
let after = space_info.after_removing(reclaiming);
let highlight = palette::highlight(theme);
let (number, unit) =
@@ -1907,6 +1910,14 @@ fn disk_section(
),
),
);
+ if let Some(left_out) = not_counted(&plan) {
+ section = section.child(
+ div()
+ .text_size(text::CAPTION)
+ .text_color(theme.secondary)
+ .child(format!("not counted here: {left_out}")),
+ );
+ }
section
.child(
div()
@@ -1924,6 +1935,24 @@ fn disk_section(
)
}
+/// What the volume meter leaves out, and why, when it leaves anything out:
+/// bytes marked on another volume come back there, and bytes no volume can
+/// be found for are not claimed at all. `None` when every marked byte is
+/// this volume's, which is the usual case.
+fn not_counted(plan: &Plan) -> Option<String> {
+ let mut parts: Vec<String> = Vec::new();
+ if plan.foreign() > 0 {
+ parts.push(format!("{} on other volumes", human_bytes(plan.foreign())));
+ }
+ if plan.unattributed() > 0 {
+ parts.push(format!(
+ "{} could not be placed",
+ human_bytes(plan.unattributed())
+ ));
+ }
+ (!parts.is_empty()).then(|| parts.join(", "))
+}
+
/// The way to the review screen, where the saving is: it says what is
/// marked and what it frees, so the number that matters is the thing to
/// press. Outlined in the highlight, so it does not compete with the filled
@@ -2475,7 +2504,13 @@ fn review_summary(
window: &mut Window,
cx: &mut Context<'_, Disktree>,
) -> Div {
- let reclaiming = plan.bytes();
+ // What comes back to the volume on screen, which is all its meter can
+ // project; the totals below say what the removal does in full.
+ let reclaiming = plan.reclaim();
+ // The part of the saving that lands elsewhere, said rather than added to
+ // this volume's projection, so the two numbers cannot be confused.
+ let left_out = not_counted(plan)
+ .map(|left_out| widgets::row("Not counted here", left_out, cx));
let trash = app.trash_backend.is_available();
// One silhouette for one either-or choice, reversible option first.
@@ -2563,9 +2598,10 @@ fn review_summary(
))
.child(widgets::row(
"Space freed",
- human_bytes(reclaiming),
+ human_bytes(plan.bytes()),
cx,
- )),
+ ))
+ .children(left_out),
);
if let Some(volume) = app.space {
diff --git a/crates/disktree-core/src/removal.rs b/crates/disktree-core/src/removal.rs
index 4c6361c..6027752 100644
--- a/crates/disktree-core/src/removal.rs
+++ b/crates/disktree-core/src/removal.rs
@@ -78,6 +78,10 @@ pub struct Plan {
pub blocked: Vec<Blocked>,
/// The scanned root the targets were judged against.
pub root: PathBuf,
+ /// Bytes the targets free on `root`'s volume; see [`Self::reclaim`].
+ reclaim: u64,
+ /// Bytes the targets free on other volumes; see [`Self::foreign`].
+ foreign: u64,
}
impl Plan {
@@ -85,6 +89,29 @@ impl Plan {
self.targets.iter().map(|target| target.bytes).sum()
}
+ /// Bytes the targets free on the volume `root` is on: the only ones the
+ /// free space meter may add to that volume's projection.
+ ///
+ /// [`Self::bytes`] counts every target, which is what a delete list
+ /// wants. A mark on another volume — `-X` crosses filesystems, and a
+ /// disk mounted under the root can be marked — gives its space back
+ /// there, so projecting it here would lend one disk another's bytes.
+ pub const fn reclaim(&self) -> u64 {
+ self.reclaim
+ }
+
+ /// Bytes the targets free on other volumes, which the meter leaves out.
+ pub const fn foreign(&self) -> u64 {
+ self.foreign
+ }
+
+ /// Bytes whose volume could not be told: no saving is claimed for them
+ /// either, since a projection the meter cannot stand behind is worse
+ /// than a smaller one.
+ pub fn unattributed(&self) -> u64 {
+ self.bytes().saturating_sub(self.reclaim + self.foreign)
+ }
+
pub const fn is_empty(&self) -> bool {
self.targets.is_empty()
}
@@ -95,6 +122,15 @@ impl Plan {
/// 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 {
+ plan_against(targets, root, &mount_points())
+}
+
+/// [`plan`] against a given read of the mount table, for testing.
+///
+/// One read answers both what may be removed and which volume each target's
+/// bytes belong to, so the projection cannot be built from a state of the
+/// machine the guards never saw.
+fn plan_against(targets: &[Target], root: &Path, mounts: &MountTable) -> Plan {
let root = normalize(root);
let mut plan = Plan {
root: root.clone(),
@@ -105,7 +141,6 @@ pub fn plan(targets: &[Target], root: &Path) -> Plan {
if targets.is_empty() {
return plan;
}
- let mounts = mount_points();
// Once per plan, not per target: the review screen plans every frame.
let home = std::env::home_dir().map(|home| Home::of(&home));
let real_root = fs::canonicalize(&root).ok();
@@ -145,6 +180,21 @@ pub fn plan(targets: &[Target], root: &Path) -> Plan {
}
}
plan.targets = outer;
+
+ // Which volume each target's bytes come back to. Only what comes back to
+ // the scanned volume may be shown against its free space.
+ for target in &plan.targets {
+ let bytes = target.bytes;
+ match crate::space::attribution(
+ mounts.mounts.as_deref(),
+ &plan.root,
+ &target.path,
+ ) {
+ crate::space::Attribution::Scanned => plan.reclaim += bytes,
+ crate::space::Attribution::Other => plan.foreign += bytes,
+ crate::space::Attribution::Unknown => {}
+ }
+ }
plan
}
@@ -544,19 +594,28 @@ const MOUNTS_FRESH: std::time::Duration = std::time::Duration::from_secs(2);
struct MountTable {
points: Vec<PathBuf>,
media: Vec<PathBuf>,
+ /// The table itself where the platform keeps one, which tells the
+ /// scanned volume from another for the meter's projection. `None` where
+ /// there is none to read, and the platform's own device test answers.
+ mounts: Option<Vec<crate::space::Mount>>,
}
fn read_mount_table() -> MountTable {
+ // The same table the scan reads for its volume, so a projection is
+ // judged by the rule the tree itself was built under.
+ let mounts = crate::space::mount_table();
if let Ok(table) = fs::read_to_string("/proc/self/mountinfo") {
let views = crate::space::parse_mountinfo(&table);
return MountTable {
points: views.iter().map(|mount| guard_key(&mount.point)).collect(),
media: media_roots(&views),
+ mounts,
};
}
MountTable {
points: read_mount_points(),
media: Vec::new(),
+ mounts,
}
}
@@ -1708,6 +1767,70 @@ mod tests {
assert_eq!(plan.bytes(), 30);
}
+ /// Marks may sit on another volume — `-X` crosses filesystems, and a
+ /// disk mounted under the root can be marked — but the meter measures
+ /// the scanned volume, so only the marks whose bytes come back to it may
+ /// be projected onto its free space.
+ #[test]
+ fn only_the_scanned_volumes_marks_are_projected() {
+ let table = "\
+/dev/mapper/root / btrfs rw,subvol=/@ 0 0
+/dev/mapper/root /home btrfs rw,subvol=/@home 0 0
+/dev/mapper/root /.snapshots btrfs rw,subvol=/@snapshots 0 0
+/dev/mapper/data /data ext4 rw 0 0
+";
+ let mounts = MountTable {
+ points: crate::space::parse_mounts(table)
+ .iter()
+ .map(|mount| guard_key(&mount.point))
+ .collect(),
+ media: Vec::new(),
+ mounts: Some(crate::space::parse_mounts(table)),
+ };
+ let marked = [
+ target(Path::new("/home/tobi/junk"), 100),
+ target(Path::new("/data/junk"), 200),
+ target(Path::new("/.snapshots/7/junk"), 400),
+ ];
+ let plan = plan_against(&marked, Path::new("/"), &mounts);
+ assert!(
+ plan.blocked.is_empty(),
+ "a mark inside another volume is legal: {:?}",
+ plan.blocked
+ );
+ assert_eq!(plan.bytes(), 700);
+ assert_eq!(plan.reclaim(), 100, "the Btrfs subvolume is this disk");
+ assert_eq!(plan.foreign(), 600, "another disk, and a snapshot");
+ assert_eq!(plan.unattributed(), 0);
+
+ // A mark inside another is removed with it, so its bytes are counted
+ // once here as they are in the total.
+ let nested = [
+ target(Path::new("/home/tobi/junk"), 100),
+ target(Path::new("/home/tobi/junk/deeper"), 60),
+ ];
+ let plan = plan_against(&nested, Path::new("/"), &mounts);
+ assert_eq!(plan.covered.len(), 1);
+ assert_eq!(plan.bytes(), 100);
+ assert_eq!(plan.reclaim(), 100);
+
+ // Where the table cannot place the paths, nothing is claimed: a
+ // projection the meter cannot stand behind is worse than none.
+ let partial = MountTable {
+ mounts: Some(crate::space::parse_mounts(
+ "/dev/mapper/data /data ext4 rw 0 0\n",
+ )),
+ ..MountTable::default()
+ };
+ let root = Path::new("/disktree-untabled");
+ let plan =
+ plan_against(&[target(&root.join("junk"), 50)], root, &partial);
+ assert_eq!(plan.bytes(), 50);
+ assert_eq!(plan.reclaim(), 0);
+ assert_eq!(plan.foreign(), 0);
+ assert_eq!(plan.unattributed(), 50, "no saving may be claimed");
+ }
+
#[test]
fn the_root_and_home_are_refused() {
let temp = tree();
diff --git a/crates/disktree-core/src/space.rs b/crates/disktree-core/src/space.rs
index 010daeb..e4abf77 100644
--- a/crates/disktree-core/src/space.rs
+++ b/crates/disktree-core/src/space.rs
@@ -179,6 +179,170 @@ fn is_snapshot(mount: &Mount) -> bool {
})
}
+/// The mount table `/proc/self/mounts` holds, where the platform keeps one.
+///
+/// `None` on macOS and Windows, which have no such table and tell a path's
+/// volume from the path itself, and wherever `/proc` cannot be read. Read
+/// once by whoever asks per frame: [`attribution`] takes it, because the
+/// review screen plans on every frame.
+#[cfg(not(any(target_os = "macos", windows)))]
+pub fn mount_table() -> Option<Vec<Mount>> {
+ std::fs::read_to_string("/proc/self/mounts")
+ .ok()
+ .map(|table| parse_mounts(&table))
+}
+
+/// [`mount_table`] where there is no `/proc/self/mounts` to read.
+#[cfg(any(target_os = "macos", windows))]
+pub const fn mount_table() -> Option<Vec<Mount>> {
+ None
+}
+
+/// Which volume's free space a removal gives back, as far as the meter can
+/// tell.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum Attribution {
+ /// The scanned volume: what is removed here is what the meter, which
+ /// measures this volume, sees come back.
+ Scanned,
+ /// Another volume. The space comes back there, so projecting it here
+ /// would lend one disk another's bytes.
+ Other,
+ /// Nothing says which: no saving is claimed.
+ Unknown,
+}
+
+/// Which volume's free space removing `path` gives back, judged against the
+/// volume `root` is on.
+///
+/// The scan's own rule decides ([`foreign_mounts`]): a volume is the mount
+/// *source* and type, not a device number, so `/home` on its own Btrfs
+/// subvolume is part of the `/` a user scans, while another disk, a tmpfs, a
+/// network share and a snapshot subvolume are not. Marks are not bounded by
+/// that rule — `-X` crosses filesystems, and a disk mounted under the root
+/// can be marked — so the meter, which measures one volume, has to ask.
+///
+/// `mounts` is a read of [`mount_table`], kept by the caller because this is
+/// asked for every frame; `None` falls back to what the platform itself
+/// calls the disk a path is on, the test the scan falls back to when the
+/// table cannot be read.
+pub fn attribution(
+ mounts: Option<&[Mount]>,
+ root: &Path,
+ path: &Path,
+) -> Attribution {
+ match mounts {
+ Some(mounts) => attribution_in(mounts, root, path),
+ None => attributed_by_platform(root, path),
+ }
+}
+
+/// [`attribution`] over a given mount table.
+fn attribution_in(mounts: &[Mount], root: &Path, path: &Path) -> Attribution {
+ let (Some(own), Some(other)) =
+ (covering(mounts, root), covering(mounts, path))
+ else {
+ // A path no mount point covers, as one reached through a spelling
+ // the table does not carry: there is nothing to attribute it to.
+ return Attribution::Unknown;
+ };
+ if same_volume(own, other) {
+ Attribution::Scanned
+ } else {
+ Attribution::Other
+ }
+}
+
+/// The mount a path is shown by: the longest of the points above it.
+fn covering<'a>(mounts: &'a [Mount], path: &Path) -> Option<&'a Mount> {
+ mounts
+ .iter()
+ .filter(|mount| path.starts_with(&mount.point))
+ .max_by_key(|mount| mount.point.as_os_str().len())
+}
+
+/// Whether two mounts are one volume, as the scan counts one: one mount, or
+/// two mounts of the same source — a Btrfs subvolume beside its filesystem,
+/// a bind mount of the same disk, the same disk mounted twice.
+///
+/// A snapshot subvolume is not its filesystem even though it shares the
+/// source: its files share their blocks with the live ones, so removing them
+/// gives back almost nothing, and the scan keeps out of it for the same
+/// reason. The mount the scan itself asked for is kept at all events, since
+/// removing something there empties the very space the meter reads.
+fn same_volume(own: &Mount, other: &Mount) -> bool {
+ if own.point == other.point {
+ return true;
+ }
+ own.source == other.source
+ && own.fstype == other.fstype
+ && !is_snapshot(own)
+ && !is_snapshot(other)
+}
+
+/// [`attribution`] with no table to go by: what the platform itself says
+/// about the path, which is what the scan compares in the same straits.
+///
+/// The device number is all Unix has without `/proc/self/mounts`: an equal
+/// number is certainly the same filesystem, and a different one may be
+/// another disk or another Btrfs subvolume of this one, which only the table
+/// could tell apart, so nothing is claimed for it.
+#[cfg(all(unix, not(target_os = "macos")))]
+fn attributed_by_platform(root: &Path, path: &Path) -> Attribution {
+ use std::os::unix::fs::MetadataExt as _;
+ let (Ok(own), Ok(other)) =
+ (std::fs::metadata(root), std::fs::metadata(path))
+ else {
+ return Attribution::Unknown;
+ };
+ if own.dev() == other.dev() {
+ Attribution::Scanned
+ } else {
+ Attribution::Unknown
+ }
+}
+
+/// [`attribution`] with no table to go by on macOS: the disk a path is
+/// mounted on, with the Data volume's mount folded into `/` by
+/// [`volume_root_for`], so `/Users` is the `/` the user thinks of.
+#[cfg(target_os = "macos")]
+fn attributed_by_platform(root: &Path, path: &Path) -> Attribution {
+ compared(root, path, volume_root_for)
+}
+
+/// [`attribution`] with no table to go by on Windows: the volume a path is
+/// mounted on, folded the way Windows folds spellings, so `C:\` and `c:\`.
+#[cfg(windows)]
+fn attributed_by_platform(root: &Path, path: &Path) -> Attribution {
+ compared(root, path, |path| {
+ crate::windows::volume_root(path)
+ .map(|root| crate::windows::guard_key(&root))
+ })
+}
+
+/// Two platform-named volume roots: one root is one volume.
+#[cfg(any(target_os = "macos", windows))]
+fn compared(
+ root: &Path,
+ path: &Path,
+ volume: impl Fn(&Path) -> Option<PathBuf>,
+) -> Attribution {
+ let (Some(own), Some(other)) = (volume(root), volume(path)) else {
+ return Attribution::Unknown;
+ };
+ if own == other {
+ Attribution::Scanned
+ } else {
+ Attribution::Other
+ }
+}
+
+/// There is no way to tell one volume from another here.
+#[cfg(not(any(unix, windows)))]
+const fn attributed_by_platform(_root: &Path, _path: &Path) -> Attribution {
+ Attribution::Unknown
+}
+
/// 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`
@@ -920,6 +1084,110 @@ portal /run/user/1000/doc fuse.portal rw 0 0
assert!(foreign_mounts(&mounts, Path::new("/home/tobi")).is_empty());
}
+ #[test]
+ fn a_volume_keeps_its_subvolumes_and_loses_everything_else() {
+ let mounts = parse_mounts(OMARCHY);
+ let there = |path: &str| {
+ attribution(Some(&mounts), Path::new("/"), Path::new(path))
+ };
+ assert_eq!(there("/home/tobi/cache"), Attribution::Scanned);
+ assert_eq!(there("/var/log/journal"), Attribution::Scanned);
+ assert_eq!(there("/boot/vmlinuz"), Attribution::Other);
+ assert_eq!(there("/mnt/nas-home/x"), Attribution::Other);
+ assert_eq!(there("/tmp/x"), Attribution::Other);
+ assert_eq!(
+ there("/.snapshots/12/x"),
+ Attribution::Other,
+ "snapshot blocks are shared with the live files"
+ );
+ // Asking for the snapshot itself measures the snapshot, so its own
+ // files are the space that report goes up by.
+ assert_eq!(
+ attribution(
+ Some(&mounts),
+ Path::new("/.snapshots"),
+ Path::new("/.snapshots/12/x")
+ ),
+ Attribution::Scanned
+ );
+ // The same disk under a home of its own is one volume throughout,
+ // and the root disk beside it is not.
+ let separate = parse_mounts(
+ "/dev/sda1 / ext4 rw 0 0\n/dev/sdb1 /home ext4 rw 0 0\n",
+ );
+ assert_eq!(
+ attribution(
+ Some(&separate),
+ Path::new("/home/tobi"),
+ Path::new("/home/tobi/x")
+ ),
+ Attribution::Scanned
+ );
+ assert_eq!(
+ attribution(
+ Some(&separate),
+ Path::new("/home/tobi"),
+ Path::new("/var/tmp/x")
+ ),
+ Attribution::Other
+ );
+ }
+
+ #[test]
+ fn a_table_that_does_not_place_a_path_claims_nothing() {
+ let partial = parse_mounts("/dev/sdb1 /data ext4 rw 0 0\n");
+ assert_eq!(
+ attribution(
+ Some(&partial),
+ Path::new("/srv/scan"),
+ Path::new("/srv/scan/x")
+ ),
+ Attribution::Unknown,
+ "the root is not in the table"
+ );
+ assert_eq!(
+ attribution(
+ Some(&partial),
+ Path::new("/data/scan"),
+ Path::new("/srv/scan/x")
+ ),
+ Attribution::Unknown,
+ "the marked path is not in it either"
+ );
+ assert_eq!(
+ attribution(Some(&[]), Path::new("/"), Path::new("/x")),
+ Attribution::Unknown,
+ "an empty table places nothing"
+ );
+ }
+
+ #[test]
+ fn without_a_table_the_same_place_is_one_volume() {
+ let temp = tempfile::TempDir::new().expect("tempdir");
+ let marked = temp.path().join("marked");
+ std::fs::write(&marked, b"x").expect("write");
+ assert_eq!(
+ attribution(None, temp.path(), &marked),
+ Attribution::Scanned,
+ "one place cannot be two volumes"
+ );
+ }
+
+ #[test]
+ #[cfg(not(any(target_os = "macos", windows)))]
+ fn the_machine_table_places_a_path_on_this_volume() {
+ let Some(mounts) = mount_table() else {
+ return;
+ };
+ let temp = std::env::temp_dir();
+ assert_eq!(
+ attribution(Some(&mounts), &temp, &temp),
+ Attribution::Scanned,
+ "{:?}",
+ mounts.iter().map(|mount| &mount.point).collect::<Vec<_>>()
+ );
+ }
+
#[test]
fn the_device_is_the_longest_matching_mount() {
let table = "\