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.

List in Orphaned Files a browser's manifest that starts nothing

Țugui Dragoșfd9fe5b6938f
diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstrings
index 67c46c0b..21752173 100644
--- a/Localization/Peel/Localizable.xcstrings
+++ b/Localization/Peel/Localizable.xcstrings
@@ -161807,6 +161807,112 @@
}
}
},
+ "These start a program that is no longer there" : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Tyto soubory spouštějí program, který už neexistuje"
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Diese starten ein Programm, das nicht mehr vorhanden ist"
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Estos archivos inician un programa que ya no está"
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Ces fichiers lancent un programme qui n’est plus là"
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Questi file avviano un programma che non c’è più"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "これらは、もう存在しないプログラムを起動します"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "이 항목이 시작하는 프로그램이 더 이상 없음"
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Deze starten een programma dat er niet meer is"
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Te pliki uruchamiają program, którego już nie ma"
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Estes itens iniciam um programa que não existe mais"
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Acestea pornesc un program care nu mai există"
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Они запускают программу, которой больше нет"
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "De här startar ett program som inte längre finns"
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Bunlar artık yerinde olmayan bir programı başlatıyor"
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Ці елементи запускають виконуваний файл, якого вже немає"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "这些文件用于启动一个已不存在的程序"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "這些項目所啟動的程式已不存在"
+ }
+ }
+ }
+ },
"These tools install with Homebrew. Run this in Terminal: its script says what it will do and waits before doing it." : {
"localizations" : {
"cs" : {
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift
index 84c8f876..2646dccd 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift
@@ -385,6 +385,7 @@ extension OrphanConfidence {
case .untouched(let months): "\(months) months since anything wrote here"
case .nothingClaimsIt, nil: "nothing installed claims these"
case .installedForAnAppThatIsGone(let name): "macOS installed these for \(Output.plain(name)), which is gone"
+ case .startsAProgramThatIsGone: "these start a program that is no longer there"
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/NativeMessagingManifest.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/NativeMessagingManifest.swift
index d7035df5..72094d29 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/NativeMessagingManifest.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/NativeMessagingManifest.swift
@@ -11,4 +11,15 @@ enum NativeMessagingManifest {
else { return nil }
return program
}
+
+ /// Whether the manifest at `url` starts nothing: a link that leads nowhere, or a manifest naming a program that is
+ /// not there, on a disk that is connected.
+ static func startsNothing(at url: URL) -> Bool {
+ let path = url.path(percentEncoded: false)
+ if (try? FileManager.default.destinationOfSymbolicLink(atPath: path)) != nil {
+ return !FileManager.default.fileExists(atPath: path)
+ }
+ guard let program = program(at: url) else { return false }
+ return !FileManager.default.fileExists(atPath: program) && !URL(filePath: program).isOnADiskNotConnected
+ }
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanConfidence.swift b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanConfidence.swift
index c3b10eee..cdf19e50 100644
--- a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanConfidence.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanConfidence.swift
@@ -12,8 +12,9 @@ public struct OrphanConfidence: Sendable, Hashable {
/// Nothing installed claims it.
case likely
/// Nothing claims it, and Peel knows why: it saw the app go and nothing wrote here after, nothing has
- /// written here in months, the files are links into an app that is gone, or they are system extension
- /// copies of an app that is gone, which macOS removes at the next restart.
+ /// written here in months, the files are links into an app that is gone or browser manifests that start a
+ /// program that is gone, or they are system extension copies of an app that is gone, which macOS removes at
+ /// the next restart.
case certain
public static func < (one: Level, other: Level) -> Bool { one.rawValue < other.rawValue }
@@ -41,6 +42,8 @@ public struct OrphanConfidence: Sendable, Hashable {
case nothingClaimsIt
/// macOS's record of these system extension copies names the app they were installed for, which is gone.
case installedForAnAppThatIsGone(name: String)
+ /// Browser manifests whose program is gone, so they start nothing.
+ case startsAProgramThatIsGone
}
/// A write this recent means something may still be running. A week covers a Mac left unused over a weekend.
@@ -71,6 +74,10 @@ public struct OrphanConfidence: Sendable, Hashable {
if group.items.allSatisfy(\.kind.isForLinks) {
return OrphanConfidence(level: .certain, reasons: [.leadsIntoAnAppThatIsGone])
}
+ // Only a browser's manifest that starts nothing is listed from the folders browsers read them from.
+ if group.items.allSatisfy({ $0.kind == .nativeMessagingHosts }) {
+ return OrphanConfidence(level: .certain, reasons: [.startsAProgramThatIsGone])
+ }
// macOS's own record of a system extension says which app it was installed for. macOS may still run a copy
// it keeps turned on, so only copies it removes at the next restart are surely unused.
if let app = group.installedFor {
diff --git a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
index d9938b03..ed0c5427 100644
--- a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
@@ -87,12 +87,16 @@ public struct OrphanScanner: Sendable {
let results = await withTaskGroup(of: (location: SearchLocation, result: LocationResult).self) { group in
let home = environment.homeDirectory.path(percentEncoded: false)
- // A browser's manifest is an app's only through the program it names (`servesAProgram`). Safari's web app
- // container is another app's container, which macOS asks about, and is read only for one of its web apps.
- let skipped: Set<SearchLocation.Kind> = [.nativeMessagingHosts, .safariWebApps]
- for location in environment.locations where !skipped.contains(location.kind) {
+ // Safari's web app container is another app's container, which macOS asks about, and is read only for one
+ // of its web apps.
+ for location in environment.locations where location.kind != .safariWebApps {
_ = group.addTaskUnlessCancelled { [exclusions, nestedFolderLimit, walk] in
- (location, await Self.scan(
+ // A browser's manifest is an app's only through the program it names (`servesAProgram`).
+ guard location.kind != .nativeMessagingHosts else {
+ let found = await Self.manifests(in: location, home: home, exclusions: exclusions, walk: walk)
+ return (location, found)
+ }
+ return (location, await Self.scan(
location, ownership: ownership, jobs: jobs, goneBundles: goneBundles, goneApps: goneApps,
goneNames: goneNames, goneFolders: goneFolders, home: home, exclusions: exclusions,
nestedFolderLimit: nestedFolderLimit, walk: walk
@@ -181,9 +185,46 @@ public struct OrphanScanner: Sendable {
/// Whether `url` is a manifest in a folder browsers read native messaging manifests from that still serves its
/// program: one that is there, or that can't be told. Whatever it is named for, it is no orphan then.
static func servesAProgram(_ url: URL, in folders: Set<String>) -> Bool {
- guard folders.contains(PathPattern.comparablePath(of: url.deletingLastPathComponent())) else { return false }
- guard let program = NativeMessagingManifest.program(at: url) else { return true }
- return FileManager.default.fileExists(atPath: program)
+ folders.contains(PathPattern.comparablePath(of: url.deletingLastPathComponent()))
+ && !NativeMessagingManifest.startsNothing(at: url)
+ }
+
+ /// The manifests in a folder browsers read native messaging manifests from that start nothing, each under its own
+ /// name, which is the name of the host it starts.
+ private static func manifests(
+ in location: SearchLocation,
+ home: String,
+ exclusions: Exclusions,
+ walk: @escaping LeftoverScanner.Measure
+ ) async -> LocationResult {
+ let entries: [String]
+ do {
+ entries = try FileManager.default.contentsOfDirectory(atPath: location.url.path(percentEncoded: false))
+ .sorted()
+ } catch CocoaError.fileReadNoSuchFile {
+ return .found([], cutShort: nil, unreadable: [])
+ } catch {
+ return .unreadable(location)
+ }
+ let parent = ParentAccess(location.url)
+ var found: [(identifier: String, item: OrphanItem)] = []
+ for name in entries {
+ let url = location.url.appending(path: name)
+ let host = (name as NSString).deletingPathExtension
+ guard NativeMessagingManifest.startsNothing(at: url), !ProtectedData.isApplesName(host),
+ !FileAccess.cannotBeRemoved(url), !ProtectedData.refuses(url.path(percentEncoded: false), home: home),
+ !exclusions.excludes(url)
+ else { continue }
+ let contents = await walk(url)
+ found.append((host, OrphanItem(
+ url: url,
+ kind: location.kind,
+ size: contents.flatMap { $0.couldNotBeRead ? nil : $0.size },
+ modificationDate: contents?.newestChange,
+ requiresPrivileges: parent.requiresPrivileges(toRemove: url)
+ )))
+ }
+ return .found(found, cutShort: nil, unreadable: [])
}
/// The apps installed now, with those `remembered` holds that are only out of sight, and the remembered apps
@@ -217,7 +258,9 @@ public struct OrphanScanner: Sendable {
let goneFolders = GoneFolders(gone, installedApps: installedApps, home: environment.homeDirectory)
var kept: [OrphanItem] = []
for item in items where !Self.servesAProgram(item.url, in: folders) {
- let isStillOrphaned = if item.productFolder != nil {
+ let isStillOrphaned = if item.kind == .nativeMessagingHosts {
+ NativeMessagingManifest.startsNothing(at: item.url)
+ } else if item.productFolder != nil {
Self.goneApp(owningFolder: item.url, kind: item.kind, among: goneFolders, ownership: ownership) != nil
} else if let app = item.namedAfter {
await Self.goneApp(
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift
index bf6f46f6..3aff8db8 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift
@@ -180,6 +180,38 @@ struct OrphanScannerTests {
#expect(await scanner.stillOrphaned([item], installedApps: installed + [back], remembered: [editor]).isEmpty)
}
+ @Test func listsABrowsersManifestThatStartsNothing() async throws {
+ let directory = try TemporaryDirectory()
+ let hosts = "root/Library/Google/Chrome/NativeMessagingHosts"
+ let program = try directory.file("programs/host")
+ let manifest = { (name: String, program: String) in
+ _ = try directory.file("\(hosts)/\(name).json", contents: Data(#"{"name": "\#(name)", "path": "\#(program)"}"#.utf8))
+ }
+ try manifest("org.example.gone.host", "/Library/PrivilegedHelperTools/Gone.app/Contents/MacOS/host")
+ try manifest("org.example.here.host", program.path(percentEncoded: false))
+ let link = { (name: String, target: URL) in
+ try FileManager.default.createSymbolicLink(
+ at: directory.url.appending(path: "\(hosts)/\(name).json"), withDestinationURL: target
+ )
+ }
+ try link("org.example.nowhere.host", directory.url.appending(path: "root/Applications/Gone.app/host.json"))
+ try link("org.example.somewhere.host", directory.url.appending(path: "\(hosts)/org.example.here.host.json"))
+ try link("com.apple.example.host", directory.url.appending(path: "root/System/host.json"))
+
+ let scanner = scanner(in: directory)
+ let scan = await scanner.scan(installedApps: installed)
+ let groups = Dictionary(scan.groups.map { ($0.identifier, $0) }, uniquingKeysWith: { first, _ in first })
+ let items = scan.groups.flatMap(\.items)
+
+ #expect(Set(groups.keys) == ["org.example.gone.host", "org.example.nowhere.host"])
+ let startsNothing = OrphanConfidence(level: .certain, reasons: [.startsAProgramThatIsGone])
+ #expect(groups.values.allSatisfy { $0.confidence == startsNothing })
+ #expect(await scanner.stillOrphaned(items, installedApps: installed, remembered: []).count == 2)
+ try directory.file("root/Applications/Gone.app/host.json")
+ let still = await scanner.stillOrphaned(items, installedApps: installed, remembered: [])
+ #expect(still.map { $0.url.lastPathComponent } == ["org.example.gone.host.json"])
+ }
+
@Test func listsAFileTwoPlacesReachOnce() async throws {
let directory = try TemporaryDirectory()
let recents = "home/Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments"
diff --git a/Peel/Views/Presentation.swift b/Peel/Views/Presentation.swift
index 8009d1dd..34951310 100644
--- a/Peel/Views/Presentation.swift
+++ b/Peel/Views/Presentation.swift
@@ -381,6 +381,8 @@ extension OrphanConfidence {
Text("No installed app claims these")
case .installedForAnAppThatIsGone(let name):
Text("macOS installed these for \(name), which is gone")
+ case .startsAProgramThatIsGone:
+ Text("These start a program that is no longer there")
}
}