Repository navigation
Rescan a single folder and move items to the Trash - #25
Conversation
"Rescan Folder" in the sidebar's context menu scans just that folder in the background and splices the result into the tree, so cleaning up a folder no longer means rescanning the whole drive. The row shows a spinner while it runs. The folder keeps its inode, so expanded folders stay expanded. AppState.replace(_:with:) recomputes only the ancestors, with a new computeAggregates(recursing: false): 0.003 ms instead of 41 ms for the whole tree on a synthetic tree of 1.37M files and 137k folders (swiftc -O). A full scan's computeAggregates is unchanged (36 ms both). Folder nodes change in place, so AppState bumps a treeVersion that the sidebar rows, the inspector and the treemap layout depend on. The sparkline now takes the values it draws instead of the node, so a row redraws when its totals change. The treemap layout reads the tree off the main thread, so replace() waits for it before changing the tree. Layouts now run one after another and stop early when cancelled, so there is only one to wait for. Only one rescan runs at a time, and a full scan cancels it. Limits: a hard link or clone whose other copy is outside the rescanned folder is counted again, since the partial scan doesn't know the inodes seen elsewhere. Freeing the old subtree runs on the main thread, about 45 ms per million nodes, as a full rescan already does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
"Move to Trash…" in the sidebar and treemap menus asks for confirmation, moves the item with NSWorkspace.recycle and removes it from the tree without rescanning. A folder's row shows a spinner until the move finishes. Before moving, lstat checks that the path still holds the inode that was scanned, so a file replaced since the scan isn't trashed by mistake. The scan root has no Move to Trash item. Trashing shares the one-at-a-time rule with rescans, checked before the confirmation, and the menu items are disabled while either runs. The move runs asynchronously: FileManager.trashItem on the main thread froze the app for 15.8 s on a folder of 890k files, and the confirmation alert stayed on screen the whole time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7500bf4 to
45f61c4
Compare
phalladar
left a comment
There was a problem hiding this comment.
Thank you for putting this together! Rescanning a single folder and trashing from inside the app are the two things I've wanted most while cleaning up a disk. The write-up made this a pleasure to review: the measurements, the limits section, and the care taken so the wrong item never gets trashed (the inode check, the confirmation, no trash on the scan root) all really show.
I'd love to merge this. A few changes first:
1. Don't allow Move to Trash for iCloud items
I'd rather the app never trash cloud-only content. Could you disable Move to Trash in both the sidebar and treemap menus for nodes where isDataless is true or cloudOnlyFileCount > 0? The second check covers a local folder that contains cloud-only files. Please also have FileActions.moveToTrash refuse those nodes, so the guard doesn't rely on the menu alone. While you're in there, it would be nice to wrap that lstat in withoutDatalessMaterialization, the way the scanner does.
2. Two windows share one layout task
layoutTask moved from the view's @State into AppState, but MacDirStatApp hands the same AppState to every window in the WindowGroup. With two windows open, each treemap's recomputeLayout cancels the other window's layout, so one window can be left empty after a scan finishes. I found this by reading the code and haven't reproduced it. Keeping one task per view, for example a dictionary in AppState keyed by a per-view id with replace awaiting all of them, would keep the wait without the cross-cancelling.
3. Detach the old subtree in replace
replace doesn't clear old.parent, so nodes from the replaced subtree still pass the isInSubtree(of: root) guard. busyID clears before the relayout lands, so for a moment the treemap still shows rects for the old nodes. If one of those is trashed in that window, the file goes to the Trash, but the removal is applied to the orphaned subtree and the fresh one keeps counting the file. Adding old.parent = nil after the splice, in both branches, fixes it. Here's the test I used. It fails on this branch and passes with that change:
@MainActor @Test func replacedSubtreeIsDetached() async {
let root = FileNode(inode: 1, name: "/", isDirectory: true)
let a = FileNode(inode: 2, name: "a", isDirectory: true)
let video = FileNode(inode: 4, name: "x.mp4", isDirectory: false, allocatedSize: 8192, category: .video)
root.addChild(a)
a.addChild(video)
root.computeAggregates()
let state = AppState()
state.setScanCompleted(root: root)
let fresh = FileNode(inode: 2, name: "/a", isDirectory: true)
fresh.addChild(FileNode(inode: 4, name: "x.mp4", isDirectory: false, allocatedSize: 8192, category: .video))
fresh.computeAggregates()
await state.replace(a, with: fresh)
// A treemap rect laid out before the replace still points at the old node.
#expect(!video.isInSubtree(of: root))
}Smaller things, not blocking
- If a folder is deleted in Finder before you rescan it, the rescan shows an error and the folder stays in the tree. Removing it in that case would be a nice touch.
- The README's FAQ still describes MacDirStat as read-only. Would you mind updating those lines to mention Move to Trash?
- You mentioned this hasn't been run on an SDK 26 build yet. When you get a chance, a quick rescan and trash pass on a CI-built binary would be great, since that's what releases ship.
Thanks again, I'm looking forward to having this in!
- Disable Move to Trash for cloud-only items and for folders holding them or other unscanned folders, with the reason as a menu subtitle; FileActions refuses them too and rechecks SF_DATALESS without materializing. - Warn that trashing an item synced with iCloud also deletes it there. - Keep one layout task per treemap view, so windows don't cancel each other. - Detach the replaced subtree and ignore stale treemap rects in trash and drill-down. - Drop a rescanned folder from the tree when it no longer exists. - Give context menu items icons; update the README FAQ. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the careful review and the feedback. MacDirStat is exactly what I needed to manage my disk space, so congratulations on what you've built. All three changes are in, plus the smaller ones. 1. iCloud items
2. Two windows 3. Detaching the old subtree Smaller things
Unrelated bug found while testing |
Conflicts in AppState (new state properties on both sides) and the tests (new tests on both sides); kept both. The cloud-only folder test now sets scanIssue to .cloudOnly, since main turned scanIssue into a ScanIssue enum. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
replace(_:with:) left AppState.skippedFolders holding nodes from the replaced subtree, so the incomplete-scan banner kept counting and listing folders that a rescan had read, or that were trashed. Rebuild the list from the updated tree; the walk only visits branches that still have issues. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Merged, thank you @horus-leonardo |
Motivation
While cleaning up a disk, every deletion meant going to Finder and then rescanning the whole drive to see the new totals. This adds the two missing pieces: rescan one folder, and move an item to the Trash from the app. Two commits.
Rescan a single folder
FileScannerand splices the result into the tree. The row shows a spinner while it runs; the folder keeps its inode, so expanded folders stay expanded.AppState.replace(_:with:)recomputes and re-sorts only the ancestors, withcomputeAggregates(recursing: false).AppStatebumps atreeVersionthat the sidebar rows, the inspector and the treemap layout take as a value. The sparkline now takes the values it draws instead of the node, so a row redraws when its totals change.replacewaits for it before changing the tree. Layouts now run one after another and stop early when cancelled, so there is only one to wait for.busyID), and a full scan cancels it. This keeps two changes from overlapping in the tree, e.g. a rescan of a folder bringing back a child trashed while it ran.Move to Trash
NSWorkspace.recycle, and removes it from the tree withreplace(_:with: nil), without rescanning. The menu items are disabled while a rescan or trash runs.lstatchecks that the path still holds the scanned inode, so a file replaced since the scan isn't trashed by mistake. The scan root has no Move to Trash item.FileManager.trashItemon the main thread, and it froze the app for 15.8 s on a folder of 890k files, with the confirmation alert stuck on screen.Measurements
swiftc -O, best of 20–30:computeAggregates, full scan (1.37M files, 137k folders)Limits
~/.Trash, the totals drop after a trash while the disk usage doesn't, until that folder is rescanned.Validation
swift build,swift test(newreplacingASubtreeUpdatesItsAncestors, covering the selection and treemap root moving to the fresh node).🤖 Generated with Claude Code