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
Duplicates asks the costly checks only of files that could be copies
Țugui Dragoș7a335690b3c6
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swiftindex f474710e..0bdd51b7 100644--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift@@ -128,7 +128,7 @@ public struct DuplicateFinder: Sendable {)}- private struct Candidate: Sendable {+ struct Candidate: Sendable {let url: URLlet identity: FileIdentity}@@ -159,8 +159,7 @@ public struct DuplicateFinder: Sendable {let excludedFolders = Self.managedPaths(home: homeDirectory)let minimumSize = max(1, options.minimumSize)let unreadable = UnreadableLocations()- var seen = Set<FileIdentity.Link>()- var candidates: [Candidate] = []+ var walked: [Candidate] = []var projects = Set<String>()var visited = 0@@ -180,7 +179,7 @@ public struct DuplicateFinder: Sendable {visited += 1if visited.isMultiple(of: 256) {guard !Task.isCancelled else { throw CancellationError() }- progress(.collecting(filesFound: candidates.count))+ progress(.collecting(filesFound: walked.count))}if ProjectArtifacts.isMarker(url.lastPathComponent) {let parent = Self.path(of: url.deletingLastPathComponent())@@ -198,7 +197,6 @@ public struct DuplicateFinder: Sendable {}continue}- guard !exclusions.excludes(url), removalGuard.allowsRemoval(of: url), !url.isInTheCloud else { continue }var info = stat()guardvalues.isRegularFile == true,@@ -208,16 +206,34 @@ public struct DuplicateFinder: Sendable {ReclaimableSpace.held(info) >= minimumSize,options.kind == .any || values.contentType.map(options.kind.includes) == trueelse { continue }- let identity = FileIdentity(info)- if seen.insert(identity.link).inserted {- candidates.append(Candidate(url: url, identity: identity))- }+ walked.append(Candidate(url: url, identity: FileIdentity(info)))}}guard !Task.isCancelled else { throw CancellationError() }- let outsideProjects = projects.isEmpty ? candidates : candidates.filter { !Self.isInside(projects, $0.url) }+ let outsideProjects = projects.isEmpty ? walked : walked.filter { !Self.isInside(projects, $0.url) }progress(.collecting(filesFound: outsideProjects.count))- return (outsideProjects, unreadable.urls)+ let movable = try Self.movable(outsideProjects) { removalGuard.allowsRemoval(of: $0) && !$0.isInTheCloud }+ return (movable, unreadable.urls)+ }++ /// The files that could be a copy of another and may be moved, each file once whatever names it has. Only a+ /// file of the same size as another can be a copy, so `isMovable`, the guard and the cloud check, which open the+ /// file and the folders above it, is asked of those alone, in the order the walk found them: a name it refuses+ /// leaves the file to its next name.+ static func movable(_ files: [Candidate], isMovable: (URL) -> Bool) throws(CancellationError) -> [Candidate] {+ let sizes = Dictionary(files.map { ($0.identity.size, 1) }, uniquingKeysWith: +)+ var seen = Set<FileIdentity.Link>()+ var movable: [Candidate] = []+ var asked = 0+ for file in files where sizes[file.identity.size, default: 0] > 1 {+ asked += 1+ if asked.isMultiple(of: 256) {+ guard !Task.isCancelled else { throw CancellationError() }+ }+ guard isMovable(file.url), seen.insert(file.identity.link).inserted else { continue }+ movable.append(file)+ }+ return movable}static func isInside(_ folders: Set<String>, _ url: URL) -> Bool {diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swiftindex 97e1cc30..0d9e3d8e 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift@@ -51,6 +51,32 @@ struct DuplicateFinderTests {#expect(FileIdentity(late).modificationDate == Date(timeIntervalSince1970: 10_000_000_000.000000005))}+ /// Only a file with another of its size can be a copy, so only those are asked whether they may move, which+ /// opens the file and the folders above it. A name that may not move leaves the file to its next name.+ @Test func asksWhetherAFileMayMoveOnlyWhenAnotherHasItsSize() throws {+ let directory = try TemporaryDirectory()+ let sizes = ["a": 10, "b": 10, "lonely": 11, "refused": 12, "other": 12]+ for (name, size) in sizes {+ try directory.file("files/\(name)", contents: randomData(count: size))+ }+ try FileManager.default.linkItem(at: directory.url.appending(path: "files/refused"), to: directory.url.appending(path: "files/linked"))+ let files = try ["a", "b", "lonely", "refused", "linked", "other"].map { name in+ let url = directory.url.appending(path: "files/\(name)")+ var info = stat()+ try #require(lstat(url.path(percentEncoded: false), &info) == 0)+ return DuplicateFinder.Candidate(url: url, identity: FileIdentity(info))+ }++ var asked: [String] = []+ let movable = try DuplicateFinder.movable(files) { url in+ asked.append(url.lastPathComponent)+ return url.lastPathComponent != "refused"+ }++ #expect(asked == ["a", "b", "refused", "linked", "other"])+ #expect(movable.map(\.url.lastPathComponent) == ["a", "b", "linked", "other"])+ }+@Test func groupsFilesWithIdenticalContents() async throws {let directory = try TemporaryDirectory()let photo = randomData(count: 4_000)