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 the privacy of an app whose own background programs an uninstall stops

Țugui Dragoș019fab0adc08
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PrivacyReset.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PrivacyReset.swift
index ce4b0543..6b332d20 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/PrivacyReset.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PrivacyReset.swift
@@ -42,7 +42,7 @@ public enum PrivacyReset {
beforeMovingWith service: TrashService
) async -> [(app: InstalledApp, result: Result)] {
var results: [(app: InstalledApp, result: Result)] = []
- for app in goingNow(apps, with: service) {
+ for app in await goingNow(apps, with: service) {
guard let identifier = app.bundleIdentifier else {
results.append((app, .refused))
continue
@@ -52,10 +52,10 @@ public enum PrivacyReset {
return results
}
- static func goingNow(_ apps: [InstalledApp], with service: TrashService) -> [InstalledApp] {
+ static func goingNow(_ apps: [InstalledApp], with service: TrashService) async -> [InstalledApp] {
let uninstallingThemselves = apps.filter { UninstallsItself.of($0, environment: service.environment) != nil }
- let refused = service.refusalsNow(
- of: apps.map(\.url),
+ let refused = await service.refusalsNow(
+ ofApps: apps.map(\.url),
lettingTheirProgramsRun: Set(uninstallingThemselves.map(\.url))
)
return apps.filter { refused[$0.url] == nil }
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index 597015ec..afd406b5 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -320,17 +320,26 @@ 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], lettingTheirProgramsRun apps: Set<URL>) -> [URL: TrashFailure.Reason] {
+ /// Why each of `apps` would not move now in an uninstall, by the checks its move makes: the guard, History, and
+ /// the programs holding it, unless only the app's own background programs do, which the move stops first. An app
+ /// missing from the answer would move.
+ func refusalsNow(
+ ofApps apps: [URL],
+ lettingTheirProgramsRun running: Set<URL>
+ ) async -> [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 let held = openFiles.refusal(of: url, lettingItsProgramsRun: apps.contains(url)) {
- refusals[url] = held
+ for app in apps {
+ if let refusal = refusal(of: app) {
+ refusals[app] = refusal
+ continue
}
+ let mayRun = running.contains(app)
+ guard let held = openFiles.refusal(of: app, lettingItsProgramsRun: mayRun) else { continue }
+ if held.isHeldByPrograms, await jobsHoldingOnly(app, in: openFiles, lettingItsProgramsRun: mayRun) != nil {
+ continue
+ }
+ refusals[app] = held
}
return refusals
}
@@ -481,11 +490,9 @@ public struct TrashService: Sendable {
var stoppedForMovedApps: [LaunchdCleanup.Running] = []
for app in apps {
guard let index = result.failures.firstIndex(where: { $0.url == app }),
- result.failures[index].reason.isHeldByPrograms
+ result.failures[index].reason.isHeldByPrograms,
+ let jobs = await jobsHoldingOnly(app, in: OpenFiles(), lettingItsProgramsRun: running.contains(app))
else { continue }
- let holders = OpenFiles().processes(holding: app, lettingItsProgramsRun: running.contains(app))
- let jobs = await runningInside(app).filter { holders.contains($0.pid) }
- guard !holders.isEmpty, holders.isSubset(of: Set(jobs.map(\.pid))) else { continue }
let stopped = await stopRunning(jobs, true)
let again = await trash(
[app], usingHelperFor: privilegedURLs, removal: removal, lettingTheirProgramsRun: running
@@ -502,6 +509,19 @@ public struct TrashService: Sendable {
return stoppedForMovedApps
}
+ /// The background programs running from inside `app` when nothing else holds it, or nil when anything else holds it
+ /// too, or nothing does.
+ private func jobsHoldingOnly(
+ _ app: URL,
+ in openFiles: OpenFiles,
+ lettingItsProgramsRun: Bool
+ ) async -> [LaunchdCleanup.Running]? {
+ let holders = openFiles.processes(holding: app, lettingItsProgramsRun: lettingItsProgramsRun)
+ guard !holders.isEmpty else { return nil }
+ let jobs = await runningInside(app).filter { holders.contains($0.pid) }
+ return holders.isSubset(of: Set(jobs.map(\.pid))) ? jobs : nil
+ }
+
/// Starts again each of `jobs` launchd reads from a file that did not move: stopped so their app could move, they
/// must not stay stopped while their file stays in place.
private func startAgain(_ jobs: [LaunchdCleanup.Running], unlessMoved moved: Set<URL>) async {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
index 00a5291f..1dcbab1f 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
@@ -657,14 +657,14 @@ 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 {
+ @Test func resetsOnlyAnAppThatIsGoing() async 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 process = try directory.runningProgram("Applications/Held.app/Contents/MacOS/held")
defer { process.terminate() }
- #expect(PrivacyReset.goingNow([held, free], with: service(in: directory)).map(\.bundleIdentifier) == ["org.example.free"])
+ #expect(await PrivacyReset.goingNow([held, free], with: service(in: directory)).map(\.bundleIdentifier) == ["org.example.free"])
}
private func service(in directory: borrowing TemporaryDirectory) -> TrashService {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
index da234c1c..eb7ab635 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
@@ -217,6 +217,18 @@ struct TrashServiceTests {
#expect(await launchd.started == [job.label])
}
+ @Test func resetsThePrivacyOfAnAppOnlyItsOwnBackgroundProgramKeepsInPlace() async throws {
+ let directory = try TemporaryDirectory()
+ let (app, agent, _, job) = try appRunningItsAgent(in: directory)
+ defer { agent.terminate() }
+ let installed = InstalledApp(url: app, bundleIdentifier: "org.example.agentful", name: "Agentful")
+ let stopsItsJob = try service(in: directory, launchd: Launchd(), agent: agent, running: [job])
+ let findsNoJob = try service(in: directory, launchd: Launchd(), agent: agent, running: [])
+
+ #expect(await PrivacyReset.goingNow([installed], with: stopsItsJob) == [installed])
+ #expect(await PrivacyReset.goingNow([installed], with: findsNoJob).isEmpty)
+ }
+
/// A command-line tool's link, and its shell completion's, lead into its app until the app has moved, and the
/// helper takes such a link only once it leads nowhere, so the links go to the helper after everything else, in
/// whatever order they came.
@@ -923,7 +935,7 @@ struct TrashServiceTests {
#expect(result.trashed.isEmpty)
#expect(result.failures.map(\.reason) == [.peelRunsFromIt])
#expect(throughTheHelper.failures.map(\.reason) == [.peelRunsFromIt])
- #expect(service.refusalsNow(of: [folder], lettingTheirProgramsRun: []) == [folder: .peelRunsFromIt])
+ #expect(await service.refusalsNow(ofApps: [folder], lettingTheirProgramsRun: []) == [folder: .peelRunsFromIt])
}
@MainActor @Test func leavesWhatAnotherAccountRunsFromWithAReasonOfItsOwn() {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UninstallsItselfTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UninstallsItselfTests.swift
index 3bf5a7ac..4213c8ee 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UninstallsItselfTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UninstallsItselfTests.swift
@@ -192,10 +192,10 @@ struct UninstallsItselfTests {
#expect(plan.needsReview == 0)
}
- @Test func itsPrivacyIsResetThoughItsOwnProgramRunsFromIt() throws {
+ @Test func itsPrivacyIsResetThoughItsOwnProgramRunsFromIt() async throws {
let installed = try Installed()
- #expect(PrivacyReset.goingNow([installed.app], with: try installed.service()) == [installed.app])
+ #expect(await PrivacyReset.goingNow([installed.app], with: try installed.service()) == [installed.app])
}
private struct Citrix: ~Copyable {