Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Duplicates never offers the inside of a package
Țugui Dragoș23b31200423b
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swiftindex 0cc09347..15071239 100644--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift@@ -21,6 +21,9 @@ struct FolderDuplicates: Sendable {private final class Folder {let url: URLlet identity: FileIdentity+ /// A package, such as an app, or a folder inside one: compared as part of the folder around it, and never+ /// offered on its own, since nothing here checks whether an app is running.+ let isNeverOffered: Boolvar files: [(name: String, identity: FileIdentity)] = []var children: [(name: String, folder: Folder)] = []var isWhole = true@@ -32,9 +35,10 @@ struct FolderDuplicates: Sendable {var totalHeld: Int64 = 0var shape: SHA256.Digest?- init(url: URL, identity: FileIdentity) {+ init(url: URL, identity: FileIdentity, isNeverOffered: Bool) {self.url = urlself.identity = identity+ self.isNeverOffered = isNeverOffered}}@@ -60,7 +64,7 @@ struct FolderDuplicates: Sendable {where visited.insert(DuplicateFinder.path(of: root)).inserted {var info = stat()guard lstat(root.path(percentEncoded: false), &info) == 0, info.st_mode & S_IFMT == S_IFDIR else { continue }- let folder = try list(root, identity: FileIdentity(info), depth: 0) {+ let folder = try list(root, identity: FileIdentity(info), depth: 0, isNeverOffered: false) {found += 1if listing.allows(isLast: false) { progress(.listing(foldersFound: found)) }}@@ -103,7 +107,7 @@ struct FolderDuplicates: Sendable {private func contents(of url: URL) -> String? {var info = stat()guard lstat(url.path(percentEncoded: false), &info) == 0, info.st_mode & S_IFMT == S_IFDIR else { return nil }- guard let folder = try? list(url, identity: FileIdentity(info), depth: 0, onListing: {}) else { return nil }+ guard let folder = try? list(url, identity: FileIdentity(info), depth: 0, isNeverOffered: false, onListing: {}) else { return nil }return identity(of: folder).map(Self.hexadecimal)}@@ -124,9 +128,10 @@ struct FolderDuplicates: Sendable {_ url: URL,identity: FileIdentity,depth: Int,+ isNeverOffered: Bool,onListing: () -> Void) throws(CancellationError) -> Folder {- let folder = Folder(url: url, identity: identity)+ let folder = Folder(url: url, identity: identity, isNeverOffered: isNeverOffered)guard !Task.isCancelled else { throw CancellationError() }onListing()guard depth < Self.depthLimit,@@ -156,7 +161,13 @@ struct FolderDuplicates: Sendable {folder.isWhole = falsecontinue}- let child = try list(entry, identity: FileIdentity(info), depth: depth + 1, onListing: onListing)+ let child = try list(+ entry,+ identity: FileIdentity(info),+ depth: depth + 1,+ isNeverOffered: isNeverOffered || Self.isAPackage(entry),+ onListing: onListing+ )guard !child.isProject else {folder.isWhole = falsecontinue@@ -211,7 +222,8 @@ struct FolderDuplicates: Sendable {var seen = Set<FileIdentity.Link>()func visit(_ folder: Folder) {- if folder.isWhole, folder.fileCount > 0, folder.totalHeld >= minimumSize, seen.insert(folder.identity.link).inserted {+ if folder.isWhole, !folder.isNeverOffered, folder.fileCount > 0, folder.totalHeld >= minimumSize,+ seen.insert(folder.identity.link).inserted {let key = ShapeKey(shape: shape(of: folder), fileCount: folder.fileCount, size: folder.size)byShape[key, default: []].append(folder)}@@ -260,7 +272,7 @@ struct FolderDuplicates: Sendable {removalGuard: RemovalGuard) -> [DuplicateFolderGroup] {let allowed = groups- .map { (digest: $0.digest, folders: $0.folders.filter { removalGuard.allowsRemoval(of: $0.url) && !Self.isAPackage($0.url) }) }+ .map { (digest: $0.digest, folders: $0.folders.filter { removalGuard.allowsRemoval(of: $0.url) }) }.filter { $0.folders.count > 1 }let offered = Set(allowed.flatMap { $0.folders.map { DuplicateFinder.path(of: $0.url) } })@@ -295,8 +307,7 @@ struct FolderDuplicates: Sendable {+ folder.children.reduce(0) { $0 + reclaimable($1.folder) }}- /// Whether `url` is a package, such as an app. A package is compared as part of the folder that holds it but- /// never offered on its own, since nothing here checks whether an app is running.+ /// Whether `url` is a package, such as an app.private static func isAPackage(_ url: URL) -> Bool {(try? url.resourceValues(forKeys: [.isPackageKey]).isPackage) == true}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swiftindex 056d4f63..f3167fb7 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift@@ -223,6 +223,7 @@ struct DuplicateFinderTests {#expect(result.groups.count == 1)#expect(result.groups.first?.files.count == 2)#expect(result.groups.first?.files.contains { $0.url.lastPathComponent == "copy.bin" && $0.url.path(percentEncoded: false).contains("/home/Pictures/") } == true)+ #expect(result.folderGroups.map { $0.folders.map(\.url.lastPathComponent) } == [])}@Test func filtersByKindAndMinimumSize() async throws {diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swiftindex b73f288a..f555e597 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift@@ -294,6 +294,19 @@ struct FolderDuplicateTests {#expect(try await scan(directory).folderGroups.isEmpty)}+ /// Nothing inside a package is offered either: an app gutted in place would stay behind, even the copy in use.+ @Test func neverOffersAnythingInsideAPackage() async throws {+ let directory = try TemporaryDirectory()+ let binary = randomData(count: 4_000)+ let resource = randomData(count: 3_000)+ for app in ["home/Documents/Tool.app", "home/Pictures/Tool.app"] {+ try directory.file("\(app)/Contents/MacOS/Tool", contents: binary)+ try directory.file("\(app)/Contents/Resources/Tool.icns", contents: resource)+ }++ #expect(names(try await scan(directory)) == [])+ }+@Test func anEmptyFolderIsNoCopyOfAnything() async throws {let directory = try TemporaryDirectory()try directory.directory("home/Documents/Empty")