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.

Say why a duplicate scan sets a chosen folder aside

Țugui Dragoș1d8a6231ac7b
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
index 00d4b752..6d1b5fc1 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
@@ -689,7 +689,7 @@ struct DuplicatesCommand: AsyncParsableCommand {
)
}
if !scan.skippedLocations.isEmpty {
- let folders = scan.skippedLocations.map(Output.path).joined(separator: ", ")
+ 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.")
}
return notes
@@ -709,7 +709,7 @@ struct DuplicatesCommand: AsyncParsableCommand {
Record(size: $0.size, reclaimableSize: $0.reclaimableSize, files: $0.files.map { Output.path($0.url) })
},
unreadableLocations: scan.unreadableLocations.map(Output.path),
- skippedLocations: scan.skippedLocations.map(Output.path)
+ skippedLocations: scan.skippedLocations.map { Output.path($0.url) }
)
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift
index a476b050..e47193df 100644
--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift
@@ -46,22 +46,70 @@ 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
+ /// `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.
+ case inAPackage
+ /// Outside the home folder and `/Users/Shared`, or on a volume Peel can't write to.
+ case outsideTheHome
+ /// A repository, or a folder inside one, which the scan leaves alone.
+ case insideARepository
+ /// Not a folder Peel can reach, as one on a disk that isn't connected.
+ case notAFolder
+ }
+
/// 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.
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.
+ public func refusal(of folder: URL) -> Refusal? {
let path = Self.path(of: folder.resolvingSymlinksInPath())
let home = Self.path(of: homeDirectory)
- guard !ProtectedData.refuses(path, home: home), !SyncedFolders().syncsToOtherDevices(folder),
- !folder.isOrIsInsideAPackage
- else { return false }
+ if SyncedFolders().syncsToOtherDevices(folder) || Self.isInACloudFolder(path) { return .inTheCloud }
+ if ProtectedData.refuses(path, home: home) { return .appData }
+ if folder.isOrIsInsideAPackage { return .inAPackage }
if path.isInside(home) {
- return !Self.managedFolders.contains { path.isInside(home + "/" + $0) }
+ return Self.managedFolders.contains { path.isInside(home + "/" + $0) } ? .appData : nil
}
guard path.isInside("/Users/Shared") || PathComponents.isPath(path, inside: "/Volumes") else {
- return false
+ return .outsideTheHome
+ }
+ let isReadOnly = (try? folder.resourceValues(forKeys: [.volumeIsReadOnlyKey]).volumeIsReadOnly) == true
+ return isReadOnly ? .outsideTheHome : nil
+ }
+
+ /// Why a scan would set `folder` aside, or nil: what `refusal(of:)` refuses, a folder that is not there, or a
+ /// repository. It is what the scan asks of each chosen folder, and what the list of folders says beside one.
+ public func reasonToSetAside(_ folder: URL) -> Refusal? {
+ refusal(of: folder)
+ ?? (!Self.isAFolder(folder) ? .notAFolder : nil)
+ ?? (Self.isInsideARepository(folder, home: homeDirectory) ? .insideARepository : nil)
+ }
+
+ /// Whether `path` is in iCloud Drive, or in the folder where file providers keep their files.
+ private static func isInACloudFolder(_ path: String) -> Bool {
+ let names = PathComponents.of(path.lowercased())
+ return zip(names, names.dropFirst()).contains { folder, inside in
+ folder == "library" && ["mobile documents", "cloudstorage"].contains(inside)
}
- return (try? folder.resourceValues(forKeys: [.volumeIsReadOnlyKey]).volumeIsReadOnly) != true
+ }
+
+ /// Whether `url` is a folder Peel can reach, links followed.
+ private static func isAFolder(_ url: URL) -> Bool {
+ var isDirectory: ObjCBool = false
+ return FileManager.default.fileExists(atPath: url.path(percentEncoded: false), isDirectory: &isDirectory)
+ && isDirectory.boolValue
}
/// Whether `url` is a photo, music, or video library. Its files belong to the library's app, so none of them
@@ -79,8 +127,10 @@ public struct DuplicateFinder: Sendable {
) async throws(CancellationError) -> DuplicateScan {
let environment = SearchEnvironment(homeDirectory: homeDirectory, rootDirectory: URL(filePath: "/"))
let removalGuard = RemovalGuard(environment: environment, exclusions: exclusions)
- let skipped = options.folders.filter { !canScan($0) || Self.isInsideARepository($0, home: homeDirectory) }
- let skippedPaths = Set(skipped.map(Self.path(of:)))
+ let skipped = options.folders.compactMap { folder in
+ reasonToSetAside(folder).map { DuplicateScan.FolderSetAside(url: folder, reason: $0) }
+ }
+ let skippedPaths = Set(skipped.map { Self.path(of: $0.url) })
let scannable = options.folders.filter { !skippedPaths.contains(Self.path(of: $0)) }
let neverProjects = Self.neverProjects(chosen: scannable, home: homeDirectory)
// Saved even when the scan is stopped: the digests computed by then are still correct.
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift
index 2529013c..c50e5b1e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift
@@ -107,15 +107,27 @@ public struct DuplicateScan: Sendable {
/// A file inside one of these is never listed in `groups` as well.
public var folderGroups: [DuplicateFolderGroup] = []
public let unreadableLocations: [URL]
- /// Chosen folders that were not scanned, such as iCloud Drive, app data, system folders, and repositories.
- public var skippedLocations: [URL] = []
+ /// Chosen folders that were not scanned, each with why: in the cloud, app data, a package, outside the home
+ /// folder, a repository, or not there.
+ public var skippedLocations: [FolderSetAside] = []
/// True when macOS privacy protection refused one of the unreadable folders, which Full Disk Access would
/// allow. It stays false for a folder locked by ordinary permissions, or one that is missing.
public var needsFullDiskAccess = false
+ /// A chosen folder the scan did not look in, and why.
+ public struct FolderSetAside: Sendable, Hashable {
+ public let url: URL
+ public let reason: DuplicateFinder.Refusal
+
+ public init(url: URL, reason: DuplicateFinder.Refusal) {
+ self.url = url
+ self.reason = reason
+ }
+ }
+
/// The folders Peel could not read or skipped, where duplicates may still be, whatever else it found.
public var notLookedIn: [URL] {
- unreadableLocations + skippedLocations
+ unreadableLocations + skippedLocations.map(\.url)
}
/// True when no duplicates were found but some of what was asked for could not be read or was skipped, even
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
index fb2ea38a..cad7afe8 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
@@ -251,13 +251,43 @@ struct DuplicateFinderTests {
let root = try await scan(directory, folders: ["home/Developer/Thing"])
#expect(root.groups.isEmpty)
- #expect(root.skippedLocations.count == 1)
+ #expect(root.skippedLocations.map(\.reason) == [.insideARepository])
#expect(root.readNothing)
let inside = try await scan(directory, folders: ["home/Developer/Thing/assets"])
#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 {
+ let directory = try TemporaryDirectory()
+ let contents = randomData(count: 3_000)
+ try directory.directory("home/.stfolder")
+ try directory.file("home/Desktop/a.bin", contents: contents)
+ try directory.file("home/Documents/a.bin", contents: contents)
+
+ let scan = try await scan(directory, folders: ["home/Desktop", "home/Documents"])
+
+ #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)
+ }
+
+ /// A chosen folder that is not there, as a folder on a disk that isn't connected, is named rather than passed over.
+ @Test func namesAChosenFolderThatIsNotThere() async throws {
+ let directory = try TemporaryDirectory()
+ try directory.directory("home")
+
+ let scan = try await scan(directory, folders: ["home/Gone"])
+
+ #expect(scan.skippedLocations.map(\.reason) == [.notAFolder])
+ #expect(scan.readNothing)
+ }
+
/// Copies found elsewhere do not hide a chosen folder Peel did not look in: the page still names it.
@Test func namesTheFoldersItDidNotLookInBesideWhatItFound() async throws {
let directory = try TemporaryDirectory()
@@ -460,6 +490,26 @@ struct DuplicateFinderTests {
#expect(!finder.canScan(URL(filePath: "/Volumes/")))
}
+ /// Each refusal says why, so a chosen folder Peel won't scan is named with its reason rather than counted as a
+ /// folder it couldn't read.
+ @Test func saysWhyAFolderIsNotScanned() throws {
+ let directory = try TemporaryDirectory()
+ let home = try directory.directory("home")
+ for path in ["Documents", "Library", "Music/Music", "Pictures/Thing.app", "Synced/.stfolder"] {
+ try directory.directory("home/" + path)
+ }
+ 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)
+ #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)
+ #expect(finder.refusal(of: URL(filePath: "/")) == .outsideTheHome)
+ #expect(finder.refusal(of: URL(filePath: "/Applications")) == .outsideTheHome)
+ }
+
@Test func refusesTheTrashOfEveryDisk() throws {
let directory = try TemporaryDirectory()
let home = try directory.directory("home")
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
index 24263cc9..77890e3e 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
@@ -592,7 +592,9 @@ struct FileCommandTests {
/// A chosen folder Peel didn't look in, such as a repository, is said, so an empty answer is not read as none.
@Test func saysWhichFoldersItDidNotLookIn() {
let skipped = URL(filePath: "/Users/me/Projects/app", directoryHint: .isDirectory)
- let scan = DuplicateScan(groups: [], unreadableLocations: [], skippedLocations: [skipped])
+ let scan = DuplicateScan(
+ groups: [], unreadableLocations: [], skippedLocations: [.init(url: skipped, reason: .insideARepository)]
+ )
let notes = DuplicatesCommand.notes(for: scan)
@@ -604,7 +606,9 @@ struct FileCommandTests {
let scan = DuplicateScan(
groups: [],
unreadableLocations: [URL(filePath: "/Users/me/Locked", directoryHint: .isDirectory)],
- skippedLocations: [URL(filePath: "/Users/me/Projects/app", directoryHint: .isDirectory)]
+ skippedLocations: [
+ .init(url: URL(filePath: "/Users/me/Projects/app", directoryHint: .isDirectory), reason: .insideARepository),
+ ]
)
let json = try Output.jsonText(DuplicatesCommand.found(in: scan))
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
index 89c77192..ae0af667 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
@@ -428,7 +428,8 @@ struct FolderDuplicateTests {
let result = try await scan(directory, folders: chosen)
#expect(names(result) == [])
#expect(result.groups.isEmpty)
- #expect(result.skippedLocations.map(\.lastPathComponent) == ["Tool.app", "Contents"])
+ #expect(result.skippedLocations.map(\.url.lastPathComponent) == ["Tool.app", "Contents"])
+ #expect(result.skippedLocations.map(\.reason) == [.inAPackage, .inAPackage])
}
/// A hidden folder, or one inside a hidden folder, is never offered, as the file scan never lists a hidden file: