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.

Take an app's receipt along when its package wrote only the app

Țugui Dragoșa0837abd1910
diff --git a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
index 6bdadaaf..e7a70043 100644
--- a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
@@ -156,6 +156,32 @@ public enum PackageReceipts {
return receipts
}
+ /// The packages that wrote `app` and nothing outside it, the folders on the way to it aside. Once the app is gone,
+ /// such a receipt describes nothing on the disk. A receipt lists its files relative to its install location, and
+ /// they are compared name by name in that form, since most of them need not exist.
+ static func receipts(writingOnly app: URL, pkgutil: Pkgutil) async -> [String] {
+ let bundle = PathPattern.canonical(app)
+ let spellings = Self.spellings(of: PathPattern.comparablePath(of: bundle))
+ var identifiers: Set<String> = []
+ for path in spellings where identifiers.isEmpty && !Task.isCancelled {
+ identifiers = Set(Self.packageIdentifiers(in: await pkgutil(["--file-info-plist", path])))
+ }
+ let names = PathComponents.of(bundle.path(percentEncoded: false))
+ var found: [String] = []
+ for identifier in identifiers.sorted() where !Task.isCancelled {
+ guard let (_, _, installLocation) = await description(of: identifier, pkgutil: pkgutil),
+ let listing = await pkgutil(["--files", identifier])
+ else { continue }
+ let location = PathComponents.of(PathPattern.canonical(installLocation).path(percentEncoded: false))
+ guard names.starts(with: location) else { continue }
+ let wanted = Array(names.dropFirst(location.count))
+ let entries = lines(of: listing).map { PathComponents.of($0).filter { $0 != "." } }
+ let isTheAppOrOnTheWay = { (entry: [String]) in entry.starts(with: wanted) || wanted.starts(with: entry) }
+ if entries.contains(wanted), entries.allSatisfy(isTheAppOrOnTheWay) { found.append(identifier) }
+ }
+ return found
+ }
+
/// Returns `path`, then the same path with its leading folders dropped one at a time (`/a/b/c`, `/b/c`,
/// `/c`), since a receipt lists what it installed relative to its install location. Any of these can also
/// match another package's file, so each answer is checked against what that package really installed.
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift b/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
index c1e6c61b..2eb16182 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
@@ -103,12 +103,16 @@ public struct Uninstallation: Sendable {
receipts: Set<String>,
installedApps: [InstalledApp],
exclusions: Exclusions,
- environment: SearchEnvironment
+ environment: SearchEnvironment,
+ pkgutil: PackageReceipts.Pkgutil = PackageReceipts.pkgutil
) async -> [Leftover] {
let identifier = app.bundleIdentifier.lowercased()
let identifiers = Set(installedApps.map { $0.bundleIdentifier.lowercased() } + [identifier])
+ let named = receipts.filter { PackageReceipts.owner(of: $0, among: identifiers) == identifier }
+ let wroteOnlyTheApp = await PackageReceipts.receipts(writingOnly: app.url, pkgutil: pkgutil)
+ .filter { !named.contains($0.lowercased()) }
var leftovers: [Leftover] = []
- for receipt in receipts.filter({ PackageReceipts.owner(of: $0, among: identifiers) == identifier }).sorted() {
+ for receipt in (Array(named) + wroteOnlyTheApp).sorted() {
for url in PackageActions.receiptFiles(of: receipt, onVolume: environment.rootDirectory)
where !exclusions.excludes(url) {
leftovers.append(await LeftoverScanner.leftover(
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
index 572ece3d..31390598 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
@@ -831,6 +831,52 @@ struct UninstallationTests {
#expect(found.map(\.url.lastPathComponent) == ["org.example.synth.app.pkg.plist"])
}
+ /// A receipt named for its package is the app's when the package wrote the app and nothing else.
+ @Test func offersAReceiptWhosePackageWroteOnlyTheApp() async throws {
+ let directory = try TemporaryDirectory()
+ let environment = SearchEnvironment(
+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),
+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)
+ )
+ let bundle = try directory.directory("root/Applications/Example.app")
+ let example = InstalledApp(url: bundle, bundleIdentifier: "org.example.app", name: "Example")
+ for name in ["org.example.package.Example.app", "org.example.suite"] {
+ try directory.file("root/private/var/db/receipts/\(name).bom", bytes: 32)
+ }
+ let applications = PathPattern.canonical(bundle.deletingLastPathComponent())
+ let location = String(PathPattern.comparablePath(of: applications).dropFirst())
+ let pkgutil: PackageReceipts.Pkgutil = { arguments in
+ switch arguments.first {
+ case "--file-info-plist":
+ arguments[1].hasSuffix("/Example.app")
+ ? PkgutilAnswer.fileInfo(
+ arguments[1], packages: "org.example.package.Example.app", "org.example.suite"
+ )
+ : PkgutilAnswer.fileInfo(arguments[1])
+ case "--pkg-info-plist":
+ PkgutilAnswer.packageInfo(arguments[1], location: location)
+ case "--files":
+ arguments[1] == "org.example.suite"
+ ? "Example.app\nExample.app/Contents\nOther.app\n"
+ : ".\nExample.app\nExample.app/Contents\nExample.app/Contents/Info.plist\n"
+ default:
+ nil
+ }
+ }
+
+ let found = await Uninstallation.receiptLeftovers(
+ for: example,
+ receipts: ["org.example.package.example.app", "org.example.suite"],
+ installedApps: [example],
+ exclusions: .none,
+ environment: environment,
+ pkgutil: pkgutil
+ )
+
+ #expect(found.map(\.url.lastPathComponent) == ["org.example.package.Example.app.bom"])
+ #expect(found.map(\.match.isRecommended) == [true])
+ }
+
@Test func offersNoReceiptWhenNoneNamesTheApp() async throws {
let directory = try TemporaryDirectory()
let environment = SearchEnvironment(