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.

Package Receipts leaves what may exist nowhere else for the person

Țugui Dragoșc1eeba395cf1
diff --git a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipt.swift b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipt.swift
index 295339cc..3c3a372a 100644
--- a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipt.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipt.swift
@@ -17,6 +17,9 @@ public struct PackageReceipt: Sendable, Hashable, Identifiable {
/// True when Peel never moves this item: `RemovalGuard` refuses it (a file in `/usr/local/bin`, for
/// example), or it needs an administrator and the helper would refuse it. It is listed but never offered.
public var isLeftAlone = false
+ /// Why the item is left for the person to choose: what measuring it saw (not measured, not read, a wallet,
+ /// or a repository), or `/Users/Shared`, which every account on the Mac uses.
+ public var heldBack: HoldBack?
public var id: URL { url }
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
index 24f50838..9555323b 100644
--- a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift
@@ -55,6 +55,7 @@ public enum PackageReceipts {
static func list(exclusions: Exclusions, pkgutil: @escaping Pkgutil, environment: SearchEnvironment = .current) async -> PackageScan {
let removalGuard = RemovalGuard(environment: environment, exclusions: exclusions)
let reach = HelperReach(environment: environment)
+ let shared = sharedFolders(in: environment)
guard let packages = packageList(in: await pkgutil(["--pkgs-plist"])) else {
return PackageScan(receipts: [], couldNotAsk: true)
}
@@ -65,12 +66,12 @@ public enum PackageReceipts {
var results: [PackageReceipt] = []
for _ in 0..<concurrentReceipts {
guard let identifier = pending.next() else { break }
- group.addTask { await Self.receipt(identifier, exclusions: exclusions, pkgutil: pkgutil, removalGuard: removalGuard, reach: reach).receipt }
+ group.addTask { await Self.receipt(identifier, exclusions: exclusions, pkgutil: pkgutil, removalGuard: removalGuard, reach: reach, shared: shared).receipt }
}
while let result = await group.next() {
results.append(result)
if let identifier = pending.next() {
- group.addTask { await Self.receipt(identifier, exclusions: exclusions, pkgutil: pkgutil, removalGuard: removalGuard, reach: reach).receipt }
+ group.addTask { await Self.receipt(identifier, exclusions: exclusions, pkgutil: pkgutil, removalGuard: removalGuard, reach: reach, shared: shared).receipt }
}
}
return results
@@ -100,8 +101,9 @@ public enum PackageReceipts {
var receipts: [PackageReceipt] = []
let removalGuard = RemovalGuard(environment: .current, exclusions: exclusions)
let reach = HelperReach(environment: .current)
+ let shared = sharedFolders(in: .current)
for identifier in identifiers.sorted() where !Task.isCancelled {
- let found = await receipt(identifier, exclusions: exclusions, pkgutil: pkgutil, removalGuard: removalGuard, reach: reach)
+ let found = await receipt(identifier, exclusions: exclusions, pkgutil: pkgutil, removalGuard: removalGuard, reach: reach, shared: shared)
guard found.installs(wanted) else { continue }
receipts.append(found.receipt)
}
@@ -135,7 +137,14 @@ public enum PackageReceipts {
}
}
- private static func receipt(_ identifier: String, exclusions: Exclusions = .none, pkgutil: Pkgutil, removalGuard: RemovalGuard, reach: HelperReach) async -> Found {
+ private static func receipt(
+ _ identifier: String,
+ exclusions: Exclusions = .none,
+ pkgutil: Pkgutil,
+ removalGuard: RemovalGuard,
+ reach: HelperReach,
+ shared: [String]
+ ) async -> Found {
guard
let data = await pkgutil(["--pkg-info-plist", identifier])?.data(using: .utf8),
let info = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any]
@@ -164,11 +173,15 @@ public enum PackageReceipts {
let url = URL(filePath: PathPattern.located(path) ?? path)
guard !exclusions.excludes(url), seen.insert(PathPattern.comparablePath(of: url)).inserted else { continue }
let requiresPrivileges = FileAccess.requiresPrivilegesToRemove(url)
+ let contents = await FileSize.contents(of: url)
+ let path = PathPattern.comparablePath(of: url)
+ let isShared = shared.contains { PathComponents.isPath(path, atOrInside: $0) }
items.append(PackageReceipt.Item(
url: url,
- size: await FileSize.measure(url),
+ size: contents.flatMap { $0.couldNotBeRead ? nil : $0.size },
requiresPrivileges: requiresPrivileges,
- isLeftAlone: !removalGuard.allowsRemoval(of: url) || (requiresPrivileges && reach.isBeyond(url))
+ isLeftAlone: !removalGuard.allowsRemoval(of: url) || (requiresPrivileges && reach.isBeyond(url)),
+ heldBack: HoldBack.seen(in: contents) ?? (isShared ? .sharedWithEveryone : nil)
))
}
@@ -190,6 +203,11 @@ public enum PackageReceipts {
)
}
+ /// The folders every account on the Mac uses, `/Users/Shared`, spelled to be compared.
+ private static func sharedFolders(in environment: SearchEnvironment) -> [String] {
+ environment.locations.filter { $0.kind == .sharedFolder }.map { PathPattern.comparablePath(of: $0.url) }
+ }
+
/// How many levels deep the search goes into a folder that also holds files this package did not install.
private static let deepestDescent = 6
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift
index 109b4519..0e55f323 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift
@@ -70,6 +70,44 @@ struct PackageReceiptsTests {
#expect(PackageReceipts.topLevel(files: files, installLocation: "/", exists: { _ in true }).offered.isEmpty)
}
+ /// What may exist nowhere else is left for the person to choose, with its reason: a folder the package made
+ /// that now holds a repository or a wallet, and anything in `/Users/Shared`, which every account on the Mac uses.
+ @Test func leavesToThePersonWhatAReceiptsFolderHolds() async throws {
+ let directory = try TemporaryDirectory()
+ let volume = PathPattern.canonical(directory.url)
+ try directory.file("Library/Application Support/Vendor Repo/.git/HEAD", bytes: 16)
+ try directory.file("Library/Application Support/Vendor Coin/wallet.dat", bytes: 16)
+ try directory.file("Library/Application Support/Vendor Plain/data.db", bytes: 16)
+ try directory.file("Users/Shared/StudioSync/project.txt", bytes: 16)
+ let locations = ["com.example.vendor": "Library/Application Support", "com.example.studio": "Users/Shared"]
+ let files = [
+ "com.example.vendor": "Vendor Repo\nVendor Coin\nVendor Plain\nVendor Plain/data.db\n",
+ "com.example.studio": "StudioSync\n",
+ ]
+ let pkgutil: PackageReceipts.Pkgutil = { arguments in
+ switch arguments.first {
+ case "--pkgs-plist":
+ return PkgutilAnswer.packages("com.example.studio", "com.example.vendor")
+ case "--pkg-info-plist":
+ let info = ["volume": volume.path(percentEncoded: false), "install-location": locations[arguments[1]] ?? ""]
+ let data = try? PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0)
+ return data.flatMap { String(data: $0, encoding: .utf8) }
+ case "--files":
+ return files[arguments[1]]
+ default:
+ return nil
+ }
+ }
+ let environment = SearchEnvironment(homeDirectory: volume.appending(path: "home"), rootDirectory: volume)
+
+ let scan = await PackageReceipts.list(exclusions: .none, pkgutil: pkgutil, environment: environment)
+
+ let reasons = scan.receipts.flatMap(\.items).map { "\($0.url.lastPathComponent): \($0.heldBack.map(\.rawValue) ?? "none")" }
+ #expect(reasons.sorted() == [
+ "StudioSync: sharedWithEveryone", "Vendor Coin: holdsAWallet", "Vendor Plain: none", "Vendor Repo: holdsRepository",
+ ])
+ }
+
/// 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.swift
index fd60ef48..1e7068be 100644
--- a/Peel/Views/Packages/PackageDetailView.swift
+++ b/Peel/Views/Packages/PackageDetailView.swift
@@ -30,12 +30,13 @@ struct PackageDetailView: View {
url: item.url,
icon: item.url.pathExtension == "app" ? .file(item.url) : .symbol("doc"),
detail: item.isLeftAlone ? "Left alone: Peel won’t move this, because of where it sits or what is inside it." : nil,
+ warning: item.isLeftAlone ? nil : item.heldBack.map { String(localized: $0.explanation) },
size: item.size ?? 0,
isMeasured: item.size != nil,
isLocked: item.requiresPrivileges && !helper.canAct,
isLeftAlone: item.isLeftAlone,
isFirst: index == 0,
- hasNoteColumn: receipt.items.contains(where: \.isLeftAlone),
+ hasNoteColumn: receipt.items.contains { $0.isLeftAlone || $0.heldBack != nil },
selection: packages, isSelected: packages.isSelected(item.url)
)
}
@@ -159,7 +160,7 @@ struct PackageDetailView: View {
}
private var selectableURLs: [URL] {
- receipt.items.filter { !$0.isLeftAlone && (helper.canAct || !$0.requiresPrivileges) }.map(\.url)
+ receipt.items.filter { !$0.isLeftAlone && $0.heldBack == nil && (helper.canAct || !$0.requiresPrivileges) }.map(\.url)
}
}