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.

Apply an app's rules to the folder a package keeps it in

Țugui Dragoșdcba2f8c4436
diff --git a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
index 6948638c..e9c3f737 100644
--- a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
@@ -309,10 +309,14 @@ public enum PackageReceipts {
&& (files.isEmpty || isTemporary(installLocation: info["install-location"] as? String ?? ""))
let found = topLevel(files: files, installLocation: installLocation.path(percentEncoded: false))
- // An app's own rules reach the rest of its package, as they reach its files on its page.
- let apps = found.offered
- .filter { ($0 as NSString).pathExtension.caseInsensitiveCompare("app") == .orderedSame }
- .compactMap { AppInspector.inspect(URL(filePath: PathPattern.located($0) ?? $0)) }
+ // An app's own rules reach the rest of its package, as they reach its files on its page, and a folder an app
+ // sits in moves that app too. What else a folder holds isn't this package's, even when the folder is.
+ let inspected = { (paths: [String]) in
+ paths.compactMap { AppInspector.inspect(URL(filePath: PathPattern.located($0) ?? $0)) }
+ }
+ let apps = inspected(found.offered.flatMap { appPaths(in: $0) })
+ let ownApps = found.othersFilled.isEmpty
+ ? apps : inspected(found.offered.filter { !found.othersFilled.contains($0) }.flatMap { appPaths(in: $0) })
var removedApps: [String: RemovedElsewhere] = [:]
for app in apps {
let carried = await AppExtensions.installed(carriedBy: app) { await installed.answer() }
@@ -355,7 +359,9 @@ public enum PackageReceipts {
heldBack: heldBack,
removedElsewhere: url.pathExtension.caseInsensitiveCompare("app") == .orderedSame
? removedApps[place] : removedWithItsApp,
- uninstallsItself: uninstallers.first { PathPattern.comparablePath(of: $0.app.url) == place }
+ uninstallsItself: uninstallers.first {
+ PathComponents.isPath(PathPattern.comparablePath(of: $0.app.url), atOrInside: place)
+ }
))
}
@@ -370,7 +376,7 @@ public enum PackageReceipts {
nothingLeftOnDisk: listing != nil && !throughScripts && found.onDisk.isEmpty,
isFileListKnown: listing != nil,
installedThroughItsScripts: throughScripts,
- apps: apps
+ apps: ownApps
),
paths: files.map { path(of: $0, in: installLocation).path(percentEncoded: false) },
installLocation: installLocation.path(percentEncoded: false)
@@ -514,6 +520,23 @@ public enum PackageReceipts {
return parts(depth + 1)
}
+ /// `path` when it is an app, or else the apps a level or two inside it, as far as the guard looks for an excluded
+ /// one. An app inside another bundle is that bundle's own.
+ static func appPaths(in path: String) -> [String] {
+ let isAnApp = { (path: String) in
+ (path as NSString).pathExtension.caseInsensitiveCompare("app") == .orderedSame
+ }
+ guard !isAnApp(path) else { return [path] }
+ guard !isBundle(path, exists: { URL(filePath: $0).isThere }), let names = namesInside(path) else { return [] }
+ return names.sorted().flatMap { name -> [String] in
+ let inside = (path as NSString).appendingPathComponent(name)
+ if isAnApp(inside) { return URL(filePath: inside).isRealFolder ? [inside] : [] }
+ guard !isBundle(inside, exists: { URL(filePath: $0).isThere }) else { return [] }
+ return (namesInside(inside) ?? []).sorted().map { (inside as NSString).appendingPathComponent($0) }
+ .filter { isAnApp($0) && URL(filePath: $0).isRealFolder }
+ }
+ }
+
/// What is inside a folder, or nil for a file or a link, which is one item whatever it leads to.
static func namesInside(_ path: String) -> [String]? {
var info = stat()
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift
index 3f3cce3d..8c1d92c3 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift
@@ -434,6 +434,89 @@ struct PackageReceiptsTests {
#expect(items["Plain.app"]?.isLeftAlone == false)
}
+ @Test(arguments: ["Vendor/Guard.app", "Vendor/Tools/Guard.app"])
+ func anAppInAPackagesOwnFolderKeepsItsRules(place: String) 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/\(place)/Contents/Info.plist", contents: info("org.example.guard", "Guard"))
+ try directory.file(
+ "Applications/\(place)/Contents/Library/SystemExtensions/org.example.guard.filter.systemextension/Contents/Info.plist",
+ contents: info("org.example.guard.filter", "Guard Filter")
+ )
+ try directory.file("Applications/Plain/Plain.app/Contents/Info.plist", contents: info("org.example.plain", "Plain"))
+ let listed = PathComponents.of(place).indices.map { PathComponents.of(place)[...$0].joined(separator: "/") }
+ let files = ["Plain", "Plain/Plain.app", "Plain/Plain.app/Contents", "Plain/Plain.app/Contents/Info.plist"] + listed
+ + ["\(place)/Contents", "\(place)/Contents/Info.plist", "\(place)/Contents/Library"]
+ + ["\(place)/Contents/Library/SystemExtensions"]
+ + ["\(place)/Contents/Library/SystemExtensions/org.example.guard.filter.systemextension"]
+ 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 files.joined(separator: "\n") + "\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 receipt = try #require(scan.receipts.first)
+ #expect(receipt.installedApps == ["Guard", "Plain"])
+ let vendor = try #require(receipt.items.first { $0.url.lastPathComponent == "Vendor" })
+ #expect(vendor.removedElsewhere != nil)
+ #expect(vendor.isLeftAlone)
+ let selectable = receipt.selectableRows(canUseHelper: true)
+ #expect(!selectable.recommended.contains(vendor.url))
+ }
+
+ @Test func anAppInAFolderOthersFilledIsNotThePackagesOwn() async throws {
+ let directory = try TemporaryDirectory()
+ let volume = PathPattern.canonical(directory.url)
+ let info = try PropertyListSerialization.data(
+ fromPropertyList: ["CFBundleIdentifier": "org.example.other", "CFBundleName": "Other"], format: .xml, options: 0
+ )
+ try directory.file("Applications/Vendor/Other.app/Contents/Info.plist", contents: info)
+ let pkgutil: PackageReceipts.Pkgutil = { arguments in
+ switch arguments.first {
+ case "--pkgs-plist":
+ return PkgutilAnswer.packages("org.example.vendor.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 "Vendor\n"
+ default:
+ return nil
+ }
+ }
+ let environment = SearchEnvironment(homeDirectory: volume.appending(path: "home"), rootDirectory: volume)
+
+ let scan = await PackageReceipts.list(
+ exclusions: .none, pkgutil: pkgutil, environment: environment, systemExtensions: { [] }
+ )
+
+ #expect(scan.receipts.first?.installedApps == [])
+ }
+
@Test func leavesTheRestOfAPackageToWhatRemovesItsApp() async throws {
let directory = try TemporaryDirectory()
let volume = PathPattern.canonical(directory.url)
diff --git a/Peel/Views/Packages/PackageDetailView.swift b/Peel/Views/Packages/PackageDetailView.swift
index c188683a..5148ceb7 100644
--- a/Peel/Views/Packages/PackageDetailView.swift
+++ b/Peel/Views/Packages/PackageDetailView.swift
@@ -21,7 +21,9 @@ struct PackageDetailView: View {
if !helper.canAct {
HelperRequiredBanner(need: .forgettingThisReceipt)
}
- let forAnAdministrator = receipt.items.filter { $0.heldBack == .beyondTheHelper }
+ let forAnAdministrator = receipt.items.filter {
+ $0.heldBack == .beyondTheHelper && $0.removedElsewhere == nil
+ }
if !forAnAdministrator.isEmpty {
Notice(
title: Text("Needs an Administrator"),