Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
sudo apt-get install -y --no-install-recommends \
pkg-config libxkbcommon-dev libxkbcommon-x11-dev libwayland-dev \
libx11-xcb-dev libxcb1-dev libfontconfig-dev libfreetype-dev \
libvulkan-dev libasound2-dev libssl-dev libzstd-dev
libvulkan-dev libasound2-dev libssl-dev libzstd-dev btrfs-progs

# Pinned: the lint gate denies clippy::all and ::pedantic, and a newer
# clippy's new lints are a change to make on purpose, not by surprise.
Expand All @@ -48,6 +48,22 @@ jobs:
- name: Test
run: cargo xtask test

# Extent sharing cannot be tested on the runner's ordinary filesystem.
# Only this disposable image is formatted; no host device is touched.
- name: Btrfs integration
run: |
btrfs_image="$RUNNER_TEMP/disktree-btrfs.img"
btrfs_fixture="$RUNNER_TEMP/disktree-btrfs"
truncate -s 512M "$btrfs_image"
mkfs.btrfs -f "$btrfs_image"
mkdir -p "$btrfs_fixture"
sudo mount -o loop,compress=zstd "$btrfs_image" "$btrfs_fixture"
trap 'sudo umount "$btrfs_fixture"' EXIT
sudo chown "$(id -u):$(id -g)" "$btrfs_fixture"
export DISKTREE_BTRFS_TEST_DIR="$btrfs_fixture"
cargo test -p disktree-core btrfs
cargo test -p disktree-app btrfs_review

check-macos:
name: Lint and test (macOS)
# Apple silicon. No system libraries to install, and no Metal toolchain
Expand Down
9 changes: 7 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ and `cargo build --release` directly; CI runs the gate on both systems.

## Invariants

1. **Sizes come from `st_blocks * 512` unless apparent size was asked for.**
That is the number that comes back when a file is deleted. On macOS the
1. **Sizes use allocated bytes unless apparent size was asked for.**
Local Btrfs scans deduplicate extents in `btrfs.rs`; compressed lengths
and measurement fallbacks are explicitly estimated. Other Unix scans
use `st_blocks * 512`. Neither number guarantees reclaimed space:
Btrfs keeps a separate conservative estimate in `Node::reclaimable`.
Shared extents, hardlinks, compressed and unknown data are excluded.
Trash projects no immediate saving. On macOS the
directory listing reports it and `st_size` for a whole buffer of entries:
every fork's allocation, and the data fork's length; see `macos.rs`. On
Windows it is the allocation the directory listing reports; see
Expand Down
48 changes: 45 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,10 @@ escaped in the prompt, so it cannot pass for another path.

## What it measures

- **Disk usage** by default: `st_blocks × 512`, the number `du` reports and the
space that actually comes back when a file is deleted. Apparent size (what
`ls -l` shows) is one toggle away.
- **Disk usage** by default: allocated bytes, with extent-aware accounting
for local Btrfs scans (see below). Other Unix filesystems use
`st_blocks × 512`, like `du`. Allocation is not a guarantee of space freed
by deletion. Apparent size (`ls -l`) is one toggle away.
- **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.
Expand All @@ -419,6 +420,47 @@ scope per root, a completion counter per directory so no directory is built
before its last subdirectory lands, and one bottom-up pass that aggregates sizes
and removes duplicate hardlinks.

## Btrfs accounting on Linux

Local Btrfs scans inspect file extents without root privileges. Reflinked
copies count once across the scanned tree, including partial overlaps and
copies in different subvolumes of the same filesystem. Shared data is
assigned to the first path in name order, so a duplicate tile can be zero;
that is its contribution to this scan, not the file's apparent length.
Scanning that directory alone can therefore give a larger contribution.
The **Apparent size** toggle still shows file lengths.

**Size and reclaimable space are different measurements.** The removal
preview uses a conservative estimate of exclusive, uncompressed data, not
selected tile sizes. It excludes shared extents (including references in
snapshots or outside the scan), hardlinked files, compressed data and files
that cannot be measured. Selecting every visible copy does not establish
that no other reference exists: shared data remains excluded. A zero
estimate means no exclusive data was verified, not that deletion can never
free space. Trash shows no immediate saving; emptying it is a separate step.

The kernel's unprivileged FIEMAP interface does not report compressed
physical lengths. Compressed extents are counted once by their extent
address using a **logical-size estimate**, visibly labelled in the scan
summary. Inline or unsupported mappings, inaccessible files and files
changed during measurement retain their ordinary size with the same
estimate label. These totals are not exact physical disk usage and can
still exceed the volume's used space. The volume meter always uses
`statvfs`; metadata, replicas, snapshots and other files also affect it.

Accounting is a cancellable second pass after indexing. Btrfs scans are
remeasured when widening the root; extent addresses are never cached across
scans. Estimates describe scan-time data and can change after writes or
new snapshots, so rescan before acting on old results. Remote SSH scans
currently retain their existing allocated-byte accounting.

To exercise real reflinks on a writable Btrfs filesystem (temporary files
only, no mounts or elevated access):

```sh
DISKTREE_BTRFS_TEST_DIR=/path/on/btrfs cargo test -p disktree-core btrfs
```

## Choosing sources and roots

