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.

Keep what is named for a background job that still runs out of Orphaned Files

Țugui Dragoș723080e928d0
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift
index 2f861b0d..780c0c94 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift
@@ -20,6 +20,7 @@ public struct BackgroundItemOwnership: Sendable {
private let byIdentifier: [String: InstalledApp]
private let byTeam: [String: [InstalledApp]]
private let teamOfProgram: @Sendable (String) -> String?
+ private var liveLabels: Set<String> = []
public init(installedApps: [InstalledApp]) {
self.init(installedApps: installedApps) { CodeSignature.teamIdentifier(at: URL(filePath: $0)) }
@@ -100,6 +101,25 @@ public struct BackgroundItemOwnership: Sendable {
return isOrphan(label: job.label, program: job.program, owner: owner)
}
+ /// This ownership, also knowing the jobs defined in `folders` that are not orphans: a helper keeps its settings
+ /// beside its job, named for the job's label, while the app behind it may have another identifier.
+ func knowingJobs(in folders: [URL]) -> BackgroundItemOwnership {
+ var knowing = self
+ let files = folders.flatMap {
+ (try? FileManager.default.contentsOfDirectory(at: $0, includingPropertiesForKeys: nil)) ?? []
+ }
+ knowing.liveLabels = Set(
+ files.filter { $0.pathExtension == "plist" }.compactMap(JobDefinition.init(contentsOf:))
+ .filter { !isOrphan($0) }.map { $0.label.lowercased() }
+ )
+ return knowing
+ }
+
+ func ownsItem(named identifier: String) -> Bool {
+ let name = identifier.lowercased()
+ return liveLabels.contains { name == $0 || name.hasPrefix($0 + ".") }
+ }
+
/// The folders launchd searches when the first item of `ProgramArguments` is a relative path.
/// `man launchd.plist` says such a path "is resolved using _PATH_STDPATH", which `paths.h` defines as these four.
private static let standardPath = ["/usr/bin", "/bin", "/usr/sbin", "/sbin"]
diff --git a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
index 8676e9ee..7c6833c6 100644
--- a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
@@ -43,6 +43,10 @@ public struct OrphanScanner: Sendable {
self.systemExtensions = systemExtensions
}
+ private var launchdFolders: [URL] {
+ environment.locations.filter { $0.kind == .launchAgents || $0.kind == .launchDaemons }.map(\.url)
+ }
+
/// Finds the items that no app claims, in every location, grouped by identifier.
///
/// `installedApps` must be the complete list: anything it does not claim can be reported as orphaned.
@@ -64,7 +68,7 @@ public struct OrphanScanner: Sendable {
let ownership = AppOwnership(
installedApps: installedApps + systemApps, goneApps: goneApps, isRegisteredApp: isRegisteredApp
)
- let jobs = BackgroundItemOwnership(installedApps: installedApps + systemApps)
+ let jobs = BackgroundItemOwnership(installedApps: installedApps + systemApps).knowingJobs(in: launchdFolders)
let goneBundles = Dictionary(
gone.map {
(
@@ -175,7 +179,7 @@ public struct OrphanScanner: Sendable {
goneApps: gone.map { $0.bundleIdentifier },
isRegisteredApp: isRegisteredApp
)
- let jobs = BackgroundItemOwnership(installedApps: installedApps + systemApps)
+ let jobs = BackgroundItemOwnership(installedApps: installedApps + systemApps).knowingJobs(in: launchdFolders)
var kept: [OrphanItem] = []
for item in items {
let isStillOrphaned = if let app = item.namedAfter {
@@ -342,6 +346,7 @@ public struct OrphanScanner: Sendable {
?? DeclaredIdentifier.of(url, kind: kind).flatMap({ orphanIdentifier(forKey: $0, kind: kind) }),
goneApps.map({ cameWithAnAppThatLeft(identifier, goneApps: $0) }) ?? true,
!ownership.isClaimed(fileName: name, kind: kind, identifier: identifier),
+ !jobs.ownsItem(named: identifier),
await !holdsFilesOfAnInstalledApp(url, kind: kind, ownership: ownership),
!isStillAJob(url, kind: kind, jobs: jobs)
else { return nil }
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift
index d9502265..28f14be2 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift
@@ -609,6 +609,29 @@ struct OrphanScannerTests {
#expect(scan.groups.flatMap(\.items).map(\.url.lastPathComponent) == ["org.example.empty.plist"])
}
+ @Test func aFileNamedForAJobThatStillRunsIsThatJobs() async throws {
+ let directory = try TemporaryDirectory()
+ let host = try directory.file("root/Library/PrivilegedHelperTools/Remote Host.app/Contents/MacOS/host")
+ let gone = directory.url.appending(path: "root/opt/gone/bin/agent").path(percentEncoded: false)
+ try directory.file("root/Library/PrivilegedHelperTools/org.example.remote.json")
+ try directory.file("root/Library/PrivilegedHelperTools/org.example.remote.settings.json")
+ try directory.file("root/Library/LaunchAgents/org.example.gone.plist", contents: job("org.example.gone", runs: [gone]))
+ try directory.file("root/Library/PrivilegedHelperTools/org.example.gone.json")
+ let scanner = scanner(in: directory)
+ let before = await scanner.scan(installedApps: installed).groups.flatMap(\.items)
+ try directory.file(
+ "root/Library/LaunchAgents/org.example.remote.plist",
+ contents: job("org.example.remote", runs: [host.path(percentEncoded: false)])
+ )
+
+ let scan = await scanner.scan(installedApps: installed)
+ let still = await scanner.stillOrphaned(before, installedApps: installed, remembered: [])
+
+ let gonesFiles: Set<String> = ["org.example.gone.json", "org.example.gone.plist"]
+ #expect(Set(scan.groups.flatMap(\.items).map(\.url.lastPathComponent)) == gonesFiles)
+ #expect(Set(still.map(\.url.lastPathComponent)) == gonesFiles)
+ }
+
/// A plug-in is named for what it does, so it is listed under the identifier it declares inside, and only when
/// Peel saw the app it came with go: its identifier is that app's, extends it, or is its maker's. One an installer
/// put there on its own, with no app, is no orphan however old it is, and neither is one an installed app claims.
diff --git a/SAFETY.md b/SAFETY.md
index e2b50c30..0cc919a7 100644
--- a/SAFETY.md
+++ b/SAFETY.md
@@ -339,7 +339,8 @@ Orphaned Files selects nothing for you, and `peel orphans --remove` leaves these
exists only on this Mac, or one Peel could not read or measure in time, moves only when you select it yourself.
The copy macOS installed of a system extension, in `/Library/SystemExtensions`, is listed and can't be selected:
only macOS removes it, when the app that uses it goes to the Trash in Finder, and its note says so, or that macOS
-removes it at the next restart.
+removes it at the next restart. A file named for a background job whose program is still there, such as the
+settings a remote access host keeps beside its job, is that job's and is never listed.
On every list, Select All selects every row you can select, what Peel holds back included, and asks first,
counting what Peel doesn't recommend; Return answers Select Recommended. Each held back row still shows its reason