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 offers a hidden folder

Țugui Dragoș8b85805389d5
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift
index 2f18dfb5..adbc4610 100644
--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift
@@ -21,8 +21,9 @@ struct FolderDuplicates: Sendable {
private final class Folder {
let url: URL
let 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.
+ /// A package, such as an app, a hidden folder, or a folder inside one of them: compared as part of the
+ /// folder around it, and never offered on its own. Nothing here checks whether an app is running, and the
+ /// file scan leaves hidden items out on purpose, since a hidden folder is a tool's settings or data.
let isNeverOffered: Bool
var files: [(name: String, identity: FileIdentity)] = []
var children: [(name: String, folder: Folder)] = []
@@ -165,7 +166,7 @@ struct FolderDuplicates: Sendable {
entry,
identity: FileIdentity(info),
depth: depth + 1,
- isNeverOffered: isNeverOffered || DuplicateFinder.isAPackage(entry),
+ isNeverOffered: isNeverOffered || Self.isHidden(entry, info) || DuplicateFinder.isAPackage(entry),
onListing: onListing
)
guard !child.isProject else {
@@ -302,6 +303,11 @@ struct FolderDuplicates: Sendable {
)
}
+ /// Whether the entry is hidden the way Finder hides it: a leading dot, or the hidden flag.
+ private static func isHidden(_ url: URL, _ info: stat) -> Bool {
+ url.lastPathComponent.hasPrefix(".") || info.st_flags & UInt32(UF_HIDDEN) != 0
+ }
+
private static func reclaimable(_ folder: Folder) -> Int64 {
folder.files.reduce(0) { $0 + ReclaimableSpace.of(folder.url.appending(path: $1.name)) }
+ folder.children.reduce(0) { $0 + reclaimable($1.folder) }
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
index 4237d03b..465c2cc7 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
@@ -323,6 +323,21 @@ struct FolderDuplicateTests {
#expect(result.skippedLocations.map(\.lastPathComponent) == ["Tool.app", "Contents"])
}
+ /// A hidden folder, or one inside a hidden folder, is never offered, as the file scan never lists a hidden file:
+ /// a tool's settings in `~/.config` stay where they are, however like a backup of them they are. Finder's hidden
+ /// flag hides a folder as a leading dot does.
+ @Test func neverOffersAHiddenFolder() async throws {
+ let directory = try TemporaryDirectory()
+ let settings = randomData(count: 4_000)
+ let flagged = try directory.directory("home/Pictures/Kept")
+ for folder in ["home/.config/tool", "home/Documents/tool", "home/Pictures/Kept/tool", "home/Movies/tool"] {
+ try directory.file("\(folder)/settings.json", contents: settings)
+ }
+ #expect(chflags(flagged.path(percentEncoded: false), UInt32(UF_HIDDEN)) == 0)
+
+ #expect(names(try await scan(directory)) == [["Documents/tool", "Movies/tool"]])
+ }
+
@Test func anEmptyFolderIsNoCopyOfAnything() async throws {
let directory = try TemporaryDirectory()
try directory.directory("home/Documents/Empty")