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.

Find a package installed into the app itself, on its page and at uninstall

Țugui Dragoș8e0898a187cd
diff --git a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
index 5b9094e2..70de0d8b 100644
--- a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
@@ -140,10 +140,7 @@ public enum PackageReceipts {
systemExtensions: @escaping AppExtensions.SystemExtensionsAnswer = AppExtensions.askingMacOS
) async -> [PackageReceipt] {
let wanted = PathPattern.comparablePath(of: PathPattern.canonical(url))
- var identifiers: Set<String> = []
- for path in Self.spellings(of: wanted) where identifiers.isEmpty && !Task.isCancelled {
- identifiers = Set(Self.packageIdentifiers(in: await pkgutil(["--file-info-plist", path])))
- }
+ let identifiers = await packages(listing: wanted, pkgutil: pkgutil)
var receipts: [PackageReceipt] = []
let removalGuard = RemovalGuard(environment: .current, exclusions: exclusions)
@@ -172,11 +169,7 @@ public enum PackageReceipts {
/// 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 identifiers = await packages(listing: PathPattern.comparablePath(of: bundle), pkgutil: pkgutil)
let names = PathComponents.of(bundle.path(percentEncoded: false))
var found: [String] = []
for identifier in identifiers.sorted() where !Task.isCancelled {
@@ -188,11 +181,32 @@ public enum PackageReceipts {
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) }
+ // A package installed into the app itself lists only what is inside it, so it wrote the app and no more.
+ if wanted.isEmpty || entries.contains(wanted), entries.allSatisfy(isTheAppOrOnTheWay) {
+ found.append(identifier)
+ }
}
return found
}
+ /// The packages whose receipts list the bundle at `path`, as `pkgutil` names them: it matches a path literally
+ /// against each receipt's file list, which is relative to the package's install location.
+ private static func packages(listing path: String, pkgutil: Pkgutil) async -> Set<String> {
+ for asked in spellings(of: path) where !Task.isCancelled {
+ let identifiers = Set(packageIdentifiers(in: await pkgutil(["--file-info-plist", asked])))
+ if !identifiers.isEmpty { return identifiers }
+ }
+ // A package installed into a bundle lists only what is inside it, as `Contents/...`, like every package
+ // installed that way, so only the one whose install location is this bundle counts.
+ var inside: Set<String> = []
+ for identifier in packageIdentifiers(in: await pkgutil(["--file-info-plist", "/Contents/Info.plist"]))
+ where !Task.isCancelled {
+ guard let location = await description(of: identifier, pkgutil: pkgutil)?.installLocation else { continue }
+ if PathPattern.comparablePath(of: PathPattern.canonical(location)) == path { inside.insert(identifier) }
+ }
+ return inside
+ }
+
/// 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.
@@ -237,9 +251,13 @@ public enum PackageReceipts {
struct Found {
let receipt: PackageReceipt
let paths: [String]
+ /// Where the package put what it lists. One installed into a bundle, an app's own folder, made that bundle.
+ var installLocation: String?
func installs(_ path: String) -> Bool {
- paths.contains { PathPattern.comparablePath(of: PathPattern.canonical(URL(filePath: $0))) == path }
+ let comparable = { PathPattern.comparablePath(of: PathPattern.canonical(URL(filePath: $0))) }
+ return paths.contains { comparable($0) == path }
+ || !paths.isEmpty && installLocation.map(comparable) == path
}
}
@@ -335,7 +353,8 @@ public enum PackageReceipts {
installedThroughItsScripts: throughScripts,
installedApps: apps.map(\.name).sorted()
),
- paths: files.map { path(of: $0, in: installLocation).path(percentEncoded: false) }
+ paths: files.map { path(of: $0, in: installLocation).path(percentEncoded: false) },
+ installLocation: installLocation.path(percentEncoded: false)
)
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift
index 9d82b587..590adfa5 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift
@@ -473,6 +473,35 @@ struct PackageReceiptsTests {
#expect(asked.withLock { $0 }.first == PathPattern.comparablePath(of: app), "the real path is asked about first")
}
+ @Test func findsAPackageInstalledIntoTheAppItself() async throws {
+ let directory = try TemporaryDirectory()
+ let volume = PathPattern.canonical(directory.url).path(percentEncoded: false)
+ let app = PathPattern.canonical(try directory.directory("Applications/Example.app"))
+ try directory.file("Applications/Example.app/Contents/Info.plist", bytes: 16)
+ // As `pkgutil` matches a path: literally, against the file list relative to the install location.
+ let pkgutil: PackageReceipts.Pkgutil = { arguments in
+ switch arguments.first {
+ case "--file-info-plist": arguments[1] == "/Contents/Info.plist"
+ ? PkgutilAnswer.fileInfo(arguments[1], packages: "org.example.inside", "org.example.other")
+ : PkgutilAnswer.fileInfo(arguments[1])
+ case "--pkg-info-plist": PkgutilAnswer.packageInfo(
+ arguments[1], volume: volume,
+ location: arguments[1] == "org.example.other" ? "Applications/Other.app" : "Applications/Example.app"
+ )
+ case "--files": "Contents\nContents/Info.plist\n"
+ default: nil
+ }
+ }
+
+ let found = await PackageReceipts.receipts(installing: app, exclusions: .none, pkgutil: pkgutil)
+ let wroteOnlyTheApp = await PackageReceipts.receipts(writingOnly: app, pkgutil: pkgutil)
+
+ #expect(found.map(\.identifier) == ["org.example.inside"])
+ let items = found.first?.items.map { PathPattern.comparablePath(of: $0.url) }
+ #expect(items == [PathPattern.comparablePath(of: app)])
+ #expect(wroteOnlyTheApp == ["org.example.inside"])
+ }
+
/// A link the package installed is on the disk even when what it leads to is gone, so the package is not
/// "Receipt only": `fileExists` follows the link and would call it missing.
@Test func aDanglingLinkThePackageInstalledIsStillThere() throws {