Skip to content

Free, open-source macOS cleaner. CleanMyMac alternative with zero telemetry. Native SwiftUI, scheduled auto-cleaning, Xcode/Homebrew/system cache cleanup. MIT licensed.

harden PR #124: off-main orphan sizing + symlink-safe FileSizeCalculator

momenbasel1eeeb9040d70
diff --git a/PureMac/Logic/Utilities/FileSize.swift b/PureMac/Logic/Utilities/FileSize.swift
index f554046..1a258cf 100644
--- a/PureMac/Logic/Utilities/FileSize.swift
+++ b/PureMac/Logic/Utilities/FileSize.swift
@@ -14,8 +14,14 @@ enum FileSizeCalculator {
/// On-disk allocated size of `url`. Recurses into directories.
/// Returns `nil` if the item can't be read at all.
static func size(of url: URL) -> Int64? {
- let values = try? url.resourceValues(forKeys: [.isDirectoryKey])
- if values?.isDirectory == true {
+ let values = try? url.resourceValues(forKeys: [.isDirectoryKey, .isSymbolicLinkKey])
+ // Treat a symlink as a file (size of the link itself), never recursing
+ // into its target. `.isDirectoryKey` resolves symlinks, so without this
+ // guard a top-level symlink-to-directory would be walked as the target's
+ // full tree — inflating the size, escaping the item's real footprint,
+ // and mismatching deletion (removeItem deletes only the link). Check
+ // isSymbolicLink first so the directory branch only sees real dirs.
+ if values?.isSymbolicLink != true, values?.isDirectory == true {
return directorySize(of: url)
}
return fileSize(of: url)
@@ -31,7 +37,7 @@ enum FileSizeCalculator {
return Int64(size)
}
guard let attrs = try? fileManager.attributesOfItem(atPath: url.path),
- let size = attrs[.size] as? Int64 else { return nil }
+ let size = (attrs[.size] as? NSNumber)?.int64Value else { return nil }
return size
}
diff --git a/PureMac/Views/Orphans/OrphanListView.swift b/PureMac/Views/Orphans/OrphanListView.swift
index 2e800a0..3da409e 100644
--- a/PureMac/Views/Orphans/OrphanListView.swift
+++ b/PureMac/Views/Orphans/OrphanListView.swift
@@ -5,6 +5,12 @@ struct OrphanListView: View {
@State private var selectedOrphans: Set<URL> = []
@State private var isRemoving = false
@State private var removalErrorMessage: String?
+ /// Orphan sizes computed off the main thread. Orphans are exactly the large
+ /// leftovers (multi-GB Caches/Containers/Application Support), and their
+ /// size is a recursive directory walk — calling it from `body` would re-walk
+ /// every realized row on each selection toggle and beachball the UI. We walk
+ /// once off-main per result set and rows read the cached value.
+ @State private var sizeCache: [URL: Int64] = [:]
@Environment(\.accessibilityReduceMotion) private var reduceMotion
var body: some View {
@@ -27,7 +33,7 @@ struct OrphanListView: View {
OrphanRowView(
fileURL: fileURL,
isSelected: orphanBinding(for: fileURL),
- fileSize: fileSize(fileURL),
+ fileSize: sizeCache[fileURL],
onReveal: { revealInFinder(fileURL) },
onCopyPath: {
NSPasteboard.general.clearContents()
@@ -49,6 +55,21 @@ struct OrphanListView: View {
}
}
.navigationTitle(orphanedFilesTitle)
+ .task(id: appState.orphanedFiles) {
+ // Recompute off the main thread whenever the orphan set changes
+ // (new scan, removals). FileSizeCalculator is a plain static helper
+ // with no main-actor isolation, so it runs safely on a detached
+ // background task; the result is applied back on the main actor.
+ let urls = appState.orphanedFiles
+ let sizes = await Task.detached(priority: .utility) { () -> [URL: Int64] in
+ var out: [URL: Int64] = [:]
+ for url in urls {
+ out[url] = FileSizeCalculator.size(of: url) ?? 0
+ }
+ return out
+ }.value
+ sizeCache = sizes
+ }
.toolbar {
ToolbarItemGroup {
if !appState.orphanedFiles.isEmpty {
@@ -124,10 +145,6 @@ struct OrphanListView: View {
)
}
- private func fileSize(_ url: URL) -> Int64? {
- FileSizeCalculator.size(of: url)
- }
-
private func removeSelectedOrphans() async {
isRemoving = true
defer { isRemoving = false }