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.

Don't ask the guard about macOS's own files in File Search

Țugui Dragoș015f5c620677
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
index 61050cb5..3e3bdc7d 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
@@ -151,6 +151,12 @@ struct RemovalGuard: Sendable {
isAFolder && Array(PathComponents.of(spelling).dropLast()) == caskroom
}
+ /// Whether a regular file at `path` sits inside `protectedPrefixes`, read from the name alone.
+ static func neverMovesAFile(at path: String) -> Bool {
+ let lowered = path.lowercased()
+ return protectedPrefixes.contains { PathComponents.isPath(lowered, inside: $0) }
+ }
+
private static func type(of path: String) -> mode_t? {
var info = stat()
return lstat(path, &info) == 0 ? info.st_mode & S_IFMT : nil
diff --git a/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift b/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift
index 2aae73e5..3cd7dd55 100644
--- a/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift
@@ -273,6 +273,8 @@ public enum FileSearch {
return kind == .movies && isTypedAsATransportStream(url) ? .skipped : .leftOut(.syncs)
}
guard kind != .movies || isAMovie(url) else { return .skipped }
+ // A search of this Mac finds macOS's own files by the tens of thousands, and the guard opens each it judges.
+ if RemovalGuard.neverMovesAFile(at: url.path(percentEncoded: false)) { return .leftOut(.protected) }
guard let refused = refusal(url) else { return .listed }
// That a file syncs is what the person needs to know, whatever else the guard says: a file in iCloud Drive
// sits in a tree the guard protects.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift
index 9dfb3a91..c02c46c5 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift
@@ -445,6 +445,26 @@ struct FileSearchTests {
/// The guard is asked only about the files that can make the list: the largest first, and no further once
/// the list is full and one more file shows it had to be cut.
+ @Test func neverAsksTheGuardAboutWhatMacOSKeepsInItsOwnFolders() throws {
+ let macOSsOwn = [
+ "/System/Library/CoreServices/SystemVersion.plist", "/usr/bin/true", "/bin/ls", "/sbin/mount",
+ ]
+ let asked = Mutex(0)
+
+ let results = FileSearch.results(from: macOSsOwn, refusal: { _ in
+ asked.withLock { $0 += 1 }
+ return nil
+ })
+
+ let timesAsked = asked.withLock { $0 }
+ #expect(timesAsked == 0)
+ #expect(results.files.isEmpty)
+ #expect(results.leftOut == [.protected: macOSsOwn.count])
+ for path in macOSsOwn {
+ #expect(RemovalGuard(environment: .current).refusal(of: URL(filePath: path)) == .protectedLocation)
+ }
+ }
+
@Test func asksTheGuardOnlyAboutFilesThatCanMakeTheList() throws {
let directory = try TemporaryDirectory()
let home = directory.url.appending(path: "home", directoryHint: .isDirectory)