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.

Scan the folders that sync and list their copies as ones that stay

Țugui Dragoș3f8fc2cc532c
diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstrings
index b4d6f6db..911354ce 100644
--- a/Localization/Peel/Localizable.xcstrings
+++ b/Localization/Peel/Localizable.xcstrings
@@ -154307,6 +154307,112 @@
}
}
},
+ "Stays" : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Synchronizuje se"
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Bleibt"
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Se sincroniza"
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Reste en place"
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Resta"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "同期済み"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "남음"
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Gesynchroniseerd"
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Synchronizowane"
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Sincronizada"
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Se sincronizează"
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Синхронизируется"
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Synkroniseras"
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Eşzamanlanıyor"
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Синхронізується"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "已同步"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "已同步"
+ }
+ }
+ }
+ },
"Step %lld of %lld" : {
"localizations" : {
"cs" : {
@@ -155791,6 +155897,112 @@
}
}
},
+ "Synced to Other Devices" : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Synchronizováno s ostatními zařízeními"
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Mit anderen Geräten synchronisiert"
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Sincronizados con otros dispositivos"
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Synchronisés avec vos autres appareils"
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Sincronizzati con gli altri tuoi dispositivi"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "ほかのデバイスに同期済み"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "다른 기기와 동기화된 항목"
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Gesynchroniseerd met je andere apparaten"
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Synchronizowane z Twoimi pozostałymi urządzeniami"
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Sincronizados com Outros Dispositivos"
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Sincronizate pe celelalte dispozitive ale dvs."
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Синхронизировано с другими Вашими устройствами"
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Synkroniseras till andra enheter"
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Diğer Aygıtlarınızla Eşzamanlananlar"
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Синхронізовано з іншими вашими пристроями"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "已同步到其他设备"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "已同步到其他裝置"
+ }
+ }
+ }
+ },
"System" : {
"localizations" : {
"cs" : {
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
index 6d1b5fc1..85ccba43 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
@@ -582,7 +582,7 @@ struct DuplicatesCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "duplicates",
abstract: "Find folders and files with identical contents.",
- discussion: "Without folders, Peel scans Desktop, Documents, Downloads, Pictures, Movies, and Music. The first copy of each group is the one Peel suggests keeping. A folder is a copy of another only when every file under it matches, so what a folder already speaks for isn't listed again on its own."
+ discussion: "Without folders, Peel scans Desktop, Documents, Downloads, Pictures, Movies, and Music. The first copy of each group is the one Peel suggests keeping. A copy in a folder that syncs to your other devices, such as iCloud Drive, is listed as one that stays and is never moved. A folder is a copy of another only when every file under it matches, so what a folder already speaks for isn't listed again on its own."
)
@Argument(help: "Folders to scan.", completion: .directory)
@@ -604,7 +604,10 @@ struct DuplicatesCommand: AsyncParsableCommand {
struct Record: Encodable {
let size: Int64
let reclaimableSize: Int64
+ /// The copies that may move, the kept one first.
let files: [String]
+ /// The copies that sync to other devices, listed and never moved.
+ let synced: [String]
}
struct FolderRecord: Encodable {
@@ -635,7 +638,7 @@ struct DuplicatesCommand: AsyncParsableCommand {
}
let rejected = urls.filter { !finder.canScan($0) }
guard rejected.isEmpty else {
- throw ValidationError("Peel can't scan \(rejected.map(Output.path).joined(separator: ", ")). Choose folders in your home folder, /Users/Shared, or a disk Peel can write to. iCloud Drive and other folders that sync to your other devices, app data, the Music and TV apps' media folders, apps and other packages, and system folders aren't scanned.")
+ throw ValidationError("Peel can't scan \(rejected.map(Output.path).joined(separator: ", ")). Choose folders in your home folder, /Users/Shared, or a disk Peel can write to. App data, the Music and TV apps' media folders, apps and other packages, and system folders aren't scanned.")
}
}
@@ -662,7 +665,7 @@ struct DuplicatesCommand: AsyncParsableCommand {
}
for group in scan.groups {
Output.line(Self.heading(for: group))
- Output.table(Self.rows(for: group.files.map(\.url)), indent: " ")
+ Output.table(Self.rows(for: group.files), indent: " ")
}
let total =
scan.groups.reduce(0) { $0 + $1.reclaimableSize }
@@ -690,7 +693,11 @@ struct DuplicatesCommand: AsyncParsableCommand {
}
if !scan.skippedLocations.isEmpty {
let folders = scan.skippedLocations.map { Output.path($0.url) }.joined(separator: ", ")
- notes.append("Peel didn't look in \(folders), so there may be duplicates there. It leaves repositories alone, and doesn't scan iCloud Drive, app data, or system folders.")
+ notes.append("Peel didn't look in \(folders), so there may be duplicates there. It leaves repositories alone, and doesn't scan app data or system folders.")
+ }
+ let synced = scan.groups.reduce(0) { total, group in total + group.files.count { $0.heldBack != nil } }
+ if synced > 0 {
+ notes.append("\(Output.count(synced, "copy syncs", "copies sync")) to your other devices, so Peel lists them as copies that stay and never moves them.")
}
return notes
}
@@ -705,8 +712,13 @@ struct DuplicatesCommand: AsyncParsableCommand {
folders: group.folders.map { Output.path($0.url) }
)
},
- files: scan.groups.map {
- Record(size: $0.size, reclaimableSize: $0.reclaimableSize, files: $0.files.map { Output.path($0.url) })
+ files: scan.groups.map { group in
+ Record(
+ size: group.size,
+ reclaimableSize: group.reclaimableSize,
+ files: group.movableFiles.map { Output.path($0.url) },
+ synced: group.files.filter { $0.heldBack != nil }.map { Output.path($0.url) }
+ )
},
unreadableLocations: scan.unreadableLocations.map(Output.path),
skippedLocations: scan.skippedLocations.map { Output.path($0.url) }
@@ -737,6 +749,15 @@ struct DuplicatesCommand: AsyncParsableCommand {
copies.enumerated().map { index, url in [index == 0 ? "keep" : "", Output.path(url)] }
}
+ /// One row per copy of a file: the first copy that may move is marked "keep", one that syncs to other devices
+ /// "stays", since Peel lists it and never moves it.
+ static func rows(for files: [DuplicateFile]) -> [[String]] {
+ let kept = files.first { $0.heldBack == nil }?.url
+ return files.map { file in
+ [file.heldBack != nil ? "stays" : file.url == kept ? "keep" : "", Output.path(file.url)]
+ }
+ }
+
func clean(
_ scan: DuplicateScan,
using service: TrashService,
@@ -744,15 +765,18 @@ struct DuplicatesCommand: AsyncParsableCommand {
refusals: RefusalLog = RefusalLog()
) async throws {
let folders = scan.folderGroups.flatMap { $0.folders.dropFirst() }
- let files = scan.groups.flatMap { $0.files.dropFirst() }
+ let files = scan.groups.flatMap { $0.movableFiles.dropFirst() }
guard !folders.isEmpty || !files.isEmpty else {
Output.line("No duplicates found.")
return
}
- // Where each group's kept copy is comes first: it is what the decision rests on.
- let kept = scan.folderGroups.compactMap { $0.folders.first?.url }
- + scan.groups.compactMap { $0.files.first?.url }
- Output.table(kept.map { ["keep", Output.path($0)] })
+ // Where each group's kept copy is comes first, and the copies that sync, which stay: it is what the
+ // decision rests on.
+ let kept = scan.folderGroups.compactMap { $0.folders.first?.url }.map { ["keep", Output.path($0)] }
+ + scan.groups.flatMap { group in
+ Self.rows(for: group.files).filter { $0[0] != "" }
+ }
+ Output.table(kept)
let cleanup = Cleanup.of(
folders.map { (url: $0.url, size: $0.reclaimableSize) }
+ files.map { (url: $0.url, size: $0.reclaimableSize) },
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift
index e47193df..e34d9ca1 100644
--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift
@@ -46,12 +46,9 @@ public struct DuplicateFinder: Sendable {
.filter { FileManager.default.fileExists(atPath: $0.path(percentEncoded: false)) }
}
- /// Why a chosen folder is not scanned. `refusal(of:)` answers the first four for any folder, which is what
+ /// Why a chosen folder is not scanned. `refusal(of:)` answers the first three for any folder, which is what
/// `canScan` refuses; a scan adds the last two for a chosen folder it finds so.
public enum Refusal: Sendable, Hashable {
- /// It syncs to other devices, through iCloud, a file provider, or a sync app's folder, where a removed copy
- /// would be removed too.
- case inTheCloud
/// App data, the Music and TV apps' media folders, or a place Peel protects, such as a library or a keychain.
case appData
/// An app or another package, or a folder inside one: what is inside belongs to it.
@@ -65,22 +62,24 @@ public struct DuplicateFinder: Sendable {
}
/// Whether `folder` may be scanned: a folder in the home folder, in `/Users/Shared`, or on another writable
- /// volume. App data, system locations, and places whose files cannot be brought back are refused, such as
- /// iCloud Drive or a folder another app keeps in step, where a removal reaches every device.
+ /// volume. App data and system locations are refused. A folder that syncs to other devices, such as iCloud
+ /// Drive or one another app keeps in step, is scanned, and each copy found in it is held back: a removal there
+ /// reaches every device, so the scan shows such copies and never moves them.
public func canScan(_ folder: URL) -> Bool {
refusal(of: folder) == nil
}
- /// Why `folder` may not be scanned, or nil. The cloud is asked first: a folder in iCloud Drive or in
- /// `Library/CloudStorage` is protected data too, and syncing is the reason that explains it.
+ /// Why `folder` may not be scanned, or nil. iCloud Drive and the folder where file providers keep their files
+ /// are in the home's Library and protected data to the guard, which refuses each copy inside when the scan
+ /// asks it; the scan shows those copies and never moves them, so neither rule refuses such a folder here.
public func refusal(of folder: URL) -> Refusal? {
let path = Self.path(of: folder.resolvingSymlinksInPath())
let home = Self.path(of: homeDirectory)
- if SyncedFolders().syncsToOtherDevices(folder) || Self.isInACloudFolder(path) { return .inTheCloud }
- if ProtectedData.refuses(path, home: home) { return .appData }
if folder.isOrIsInsideAPackage { return .inAPackage }
+ let syncs = Self.isInACloudFolder(path)
+ if !syncs, ProtectedData.refuses(path, home: home) { return .appData }
if path.isInside(home) {
- return Self.managedFolders.contains { path.isInside(home + "/" + $0) } ? .appData : nil
+ return !syncs && Self.managedFolders.contains { path.isInside(home + "/" + $0) } ? .appData : nil
}
guard path.isInside("/Users/Shared") || PathComponents.isPath(path, inside: "/Volumes") else {
return .outsideTheHome
@@ -181,7 +180,13 @@ public struct DuplicateFinder: Sendable {
}
}
guard !Task.isCancelled else { throw CancellationError() }
- let sampledGroups = try Self.movable(Self.grouped(sampled), names: names, isMovable: removalGuard.allowsRemoval)
+ let synced = SyncedFolders()
+ let sampledGroups = try Self.movable(
+ Self.grouped(sampled),
+ names: names,
+ refusal: removalGuard.refusal(of:),
+ syncsToOtherDevices: { synced.syncsToOtherDevices($0) || Self.isInACloudFolder(Self.path(of: $0)) }
+ )
let finalGroups = sampledGroups.filter { !FileDigest.isSampled(size: $0[0].item.identity.size) }
let toVerify = sampledGroups.filter { FileDigest.isSampled(size: $0[0].item.identity.size) }
@@ -210,6 +215,8 @@ public struct DuplicateFinder: Sendable {
struct Candidate: Sendable {
let url: URL
let identity: FileIdentity
+ /// Set by `movable` for a copy that is kept in its group but never moved.
+ var heldBack: HoldBack? = nil
}
typealias HashedCandidate = (item: Candidate, digest: ContentDigest)
@@ -302,28 +309,40 @@ public struct DuplicateFinder: Sendable {
}
/// The groups as they may move: each file under the first of its `names`, in the order the walk found them,
- /// that `isMovable` allows, and a group only while two files are left. `isMovable`, the guard, which opens the
- /// file and the folders above it, is asked only here, of files whose first and last bytes already match another's.
+ /// that `refusal` allows, else under the first that syncs to other devices, held back, and a group only while
+ /// two files are left. `refusal`, the guard, which opens the file and the folders above it, is asked only here,
+ /// of files whose first and last bytes already match another's. A name the guard refuses that syncs is kept
+ /// whatever the guard's own reason: the guard would refuse the move either way, and that it syncs is what the
+ /// person needs to know.
static func movable(
_ groups: [[HashedCandidate]],
names: [FileIdentity.Link: [Candidate]],
- isMovable: (URL) -> Bool
+ refusal: (URL) -> GuardRefusal?,
+ syncsToOtherDevices: (URL) -> Bool
) throws(CancellationError) -> [[HashedCandidate]] {
var asked = 0
var movable: [[HashedCandidate]] = []
for group in groups {
var kept: [HashedCandidate] = []
for member in group {
+ var synced: Candidate?
for name in names[member.item.identity.link] ?? [member.item] {
asked += 1
if asked.isMultiple(of: 256) {
guard !Task.isCancelled else { throw CancellationError() }
}
- if isMovable(name.url) {
+ if refusal(name.url) == nil {
kept.append((item: name, digest: member.digest))
+ synced = nil
break
}
+ if synced == nil, syncsToOtherDevices(name.url) {
+ var held = name
+ held.heldBack = .inTheCloud
+ synced = held
+ }
}
+ if let synced { kept.append((item: synced, digest: member.digest)) }
}
if kept.count > 1 { movable.append(kept) }
}
@@ -366,7 +385,8 @@ public struct DuplicateFinder: Sendable {
size: candidate.identity.size,
reclaimableSize: ReclaimableSpace.of(candidate.url),
allocatedSize: ReclaimableSpace.allocated(candidate.url),
- identity: candidate.identity
+ identity: candidate.identity,
+ heldBack: candidate.heldBack
)
}
return DuplicateGroup(id: members[0].digest.hexadecimal, files: files)
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift
index c50e5b1e..ba627bc2 100644
--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift
@@ -39,6 +39,9 @@ public struct DuplicateFile: Sendable, Hashable, Identifiable {
reclaimableSize < allocatedSize
}
let identity: FileIdentity
+ /// Why the copy is listed but never moved: it syncs to the person's other devices, where a move to the Trash
+ /// would remove it too. Nil for a copy that may move.
+ public var heldBack: HoldBack? = nil
public var id: URL { url }
@@ -50,15 +53,28 @@ public struct DuplicateFile: Sendable, Hashable, Identifiable {
public struct DuplicateGroup: Sendable, Hashable, Identifiable {
/// The SHA-256 of the contents.
public let id: String
- /// Identical files, the suggested one to keep first.
+ /// Identical files, the suggested one to keep first. A copy that syncs to other devices is among them, held
+ /// back: it is shown and never moved.
public let files: [DuplicateFile]
public var size: Int64 {
files.first?.size ?? 0
}
+ /// The copies that may move, in the order of `files`: the first of them is the one Peel keeps.
+ public var movableFiles: [DuplicateFile] {
+ files.filter { $0.heldBack == nil }
+ }
+
+ /// True when every copy syncs to other devices, so nothing in the group can be freed: the app lists such a
+ /// group apart.
+ public var everyCopySyncs: Bool {
+ files.allSatisfy { $0.heldBack == .inTheCloud }
+ }
+
+ /// What moving every movable copy but the first would free.
public var reclaimableSize: Int64 {
- files.dropFirst().reduce(0) { $0 + $1.reclaimableSize }
+ movableFiles.dropFirst().reduce(0) { $0 + $1.reclaimableSize }
}
}
@@ -136,9 +152,9 @@ public struct DuplicateScan: Sendable {
groups.isEmpty && folderGroups.isEmpty && !notLookedIn.isEmpty
}
- /// Every file except the suggested one to keep in each group.
+ /// Every copy that may move except the first of them in each group. A copy that syncs is never suggested.
public var suggestedSelection: Set<URL> {
- Set(groups.flatMap { $0.files.dropFirst().map(\.url) })
+ Set(groups.flatMap { $0.movableFiles.dropFirst().map(\.url) })
}
public var suggestedFolderSelection: Set<URL> {
@@ -174,6 +190,12 @@ public struct DuplicateScan: Sendable {
selected.contains(url) || copies.count { !selected.contains($0) } > 1
}
+ /// `canChange(_:among:selected:)` for a copy of `group`: one held back can never be selected, and it counts as a
+ /// copy that stays for the others.
+ public static func canChange(_ file: DuplicateFile, in group: DuplicateGroup, selected: Set<URL>) -> Bool {
+ file.heldBack == nil && canChange(file.url, among: group.files.map(\.url), selected: selected)
+ }
+
private static func keepingOne(of groups: [[URL]], _ selection: Set<URL>) -> Set<URL> {
Set(groups.flatMap { copies in
let chosen = copies.filter(selection.contains)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AttackDuplicateTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AttackDuplicateTests.swift
index e1a8d7d4..d435344b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AttackDuplicateTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AttackDuplicateTests.swift
@@ -120,9 +120,9 @@ struct AttackDuplicateTests {
#expect(!left.isEmpty, "ATTACK SUCCEEDED: the group lost every copy")
}
- /// Duplicates does not scan iCloud Drive: the removal guard refuses everything in it, so every copy offered
- /// there would fail to move.
- @Test func iCloudDuplicatesAreOfferedAndThenRefused() async throws {
+ /// Duplicates scans iCloud Drive and lists every copy found there held back, never suggested; a removal asked
+ /// for anyway is refused by the guard, since a deletion there reaches every device.
+ @Test func iCloudDuplicatesAreListedHeldBackAndNeverMoved() async throws {
let directory = try TemporaryDirectory()
let contents = bytes(4_000)
let cloud = "home/Library/Mobile Documents/com~apple~CloudDocs"
@@ -130,10 +130,12 @@ struct AttackDuplicateTests {
try directory.file("\(cloud)/a copy.pdf", contents: contents)
let found = try await scan(directory, folders: [cloud])
- #expect(found.groups.isEmpty, "iCloud Drive is offered for duplicate cleaning")
- guard !found.groups.isEmpty else { return }
- let result = await DuplicateRemoval.trash(found.suggestedSelection, from: found, using: try service(directory))
- #expect(result.failures.map(\.reason) == [.guarded(.protectedLocation)], "and then every removal is refused")
+ let group = try #require(found.groups.first, "iCloud Drive is scanned")
+ #expect(group.everyCopySyncs, "ATTACK SUCCEEDED: a copy in iCloud Drive may move")
+ #expect(found.suggestedSelection.isEmpty)
+ let result = await DuplicateRemoval.trash([group.files[1].url], from: found, using: try service(directory))
+ #expect(result.trashed.isEmpty)
+ #expect(result.failures.map(\.reason) == [.guarded(.protectedLocation)], "and the removal is refused")
}
/// A media library whose app is not installed is a plain folder to the enumerator. A file inside one must not
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
index cad7afe8..45e56355 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
@@ -62,10 +62,11 @@ struct DuplicateFinderTests {
#expect(FileIdentity(late).modificationDate == Date(timeIntervalSince1970: 10_000_000_000.000000005))
}
- /// A name that may not move leaves the file to its next name, and a group left with one file is no group.
+ /// A name that may not move leaves the file to its next name, and a group left with one file is no group. A name
+ /// the guard refuses because it syncs to other devices is kept, held back: the group shows it and never moves it.
@Test func asksWhetherAFileMayMoveOnlyOnceItsBytesMatchAnother() throws {
let directory = try TemporaryDirectory()
- for name in ["a", "b", "refused", "other", "alone", "partner"] {
+ for name in ["a", "b", "refused", "other", "alone", "partner", "synced", "twin"] {
try directory.file("files/\(name)", contents: randomData(count: 10))
}
try FileManager.default.linkItem(at: directory.url.appending(path: "files/refused"), to: directory.url.appending(path: "files/linked"))
@@ -81,19 +82,27 @@ struct DuplicateFinderTests {
[(try candidate("a"), digest), (try candidate("b"), digest)],
[(refused, digest), (try candidate("other"), digest)],
[(try candidate("alone"), digest), (try candidate("partner"), digest)],
+ [(try candidate("synced"), digest), (try candidate("twin"), digest)],
]
var asked: [String] = []
let movable = try DuplicateFinder.movable(
groups,
- names: [refused.identity.link: [refused, try candidate("linked")]]
- ) { url in
- asked.append(url.lastPathComponent)
- return !["refused", "partner"].contains(url.lastPathComponent)
- }
+ names: [refused.identity.link: [refused, try candidate("linked")]],
+ refusal: { url in
+ asked.append(url.lastPathComponent)
+ switch url.lastPathComponent {
+ case "refused", "partner": return .protectedLocation
+ case "synced": return .inTheCloud
+ default: return nil
+ }
+ },
+ syncsToOtherDevices: { $0.lastPathComponent == "synced" }
+ )
- #expect(asked == ["a", "b", "refused", "linked", "other", "alone", "partner"])
- #expect(movable.map { $0.map(\.item.url.lastPathComponent) } == [["a", "b"], ["linked", "other"]])
+ #expect(asked == ["a", "b", "refused", "linked", "other", "alone", "partner", "synced", "twin"])
+ #expect(movable.map { $0.map(\.item.url.lastPathComponent) } == [["a", "b"], ["linked", "other"], ["synced", "twin"]])
+ #expect(movable[2].map(\.item.heldBack) == [.inTheCloud, nil])
}
@Test func groupsFilesWithIdenticalContents() async throws {
@@ -258,9 +267,34 @@ struct DuplicateFinderTests {
#expect(inside.groups.isEmpty, "a folder inside a repository was scanned")
}
- /// A sync app's marker at the top of the home folder sets every folder in it aside at once, each named with that
- /// reason: nothing was unread, so Full Disk Access is not what the person needs.
- @Test func namesWhyEveryChosenFolderWasSetAside() async throws {
+ /// A folder that syncs to other devices is scanned, and each copy found in it is listed and held back: a copy
+ /// moved there would be removed from every device. Nothing in such a group is suggested, since the one copy that
+ /// could move is the only one that stays on this Mac.
+ @Test func listsACopyThatSyncsApartAndNeverSelectsIt() async throws {
+ let directory = try TemporaryDirectory()
+ let contents = randomData(count: 3_000)
+ try directory.directory("home/Synced/.stfolder")
+ try directory.file("home/Synced/a.bin", contents: contents)
+ try directory.file("home/Documents/a.bin", contents: contents)
+
+ let scan = try await scan(directory, folders: ["home/Synced", "home/Documents"])
+ let group = try #require(scan.groups.first)
+
+ #expect(scan.skippedLocations.isEmpty)
+ let synced = try #require(group.files.first { $0.url.pathComponents.contains("Synced") })
+ let local = try #require(group.files.first { $0.url.pathComponents.contains("Documents") })
+ #expect(synced.heldBack == .inTheCloud)
+ #expect(local.heldBack == nil)
+ #expect(!group.everyCopySyncs)
+ #expect(group.movableFiles.map(\.url) == [local.url])
+ #expect(group.reclaimableSize == 0)
+ #expect(scan.suggestedSelection.isEmpty)
+ #expect(!DuplicateScan.canChange(synced, in: group, selected: []))
+ #expect(DuplicateScan.canChange(local, in: group, selected: []))
+ }
+
+ /// A group whose every copy syncs is listed too, under its own heading in the app, with nothing to free.
+ @Test func listsAGroupWhoseEveryCopySyncs() async throws {
let directory = try TemporaryDirectory()
let contents = randomData(count: 3_000)
try directory.directory("home/.stfolder")
@@ -268,13 +302,14 @@ struct DuplicateFinderTests {
try directory.file("home/Documents/a.bin", contents: contents)
let scan = try await scan(directory, folders: ["home/Desktop", "home/Documents"])
+ let group = try #require(scan.groups.first)
- #expect(scan.groups.isEmpty)
- #expect(scan.skippedLocations.map(\.url.lastPathComponent) == ["Desktop", "Documents"])
- #expect(scan.skippedLocations.map(\.reason) == [.inTheCloud, .inTheCloud])
- #expect(scan.unreadableLocations.isEmpty)
- #expect(!scan.needsFullDiskAccess)
- #expect(scan.readNothing)
+ #expect(scan.skippedLocations.isEmpty)
+ #expect(group.files.count == 2)
+ #expect(group.everyCopySyncs)
+ #expect(group.movableFiles.isEmpty)
+ #expect(group.reclaimableSize == 0)
+ #expect(scan.suggestedSelection.isEmpty)
}
/// A chosen folder that is not there, as a folder on a disk that isn't connected, is named rather than passed over.
@@ -478,9 +513,9 @@ struct DuplicateFinderTests {
#expect(finder.canScan(home))
#expect(finder.canScan(home.appending(path: "Documents")))
- // A file removed from iCloud Drive is removed from every device, so it is never offered.
- #expect(!finder.canScan(home.appending(path: "Library/Mobile Documents/com~apple~CloudDocs/Photos")))
- #expect(!finder.canScan(home.appending(path: "Library/CloudStorage/Dropbox")))
+ // A file removed from iCloud Drive is removed from every device: scanned, its copies are held back.
+ #expect(finder.canScan(home.appending(path: "Library/Mobile Documents/com~apple~CloudDocs/Photos")))
+ #expect(finder.canScan(home.appending(path: "Library/CloudStorage/Dropbox")))
#expect(!finder.canScan(home.appending(path: "Library")))
#expect(!finder.canScan(home.appending(path: "Library/Application Support")))
#expect(!finder.canScan(home.appending(path: "Music/Music")))
@@ -501,8 +536,10 @@ struct DuplicateFinderTests {
let finder = DuplicateFinder(homeDirectory: home)
#expect(finder.refusal(of: home.appending(path: "Documents")) == nil)
- #expect(finder.refusal(of: home.appending(path: "Synced")) == .inTheCloud)
- #expect(finder.refusal(of: home.appending(path: "Library/Mobile Documents/com~apple~CloudDocs")) == .inTheCloud)
+ // A folder that syncs is scanned; what is found in it is held back, never moved.
+ #expect(finder.refusal(of: home.appending(path: "Synced")) == nil)
+ #expect(finder.refusal(of: home.appending(path: "Library/Mobile Documents/com~apple~CloudDocs")) == nil)
+ #expect(finder.refusal(of: home.appending(path: "Library/CloudStorage/Dropbox")) == nil)
#expect(finder.refusal(of: home.appending(path: "Library")) == .appData)
#expect(finder.refusal(of: home.appending(path: "Music/Music")) == .appData)
#expect(finder.refusal(of: home.appending(path: "Pictures/Thing.app")) == .inAPackage)
@@ -522,7 +559,8 @@ struct DuplicateFinderTests {
}
- @Test func refusesAFolderASyncAppKeepsInStep() throws {
+ /// A folder a sync app keeps in step is scanned like any other; what is found in it is held back, never moved.
+ @Test func scansAFolderASyncAppKeepsInStep() throws {
let directory = try TemporaryDirectory()
let home = try directory.directory("home")
try directory.directory("home/Sync/.stfolder")
@@ -530,8 +568,8 @@ struct DuplicateFinderTests {
try directory.directory("home/Documents")
let finder = DuplicateFinder(homeDirectory: home)
- #expect(!finder.canScan(home.appending(path: "Sync", directoryHint: .isDirectory)))
- #expect(!finder.canScan(home.appending(path: "Sync/Photos", directoryHint: .isDirectory)))
+ #expect(finder.canScan(home.appending(path: "Sync", directoryHint: .isDirectory)))
+ #expect(finder.canScan(home.appending(path: "Sync/Photos", directoryHint: .isDirectory)))
#expect(finder.canScan(home.appending(path: "Documents", directoryHint: .isDirectory)))
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
index 77890e3e..1fc797a6 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
@@ -464,7 +464,7 @@ struct FileCommandTests {
let scan = try await DuplicateFinder(homeDirectory: home).scan(.everySize(in: [home]))
let group = try #require(scan.groups.first)
- let rows = DuplicatesCommand.rows(for: group.files.map(\.url))
+ let rows = DuplicatesCommand.rows(for: group.files)
#expect(rows.count == 3)
#expect(rows.map(\.first) == ["keep", "", ""])
@@ -472,6 +472,38 @@ struct FileCommandTests {
#expect(DuplicatesCommand.heading(for: group).hasPrefix("3 copies of 4 kB, "))
}
+ /// A copy in a folder that syncs to other devices is listed as one that stays, and never moved; the JSON names
+ /// it apart from the copies that may move.
+ @Test func listsACopyThatSyncsAsOneThatStays() async throws {
+ let directory = try TemporaryDirectory()
+ let logs = logs(in: directory)
+ let contents = Data((0..<4_000).map { _ in UInt8.random(in: 0...255) })
+ try directory.directory("home/Synced/.stfolder")
+ try directory.file("home/Synced/a.bin", contents: contents)
+ try directory.file("home/Documents/a.bin", contents: contents)
+ try directory.file("home/Documents/a copy.bin", contents: contents)
+ let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
+ let scan = try await DuplicateFinder(homeDirectory: home).scan(.everySize(in: [home]))
+ let group = try #require(scan.groups.first)
+ let synced = try #require(group.files.first { $0.heldBack == .inTheCloud })
+
+ let rows = DuplicatesCommand.rows(for: group.files)
+ #expect(rows.first { $0[1] == Output.path(synced.url) }?[0] == "stays")
+ #expect(rows.map { $0[0] }.sorted() == ["", "keep", "stays"])
+
+ let json = try Output.jsonText(DuplicatesCommand.found(in: scan))
+ let found = try #require(JSONSerialization.jsonObject(with: Data(json.utf8)) as? [String: Any])
+ let record = try #require((found["files"] as? [[String: Any]])?.first)
+ #expect(record["synced"] as? [String] == [Output.path(synced.url)])
+ #expect((record["files"] as? [String])?.count == 2)
+
+ try await (command(["duplicates", "--remove", "-y"]) as DuplicatesCommand)
+ .clean(scan, using: try service(in: directory), recordingIn: logs.removals, refusals: logs.refusals)
+
+ #expect(await moved(logs) == ["a copy.bin"])
+ #expect(FileManager.default.fileExists(atPath: synced.url.path(percentEncoded: false)))
+ }
+
@Test func saysHowManyCopiesOfAFolderAndHowBigItIs() async throws {
let directory = try TemporaryDirectory()
let contents = Data((0..<4_000).map { _ in UInt8.random(in: 0...255) })
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
index ae0af667..a3df0bbf 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
@@ -115,7 +115,11 @@ struct FolderDuplicateTests {
let result = try await scan(directory)
#expect(names(result).isEmpty)
- #expect(!result.groups.flatMap(\.files).contains { $0.url.path(percentEncoded: false).contains("/Pictures/") })
+ // The files inside it are listed beside the copies outside it, each held back, and nothing is suggested.
+ let synced = result.groups.flatMap(\.files).filter { $0.url.path(percentEncoded: false).contains("/Pictures/") }
+ #expect(synced.map(\.url.lastPathComponent).sorted() == ["a.jpg", "b.jpg"])
+ #expect(synced.allSatisfy { $0.heldBack == .inTheCloud })
+ #expect(result.suggestedSelection.isEmpty)
}
/// A folder kept only in the cloud, which no test can make, is named to the scan.
diff --git a/Peel/Model/DuplicateLibrary.swift b/Peel/Model/DuplicateLibrary.swift
index 71e33f7b..2cc5cfbb 100644
--- a/Peel/Model/DuplicateLibrary.swift
+++ b/Peel/Model/DuplicateLibrary.swift
@@ -201,7 +201,7 @@ final class DuplicateLibrary {
}
func canChange(_ file: DuplicateFile, in group: DuplicateGroup) -> Bool {
- DuplicateScan.canChange(file.url, among: group.files.map(\.url), selected: selectedURLs)
+ DuplicateScan.canChange(file, in: group, selected: selectedURLs)
}
/// Changes nothing that would leave the group with no copy.
diff --git a/Peel/Views/Duplicates/DuplicateGroupView.swift b/Peel/Views/Duplicates/DuplicateGroupView.swift
index 88f85670..2e9ebe6c 100644
--- a/Peel/Views/Duplicates/DuplicateGroupView.swift
+++ b/Peel/Views/Duplicates/DuplicateGroupView.swift
@@ -125,7 +125,11 @@ private struct DuplicateFileRow: View {
.foregroundStyle(.secondary)
}
Spacer(minLength: 12)
- if !isSelected {
+ if let heldBack = file.heldBack {
+ // A copy that syncs to other devices is shown and never moved; its words say why.
+ Badge(title: Text("Stays"), systemImage: "icloud", tint: .blue)
+ .accessibilityHint(Text(heldBack.explanation))
+ } else if !isSelected {
Badge(title: Text("Keep"), systemImage: "checkmark", tint: .green)
}
Text(file.size.byteCount)
@@ -135,7 +139,7 @@ private struct DuplicateFileRow: View {
}
.toggleStyle(.checkbox)
.disabled(!canChangeSelection)
- .help(canChangeSelection ? Text(verbatim: file.url.path(percentEncoded: false)) : Text("Peel always keeps at least one copy."))
+ .help(help)
Button {
preview.url = file.url
@@ -153,6 +157,12 @@ private struct DuplicateFileRow: View {
ItemMenu(url: file.url)
}
}
+
+ /// The path, or why the checkbox can't be changed: the copy syncs to other devices, or it is the last one.
+ private var help: Text {
+ if let heldBack = file.heldBack { return Text(heldBack.explanation) }
+ return canChangeSelection ? Text(verbatim: file.url.path(percentEncoded: false)) : Text("Peel always keeps at least one copy.")
+ }
}
/// The copy Quick Look shows, held by the page.
diff --git a/Peel/Views/Duplicates/DuplicateList.swift b/Peel/Views/Duplicates/DuplicateList.swift
index 96f49f0b..025a98a9 100644
--- a/Peel/Views/Duplicates/DuplicateList.swift
+++ b/Peel/Views/Duplicates/DuplicateList.swift
@@ -17,23 +17,36 @@ struct DuplicateList: View {
if let scan = duplicates.scan {
let folderGroups = scan.folderGroups.filter { $0.matches(searchText) }
let groups = scan.groups.filter { $0.matches(searchText) }
- // Headings appear only when folder groups are listed. A list of files alone has none.
+ let local = groups.filter { !$0.everyCopySyncs }
+ // The groups whose every copy syncs to other devices, which Peel shows and never moves, in a
+ // section of their own, so they never mix with what is on this Mac.
+ let synced = groups.filter(\.everyCopySyncs)
+ // Headings appear only when folder groups or synced groups are listed. A list of files alone has none.
List(selection: $duplicates.selection) {
- if !folderGroups.isEmpty {
- Section {
- folderRows(folderGroups)
- } header: {
- Text("Folders")
+ if !folderGroups.isEmpty || !synced.isEmpty {
+ if !folderGroups.isEmpty {
+ Section {
+ folderRows(folderGroups)
+ } header: {
+ Text("Folders")
+ }
}
- if !groups.isEmpty {
+ if !local.isEmpty {
Section {
- fileRows(groups)
+ fileRows(local)
} header: {
Text("Files")
}
}
+ if !synced.isEmpty {
+ Section {
+ fileRows(synced)
+ } header: {
+ Text("Synced to Other Devices")
+ }
+ }
} else {
- fileRows(groups)
+ fileRows(local)
}
}
.accessibilityLabel(Text(Tool.duplicates.title))
@@ -235,8 +248,19 @@ private struct DuplicateGroupRow: View {
.foregroundStyle(.secondary)
}
Spacer(minLength: 6)
- Text(group.reclaimableSize.byteCount)
+ if group.everyCopySyncs {
+ // Nothing here frees space on this Mac: the figure is the copies' size, with the symbol that says
+ // they sync.
+ Label {
+ Text(group.size.byteCount)
+ } icon: {
+ Image(systemName: "icloud")
+ }
.rowFigure()
+ } else {
+ Text(group.reclaimableSize.byteCount)
+ .rowFigure()
+ }
}
.padding(.vertical, 2)
}
diff --git a/Peel/Views/Presentation.swift b/Peel/Views/Presentation.swift
index 3d90e775..b4a8d23a 100644
--- a/Peel/Views/Presentation.swift
+++ b/Peel/Views/Presentation.swift
@@ -226,7 +226,6 @@ extension DuplicateFinder.Refusal {
/// Why a chosen folder isn't scanned for duplicates, shown where the folder was chosen and with the result.
var explanation: String {
switch self {
- case .inTheCloud: String(localized: "It is in a folder that syncs to your other devices through iCloud or another app, which Peel leaves to that app.")
case .appData: String(localized: "It is app data, the Music or TV app’s media folder, or something Peel protects, such as a photo library or a keychain.")
case .inAPackage: String(localized: "It is an app or another package, or inside one, and what is inside belongs to it.")
case .outsideTheHome: String(localized: "It is outside your home folder and /Users/Shared, or on a disk Peel can’t write to.")
diff --git a/Support/peel.1 b/Support/peel.1
index feee7d3b..7dd87da4 100644
--- a/Support/peel.1
+++ b/Support/peel.1
@@ -1,5 +1,5 @@
.\" "Generated by swift-argument-parser"
-.Dd October 13, 2026
+.Dd October 25, 2026
.Dt PEEL 1
.Os
.Sh NAME
@@ -237,7 +237,7 @@ Show help information.
.It Em duplicates
Find folders and files with identical contents.
.Pp
-Without folders, Peel scans Desktop, Documents, Downloads, Pictures, Movies, and Music. The first copy of each group is the one Peel suggests keeping. A folder is a copy of another only when every file under it matches, so what a folder already speaks for isn't listed again on its own.
+Without folders, Peel scans Desktop, Documents, Downloads, Pictures, Movies, and Music. The first copy of each group is the one Peel suggests keeping. A copy in a folder that syncs to your other devices, such as iCloud Drive, is listed as one that stays and is never moved. A folder is a copy of another only when every file under it matches, so what a folder already speaks for isn't listed again on its own.
.Bl -tag -width 6n
.It Ar folders...
Folders to scan.