Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
The removal guard works out each name's spellings once
Țugui Dragoșf7569679ef25
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swiftindex a7434355..b46d1f3b 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift@@ -124,17 +124,19 @@ struct RemovalGuard: Sendable {// The kernel's own name for the item is judged too: `/.vol/<device>/<inode>` names a file by its numbers// alone and shows none of the folders it sits in.let names = Set([path, located, PathPattern.kernelName(of: path, followingLinks: false)].compactMap(\.self))- for name in names {- let named = URL(filePath: name)- guard !exclusions.excludes(named), !exclusions.holds(named) else { return .excluded }+ // Worked out once per name, since each resolves links on disk. The names are standardized already, so these+ // are the spellings the exclusions would work out themselves.+ let spelled = Dictionary(uniqueKeysWithValues: names.map { ($0, ProtectedData.spellings(of: $0)) })+ for (name, spellings) in spelled {+ guard !exclusions.excludes(spellings: spellings), !exclusions.holds(spellings: spellings) else { return .excluded }if let refusal = protectedObjects.refusal(of: name) { return refusal }// The rules the helper follows. They protect every account's keychain and mail, not only those of the// account Peel runs in.- guard !ProtectedData.refuses(name, home: home) else { return .protectedLocation }- guard !ProtectedData.holds(name, home: home) else { return .holdsProtectedData }+ guard !ProtectedData.refuses(spellings: spellings, home: home) else { return .protectedLocation }+ guard !ProtectedData.holds(spellings: spellings, home: home) else { return .holdsProtectedData }}- for spelling in names.reduce(into: Set<String>(), { $0.formUnion(ProtectedData.spellings(of: $1)) }) {+ for spelling in spelled.values.reduce(into: Set<String>(), { $0.formUnion($1) }) {let isUnderAPrefix = Self.protectedPrefixes.contains { PathComponents.isPath(spelling, inside: $0) }guard !protectedPaths.contains(spelling) else { return .staysItself }guard !isUnderAPrefix || Self.isAToolsLink(spelling, isALink: isALink) else { return .protectedLocation }diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swiftindex d24fc4de..09476050 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift@@ -289,7 +289,7 @@ public struct TrashService: Sendable {let jobs = LaunchdCleanup.jobs(for: allowed, environment: environment)var result = TrashResult()- var moved: [String] = []+ var moved = Folders()ownMoves.began()for url in urls {if let refusal = refusals[url] {@@ -298,7 +298,7 @@ public struct TrashService: Sendable {}// An item inside a folder that just moved went with it: it is neither moved again nor a failure.let path = PathPattern.comparablePath(of: url)- guard !moved.contains(where: { PathComponents.isPath(path, inside: $0) }) else { continue }+ guard !moved.hold(path) else { continue }let holders = openFiles.holders(of: url)guard holders.isEmpty else {result.failures.append(TrashFailure(url: url, reason: .heldOpen(by: holders)))@@ -311,7 +311,7 @@ public struct TrashService: Sendable {journal?.note([item], batch: removal)}result.trashed.append(item)- moved.append(path)+ moved.add(path)MoveCount.current?.add(1)} catch {result.failures.append(TrashFailure(url: url, reason: Self.reason(for: error)))@@ -341,12 +341,12 @@ public struct TrashService: Sendable {}// An item inside another folder of this request goes with that folder. Sent on its own, the helper would// find it gone and report a failure.- let paths = permitted.map(PathPattern.comparablePath)+ var paths = Folders()+ permitted.forEach { paths.add(PathPattern.comparablePath(of: $0)) }let openFiles = OpenFiles()var allowed: [URL] = []for url in permitted {- let path = PathPattern.comparablePath(of: url)- guard !paths.contains(where: { PathComponents.isPath(path, inside: $0) }) else { continue }+ guard !paths.hold(PathPattern.comparablePath(of: url)) else { continue }let holders = openFiles.holders(of: url)guard holders.isEmpty else {result.failures.append(TrashFailure(url: url, reason: .heldOpen(by: holders)))@@ -573,3 +573,19 @@ public struct TrashService: Sendable {return contents.first { FileIdentity.Link.of($0) == link }}}++/// Folders a request takes, looked up by a path's own names, one level at a time, rather than compared one by one+/// with every folder: a request of thousands of items would otherwise compare each with all the others.+private struct Folders {+ private var names: Set<[String]> = []++ mutating func add(_ path: String) {+ names.insert(PathComponents.of(path))+ }++ /// Whether `path` sits inside one of the folders, as `PathComponents.isPath(_:inside:)` asks.+ func hold(_ path: String) -> Bool {+ let parts = PathComponents.of(path)+ return (0..<parts.count).contains { names.contains(Array(parts[..<$0])) }+ }+}diff --git a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swiftindex 8f50bdb6..3d31038b 100644--- a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift+++ b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift@@ -106,7 +106,12 @@ public struct Exclusions: Sendable, Codable, Hashable {/// inside its folder to a comparison of characters.public func excludes(_ url: URL) -> Bool {guard !spellings.isEmpty else { return false }- return Self.spellings(of: url).contains { path in+ return excludes(spellings: Self.spellings(of: url))+ }++ /// `excludes(_:)` for a path whose spellings are already worked out.+ func excludes(spellings asked: Set<String>) -> Bool {+ asked.contains { path inlet names = PathComponents.of(path)return spellings.contains { names.starts(with: $0) }}@@ -115,7 +120,12 @@ public struct Exclusions: Sendable, Codable, Hashable {/// True when something excluded sits inside `url`, so moving `url` would take it along.public func holds(_ url: URL) -> Bool {guard !spellings.isEmpty else { return false }- let asked = Self.spellings(of: url).map(PathComponents.of)+ return holds(spellings: Self.spellings(of: url))+ }++ /// `holds(_:)` for a path whose spellings are already worked out.+ func holds(spellings asked: Set<String>) -> Bool {+ let asked = asked.map(PathComponents.of)return spellings.contains { inside inasked.contains { inside.count > $0.count && inside.starts(with: $0) }}diff --git a/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift b/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swiftindex 44b89a7a..98aabe9d 100644--- a/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift+++ b/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift@@ -261,7 +261,12 @@ public enum ProtectedData: Sendable {/// inside, something protected. The two stay apart because scanners also use `refuses` to decide whether/// to look inside a folder, and every folder from `/` down to the home folder holds something protected.public static func holds(_ path: String, home: String) -> Bool {- spellings(of: path).contains { spelling in+ holds(spellings: spellings(of: path), home: home)+ }++ /// `holds(_:home:)` for a path whose `spellings(of:)` are already worked out.+ public static func holds(spellings: Set<String>, home: String) -> Bool {+ spellings.contains { spelling inlet names = PathComponents.of(spelling)let holdsIt = { (tree: [String]) in tree.count > names.count && tree.starts(with: names) }for home in homes(of: spelling, given: home) where trees(under: home).sitInside(names) {@@ -364,7 +369,12 @@ public enum ProtectedData: Sendable {/// Running as root, the helper can see every account on the Mac, so a home folder is any folder directly/// inside `/Users`, as well as the one given.public static func refuses(_ path: String, home: String) -> Bool {- spellings(of: path).contains { spelling in+ refuses(spellings: spellings(of: path), home: home)+ }++ /// `refuses(_:home:)` for a path whose `spellings(of:)` are already worked out.+ public static func refuses(spellings: Set<String>, home: String) -> Bool {+ spellings.contains { spelling inlet names = PathComponents.of(spelling)if names.contains(where: { name in extensions.contains { name.hasSuffix("." + $0) } }) { return true }if isGlobalPreferences(spelling) || isInAnApplesGroupContainer(spelling) { return true }