Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
An app of the same maker elsewhere keeps what it shares
Țugui Dragoșc8183a10877d
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.mdindex bd4a198f..40c38c57 100644--- a/ARCHITECTURE.md+++ b/ARCHITECTURE.md@@ -64,8 +64,9 @@ Take the most common case, uninstalling an app. Every other page follows the sam2. **Match.** For each file, `LeftoverMatcher` weighs the evidence that it belongs to the app: its bundleidentifier, the identifiers of what the app embeds, its application groups, its signing team, its name. Theanswer is a `LeftoverMatch`: a confidence (`certain`, `likely`, or `possible`), the reason, the other- installed apps that claim the file too, the other copies of the app that use it (any macOS knows, wherever- they are, named by their place), and, when Peel holds it back, why (`HoldBack`).+ installed apps that claim the file too (with the apps of the same maker that macOS knows outside the+ Applications folders), the other copies of the app that use it (any macOS knows, wherever they are, named by+ their place), and, when Peel holds it back, why (`HoldBack`).3. **Measure.** `FileSize` walks each folder on a thread of its own, with a time budget. A folder that doesn'tanswer in time, or that macOS won't open, has an unknown size, never zero, and every list shows it that way.4. **Plan.** The page shows every match with its reason. Only `certain` and `likely` matches that nothing elsediff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swiftindex 54ba5e0c..4b8bd4db 100644--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift@@ -20,6 +20,31 @@ public enum AppInspector {}}+ /// The identifiers of the apps Spotlight has indexed whose identifier begins with `prefix`, wherever they are, such+ /// as every app of one maker. Empty when Spotlight is off, has none, or does not answer within `budget`, and for a+ /// prefix that is not made of an identifier's characters, since it goes into the query as it is.+ static func indexedIdentifiers(+ beginningWith prefix: String,+ within budget: TimeInterval = spotlightBudget+ ) async -> Set<String> {+ guard !prefix.isEmpty, prefix.allSatisfy({ $0.isASCII && ($0.isLetter || $0.isNumber || "._-".contains($0)) }) else { return [] }+ let text = "kMDItemContentTypeTree == \"com.apple.application-bundle\" && kMDItemCFBundleIdentifier == \"\(prefix)*\"c"+ let found = await SlowRead.answer(within: budget) { _ -> Set<String> in+ guard let query = MDQueryCreate(kCFAllocatorDefault, text as CFString, nil, nil),+ MDQueryExecute(query, CFOptionFlags(kMDQuerySynchronous.rawValue))+ else { return [] }+ return Set((0..<MDQueryGetResultCount(query)).compactMap { index in+ guard let result = MDQueryGetResultAtIndex(query, index) else { return nil }+ return MDItemCopyAttribute(Unmanaged<MDItem>.fromOpaque(result).takeUnretainedValue(), kMDItemCFBundleIdentifier) as? String+ })+ }+ return found ?? []+ }++ /// How long a scan waits for Spotlight. It answers a query on one attribute at once unless it is rebuilding its+ /// index, and a scan can do without the apps it would name.+ private static let spotlightBudget: TimeInterval = 2+public static func inspect(_ url: URL) -> InstalledApp? {let contents = url.appending(path: "Contents", directoryHint: .isDirectory)guarddiff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swiftindex 49783c9d..f86656a0 100644--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift@@ -31,7 +31,8 @@ struct LeftoverMatcher: Sendable {self.registeredApp = registeredApp}- func match(fileName: String, kind: SearchLocation.Kind) -> LeftoverMatch? {+ /// This app's claim on the item called `fileName` in a location of `kind`, found at `url` when the caller knows it.+ func match(fileName: String, kind: SearchLocation.Kind, at url: URL? = nil) -> LeftoverMatch? {let key = Self.key(from: fileName, kind: kind)// A hidden file outside the home folder is the system's own: `.GlobalPreferences.plist` is the global domain.guard !key.hasPrefix(".") else { return nil }@@ -47,7 +48,13 @@ struct LeftoverMatcher: Sendable {sharedWith.insert(other.bundleIdentifier)}}- for other in others {+ // An app inside the item goes with it, such as an agent or an update an app keeps in its own folder.+ let item = url.map(PathPattern.comparablePath)+ func goesWithTheItem(_ other: Profile) -> Bool {+ guard let item, other.path.hasPrefix(item) else { return false }+ return PathComponents.isPath(other.path, atOrInside: item)+ }+ for other in others where !goesWithTheItem(other) {guard let rival = other.evidence(for: candidate) else {// Siblings from the same maker, like Firefox Nightly beside Firefox, use the same files.if target.isSibling(of: other), other.sharesName(with: candidate) {@@ -228,6 +235,8 @@ extension LeftoverMatcher {static let appleTeam = "59gab85efg."let url: URL+ /// Where the bundle is, spelled to be compared (`PathPattern.comparablePath`).+ let path: Stringlet bundleIdentifier: Stringlet identifier: Stringlet embeddedIdentifiers: [EmbeddedIdentifier]@@ -241,6 +250,7 @@ extension LeftoverMatcher {init(_ app: InstalledApp) {url = app.url+ path = PathPattern.comparablePath(of: app.url)bundleIdentifier = app.bundleIdentifieridentifier = app.bundleIdentifier.lowercased()let vendor = Identifier.vendor(of: identifier)diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swiftindex ed3dd074..4409424b 100644--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift@@ -44,17 +44,37 @@ public struct LeftoverScanner: Sendable {/// What tells `app`'s files from those of every other app. The apps macOS ships are rivals too: Apple keeps/// folders under their names (`Application Support/Music`) that an app of the same name would otherwise get- /// outright. So is every other copy of the app macOS knows, such as an older one kept in Downloads or one on- /// another disk, since it carries the app's identifier and uses the same files.+ /// outright. So are the apps macOS knows outside the list that may use the same files (`appsElsewhere`).func matcher(for app: InstalledApp, installedApps: [InstalledApp]) async -> LeftoverMatcher {let known = installedApps + (await AppCatalog.systemApps.value)+ return LeftoverMatcher(app: app, installedApps: known + (await appsElsewhere(like: app, besides: known)))+ }++ /// The apps macOS knows, outside `known`, that may use `app`'s files: every other copy of it, such as an older one+ /// kept in Downloads or one on another disk, and the apps of the same maker, such as a Nightly build beside it.+ /// Spotlight names the maker's apps and Launch Services says where each is installed, so a copy inside a backup+ /// never counts. Apple's own apps are left to the list of what macOS ships, which Spotlight would name whole.+ private func appsElsewhere(like app: InstalledApp, besides known: [InstalledApp]) async -> [InstalledApp] {+ var identifiers = [app.bundleIdentifier]+ if !ProtectedData.isApplesName(app.bundleIdentifier), let vendor = Identifier.vendor(of: app.bundleIdentifier) {+ let own = app.bundleIdentifier.lowercased()+ identifiers += await AppInspector.indexedIdentifiers(beginningWith: vendor + ".").filter { $0.lowercased() != own }.sorted()+ }+ let wanted = Set(identifiers.map { $0.lowercased() })let bundle = PathPattern.comparablePath(of: PathPattern.canonical(app.url))- let listed = Set(known.filter { $0.bundleIdentifier == app.bundleIdentifier }.map { PathPattern.comparablePath(of: PathPattern.canonical($0.url)) })- let elsewhere = AppInspector.applicationURLs(forBundleIdentifier: app.bundleIdentifier).filter { url in+ let listed = Set(known.filter { wanted.contains($0.bundleIdentifier.lowercased()) }.map { PathPattern.comparablePath(of: PathPattern.canonical($0.url)) })+ return identifiers.flatMap(AppInspector.applicationURLs).filter { url inlet path = PathPattern.comparablePath(of: PathPattern.canonical(url))- return !listed.contains(path) && !PathComponents.isPath(path, atOrInside: bundle)+ return !listed.contains(path) && !PathComponents.isPath(path, atOrInside: bundle) && Self.standsOnItsOwn(path, in: environment)+ }.compactMap(AppInspector.inspect)+ }++ /// True for an app at `path` that a person keeps on its own. One inside the home's Library or the Mac's is part of+ /// another app (a helper, an agent, an update being prepared, a build) and goes with that app's folder.+ static func standsOnItsOwn(_ path: String, in environment: SearchEnvironment) -> Bool {+ ![environment.homeDirectory, environment.rootDirectory].contains { folder in+ PathComponents.isPath(path, inside: PathPattern.comparablePath(of: folder.appending(path: "Library", directoryHint: .isDirectory)))}- return LeftoverMatcher(app: app, installedApps: known + elsewhere.compactMap(AppInspector.inspect))}@concurrent@@ -215,7 +235,7 @@ public struct LeftoverScanner: Sendable {for name in names {let url = folder.appending(path: name)let path = url.path(percentEncoded: false)- if let match = matcher.match(fileName: name, kind: kind), match.confidence >= .likely,+ if let match = matcher.match(fileName: name, kind: kind, at: url), match.confidence >= .likely,!isSharedWithTheWholeMac(url, home: home) {guard !refuses(path, home) else { continue }found.append(await leftover(at: url, kind: kind, match: match, parent: parent, home: home, isInsideAnotherAppsFolder: isAnotherApps, measure: measure))@@ -308,11 +328,11 @@ public struct LeftoverScanner: Sendable {bundle: String) -> LeftoverMatch? {guard kind != .commandLineTools else { return leadsInside(bundle, link: url) }- let byName = matcher.match(fileName: name, kind: kind)+ let byName = matcher.match(fileName: name, kind: kind, at: url)guard let inside = runsSomethingInside(bundle, job: url, kind: kind) else {guard byName == nil, let identifier = DeclaredIdentifier.of(url, kind: kind) else { return byName }// `.elsewhere`, so no extension is taken off the identifier as if it were a file name.- return matcher.match(fileName: identifier, kind: .elsewhere)+ return matcher.match(fileName: identifier, kind: .elsewhere, at: url)}return byName?.confidence == .certain ? byName : inside}diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift b/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swiftindex cc5d84b2..2a36b30d 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift@@ -101,7 +101,7 @@ public struct Uninstallation: Sendable {let bundle = PathPattern.comparablePath(of: app.url)for item in evidence.items where !exclusions.excludes(item.url) && !isAtOrInside(bundle, item.url) {let place = place(of: item.url, in: environment)- let own = matcher.match(fileName: item.url.lastPathComponent, kind: place.kind)+ let own = matcher.match(fileName: item.url.lastPathComponent, kind: place.kind, at: item.url)// Every folder on the way counts: a path inside a folder another app claims is not this app's alone.let claims = place.components.map { matcher.othersClaiming(fileName: $0, kind: place.kind) }let leftover = await LeftoverScanner.leftover(diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swiftindex 603b0c0a..3614f247 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift@@ -29,6 +29,26 @@ struct AppInspectorTests {}/// Reads `/usr/bin/true`, the real binary the `universal` fixture above is modeled on.+ /// Spotlight names the apps whose identifier begins with a maker's, wherever they are: every Mac has the+ /// dictation input method in its input methods folder, outside the Applications folders. A Mac with Spotlight off+ /// answers nothing, and the test waits for one that indexes. The other tests' scans ask Spotlight at the same+ /// time, so this one is given longer than a scan waits.+ @Test(.enabled("Spotlight indexes this Mac") { await !AppInspector.indexedIdentifiers(beginningWith: "com.apple.", within: 60).isEmpty })+ func spotlightNamesTheAppsOfOneMaker() async {+ let found = await AppInspector.indexedIdentifiers(beginningWith: "com.apple.inputmethod.", within: 60)++ #expect(found.contains("com.apple.inputmethod.ironwood"))+ #expect(found.allSatisfy { $0.lowercased().hasPrefix("com.apple.inputmethod.") })+ }++ /// A bundle writes its own identifier, and a maker's prefix taken from it goes into a Spotlight query. One that+ /// could change the query asks nothing.+ @Test func aPrefixThatCouldChangeTheQueryAsksNothing() async {+ for prefix in ["com.example\" || kMDItemFSName == \"*", "com.example *", "", "com.example.\\"] {+ #expect(await AppInspector.indexedIdentifiers(beginningWith: prefix).isEmpty, "\(prefix)")+ }+ }+@Test func readsARealBinaryTheSameWay() {#expect(MachOHeader.architectures(ofExecutableAt: URL(filePath: "/usr/bin/true")).contains(.arm64))}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverMatcherTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverMatcherTests.swiftindex b3d0c9f4..d092ffc2 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverMatcherTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverMatcherTests.swift@@ -66,6 +66,26 @@ struct LeftoverMatcherTests {#expect(updater.isRecommended)}+ /// An app that sits inside the item goes with it, so it keeps none of it: an agent or an update the app keeps in+ /// its own support folder does not make that folder shared. The same apps kept anywhere else do.+ @Test func anAppInsideTheItemKeepsNoneOfIt() throws {+ let sketchpad = app("org.example.Sketchpad", name: "Sketchpad")+ let folder = URL(filePath: "/Users/x/Library/Application Support/Sketchpad")+ func claim(besides rivals: [InstalledApp]) throws -> LeftoverMatch {+ try #require(LeftoverMatcher(app: sketchpad, installedApps: [sketchpad] + rivals).match(fileName: "Sketchpad", kind: .applicationSupport, at: folder))+ }+ func agent(at url: URL) -> InstalledApp {+ InstalledApp(url: url, bundleIdentifier: "org.example.agent", name: "SketchpadAgent")+ }+ func update(at url: URL) -> InstalledApp {+ InstalledApp(url: url, bundleIdentifier: sketchpad.bundleIdentifier, name: "Sketchpad")+ }++ #expect(try claim(besides: [agent(at: folder.appending(path: "SketchpadAgent.app")), update(at: folder.appending(path: "Updates/Sketchpad.app"))]).isRecommended)+ #expect(try claim(besides: [agent(at: URL(filePath: "/Volumes/Disk/SketchpadAgent.app"))]).sharedWith == ["org.example.agent"])+ #expect(try claim(besides: [update(at: URL(filePath: "/Users/x/Downloads/Sketchpad.app"))]).otherCopies.count == 1)+ }+@Test(arguments: [("com.spotify.client.plist", SearchLocation.Kind.preferences),("COM.SPOTIFY.CLIENT", .caches),diff --git a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swiftindex 30c5d795..7946228e 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift@@ -91,6 +91,19 @@ struct LeftoverScannerTests {}}+ /// An app inside the home's Library or the Mac's is part of another app, such as an agent kept in its maker's+ /// support folder or a build, so it keeps no file of an app it resembles. macOS's own Library is not one of them.+ @Test func anAppInALibraryFolderIsPartOfAnotherApp() {+ let mac = SearchEnvironment(homeDirectory: URL(filePath: "/Users/x"), rootDirectory: URL(filePath: "/"))++ #expect(!LeftoverScanner.standsOnItsOwn("/Users/x/Library/Application Support/Sketchpad/SketchpadAgent.app", in: mac))+ #expect(!LeftoverScanner.standsOnItsOwn("/Library/Application Support/Sketchpad/Updater.app", in: mac))+ #expect(LeftoverScanner.standsOnItsOwn("/Users/x/Downloads/Sketchpad Nightly.app", in: mac))+ #expect(LeftoverScanner.standsOnItsOwn("/Volumes/Disk/Sketchpad.app", in: mac))+ #expect(LeftoverScanner.standsOnItsOwn("/System/Library/Input Methods/CharacterPalette.app", in: mac))+ #expect(LeftoverScanner.standsOnItsOwn("/Users/x/Libraryish/Sketchpad.app", in: mac))+ }+/// The guard, which reads every spelling of a path from the disk, is asked only about what the scan would take/// or walk into, and what it refuses stays out.@Test func asksTheGuardOnlyAboutWhatItWouldTakeOrWalkInto() async throws {diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swiftindex ffd3b568..bcc00003 100644--- a/Peel/Model/AppLibrary.swift+++ b/Peel/Model/AppLibrary.swift@@ -631,10 +631,15 @@ final class AppLibrary {AppCatalog.app(at: url, among: apps)}+ /// The name of the app with `bundleIdentifier`: the listed app's, or the one Finder shows for an app macOS knows+ /// elsewhere, such as a helper or a Nightly build on another disk. The identifier itself when macOS knows none.func name(forBundleIdentifier bundleIdentifier: String) -> String {if lastNames?.revision != revision {lastNames = (revision, Dictionary(apps.map { ($0.bundleIdentifier, $0.name) }, uniquingKeysWith: { first, _ in first }))}- return lastNames?.names[bundleIdentifier] ?? bundleIdentifier+ if let name = lastNames?.names[bundleIdentifier] { return name }+ let name = AppInspector.applicationURL(forBundleIdentifier: bundleIdentifier).map(AppInspector.displayName(of:)) ?? bundleIdentifier+ lastNames?.names[bundleIdentifier] = name+ return name}}diff --git a/SAFETY.md b/SAFETY.mdindex 6a5c6d42..e96150b9 100644--- a/SAFETY.md+++ b/SAFETY.md@@ -131,13 +131,14 @@ root of another disk), asked of the folder itself with every link resolved: a foOnly matches Peel is `certain` or `likely` about, and only when nothing else installed on the Mac uses them.Anything shared with another app is shown and left unselected, and so is what another copy of the app uses: one-macOS knows anywhere, such as an older copy in Downloads or one on another disk, is named by where it is. Something-matched by the app's name alone inside another app's folder, or one of Apple's, is shown and never selected, since-it is most likely that app's data about this one; so is something matched by name alone at the top of your home-folder or of a Library, where a name is only a guess. And nothing at all is selected for an app that would stay: one-macOS keeps, one the helper may not move, or one that needs the helper while it cannot act. When you remove several-apps at once and deselect one of them, it stays too: its files leave the selection, those it shares with the other-apps included, and selecting it again brings them back.+macOS knows anywhere, such as an older copy in Downloads or one on another disk, is named by where it is. An app of+the same maker kept outside the Applications folders, such as a Nightly build on another disk, counts like one+inside them. Something matched by the app's name alone inside another app's folder, or one of Apple's, is shown and+never selected, since it is most likely that app's data about this one; so is something matched by name alone at the+top of your home folder or of a Library, where a name is only a guess. And nothing at all is selected for an app+that would stay: one macOS keeps, one the helper may not move, or one that needs the helper while it cannot act.+When you remove several apps at once and deselect one of them, it stays too: its files leave the selection, those it+shares with the other apps included, and selecting it again brings them back.Peel itself is listed on its own page with what it keeps, and nothing of it can be selected there or among otherapps. It is removed only by Remove Peel, in Settings, which takes its helper and login item away first.