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.

Ask whether a job's app is excluded when its file moves

Țugui Dragoș8d54a4233357
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
index 138fe261..160415c6 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
@@ -128,6 +128,11 @@ public enum BackgroundItemActions {
stop: (BackgroundItem) async -> Failure?
) 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) {
+ let refused = TrashFailure(url: plist, reason: .guarded(.excluded))
+ return Moved(result: TrashResult(failures: [refused]), stillRunning: nil)
+ }
if item.removalRequiresPrivileges, !isHelperEnabled {
throw .requiresPrivileges
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
index d6a9a87e..d31743ee 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
@@ -68,6 +68,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) }
private let home: String
init(
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index 2f1521dc..935c78a7 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -389,6 +389,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) }
/// 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 b7cb7d4d..fdb6407b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
@@ -47,6 +47,32 @@ struct BackgroundItemActionsTests {
#expect(rows.map(\.canMoveToTrash) == [false, true])
}
+ @Test func movesNoJobOfAnAppExcludedSinceItWasRead() async throws {
+ let plist = URL(filePath: "/Users/x/Library/LaunchAgents/org.example.guard.agent.plist")
+ let job = BackgroundItem(
+ label: "org.example.guard.agent", kind: .agent, source: .userLibrary, plistURL: plist,
+ program: "/Applications/x", runsAtLoad: true, keepsAlive: false, ownerBundleIdentifier: "org.example.guard",
+ ownerName: nil, ownerURL: nil, isOrphan: false, state: .notLoaded, isDisabled: false
+ )
+ let moved = Mutex<[URL]>([])
+ let service = TrashService(
+ environment: SearchEnvironment(
+ homeDirectory: URL(filePath: "/Users/x"), rootDirectory: URL(filePath: "/")
+ ),
+ exclusions: Exclusions(bundleIdentifiers: ["org.example.guard"])
+ ) { url in
+ moved.withLock { $0.append(url) }
+ return url
+ }
+
+ let result = try await BackgroundItemActions.moveToTrash(
+ job, isHelperEnabled: false, trash: service
+ ) { _ in nil }
+
+ #expect(moved.withLock { $0 }.isEmpty)
+ #expect(result.result.failures.map(\.reason) == [.guarded(.excluded)])
+ }
+
@Test func knowsAJobItsGoneAppLeftLoaded() {
let job = { (source: BackgroundItem.Source, owner: URL?, isOrphan: Bool, program: String?) in
BackgroundItem(