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.

Duplicates never scans a package it is given

Țugui Dragoș9115d1abd522
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
index eb0efe1f..f9b1c799 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
@@ -470,7 +470,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, app data, the Music and TV apps' media folders, 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. iCloud Drive, app data, the Music and TV apps' media folders, apps and other packages, and system folders aren't scanned.")
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift
index 2120d76e..22d10e7e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift
@@ -34,7 +34,7 @@ public struct DuplicateFinder: Sendable {
public func canScan(_ folder: URL) -> Bool {
let path = Self.path(of: folder.resolvingSymlinksInPath())
let home = Self.path(of: homeDirectory)
- guard !ProtectedData.refuses(path, home: home), !Self.isInTheCloud(folder) else { return false }
+ guard !ProtectedData.refuses(path, home: home), !Self.isInTheCloud(folder), !Self.isInsideAPackage(folder) else { return false }
if path.isInside(home) {
return !Self.managedFolders.contains { path.isInside(home + "/" + $0) }
}
@@ -51,6 +51,22 @@ public struct DuplicateFinder: Sendable {
(try? url.resourceValues(forKeys: [.isUbiquitousItemKey]).isUbiquitousItem) == true
}
+ /// Whether `url` is a package, such as an app.
+ static func isAPackage(_ url: URL) -> Bool {
+ (try? url.resourceValues(forKeys: [.isPackageKey]).isPackage) == true
+ }
+
+ /// Whether `url` is a package or sits inside one. What is inside a package belongs to it and is never a copy to
+ /// offer, and nothing here checks whether an app is running.
+ static func isInsideAPackage(_ url: URL) -> Bool {
+ var folder = url.standardizedFileURL
+ while folder.pathComponents.count > 1 {
+ if isAPackage(folder) { return true }
+ folder = folder.deletingLastPathComponent()
+ }
+ return false
+ }
+
/// Whether `url` is a photo, music, or video library. Its files belong to the library's app, so none of them
/// is offered as a copy, whatever its bytes.
static func isAUserLibrary(_ url: URL) -> Bool {
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift
index 15071239..2f18dfb5 100644
--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift
@@ -165,7 +165,7 @@ struct FolderDuplicates: Sendable {
entry,
identity: FileIdentity(info),
depth: depth + 1,
- isNeverOffered: isNeverOffered || Self.isAPackage(entry),
+ isNeverOffered: isNeverOffered || DuplicateFinder.isAPackage(entry),
onListing: onListing
)
guard !child.isProject else {
@@ -307,11 +307,6 @@ struct FolderDuplicates: Sendable {
+ folder.children.reduce(0) { $0 + reclaimable($1.folder) }
}
- /// Whether `url` is a package, such as an app.
- private static func isAPackage(_ url: URL) -> Bool {
- (try? url.resourceValues(forKeys: [.isPackageKey]).isPackage) == true
- }
-
private static func files(in folders: [Folder]) -> [(url: URL, identity: FileIdentity)] {
var seen = Set<FileIdentity>()
var files: [(url: URL, identity: FileIdentity)] = []
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
index f555e597..4237d03b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
@@ -307,6 +307,22 @@ struct FolderDuplicateTests {
#expect(names(try await scan(directory)) == [])
}
+ /// A package chosen to be scanned, or a folder inside one, is not looked into at all, and says so: nothing inside
+ /// it, folder or file, is offered.
+ @Test func neverScansAChosenPackage() async throws {
+ let directory = try TemporaryDirectory()
+ let binary = randomData(count: 4_000)
+ for app in ["home/Documents/Tool.app", "home/Pictures/Tool.app"] {
+ try directory.file("\(app)/Contents/MacOS/Tool", contents: binary)
+ }
+
+ let chosen = ["home/Documents/Tool.app", "home/Pictures/Tool.app/Contents"]
+ let result = try await scan(directory, folders: chosen)
+ #expect(names(result) == [])
+ #expect(result.groups.isEmpty)
+ #expect(result.skippedLocations.map(\.lastPathComponent) == ["Tool.app", "Contents"])
+ }
+
@Test func anEmptyFolderIsNoCopyOfAnything() async throws {
let directory = try TemporaryDirectory()
try directory.directory("home/Documents/Empty")