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.

Read a background job's signature once while its program stays the same

Țugui Dragoș9b112e1245d8
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift
index 82ff0e87..815fe8ec 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift
@@ -18,6 +18,28 @@ public struct BackgroundItemOwnership: Sendable {
public var isInstalled: Bool { url != nil }
}
+ /// The team each program is signed by, read once while its file stays the same: a page scans every job's program
+ /// each time Peel comes forward, and checking a signature takes milliseconds.
+ final class ProgramTeams: Sendable {
+ static let shared = ProgramTeams { CodeSignature.teamIdentifier(at: URL(filePath: $0)) }
+
+ private let known = Mutex<[String: (file: FileIdentity, team: String?)]>([:])
+ private let read: @Sendable (String) -> String?
+
+ init(read: @escaping @Sendable (String) -> String?) {
+ self.read = read
+ }
+
+ func team(of program: String) -> String? {
+ guard let file = FileIdentity.of(URL(filePath: program)) else { return read(program) }
+ let last = known.withLock { $0[program] }
+ if let last, last.file == file { return last.team }
+ let team = read(program)
+ known.withLock { $0[program] = (file, team) }
+ return team
+ }
+ }
+
private let byIdentifier: [String: InstalledApp]
private let byTeam: [String: [InstalledApp]]
private let teamOfProgram: @Sendable (String) -> String?
@@ -25,7 +47,7 @@ public struct BackgroundItemOwnership: Sendable {
private let declared: DeclaredJobs
public init(installedApps: [InstalledApp]) {
- self.init(installedApps: installedApps) { CodeSignature.teamIdentifier(at: URL(filePath: $0)) }
+ self.init(installedApps: installedApps, teamOfProgram: ProgramTeams.shared.team(of:))
}
/// For tests: `teamOfProgram` stands in for reading each program's code signature.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift
index 700c942a..b8e38cb3 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift
@@ -260,4 +260,39 @@ struct BackgroundItemOwnershipTests {
#expect(JobDefinition(["Program": "/usr/local/bin/tool"]) == nil)
}
+
+ @Test func readsAProgramsTeamOnceWhileTheFileStaysTheSame() throws {
+ let directory = try TemporaryDirectory()
+ let program = try directory.file("agent", bytes: 16).path(percentEncoded: false)
+ let reads = Mutex(0)
+ let teams = BackgroundItemOwnership.ProgramTeams { _ in
+ reads.withLock { $0 += 1 }
+ return "ORGEXAMPLE"
+ }
+
+ let first = teams.team(of: program)
+ let second = teams.team(of: program)
+ let readsOfTheSameFile = reads.withLock { $0 }
+ try Data(count: 32).write(to: URL(filePath: program))
+ let third = teams.team(of: program)
+ let readsOnceWritten = reads.withLock { $0 }
+
+ #expect([first, second, third] == ["ORGEXAMPLE", "ORGEXAMPLE", "ORGEXAMPLE"])
+ #expect(readsOfTheSameFile == 1)
+ #expect(readsOnceWritten == 2)
+ }
+
+ @Test func readsTheTeamOfAProgramItCanNotIdentifyEveryTime() {
+ let reads = Mutex(0)
+ let teams = BackgroundItemOwnership.ProgramTeams { _ in
+ reads.withLock { $0 += 1 }
+ return nil
+ }
+
+ _ = teams.team(of: "/org.example/gone")
+ _ = teams.team(of: "/org.example/gone")
+
+ let timesRead = reads.withLock { $0 }
+ #expect(timesRead == 2)
+ }
}