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
Leave an app a package installed to the rules of its own page
Țugui Dragoș3bbd808b6bff
diff --git a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipt.swift b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipt.swiftindex a6d65124..8ff86136 100644--- a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipt.swift+++ b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipt.swift@@ -20,10 +20,12 @@ public struct PackageReceipt: Sendable, Hashable, Identifiable {/// or a repository), or `/Users/Shared`, which every account on the Mac uses. Or why it cannot be moved at/// all: it needs an administrator, and the helper would refuse it.public var heldBack: HoldBack?+ /// How an app the package installed is removed instead, by the rules its own page follows.+ public var removedElsewhere: RemovedElsewhere?/// True when Peel never moves this item. It is listed but never offered.public var isLeftAlone: Bool {- refusal != nil || heldBack?.cannotBeMoved == true+ refusal != nil || heldBack?.cannotBeMoved == true || removedElsewhere != nil}public var id: URL { url }diff --git a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swiftindex b9ff0fc6..ecd16f03 100644--- a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift+++ b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift@@ -66,11 +66,13 @@ public enum PackageReceipts {static func list(exclusions: Exclusions,pkgutil: @escaping Pkgutil,- environment: SearchEnvironment = .current+ environment: SearchEnvironment = .current,+ systemExtensions: @escaping AppExtensions.SystemExtensionsAnswer = AppExtensions.askingMacOS) async -> PackageScan {let removalGuard = RemovalGuard(environment: environment, exclusions: exclusions)let reach = HelperReach(environment: environment)let shared = sharedFolders(in: environment)+ let installed = InstalledSystemExtensions(asking: systemExtensions)guard let packages = packageList(in: await pkgutil(["--pkgs-plist"])) else {return PackageScan(receipts: [], couldNotAsk: true)}@@ -88,7 +90,8 @@ public enum PackageReceipts {pkgutil: pkgutil,removalGuard: removalGuard,reach: reach,- shared: shared+ shared: shared,+ installed: installed).receipt}}@@ -102,7 +105,8 @@ public enum PackageReceipts {pkgutil: pkgutil,removalGuard: removalGuard,reach: reach,- shared: shared+ shared: shared,+ installed: installed).receipt}}@@ -129,7 +133,8 @@ public enum PackageReceipts {static func receipts(installing url: URL,exclusions: Exclusions,- pkgutil: @escaping Pkgutil+ pkgutil: @escaping Pkgutil,+ systemExtensions: @escaping AppExtensions.SystemExtensionsAnswer = AppExtensions.askingMacOS) async -> [PackageReceipt] {let wanted = PathPattern.comparablePath(of: PathPattern.canonical(url))var identifiers: Set<String> = []@@ -141,6 +146,7 @@ public enum PackageReceipts {let removalGuard = RemovalGuard(environment: .current, exclusions: exclusions)let reach = HelperReach(environment: .current)let shared = sharedFolders(in: .current)+ let installed = InstalledSystemExtensions(asking: systemExtensions)for identifier in identifiers.sorted() where !Task.isCancelled {let found = await receipt(identifier,@@ -148,7 +154,8 @@ public enum PackageReceipts {pkgutil: pkgutil,removalGuard: removalGuard,reach: reach,- shared: shared+ shared: shared,+ installed: installed)guard found.installs(wanted) else { continue }receipts.append(found.receipt)@@ -215,7 +222,8 @@ public enum PackageReceipts {pkgutil: Pkgutil,removalGuard: RemovalGuard,reach: HelperReach,- shared: [String]+ shared: [String],+ installed: InstalledSystemExtensions) async -> Found {guard let (info, volume, installLocation) = await description(of: identifier, pkgutil: pkgutil) else {// `pkgutil` named the package but did not describe it. It stays listed, with what it installed unknown,@@ -254,7 +262,8 @@ public enum PackageReceipts {heldBack: requiresPrivileges && reach.isBeyond(url)? .beyondTheHelper: HoldBack.seen(in: contents) ?? (isShared ? .sharedWithEveryone : nil)- ?? (isFilledByOthers ? .holdsWhatOthersPut : nil)+ ?? (isFilledByOthers ? .holdsWhatOthersPut : nil),+ removedElsewhere: await removedElsewhere(url, installed: installed)))}@@ -273,6 +282,15 @@ public enum PackageReceipts {)}+ /// How an app a package installed is removed instead, by the rules its own page follows: Finder for its system+ /// extensions, Remove Peel for Peel, its maker's uninstaller for an agent.+ private static func removedElsewhere(_ url: URL, installed: InstalledSystemExtensions) async -> RemovedElsewhere? {+ guard url.pathExtension.caseInsensitiveCompare("app") == .orderedSame, let app = AppInspector.inspect(url)+ else { return nil }+ let names = await AppExtensions.installedNames(carriedBy: app) { await installed.answer() }+ return RemovedElsewhere.of(app, installedSystemExtensions: names)+ }+/// What `pkgutil --pkg-info-plist` says of a package, with the volume and the folder its file list is relative to.private static func description(of identifier: String,@@ -394,3 +412,21 @@ public enum PackageReceipts {return output.status == 0 ? output.text : nil}}++/// Which system extensions macOS has installed, asked at most once for a scan, and only once an app carries one.+actor InstalledSystemExtensions {+ private let asking: AppExtensions.SystemExtensionsAnswer+ private var asked: Task<[AppExtension]?, Never>?++ init(asking: @escaping AppExtensions.SystemExtensionsAnswer) {+ self.asking = asking+ }++ func answer() async -> [AppExtension]? {+ if let asked { return await asked.value }+ let asking = asking+ let task = Task { await asking() }+ asked = task+ return await task.value+ }+}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swiftindex 377c713a..10380bb0 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift@@ -115,6 +115,53 @@ struct PackageReceiptsTests {])}+ @Test func leavesAnAppToFinderWhenMacOSInstalledItsSystemExtension() async throws {+ let directory = try TemporaryDirectory()+ let volume = PathPattern.canonical(directory.url)+ func info(_ identifier: String, _ name: String) throws -> Data {+ try PropertyListSerialization.data(+ fromPropertyList: ["CFBundleIdentifier": identifier, "CFBundleName": name], format: .xml, options: 0+ )+ }+ try directory.file("Applications/Guard.app/Contents/Info.plist", contents: info("org.example.guard", "Guard"))+ try directory.file(+ "Applications/Guard.app/Contents/Library/SystemExtensions/org.example.guard.filter.systemextension/Contents/Info.plist",+ contents: info("org.example.guard.filter", "Guard Filter")+ )+ try directory.file("Applications/Plain.app/Contents/Info.plist", contents: info("org.example.plain", "Plain"))+ let pkgutil: PackageReceipts.Pkgutil = { arguments in+ switch arguments.first {+ case "--pkgs-plist":+ return PkgutilAnswer.packages("org.example.guard.pkg")+ case "--pkg-info-plist":+ let info = ["volume": volume.path(percentEncoded: false), "install-location": "Applications"]+ let data = try? PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0)+ return data.flatMap { String(data: $0, encoding: .utf8) }+ case "--files":+ return "Guard.app\nPlain.app\n"+ default:+ return nil+ }+ }+ let filter = AppExtension(+ identifier: "org.example.guard.filter", name: "Guard Filter", kind: .systemExtension, point: nil, owner: nil,+ url: nil, election: .on, teamIdentifier: nil, reportedState: nil+ )+ let environment = SearchEnvironment(homeDirectory: volume.appending(path: "home"), rootDirectory: volume)++ let scan = await PackageReceipts.list(+ exclusions: .none, pkgutil: pkgutil, environment: environment, systemExtensions: { [filter] }+ )++ let items = Dictionary(+ uniqueKeysWithValues: scan.receipts.flatMap(\.items).map { ($0.url.lastPathComponent, $0) }+ )+ #expect(items["Guard.app"]?.removedElsewhere == .byFinder(systemExtensions: ["Guard Filter"]))+ #expect(items["Guard.app"]?.isLeftAlone == true)+ #expect(items["Plain.app"]?.removedElsewhere == nil)+ #expect(items["Plain.app"]?.isLeftAlone == false)+ }+/// A receipt's file list leaves out the install prefix, such as `Applications`, so the path is asked about again/// without its leading folders, and the answer is believed only if the receipt really installed that path.@Test func findsThePackageThatInstalledAnAppUnderAnInstallPrefix() async throws {diff --git a/Peel/Views/Packages/PackageDetailView.swift b/Peel/Views/Packages/PackageDetailView.swiftindex 73ba556a..3b2a878f 100644--- a/Peel/Views/Packages/PackageDetailView.swift+++ b/Peel/Views/Packages/PackageDetailView.swift@@ -30,7 +30,7 @@ struct PackageDetailView: View {url: item.url,icon: item.url.pathExtension == "app" ? .file(item.url) : .symbol("doc"),detail: item.refusal?.explanation,- warning: item.refusal == nil ? item.heldBack.map { String(localized: $0.explanation) } : nil,+ warning: item.refusal == nil ? note(for: item) : nil,size: item.size ?? 0,isMeasured: item.size != nil,isLocked: isLocked(item),@@ -170,4 +170,9 @@ struct PackageDetailView: View {item.requiresPrivileges && !helper.canAct}+ private func note(for item: PackageReceipt.Item) -> String? {+ if let removedElsewhere = item.removedElsewhere { return String(localized: removedElsewhere.explanation) }+ return item.heldBack.map { String(localized: $0.explanation) }+ }+}