Launch without a path to choose **This computer** or enter an SSH alias /
Expand Down
6 changes: 5 additions & 1 deletion crates/disktree-app/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2210,7 +2210,11 @@ impl Disktree {
// this computer's mounts or project an unmeasured saving.
return selection;
}
plan(self.marks.items(), &self.root_path)
let mut result = plan(self.marks.items(), &self.root_path);
if let Some(tree) = self.tree() {
result.account_storage(tree, self.removal_mode);
}
result
}

// ── layout and hit-testing ──────────────────────────────────────────
Expand Down
44 changes: 44 additions & 0 deletions crates/disktree-app/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2357,3 +2357,47 @@ fn the_power_menu_offers_only_what_the_cpus_can_tell_apart(
draw(cx);
assert!(read(&view, cx, |app| app.power_menu.is_none()));
}

/// The same real-Btrfs opt-in as the core fixtures, rendered through the UI.
#[gpui_kit::test]
#[cfg(target_os = "linux")]
fn btrfs_review_uses_reclaim_estimates_in_apparent_mode(
cx: &mut TestAppContext,
) {
let Some(parent) = std::env::var_os("DISKTREE_BTRFS_TEST_DIR") else {
return;
};
cx.update(gpui_omarchy::init);
let temp = tempfile::tempdir_in(parent).unwrap();
let first = temp.path().join("first");
std::fs::write(&first, vec![b'x'; 1024 * 1024]).unwrap();
std::fs::File::open(&first).unwrap().sync_all().unwrap();
assert!(
std::process::Command::new("cp")
.arg("--reflink=always")
.arg(&first)
.arg(temp.path().join("second"))
.status()
.unwrap()
.success()
);
let (view, cx) = view_over(temp.path(), cx);
update(&view, cx, |app, cx| {
let crumbs = app.crumbs_for_path(&first).unwrap();
app.select(Some(crumbs), cx);
app.toggle_mark_selected(cx);
app.removal_mode = RemovalMode::Permanent;
assert_eq!(app.plan().bytes(), 1024 * 1024);
assert_eq!(app.plan().reclaim(), 0);
assert!(app.plan().btrfs_estimate);
app.screen = Screen::Review;
});
draw(cx);
update(&view, cx, |app, _| {
app.removal_mode = RemovalMode::Trash;
assert_eq!(app.plan().reclaim(), 0);
assert!(app.plan().deferred_to_trash);
});
draw(cx);
assert!(first.exists(), "preview never deletes the selection");
}
51 changes: 49 additions & 2 deletions crates/disktree-app/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,18 @@ fn scan_totals(app: &Disktree, theme: &Theme) -> Div {
.text_size(text::CAPTION)
.text_color(theme.secondary)
.child(div().text_color(theme.foreground).child(human_bytes(bytes)))
.when(
tree.is_some_and(|node| node.reclaimable().is_some()),
|this| {
this.child(if tree.is_some_and(|node| node.size_estimated) {
"· Btrfs estimated size"
} else if app.options.apparent_size {
"· apparent size"
} else {
"· Btrfs data (shared blocks counted once)"
})
},
)
.child(format!(
"· {} files · {} dirs",
widgets::human_count(files),
Expand Down Expand Up @@ -1584,7 +1596,25 @@ fn selection_section(
text::TITLE,
cx,
))
.child(widgets::bar(share, highlight, cx));
.child(widgets::bar(share, highlight, cx))
.children(node.reclaimable().map(|bytes| {
widgets::row(
"Conservative reclaim estimate",
human_bytes(bytes),
cx,
)
}))
.when(node.reclaimable().is_some(), |this| {
this.child(
div()
.text_size(text::CAPTION)
.text_color(theme.secondary)
.child(
"Size is this path’s contribution to the scan. \
Reclaim excludes shared, compressed and unknown data.",
),
)
});

let fourth = if app.remote.is_none()
&& node.is_dir()
Expand Down Expand Up @@ -2293,6 +2323,16 @@ fn disk_section(
/// this volume's, which is the usual case.
fn not_counted(plan: &Plan) -> Option<String> {
let mut parts: Vec<String> = Vec::new();
if plan.btrfs_estimate {
parts.push(
"Conservative Btrfs estimate: shared, compressed and \
unknown data excluded; rescan after changes"
.into(),
);
}
if plan.deferred_to_trash {
parts.push("Space is released only after the trash is emptied".into());
}
if plan.foreign() > 0 {
parts.push(format!("{} on other volumes", human_bytes(plan.foreign())));
}
Expand Down Expand Up @@ -2584,6 +2624,8 @@ fn indexing_progress(
.child(div().h(size::PROGRESS_ROW).truncate().child(
if app.remote.is_some() {
app.remote_progress.stage.label()
} else if app.progress.accounting {
"Measuring shared disk space (sizes are provisional)"
} else {
"Reading this computer"
},
Expand Down Expand Up @@ -3042,10 +3084,15 @@ fn review_summary(
cx,
))
.child(widgets::row(
"Space freed",
"Selected size",
human_bytes(plan.bytes()),
cx,
))
.child(widgets::row(
"Estimated freed here",
human_bytes(plan.reclaim()),
cx,
))
.children(left_out),
);

Expand Down
Loading