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.

A background job an app registered can always be enabled again

Țugui Dragoșf286726bb807
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift
index 5fbc6fd8..4d9f02e4 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift
@@ -45,6 +45,7 @@ public enum BackgroundItems {
}
return results
}
+ items += disabledJobs(in: loaded, listed: items, ownership: ownership)
return items
.filter { $0.ownerBundleIdentifier.map(exclusions.excludes(bundleIdentifier:)) != true }
@@ -68,6 +69,36 @@ public enum BackgroundItems {
}.sorted { $0.label < $1.label }
}
+ /// The jobs launchd keeps disabled that no row lists, because they are not loaded and no file here declares
+ /// them, when they belong to an installed app. A job an app registered is seen only while it is loaded, so without
+ /// this row, one disabled here would leave the list after a restart, with no way to Enable it again.
+ static func disabledJobs(in loaded: Loaded, listed: [BackgroundItem], ownership: BackgroundItemOwnership) -> [BackgroundItem] {
+ let known = Set(listed.map { "\($0.kind.rawValue)/\($0.label)" })
+ let overrides = (loaded.userDisabled ?? [:]).map { ($0.key, $0.value, BackgroundItem.Kind.agent) }
+ + (loaded.systemDisabled ?? [:]).map { ($0.key, $0.value, BackgroundItem.Kind.daemon) }
+ return overrides.compactMap { label, isDisabled, kind in
+ guard isDisabled, !label.hasPrefix("com.apple."), !label.hasPrefix("application."),
+ !known.contains("\(kind.rawValue)/\(label)"), loaded.state(of: label, kind) == .notLoaded,
+ let owner = ownership.owner(label: label, associated: [], program: nil), owner.isInstalled
+ else { return nil }
+ return BackgroundItem(
+ label: label,
+ kind: kind,
+ source: .app,
+ plistURL: nil,
+ program: nil,
+ runsAtLoad: false,
+ keepsAlive: false,
+ ownerBundleIdentifier: owner.bundleIdentifier,
+ ownerName: owner.name,
+ isOwnerInstalled: true,
+ isOrphan: false,
+ state: .notLoaded,
+ isDisabled: true
+ )
+ }
+ }
+
/// What `launchctl` reports right now. Each part is nil when `launchctl` answered in a form Peel cannot read.
struct Loaded {
var user: [String: Int32?]? = [:]
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
index c9fa74e6..40eab269 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
@@ -180,6 +180,32 @@ struct DeclaredBackgroundItemsTests {
#expect(item("org.example.stray", .init(path: stray.path(percentEncoded: false), program: "/bin/sh"), exclusions: Exclusions(paths: [stray])) == nil)
}
+ /// A job an app registered is known only while it is loaded, so after Disable and a restart it would leave the
+ /// list with no way to Enable it. launchd keeps the override by label, and an override that disables a job no row
+ /// lists yet, of an app that is installed, is a row of its own. Apple's labels and a gone app's stay out.
+ @Test func keepsADisabledJobOfAnInstalledAppListed() throws {
+ let app = InstalledApp(url: URL(filePath: "/Applications/Example.app"), bundleIdentifier: "org.example.app", name: "Example")
+ let ownership = BackgroundItemOwnership(installedApps: [app])
+ let loaded = BackgroundItems.Loaded(
+ user: ["org.example.app.running": nil],
+ system: [:],
+ userDisabled: ["org.example.app.helper": true, "org.example.app.on": false, "org.example.gone.helper": true,
+ "com.apple.example": true, "org.example.app.running": true],
+ systemDisabled: ["org.example.app.daemon": true]
+ )
+
+ let items = BackgroundItems.disabledJobs(in: loaded, listed: [], ownership: ownership)
+
+ #expect(items.map(\.label).sorted() == ["org.example.app.daemon", "org.example.app.helper"])
+ for item in items {
+ #expect(item.isDisabled)
+ #expect(item.state == .notLoaded)
+ #expect(item.source == .app)
+ #expect(item.ownerBundleIdentifier == "org.example.app")
+ }
+ #expect(items.first { $0.label == "org.example.app.daemon" }?.kind == .daemon)
+ }
+
/// One label declared in two folders is two files, and each row opens its own.
@Test func keepsTwoFilesThatDeclareOneLabelApart() throws {
let directory = try TemporaryDirectory()
diff --git a/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift b/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
index a1fe7f50..8f3c13c9 100644
--- a/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
+++ b/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
@@ -271,10 +271,11 @@ struct BackgroundItemDetailView: View {
.contentTransition(.opacity)
}
- /// Start, Stop, Enable, and Disable, which Peel's own helper, a job under one of macOS's labels, and a job whose
- /// state Peel could not read don't have.
+ /// Start, Stop, Enable, and Disable, which Peel's own helper, a job under one of macOS's labels, a job whose
+ /// state Peel could not read, and a job loaded from a file elsewhere don't have. Disabled, that last one would
+ /// leave the list at the next logout or restart with nothing left to Enable it from.
private var hasOwnControls: Bool {
- !item.isPeelsHelper && !item.usesALabelOfMacOS && item.state != .unknown
+ !item.isPeelsHelper && !item.usesALabelOfMacOS && item.state != .unknown && item.source != .otherFile
}
private var controls: some View {