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.

Treat a job as excluded when its app is excluded by its place

Țugui Dragoș146956168e11
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift
index 39c2001d..afd40387 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift
@@ -79,6 +79,12 @@ public struct BackgroundItem: Sendable, Hashable, Identifiable {
public var isOwnerInstalled: Bool { ownerURL != nil }
+ /// Whether `exclusions` keep the job's app, by its identifier or its place, as they keep any app.
+ func isOfAnApp(excludedBy exclusions: Exclusions) -> Bool {
+ ownerBundleIdentifier.map(exclusions.excludes(bundleIdentifier:)) == true
+ || ownerURL.map(exclusions.excludes(_:)) == true
+ }
+
/// True for a job an app submitted that is still loaded after the app went, its program gone or not named by
/// launchctl: the app that would unregister it is gone, so only taking it out of launchd removes it.
public var isLeftByAGoneApp: Bool { source == .app && !isOwnerInstalled && (isOrphan || program == nil) }
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
index 160415c6..3ba2b0c5 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
@@ -129,7 +129,7 @@ public enum BackgroundItemActions {
) async throws(Failure) -> Moved {
guard item.canMoveToTrash, let plist = item.plistURL else { throw .noFileToMove }
// Asked now, with the exclusions this move goes by: the app may have been excluded since the job was read.
- if let owner = item.ownerBundleIdentifier, trash.excludes(bundleIdentifier: owner) {
+ if trash.excludes(ownerOf: item) {
let refused = TrashFailure(url: plist, reason: .guarded(.excluded))
return Moved(result: TrashResult(failures: [refused]), stillRunning: nil)
}
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift
index e7a3bd0f..7cd9d514 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift
@@ -127,7 +127,7 @@ public enum BackgroundItems {
items
.map { item in
var item = item
- item.isExcluded = item.ownerBundleIdentifier.map(exclusions.excludes(bundleIdentifier:)) == true
+ item.isExcluded = item.isOfAnApp(excludedBy: exclusions)
return item
}
.sorted { $0.label.localizedStandardCompare($1.label) == .orderedAscending }
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
index 3e3bdc7d..c0c5d040 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
@@ -84,7 +84,7 @@ struct RemovalGuard: Sendable {
var knowsTheExclusions: Bool { exclusions.isKnown }
func excludes(_ app: InstalledApp) -> Bool { exclusions.excludes(app) }
- func excludes(bundleIdentifier: String) -> Bool { exclusions.excludes(bundleIdentifier: bundleIdentifier) }
+ func excludes(ownerOf item: BackgroundItem) -> Bool { item.isOfAnApp(excludedBy: exclusions) }
private let home: String
init(
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index ce3992a4..b9aead65 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -400,7 +400,7 @@ public struct TrashService: Sendable {
public var knowsTheExclusions: Bool { removalGuard.knowsTheExclusions }
func excludes(_ app: InstalledApp) -> Bool { removalGuard.excludes(app) }
- func excludes(bundleIdentifier: String) -> Bool { removalGuard.excludes(bundleIdentifier: bundleIdentifier) }
+ func excludes(ownerOf item: BackgroundItem) -> Bool { removalGuard.excludes(ownerOf: item) }
/// 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? {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
index 98b68b7c..4581403a 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
@@ -88,6 +88,38 @@ struct BackgroundItemActionsTests {
#expect(rows.map(\.canMoveToTrash) == [false, true])
}
+ @Test func aJobIsExcludedWithAnAppExcludedByItsPlace() async throws {
+ let vendor = URL(filePath: "/Applications/Vendor", directoryHint: .isDirectory)
+ let plist = URL(filePath: "/Users/x/Library/LaunchAgents/org.example.vendor.agent.plist")
+ let job = BackgroundItem(
+ label: "org.example.vendor.agent", kind: .agent, source: .userLibrary, plistURL: plist,
+ program: "/Applications/Vendor/Tool.app/Contents/MacOS/agent", runsAtLoad: true, keepsAlive: false,
+ ownerBundleIdentifier: "org.example.vendor", ownerName: nil,
+ ownerURL: vendor.appending(path: "Tool.app", directoryHint: .isDirectory), isOrphan: false,
+ state: .running(pid: 42), isDisabled: false
+ )
+ let excluded = Exclusions(paths: [vendor])
+ let moved = Mutex<[URL]>([])
+ let service = TrashService(
+ environment: SearchEnvironment(homeDirectory: URL(filePath: "/Users/x"), rootDirectory: URL(filePath: "/")),
+ exclusions: excluded
+ ) { url in
+ moved.withLock { $0.append(url) }
+ return url
+ }
+
+ let row = try #require(BackgroundItems.rows([job], exclusions: excluded).first)
+ let result = try await BackgroundItemActions.moveToTrash(
+ job, isHelperEnabled: false, trash: service
+ ) { _ in nil }
+
+ #expect(row.isExcluded)
+ #expect(!row.canBeControlled)
+ #expect(!row.canMoveToTrash)
+ #expect(moved.withLock { $0 }.isEmpty)
+ #expect(result.result.failures.map(\.reason) == [.guarded(.excluded)])
+ }
+
@Test func movesNoJobOfAnAppExcludedSinceItWasRead() async throws {
let plist = URL(filePath: "/Users/x/Library/LaunchAgents/org.example.guard.agent.plist")
let job = BackgroundItem(