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 copies with another Finder comment apart in Duplicates

Țugui Dragoșfdbc7f6692a8
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/FileExtras.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/FileExtras.swift
index dbbb32fb..44ad03fe 100644
--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/FileExtras.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/FileExtras.swift
@@ -2,24 +2,29 @@ import CryptoKit
import Darwin
import Foundation
-/// What a copy carries besides its bytes, which moving it to the Trash takes away from where it was: its Finder tags
-/// and its resource fork. Copies that differ in either are not duplicates.
+/// What a copy carries besides its bytes, which moving it to the Trash takes away from where it was: its Finder tags,
+/// its resource fork, and its Finder comment. Copies that differ in any of them are not duplicates.
enum FileExtras {
- /// The tags, in order, and a digest of the resource fork, as bytes to compare. Nil when the fork is there and
- /// cannot be read, since two copies are then not known to be alike.
+ /// The tags, in order, and a digest of the resource fork and of the comment, as bytes to compare. Nil when one
+ /// of those is there and cannot be read, since two copies are then not known to be alike.
static func of(_ url: URL) -> [UInt8]? {
var bytes: [UInt8] = []
for tag in ((try? url.resourceValues(forKeys: [.tagNamesKey]).tagNames) ?? []).sorted() {
bytes += Array(tag.utf8) + [0]
}
let path = url.path(percentEncoded: false)
- let size = getxattr(path, resourceFork, nil, 0, 0, XATTR_NOFOLLOW)
- guard size > 0 else { return bytes }
- var fork = [UInt8](repeating: 0, count: size)
- guard getxattr(path, resourceFork, &fork, size, 0, XATTR_NOFOLLOW) == size else { return nil }
- return bytes + [1] + Array(SHA256.hash(data: fork))
+ for (mark, name) in [(UInt8(1), resourceFork), (UInt8(2), finderComment)] {
+ let size = getxattr(path, name, nil, 0, 0, XATTR_NOFOLLOW)
+ guard size > 0 else { continue }
+ var value = [UInt8](repeating: 0, count: size)
+ guard getxattr(path, name, &value, size, 0, XATTR_NOFOLLOW) == size else { return nil }
+ bytes += [mark] + Array(SHA256.hash(data: value))
+ }
+ return bytes
}
/// `XATTR_RESOURCEFORK_NAME` in `<sys/xattr.h>`.
private static let resourceFork = "com.apple.ResourceFork"
+ /// Where the Finder keeps a file's comment, which Spotlight reads as `kMDItemFinderComment`.
+ private static let finderComment = "com.apple.metadata:kMDItemFinderComment"
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
index a1fe6998..5853d0f9 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
@@ -141,6 +141,26 @@ struct DuplicateFinderTests {
#expect(result.folderGroups.isEmpty)
}
+ /// The Finder keeps a comment in the copy's extended attributes, so it goes with the copy, as tags do.
+ @Test func copiesWithAnotherFinderCommentAreNotDuplicates() async throws {
+ let directory = try TemporaryDirectory()
+ let report = randomData(count: 4_000)
+ try directory.file("home/Documents/Report.pdf", contents: report)
+ let commented = try directory.file("home/Desktop/Report.pdf", contents: report)
+ let comment = try PropertyListSerialization.data(fromPropertyList: "Signed copy", format: .binary, options: 0)
+ let written = comment.withUnsafeBytes {
+ setxattr(
+ commented.path(percentEncoded: false), "com.apple.metadata:kMDItemFinderComment", $0.baseAddress,
+ comment.count, 0, 0
+ )
+ }
+ #expect(written == 0)
+
+ let result = try await scan(directory)
+
+ #expect(result.groups.isEmpty)
+ }
+
/// Duplicates scans again only when asked, so what is excluded after a scan is taken out of it: the copy
/// leaves its group, a group left with one copy goes, and a group that lost its kept copy keeps the next one.
@Test func whatIsExcludedAfterTheScanLeavesIt() async throws {
diff --git a/SAFETY.md b/SAFETY.md
index 61f64ad5..718afcfc 100644
--- a/SAFETY.md
+++ b/SAFETY.md
@@ -349,9 +349,11 @@ On every list, Select All selects every row you can select, what Peel holds back
counting what Peel doesn't recommend; Return answers Select Recommended. Each held back row still shows its reason
beside it.
-In Duplicates, one copy of every group always stays, and a file that changed since the scan is refused. Nothing
-inside an app or another package is ever offered, nor a hidden folder, such as a tool's settings in `~/.config`:
-they only count toward the folder around them.
+In Duplicates, one copy of every group always stays, and a file that changed since the scan is refused. Nothing inside
+an app or another package is ever offered, nor a hidden folder, such as a tool's settings in `~/.config`: they only
+count toward the folder around them. Copies that differ in their Finder tags, their Finder comment, or their resource
+fork are not duplicates, since moving one would take that away, and a folder kept only in the cloud is never looked
+into.
What you select on the pages that free space (Orphaned Files, Space, Developer, Build Artifacts, Installers and
Backups, Duplicates, and File Search) stays selected while you look at the others, and Move to Trash on any of