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.

Don't offer an app's leftovers when another copy of it is still installed

Țugui Dragoșd2f199ec32e4
diff --git a/GUIDE.md b/GUIDE.md
index e850b79f..0b3ee360 100644
--- a/GUIDE.md
+++ b/GUIDE.md
@@ -29,9 +29,10 @@ and how to get it, [SAFETY.md](SAFETY.md) what it protects, [PRIVACY.md](PRIVACY
- **Several ways in.** Choose an app in the list, drop one on Peel's Dock icon, select several apps at once, or
Control-click an app in Finder and choose Uninstall with Peel, once the Finder extension is on in System
Settings. With Watch the Trash on, Peel notices when you drag an app to the Trash yourself and offers to clear
- what it left behind, while Peel is open or in the menu bar, and once it has Full Disk Access. Apps you keep
- outside the Applications folders, such as on another disk, join the list once you add their folder in Settings >
- General > App Folders.
+ what it left behind, while Peel is open or in the menu bar, and once it has Full Disk Access. An old copy thrown
+ away while another copy of the app is still installed brings no such offer, since nothing was left behind. Apps
+ you keep outside the Applications folders, such as on another disk, join the list once you add their folder in
+ Settings > General > App Folders.
- **Reset instead of remove.** Clear an app's settings without uninstalling it. Peel saves them first, so you can
put them back, and never selects your own work.
- **Privacy permissions too, if you want.** Peel can also reset the permissions macOS gave the app, such as access
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
index fd88d371..84ffa81b 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
@@ -20,6 +20,16 @@ public enum AppInspector {
}
}
+ /// Whether a copy of the app at `url` other than that one is installed, outside a Trash: one of `installedApps`, or
+ /// one macOS knows, with the same identifier. An app with no valid identifier has no copies to tell apart.
+ public static func anotherCopyIsInstalled(of url: URL, among installedApps: [InstalledApp]) -> Bool {
+ guard let identifier = inspect(url)?.bundleIdentifier, Identifier.isValid(identifier) else { return false }
+ let own = PathPattern.comparablePath(of: url)
+ let copies = installedApps.filter { $0.bundleIdentifier?.lowercased() == identifier.lowercased() }.map(\.url)
+ + applicationURLs(forBundleIdentifier: identifier)
+ return copies.contains { !isInATrash($0) && PathPattern.comparablePath(of: $0) != own }
+ }
+
static func isInATrash(_ url: URL) -> Bool {
let names = PathComponents.of(url.path(percentEncoded: false))
return names.contains(".Trash") || names.contains(".Trashes")
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
index 274345c1..cd85b147 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
@@ -18,6 +18,27 @@ struct AppInspectorTests {
#expect(!AppInspector.isInATrash(URL(filePath: "/Users/x/.Trash Old/Example.app")))
}
+ @Test func knowsWhenAnotherCopyOfATrashedAppIsInstalled() throws {
+ let directory = try TemporaryDirectory()
+ func bundle(_ path: String, _ identifier: String?) throws -> URL {
+ var info: [String: Any] = ["CFBundleName": "Probe", "CFBundlePackageType": "APPL"]
+ info["CFBundleIdentifier"] = identifier
+ let data = try PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0)
+ try directory.file("\(path)/Contents/Info.plist", contents: data)
+ return directory.url.appending(path: path, directoryHint: .isDirectory)
+ }
+ let trashed = try bundle("home/.Trash/Probe.app", "org.example.inspector.copy")
+ let kept = try bundle("home/Applications/Probe.app", "org.example.inspector.copy")
+ let alone = try bundle("home/.Trash/Alone.app", "org.example.inspector.alone")
+ let nameless = try bundle("home/.Trash/Nameless.app", nil)
+ let installed = [kept, trashed, nameless].compactMap(AppInspector.inspect)
+
+ #expect(AppInspector.anotherCopyIsInstalled(of: trashed, among: installed))
+ #expect(!AppInspector.anotherCopyIsInstalled(of: alone, among: installed))
+ #expect(!AppInspector.anotherCopyIsInstalled(of: nameless, among: installed))
+ #expect(!AppInspector.anotherCopyIsInstalled(of: trashed, among: [trashed].compactMap(AppInspector.inspect)))
+ }
+
/// The shapes a binary comes in: thin (the usual shape of an Intel-only app), and fat with 20-byte or 32-byte
/// entries. A fat header that claims too many architectures to be real is read as nothing: a Java class file
/// also starts with `cafebabe`, and its version sits where the count would be.
diff --git a/Peel/PeelApp.swift b/Peel/PeelApp.swift
index a8cf29f2..c2a720c0 100644
--- a/Peel/PeelApp.swift
+++ b/Peel/PeelApp.swift
@@ -296,7 +296,9 @@ struct PeelApp: App {
// which every page reads to lock the rows that need it, so nothing below waits for them.
async let homeChecked: Void = Marks.interval("Home checks") { await home.refresh() }
await exclusionsRead
- trashMonitor.onApplicationTrashed = { [notifications] url in
+ trashMonitor.onApplicationTrashed = { [notifications, library] url in
+ // An old copy thrown away while another is installed, as some updaters do, leaves nothing behind.
+ guard !AppInspector.anotherCopyIsInstalled(of: url, among: library.apps) else { return }
notifications.notify(applicationTrashed: url)
}
if watchesTrash {