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.

Start no reset while History or the exclusions can't be read

Țugui Dragoșe3380c413ce7
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/AppReset.swift b/Packages/PeelCore/Sources/PeelCore/Removal/AppReset.swift
index 730628d8..9ec46cbb 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/AppReset.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/AppReset.swift
@@ -81,6 +81,7 @@ public struct AppReset: Sendable {
/// What a reset did, or why it did nothing.
public struct Outcome: Sendable {
public enum Stop: Sendable, Equatable {
+ case removalsHeld(TrashFailure.Reason)
case settingsNotSaved
case appOpened
}
@@ -97,8 +98,8 @@ public struct AppReset: Sendable {
}
/// Copies the settings in `selection`, asks again whether the app is open, since the copy takes a moment, then
- /// moves the selection to the Trash and resets the privacy permissions when asked. Nothing moves when the copy
- /// fails or the app opened meanwhile, and the copy then stays.
+ /// moves the selection to the Trash and resets the privacy permissions when asked. Nothing starts while every move
+ /// would be refused, and nothing moves when the copy fails or the app opened meanwhile, the copy then staying.
@concurrent
public func run(
_ selection: Set<URL>,
@@ -126,6 +127,10 @@ public struct AppReset: Sendable {
resetPrivacy: @Sendable (String) async -> PrivacyReset.Result
) async -> Outcome {
var outcome = Outcome()
+ if let refusal = service.refusalOfEveryMove {
+ outcome.stop = .removalsHeld(refusal)
+ return outcome
+ }
let urls = selected(selection)
switch await save(urls) {
case .failed:
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index 2494b505..9ad7f6da 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -335,6 +335,12 @@ public struct TrashService: Sendable {
/// False while the saved exclusions are not read yet or cannot be read, when nothing moves.
public var knowsTheExclusions: Bool { removalGuard.knowsTheExclusions }
+ /// Why every move would be refused now, whatever it is asked to move, or nil when a move may go ahead.
+ public var refusalOfEveryMove: TrashFailure.Reason? {
+ guard knowsTheExclusions else { return .guarded(.exclusionsNotKnown) }
+ return historyCanBeRead ? nil : .historyUnreadable
+ }
+
/// Why `url` would not move, or nil when it would. A plan can show this before anything moves, since the move
/// asks the same guard.
public func refusal(of url: URL) -> TrashFailure.Reason? {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppResetTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppResetTests.swift
index 529102e0..9469808c 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppResetTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppResetTests.swift
@@ -399,4 +399,47 @@ struct AppResetTests {
#expect(!outcome.clearedSettings)
#expect(outcome.settingsToPutBack == nil)
}
+
+ @Test func aResetStartsNothingWhileEveryMoveWouldBeRefused() async throws {
+ let directory = try TemporaryDirectory()
+ let settings = try directory.file("Library/Preferences/com.example.app.plist", bytes: 4096)
+ let reset = await AppReset.prepare(app(), installedApps: [app()], environment: home(directory))
+ let history = try directory.directory("Library/Application Support/Peel/removals.json")
+ let environment = home(directory)
+ let trash = try directory.directory("Trash")
+ let services: [(TrashService, TrashFailure.Reason)] = [
+ (.unreadable, nil, .guarded(.exclusionsNotKnown)),
+ (.notYetRead, nil, .guarded(.exclusionsNotKnown)),
+ (.none, RemovalJournal(beside: history), .historyUnreadable),
+ ].map { exclusions, journal, reason in
+ let service = TrashService(environment: environment, exclusions: exclusions, journal: journal) { url in
+ let destination = trash.appending(path: url.lastPathComponent)
+ try FileManager.default.moveItem(at: url, to: destination)
+ return destination
+ }
+ return (service, reason)
+ }
+
+ for (service, reason) in services {
+ let steps = Mutex<[String]>([])
+ let outcome = await reset.run(
+ reset.suggestedSelection,
+ resettingPrivacy: true,
+ through: service,
+ isOpen: { false },
+ save: { _ in
+ steps.withLock { $0.append("copy") }
+ return .nothingToSave
+ },
+ resetPrivacy: { _ in
+ steps.withLock { $0.append("privacy") }
+ return .reset
+ }
+ )
+
+ #expect(outcome.stop == .removalsHeld(reason))
+ #expect(steps.withLock { $0 }.isEmpty)
+ }
+ #expect(settings.isThere)
+ }
}
diff --git a/Peel/Views/ResetSheet.swift b/Peel/Views/ResetSheet.swift
index acf6cea4..b3d165b4 100644
--- a/Peel/Views/ResetSheet.swift
+++ b/Peel/Views/ResetSheet.swift
@@ -92,6 +92,8 @@ struct ResetSheet: View {
header
.listRowSeparator(.hidden)
+ RemovalsHeldBanner()
+
if plan.needsFullDiskAccess {
FullDiskAccessBanner()
}
@@ -221,7 +223,10 @@ struct ResetSheet: View {
// something.
Button("Reset") { isConfirming = true }
.buttonStyle(.borderedProminent)
- .disabled(!plan.hasAnythingToReset || plan.isResetting || plan.isAppRunning)
+ .disabled(
+ !plan.hasAnythingToReset || plan.isResetting || plan.isAppRunning
+ || !exclusions.exclusions.isKnown || history.isUnreadable
+ )
}
.padding(.horizontal, 20)
.padding(.vertical, 14)