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.

Look up the places an app keeps what exists only here by folder

Țugui Dragoș400a6ad90a9d
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/KeptOnlyHere.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/KeptOnlyHere.swift
index ddb01b32..f2f95d6c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/KeptOnlyHere.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/KeptOnlyHere.swift
@@ -150,18 +150,40 @@ enum KeptOnlyHere {
static func reason(for path: String, home: String, root: String) -> HoldBack? {
let item = PathComponents.of((path as NSString).standardizingPath.lowercased())
- for (base, list) in [(home, places), (root, localPlaces)] {
- let base = (base as NSString).standardizingPath
- for (folder, inside, reason) in list {
- let folder = PathComponents.of((base as NSString).appendingPathComponent(folder).lowercased())
- let place = folder + (inside.map { PathComponents.of($0.lowercased()) } ?? [])
- if item.starts(with: place) || item == folder { return reason }
+ for (base, places) in [(home, placesByFolder), (root, localPlacesByFolder)] {
+ let base = PathComponents.of((base as NSString).standardizingPath.lowercased())
+ guard item.starts(with: base), item.count > base.count else { continue }
+ let rest = item.dropFirst(base.count)
+ // An item at or inside several places takes the one listed first.
+ let held = (1...rest.count).flatMap { depth in
+ (places[Array(rest.prefix(depth))] ?? []).filter {
+ depth == rest.count || rest.dropFirst(depth).starts(with: $0.inside)
+ }
}
+ if let first = held.min(by: { $0.order < $1.order }) { return first.reason }
}
// What an app names as its backups is a copy the person may need, whichever app made it.
return namesBackups(item.last ?? "") ? .holdsWorkMadeWithTheApp : nil
}
+ private struct Place {
+ let inside: [String]
+ let reason: HoldBack
+ let order: Int
+ }
+
+ private static let placesByFolder = byFolder(places)
+ private static let localPlacesByFolder = byFolder(localPlaces)
+
+ private static func byFolder(_ list: [(folder: String, inside: String?, reason: HoldBack)]) -> [[String]: [Place]] {
+ var index: [[String]: [Place]] = [:]
+ for (order, (folder, inside, reason)) in list.enumerated() {
+ let place = Place(inside: PathComponents.of((inside ?? "").lowercased()), reason: reason, order: order)
+ index[PathComponents.of(folder.lowercased()), default: []].append(place)
+ }
+ return index
+ }
+
static func reason(seenIn contents: FolderContents) -> HoldBack? {
contents.holdsBackups ? .holdsWorkMadeWithTheApp : nil
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/KeptOnlyHereTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/KeptOnlyHereTests.swift
index b9ffc413..108514ad 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/KeptOnlyHereTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/KeptOnlyHereTests.swift
@@ -4,6 +4,14 @@ import Testing
struct KeptOnlyHereTests {
private let home = "/Users/me"
+ @Test func answersForThousandsOfItemsAtOnce() {
+ let paths = (0..<5000).map { "\(home)/Library/Application Support/org.example.app\($0)/data" }
+ let took = processorTime {
+ for path in paths { _ = KeptOnlyHere.reason(for: path, home: home, root: "/") }
+ }
+ #expect(took < .milliseconds(300))
+ }
+
@Test func knowsEachPlaceWhatIsInsideAndTheAppsFolderAroundIt() {
let held: [(String, HoldBack)] = [
("Library/Group Containers/2E337YPCZY.airmail", .holdsLocalMail),