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.

Reset an app's privacy permissions only when it is going

Țugui Dragoș7eb91a2a730f
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
index ee640656..016e59de 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
@@ -557,7 +557,7 @@ struct UninstallCommand: AsyncParsableCommand {
let (reset, result, recorded) = await Uninterrupted.run {
// The privacy reset comes before the move, because `tccutil` only finds an app that is still in place.
- let reset = resetPrivacy ? await PrivacyReset.reset(bundleIdentifier: target.bundleIdentifier) : nil
+ let reset = resetPrivacy ? await PrivacyReset.reset([target], beforeMovingWith: service).first?.result : nil
let result = await plan.move(using: service)
return (reset, result, await plan.record(result, from: target.name))
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PrivacyReset.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PrivacyReset.swift
index c9a31b8a..c49f9678 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/PrivacyReset.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PrivacyReset.swift
@@ -34,16 +34,25 @@ public enum PrivacyReset {
}
}
- /// Resets each app in turn. Call it just before the removal moves them: `tccutil` only finds an app still in place.
+ /// Resets each of `apps` that `service` would move now, in turn. Call it just before the removal moves them:
+ /// `tccutil` only finds an app still in place, and an app something keeps in place stays, with its permissions.
@concurrent
- public static func reset(_ apps: [InstalledApp]) async -> [(app: InstalledApp, result: Result)] {
+ public static func reset(
+ _ apps: [InstalledApp],
+ beforeMovingWith service: TrashService
+ ) async -> [(app: InstalledApp, result: Result)] {
var results: [(app: InstalledApp, result: Result)] = []
- for app in apps {
+ for app in goingNow(apps, with: service) {
results.append((app, await reset(bundleIdentifier: app.bundleIdentifier)))
}
return results
}
+ static func goingNow(_ apps: [InstalledApp], with service: TrashService) -> [InstalledApp] {
+ let refused = service.refusalsNow(of: apps.map(\.url))
+ return apps.filter { refused[$0.url] == nil }
+ }
+
/// Returns the resets worth reporting once the removal is over: those that did not happen, and those that did
/// for an app that then stayed. A reset whose app was moved is what the user asked for.
public static func worthTelling(
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index e9999b90..3ef5f552 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -263,6 +263,21 @@ public struct TrashService: Sendable {
return historyCanBeRead ? nil : .historyUnreadable
}
+ /// Why each of `urls` would not move now, by the checks a move makes before it starts: the guard, History, and the
+ /// programs holding it. An item missing from the answer would move.
+ func refusalsNow(of urls: [URL]) -> [URL: TrashFailure.Reason] {
+ let openFiles = OpenFiles()
+ var refusals: [URL: TrashFailure.Reason] = [:]
+ for url in urls {
+ if let refusal = refusal(of: url) {
+ refusals[url] = refusal
+ } else if case let holders = openFiles.holders(of: url), !holders.isEmpty {
+ refusals[url] = .heldOpen(by: holders)
+ }
+ }
+ return refusals
+ }
+
/// Whether the History this service's moves are recorded in can be read. Nothing moves while it cannot, since
/// what moved could not be listed for Put Back.
private var historyCanBeRead: Bool {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
index badc5401..e8ea4612 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
@@ -582,6 +582,30 @@ struct RemovalHygieneTests {
}
struct PrivacyResetTests {
+ /// The reset can't be undone, so it is only for an app the move will take: one a program is running from stays,
+ /// and keeps its permissions.
+ @Test func resetsOnlyAnAppThatIsGoing() throws {
+ let directory = try TemporaryDirectory()
+ let held = InstalledApp(url: try directory.directory("Applications/Held.app"), bundleIdentifier: "org.example.held", name: "Held")
+ let free = InstalledApp(url: try directory.directory("Applications/Free.app"), bundleIdentifier: "org.example.free", name: "Free")
+ let program = try directory.directory("Applications/Held.app/Contents/MacOS").appending(path: "held")
+ try FileManager.default.copyItem(atPath: "/bin/sleep", toPath: program.path(percentEncoded: false))
+ let process = Process()
+ process.executableURL = program
+ process.arguments = ["30"]
+ try process.run()
+ defer { process.terminate() }
+
+ #expect(PrivacyReset.goingNow([held, free], with: service(in: directory)).map(\.bundleIdentifier) == ["org.example.free"])
+ }
+
+ private func service(in directory: borrowing TemporaryDirectory) -> TrashService {
+ TrashService(environment: SearchEnvironment(
+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),
+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)
+ )) { _ in throw CocoaError(.fileWriteNoPermission) }
+ }
+
@Test func refusesIdentifiersItShouldNeverTouch() {
#expect(PrivacyReset.isAllowed(bundleIdentifier: "com.example.app"))
// Two components name one app as well as three do: Obsidian ships as `md.obsidian`.
@@ -623,11 +647,12 @@ struct PrivacyResetTests {
}
/// Each app of a batch gets one answer, in order, and `tccutil` is never run for an app Peel refuses.
- @Test func answersForEachAppOfABatch() async {
- let safari = InstalledApp(url: URL(filePath: "/Applications/Safari.app", directoryHint: .isDirectory), bundleIdentifier: "com.apple.Safari", name: "Safari")
- let signal = InstalledApp(url: URL(filePath: "/Applications/Signal.app", directoryHint: .isDirectory), bundleIdentifier: "Signal", name: "Signal")
+ @Test func answersForEachAppOfABatch() async throws {
+ let directory = try TemporaryDirectory()
+ let safari = InstalledApp(url: try directory.directory("Applications/Safari.app"), bundleIdentifier: "com.apple.Safari", name: "Safari")
+ let signal = InstalledApp(url: try directory.directory("Applications/Signal.app"), bundleIdentifier: "Signal", name: "Signal")
- let resets = await PrivacyReset.reset([safari, signal])
+ let resets = await PrivacyReset.reset([safari, signal], beforeMovingWith: service(in: directory))
#expect(resets.map(\.app.name) == ["Safari", "Signal"])
#expect(resets.map(\.result) == [.refused, .refused])
diff --git a/Peel/Views/AppDetailView.swift b/Peel/Views/AppDetailView.swift
index cf938399..3fa4fae6 100644
--- a/Peel/Views/AppDetailView.swift
+++ b/Peel/Views/AppDetailView.swift
@@ -464,7 +464,8 @@ struct AppDetailView: View {
guard plan.runningProcesses.isEmpty else { return requestRemoval() }
let result = await QuitGuard.shared.run {
// The one step that goes before the move: `tccutil` only finds an app that is still in its place.
- let privacy = await PrivacyReset.reset(resetting(request.urls))
+ let service = TrashService(exclusions: ExclusionsStore.shared.exclusions)
+ let privacy = await PrivacyReset.reset(resetting(request.urls), beforeMovingWith: service)
let result = await plan.move(request)
let keptItsFiles = plan.uninstallation?.keptItsFiles(after: result, selection: request.urls) == true
outcome.report(result, privacy: privacy, keptTheirFiles: keptItsFiles ? [plan.app.name] : [])
diff --git a/Peel/Views/MultipleAppsView.swift b/Peel/Views/MultipleAppsView.swift
index a5af2ee1..2ac24a74 100644
--- a/Peel/Views/MultipleAppsView.swift
+++ b/Peel/Views/MultipleAppsView.swift
@@ -347,7 +347,8 @@ struct MultipleAppsView: View {
guard plan.runningProcesses.isEmpty else { return requestRemoval() }
let result = await QuitGuard.shared.run {
// The privacy reset runs before the move, because `tccutil` only finds an app that is still in its place.
- let privacy = await PrivacyReset.reset(resetting(request.urls))
+ let service = TrashService(exclusions: ExclusionsStore.shared.exclusions)
+ let privacy = await PrivacyReset.reset(resetting(request.urls), beforeMovingWith: service)
let result = await plan.move(request)
let kept = plan.bulk?.appsThatKeptTheirFiles(after: result, selection: request.urls) ?? []
outcome.report(result, privacy: privacy, keptTheirFiles: kept.map(\.name))