Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Read each app's background jobs once per scan, not once per label
Țugui Dragoș3720c7153832
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swiftindex 06dce11a..82ff0e87 100644--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift@@ -1,5 +1,6 @@public import Foundationimport PeelPrivileged+import Synchronization/// Finds the app a launchd job belongs to. Every clue is tried before a job is called orphaned, because/// one key that does not match is not enough reason to remove a job that an app may still need.@@ -17,11 +18,11 @@ public struct BackgroundItemOwnership: Sendable {public var isInstalled: Bool { url != nil }}- private let apps: [InstalledApp]private let byIdentifier: [String: InstalledApp]private let byTeam: [String: [InstalledApp]]private let teamOfProgram: @Sendable (String) -> String?private var liveLabels: Set<String> = []+ private let declared: DeclaredJobspublic init(installedApps: [InstalledApp]) {self.init(installedApps: installedApps) { CodeSignature.teamIdentifier(at: URL(filePath: $0)) }@@ -30,13 +31,13 @@ public struct BackgroundItemOwnership: Sendable {/// For tests: `teamOfProgram` stands in for reading each program's code signature.init(installedApps: [InstalledApp], teamOfProgram: @escaping @Sendable (String) -> String?) {self.teamOfProgram = teamOfProgram- apps = installedAppsbyIdentifier = Dictionary(installedApps.compactMap { app in app.bundleIdentifier.map { ($0.lowercased(), app) } },uniquingKeysWith: { first, _ in first })byTeam = Dictionary(grouping: installedApps.compactMap { app in app.teamIdentifier.map { ($0, app) } }, by: \.0).mapValues { $0.map(\.1) }+ declared = DeclaredJobs(installedApps)}/// Returns the app that owns a job, trying each clue in turn, or nil when no clue names an app.@@ -102,7 +103,7 @@ public struct BackgroundItemOwnership: Sendable {/// The installed app whose own bundle declares the job `label`: a label launchd still keeps after a restart, when/// the job is no longer loaded and nothing else points to its app.func owner(declaring label: String, kind: BackgroundItem.Kind) -> Owner? {- apps.first { StartsAtLogin.declaration(of: label, kind: kind, in: $0.url) != nil }+ declared.app(declaring: label, kind: kind).map { Owner(bundleIdentifier: $0.bundleIdentifier, name: $0.name, url: $0.url) }}@@ -204,3 +205,28 @@ public struct BackgroundItemOwnership: Sendable {return (path, identifier)}}++/// The jobs the installed apps' bundles declare, read once when first asked about: a scan asks about many labels.+private final class DeclaredJobs: Sendable {+ private let apps: [InstalledApp]+ private let byJob = Mutex<[String: InstalledApp]?>(nil)++ init(_ apps: [InstalledApp]) {+ self.apps = apps+ }++ func app(declaring label: String, kind: BackgroundItem.Kind) -> InstalledApp? {+ byJob.withLock { byJob in+ if byJob == nil {+ var read: [String: InstalledApp] = [:]+ for app in apps {+ for job in StartsAtLogin.declarations(in: app.url) where read["\(job.kind)/\(job.label)"] == nil {+ read["\(job.kind)/\(job.label)"] = app+ }+ }+ byJob = read+ }+ return byJob?["\(kind)/\(label)"]+ }+ }+}diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/StartsAtLogin.swift b/Packages/PeelCore/Sources/PeelCore/Removal/StartsAtLogin.swiftindex 8862f3bb..64da8506 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/StartsAtLogin.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/StartsAtLogin.swift@@ -37,19 +37,26 @@ enum StartsAtLogin {}static func declaration(of label: String, kind: BackgroundItem.Kind, in app: URL) -> Declaration? {+ declarations(in: app).first { $0.label == label && $0.kind == kind }?.declaration+ }++ /// Every job `app`'s bundle declares, in the order `declaration(of:kind:in:)` weighs them.+ static func declarations(in app: URL) -> [(label: String, kind: BackgroundItem.Kind, declaration: Declaration)] {let library = app.appending(path: "Contents/Library", directoryHint: .isDirectory)- if kind == .agent {- if identifier(of: app) == label { return Declaration(runsAtLoad: true, keepsAlive: nil) }- let loginItems = entries(of: library.appending(path: "LoginItems", directoryHint: .isDirectory))- .lazy.filter { $0.pathExtension == "app" }.prefix(mostAsked)- if loginItems.contains(where: { identifier(of: $0) == label }) {- return Declaration(runsAtLoad: true, keepsAlive: true)- }+ var found: [(label: String, kind: BackgroundItem.Kind, declaration: Declaration)] = []+ if let own = identifier(of: app) {+ found.append((own, .agent, Declaration(runsAtLoad: true, keepsAlive: nil)))+ }+ let loginItems = entries(of: library.appending(path: "LoginItems", directoryHint: .isDirectory))+ .lazy.filter { $0.pathExtension == "app" }.prefix(mostAsked).compactMap(identifier(of:))+ found += loginItems.map { ($0, .agent, Declaration(runsAtLoad: true, keepsAlive: true)) }+ for (folder, kind) in [("LaunchAgents", BackgroundItem.Kind.agent), ("LaunchDaemons", .daemon)] {+ let jobs = entries(of: library.appending(path: folder, directoryHint: .isDirectory))+ .lazy.filter { $0.pathExtension == "plist" }.prefix(mostAsked)+ .compactMap(JobDefinition.init(contentsOf:))+ found += jobs.map { ($0.label, kind, Declaration(runsAtLoad: $0.runsAtLoad, keepsAlive: $0.keepsAlive)) }}- let folder = library.appending(path: kind == .agent ? "LaunchAgents" : "LaunchDaemons", directoryHint: .isDirectory)- let job = entries(of: folder).lazy.filter { $0.pathExtension == "plist" }.prefix(mostAsked)- .compactMap(JobDefinition.init(contentsOf:)).first { $0.label == label }- return job.map { Declaration(runsAtLoad: $0.runsAtLoad, keepsAlive: $0.keepsAlive) }+ return found}/// What launchd holds now of what `app` registered, or nil when it holds nothing.diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swiftindex 65187f0c..1019a1d3 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift@@ -345,6 +345,33 @@ struct BackgroundItemActionsTests {#expect(items.first?.runsAtLoad == true)}+ @Test func readsEachAppsBundleOnceHoweverManyLabelsAreAsked() throws {+ let directory = try TemporaryDirectory()+ func write(_ plist: [String: Any], at path: String) throws {+ let data = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0)+ try directory.file(path, contents: data)+ }+ try write(["CFBundleIdentifier": "org.example.tide"], at: "Applications/Tide.app/Contents/Info.plist")+ try write(+ ["Label": "org.example.tide-sync", "Program": "/bin/true"],+ at: "Applications/Tide.app/Contents/Library/LaunchAgents/sync.plist"+ )+ let app = InstalledApp(+ url: directory.url.appending(path: "Applications/Tide.app"), bundleIdentifier: "org.example.tide", name: "Tide"+ )+ let ownership = BackgroundItemOwnership(installedApps: [app])++ #expect(ownership.owner(declaring: "org.example.tide-sync", kind: .agent)?.url == app.url)+ try write(+ ["Label": "org.example.tide-later", "Program": "/bin/true"],+ at: "Applications/Tide.app/Contents/Library/LaunchAgents/later.plist"+ )++ #expect(ownership.owner(declaring: "org.example.tide-later", kind: .agent) == nil, "read again for a second label")+ #expect(ownership.owner(declaring: "org.example.tide-sync", kind: .daemon) == nil)+ #expect(BackgroundItemOwnership(installedApps: [app]).owner(declaring: "org.example.tide-later", kind: .agent) != nil)+ }+@Test func aJobWhoseLabelLeadsNowhereIsNotDisabled() throws {let directory = try TemporaryDirectory()let info = try PropertyListSerialization.data(