Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Never select in Space what waits for the helper while it cannot act

Țugui Dragoșb740f0cde430
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift b/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
index 8b688f62..393dd110 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
@@ -41,6 +41,17 @@ public enum SpaceRemoval {
needsTheHelper.contains(url) && heldBack[url]?.cannotBeMoved != true && !canUseHelper
}
+ /// What stays selected in the area as the plan is made again or the helper comes or goes: a child that waits
+ /// for the helper is offered and suggested only while the helper can act.
+ public func selection(
+ keeping selected: Set<URL>,
+ in choices: inout KeptSelection,
+ canUseHelper: Bool
+ ) -> Set<URL> {
+ let rows = selectableRows(canUseHelper: canUseHelper)
+ return choices.update(selected, selectable: Set(rows.selectable), suggested: Set(rows.recommended))
+ }
+
public func selectableRows(canUseHelper: Bool) -> SelectableRows<URL> {
let unlocked = removable.filter { !isLocked($0, canUseHelper: canUseHelper) }
return SelectableRows(
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift
index c2f6e4ad..15aa8801 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift
@@ -235,6 +235,29 @@ struct SpaceRemovalTests {
#expect(plan.heldBack.isEmpty, "the helper serves the caches every account shares")
}
+ @Test(.permissionsHold) func whatOnlyAnAdministratorCanMoveWaitsForTheHelperToBeSelected() async throws {
+ let directory = try TemporaryDirectory()
+ try directory.file("root/Library/Caches/org.example.updater/data.bin")
+ try directory.setPermissions(0o555, of: "root/Library/Caches")
+ defer { try? directory.setPermissions(0o755, of: "root/Library/Caches") }
+ let shared = SpaceItem(
+ id: "system-caches",
+ category: .library,
+ urls: [directory.url.appending(path: "root/Library/Caches", directoryHint: .isDirectory)],
+ size: 0,
+ handling: .trash,
+ leavesMacOSsOwn: true
+ )
+
+ let plan = await SpaceRemoval.plan(for: shared, environment: environment(directory), running: [:])
+ let updater = try #require(plan.removable.first)
+ var choices = KeptSelection()
+
+ #expect(plan.needsTheHelper == [updater])
+ #expect(plan.selection(keeping: [], in: &choices, canUseHelper: false).isEmpty)
+ #expect(plan.selection(keeping: [], in: &choices, canUseHelper: true) == [updater])
+ }
+
@Test func emptiesAContainersCachesUnlessItsAppIsOpenOrApples() async throws {
let directory = try TemporaryDirectory()
let containers = "home/Library/Containers"
@@ -560,7 +583,7 @@ struct SpaceRemovalTests {
)
var choices = KeptSelection()
#expect(
- choices.update([], selectable: Set(first.removable), suggested: first.suggested) == [
+ first.selection(keeping: [], in: &choices, canUseHelper: true) == [
kept, deselected, gone, noLongerMeasured,
]
)
@@ -573,9 +596,23 @@ struct SpaceRemovalTests {
)
let selected: Set = [kept, chosenByHand, gone, noLongerMeasured]
#expect(
- choices.update(selected, selectable: Set(again.removable), suggested: again.suggested) == [
+ again.selection(keeping: selected, in: &choices, canUseHelper: true) == [
kept, chosenByHand, new,
]
)
}
+
+ @Test func aChildThatWaitsForTheHelperIsSelectedOnlyOnceTheHelperCanAct() {
+ let caches = URL(filePath: "/Library/Caches", directoryHint: .isDirectory)
+ let vendor = caches.appending(path: "org.example.Vendor")
+ let system = caches.appending(path: "org.example.System")
+ let plan = SpaceRemoval.Plan(
+ removable: [vendor, system], inUse: [], leftToDeveloper: [], sizes: [vendor: 1, system: 2],
+ needsTheHelper: [system]
+ )
+ var choices = KeptSelection()
+
+ #expect(plan.selection(keeping: [], in: &choices, canUseHelper: false) == [vendor])
+ #expect(plan.selection(keeping: [vendor], in: &choices, canUseHelper: true) == [vendor, system])
+ }
}
diff --git a/Peel/Model/SpaceLibrary.swift b/Peel/Model/SpaceLibrary.swift
index 4ceb052d..662da355 100644
--- a/Peel/Model/SpaceLibrary.swift
+++ b/Peel/Model/SpaceLibrary.swift
@@ -18,6 +18,7 @@ final class SpaceLibrary: RowSelection {
@ObservationIgnored private var plannedFor: [SpaceItem.ID: (item: SpaceItem, exclusions: Int)] = [:]
/// What was chosen in each area, kept through its plans.
@ObservationIgnored private var choices: [SpaceItem.ID: KeptSelection] = [:]
+ @ObservationIgnored private var canUseHelper = false
var selectedItem: SpaceItem? {
report?.items.first { $0.id == selection }
@@ -47,10 +48,10 @@ final class SpaceLibrary: RowSelection {
private func keep(_ plan: SpaceRemoval.Plan, for item: SpaceItem, madeWith revision: Int) {
let previous = plans[item.id]
- let chosen = choices[item.id, default: KeptSelection()].update(
- selectedURLs,
- selectable: Set(plan.removable.filter { plan.heldBack[$0]?.cannotBeMoved != true }),
- suggested: plan.suggested
+ let chosen = plan.selection(
+ keeping: selectedURLs,
+ in: &choices[item.id, default: KeptSelection()],
+ canUseHelper: canUseHelper
)
selectedURLs.subtract(previous?.removable ?? [])
selectedURLs.formUnion(chosen)
@@ -58,6 +59,21 @@ final class SpaceLibrary: RowSelection {
plannedFor[item.id] = (item, revision)
}
+ /// Brings what is selected in each area in line with the helper as it is now, without measuring again.
+ func follow(canUseHelper: Bool) {
+ guard canUseHelper != self.canUseHelper else { return }
+ self.canUseHelper = canUseHelper
+ for (id, plan) in plans {
+ let chosen = plan.selection(
+ keeping: selectedURLs,
+ in: &choices[id, default: KeptSelection()],
+ canUseHelper: canUseHelper
+ )
+ selectedURLs.subtract(plan.removable)
+ selectedURLs.formUnion(chosen)
+ }
+ }
+
/// Scans again, and makes again the plans of the areas in `replanning` and of every area that changed since
/// its plan was made, so what is carried from them follows what is on disk. The plans are part of the scan, so
/// an area's page stays busy until it shows what is there now.
diff --git a/Peel/PeelApp.swift b/Peel/PeelApp.swift
index 02b9cb60..f75d7621 100644
--- a/Peel/PeelApp.swift
+++ b/Peel/PeelApp.swift
@@ -105,6 +105,14 @@ struct PeelApp: App {
))
}
+ /// Keeps what Space selects in step with the helper whichever page is open, since its selection travels to the
+ /// other storage pages.
+ private func followHelper() async {
+ for await canAct in Observations({ helper.canAct }) {
+ space.follow(canUseHelper: canAct)
+ }
+ }
+
/// Tells the Trash watch where the listed apps are, so it watches the Trash of each other disk they are on.
private func followAppsForTheTrash() async {
for await _ in Observations({ library.revision }) {
@@ -253,6 +261,7 @@ struct PeelApp: App {
Navigator.shared.openWindow = openWindow
background.start([
followFolders, followAppsForTheTrash, askWhenDue, followFindings, followActivations, watchFreeSpace,
+ followHelper,
])
textEditing.start()
sheetInFront.start()