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.

A copy of an app in the Trash is found by its folders, not its text

Țugui Dragoș1b3da8cd9157
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
index 9f84816b..d7317d5e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
@@ -3,6 +3,7 @@ import Darwin
import AppKit
public import Foundation
import Security
+internal import PeelPrivileged
public enum AppInspector {
/// Where macOS thinks an app is, including folders Peel doesn't scan, such as a build folder.
@@ -15,11 +16,15 @@ public enum AppInspector {
/// doesn't count as installed.
static func applicationURLs(forBundleIdentifier identifier: String) -> [URL] {
NSWorkspace.shared.urlsForApplications(withBundleIdentifier: identifier).filter { url in
- let path = url.path(percentEncoded: false)
- return !path.contains("/.Trash/") && !path.contains("/.Trashes/") && FileManager.default.fileExists(atPath: path)
+ !isInATrash(url) && FileManager.default.fileExists(atPath: url.path(percentEncoded: false))
}
}
+ static func isInATrash(_ url: URL) -> Bool {
+ let names = PathComponents.of(url.path(percentEncoded: false))
+ return names.contains(".Trash") || names.contains(".Trashes")
+ }
+
/// 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.
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/InstalledApp.swift b/Packages/PeelCore/Sources/PeelCore/Apps/InstalledApp.swift
index 1903eb83..843e8961 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/InstalledApp.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/InstalledApp.swift
@@ -99,7 +99,8 @@ public struct InstalledApp: Sendable, Hashable, Identifiable {
let own = HelperIdentity.appIdentifier.lowercased()
let identifier = bundleIdentifier.lowercased()
if identifier == own || identifier.hasPrefix(own + ".") { return true }
- return PathPattern.comparablePath(of: Bundle.main.bundleURL).hasPrefix(PathPattern.comparablePath(of: url) + "/")
+ let running = PathPattern.comparablePath(of: Bundle.main.bundleURL)
+ return PathComponents.isPath(running, inside: PathPattern.comparablePath(of: url))
}
/// The name Finder shows, which follows the user's language when the app translates it, and the bundle's file
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
index 3614f247..3f9bbcbb 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
@@ -9,6 +9,14 @@ struct AppInspectorTests {
+ [0x01, 0x00, 0x00, 0x07] + [UInt8](repeating: 0, count: 16)
+ [0x01, 0x00, 0x00, 0x0C] + [UInt8](repeating: 0, count: 16)
+ @Test func aCopyInATrashIsNotInstalledWhateverItsNameBeginsWith() {
+ #expect(AppInspector.isInATrash(URL(filePath: "/Users/x/.Trash/Example.app")))
+ #expect(AppInspector.isInATrash(URL(filePath: "/Users/x/.Trash/\u{301}Example.app")))
+ #expect(AppInspector.isInATrash(URL(filePath: "/Volumes/Disk/.Trashes/501/Example.app")))
+ #expect(!AppInspector.isInATrash(URL(filePath: "/Applications/Example.app")))
+ #expect(!AppInspector.isInATrash(URL(filePath: "/Users/x/.Trash Old/Example.app")))
+ }
+
/// 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.