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
File Search asks the guard only about files that can make its list
Țugui Dragoșda0e5999af5a
diff --git a/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift b/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swiftindex fb53009c..2b7edb78 100644--- a/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift+++ b/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift@@ -91,38 +91,43 @@ public enum FileSearch {}/// Turns the paths Spotlight found into results: checked on disk, sorted, and cut to `maximumResults`. It is- /// separate from the query so a test can pass its own paths instead of relying on the Spotlight index.+ /// separate from the query so a test can pass its own paths, and its own answer in place of the guard's.static func results(from paths: [String],environment: SearchEnvironment = .current,- exclusions: Exclusions = .none+ exclusions: Exclusions = .none,+ allows: ((URL) -> Bool)? = nil) -> FileSearchResults {- let removalGuard = RemovalGuard(environment: environment, exclusions: exclusions)- var results = FileSearchResults()+ let allows = allows ?? RemovalGuard(environment: environment, exclusions: exclusions).allowsRemoval(of:)+ var candidates: [(url: URL, info: stat, belongsToAnApp: Bool)] = []for path in paths {var info = stat()guard lstat(path, &info) == 0, info.st_mode & S_IFMT == S_IFREG else { continue }let url = URL(filePath: path)- guard removalGuard.allowsRemoval(of: url) else { continue }- results.files.append(FoundFile(- url: url,- size: Int64(info.st_size),- modificationDate: Date(timeIntervalSince1970: TimeInterval(info.st_mtimespec.tv_sec)),- requiresPrivileges: FileAccess.requiresPrivilegesToRemove(url),- belongsToAnApp: isAppData(url, home: environment.homeDirectory),- identity: FileIdentity(info)- ))+ candidates.append((url, info, isAppData(url, home: environment.homeDirectory)))}+ // In the list's order before the guard is asked, so it is asked only about files that can make the list.// What an app keeps for itself goes last: the tool is for finding what the user made.- results.files.sort {+ candidates.sort {if $0.belongsToAnApp != $1.belongsToAnApp { return !$0.belongsToAnApp }- return $0.size != $1.size ? $0.size > $1.size : $0.url.path(percentEncoded: false) < $1.url.path(percentEncoded: false)+ return $0.info.st_size != $1.info.st_size+ ? $0.info.st_size > $1.info.st_size+ : $0.url.path(percentEncoded: false) < $1.url.path(percentEncoded: false)}- // Cut after the sort: `MDQuery.h` promises no order, so cutting first would keep an arbitrary set of- // files rather than the largest.- if results.files.count > maximumResults {- results.files = Array(results.files.prefix(maximumResults))- results.isTruncated = true+ var results = FileSearchResults()+ for candidate in candidates where allows(candidate.url) {+ guard results.files.count < maximumResults else {+ results.isTruncated = true+ break+ }+ results.files.append(FoundFile(+ url: candidate.url,+ size: Int64(candidate.info.st_size),+ modificationDate: Date(timeIntervalSince1970: TimeInterval(candidate.info.st_mtimespec.tv_sec)),+ requiresPrivileges: FileAccess.requiresPrivilegesToRemove(candidate.url),+ belongsToAnApp: candidate.belongsToAnApp,+ identity: FileIdentity(candidate.info)+ ))}return results}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swiftindex a0c1fbeb..2b5a0277 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift@@ -1,4 +1,5 @@import Foundation+import Synchronization@testable import PeelCoreimport Testingimport UniformTypeIdentifiers@@ -127,6 +128,32 @@ struct FileSearchTests {#expect(results.isTruncated)}+ /// 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 asksTheGuardOnlyAboutFilesThatCanMakeTheList() throws {+ let directory = try TemporaryDirectory()+ let home = directory.url.appending(path: "home", directoryHint: .isDirectory)+ var paths: [String] = []+ for index in 0..<(FileSearch.maximumResults + 50) {+ paths.append(try directory.file("home/Documents/file\(index).png", bytes: index + 1).path(percentEncoded: false))+ }+ let asked = Mutex(0)++ let results = FileSearch.results(+ from: paths,+ environment: SearchEnvironment(homeDirectory: home, rootDirectory: directory.url),+ allows: { _ in+ asked.withLock { $0 += 1 }+ return true+ }+ )++ #expect(results.files.count == FileSearch.maximumResults)+ #expect(results.isTruncated)+ #expect(results.files.first?.size == Int64(FileSearch.maximumResults + 50))+ #expect(asked.withLock { $0 } == FileSearch.maximumResults + 1)+ }+/// An excluded file is never listed, whatever Spotlight says about it.@Test func leavesOutWhatTheUserExcluded() throws {let directory = try TemporaryDirectory()