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.

Keep the copy on the home's disk even when it is in Downloads

Țugui Dragoș8dad70acc205
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift
index 1aefe036..fa1ee136 100644
--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift
@@ -452,14 +452,14 @@ public struct DuplicateFinder: Sendable {
homeDevice = stat(home.path(percentEncoded: false), &info) == 0 ? info.st_dev : nil
}
- /// Ranks a copy for keeping, lower being better: outside Downloads, without a copy suffix, on the disk the
- /// home folder is on, created earlier, less deeply nested, then with a shorter name. The path breaks any tie
- /// that is left.
+ /// Ranks a copy for keeping, lower being better: on the disk the home folder is on, outside Downloads, without
+ /// a copy suffix, created earlier, less deeply nested, then with a shorter name. The path breaks any tie that
+ /// is left.
func rank(of url: URL, identity: FileIdentity) -> (Int, Int, Int, Int, String) {
let path = DuplicateFinder.path(of: url)
let name = url.deletingPathExtension().lastPathComponent
let flags = [
- path.isInside(downloads), DuplicateFinder.hasCopySuffix(name), identity.link.device != homeDevice,
+ identity.link.device != homeDevice, path.isInside(downloads), DuplicateFinder.hasCopySuffix(name),
]
return (
// Each flag outweighs those after it.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
index 83514ed6..ee994af6 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
@@ -763,6 +763,32 @@ struct DuplicateFinderTests {
#expect(kept(files.groups.first?.files.first?.url) == kept(onTheMac))
}
+ @Test func keepsTheCopyOnTheHomeDiskEvenInDownloads() async throws {
+ let directory = try TemporaryDirectory()
+ let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
+ let volume = try ScratchVolume(
+ fileSystem: "APFS", mountedAt: home.appending(path: "Card", directoryHint: .isDirectory)
+ )
+ let contents = Data(repeating: 9, count: 200_000)
+ let onTheMac = try directory.file("home/Downloads/Holiday/photo.jpg", contents: contents)
+ let onTheCard = volume.url.appending(path: "Holiday/photo.jpg")
+ try FileManager.default.createDirectory(
+ at: onTheCard.deletingLastPathComponent(), withIntermediateDirectories: true
+ )
+ try contents.write(to: onTheCard)
+
+ let downloads = home.appending(path: "Downloads", directoryHint: .isDirectory)
+ let finder = DuplicateFinder(homeDirectory: home)
+ let kept = { (url: URL?) in url.map { PathPattern.comparablePath(of: $0) } }
+
+ let folders = try await finder.scan(.everySize(in: [downloads, volume.url]))
+ #expect(kept(folders.folderGroups.first?.folders.first?.url) == kept(onTheMac.deletingLastPathComponent()))
+
+ try directory.file("home/Downloads/Holiday/notes.txt", contents: Data("only here".utf8))
+ let files = try await finder.scan(.everySize(in: [downloads, volume.url]))
+ #expect(kept(files.groups.first?.files.first?.url) == kept(onTheMac))
+ }
+
@Test func selectsWhatItRecommendsOnlyInTheGroupsShown() async throws {
let directory = try TemporaryDirectory()
let first = randomData(count: 4_000)