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.

Count an item inside another moving item once in every total

Țugui Dragoșfd75d5bf379a
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/UninstallPlan.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/UninstallPlan.swift
index 169e040c..78742314 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/UninstallPlan.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/UninstallPlan.swift
@@ -25,7 +25,9 @@ struct UninstallPlan {
var moving: [Item] { items.filter { $0.refusal == nil } }
var staying: [Item] { items.filter { $0.refusal != nil } }
- var total: SizeTotal { SizeTotal(moving.map(\.size)) }
+ var total: SizeTotal {
+ SizeTotal(movingItemsAt: Dictionary(moving.map { ($0.url, $0.size) }) { first, _ in first })
+ }
/// Why the app itself stays, or nil when it moves. When the app stays, nothing else may move: the files of an
/// app that is still installed aren't leftovers.
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/BulkUninstallation.swift b/Packages/PeelCore/Sources/PeelCore/Removal/BulkUninstallation.swift
index 4d7a288a..ab60d93e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/BulkUninstallation.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/BulkUninstallation.swift
@@ -69,12 +69,12 @@ public struct BulkUninstallation: Sendable {
let items = Self.merge(uninstallations)
self.uninstallations = uninstallations
self.items = items
- total = SizeTotal(items
+ total = SizeTotal(movingItemsAt: Dictionary(items
.filter {
!$0.isExcluded && !$0.isPeels && !($0.isApplication && $0.isKeptByMacOS) && !$0.isBeyondTheHelper
&& !$0.isInTheTrash && $0.enclosingPackage == nil && $0.match?.heldBack?.cannotBeMoved != true
}
- .map { $0.isMeasured ? $0.size : nil })
+ .map { ($0.url, $0.isMeasured ? $0.size : nil) }) { first, _ in first })
privilegedURLs = Set(
items.filter {
$0.requiresPrivileges && !$0.isExcluded && !$0.isPeels && !$0.isBeyondTheHelper && !$0.isInTheTrash
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalRequest.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalRequest.swift
index fe580718..c97af918 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalRequest.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalRequest.swift
@@ -13,6 +13,6 @@ public struct RemovalRequest: Sendable, Equatable {
}
public var total: SizeTotal {
- SizeTotal(urls.map { sizes[$0] })
+ SizeTotal(movingItemsAt: Dictionary(uniqueKeysWithValues: urls.map { ($0, sizes[$0]) }))
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift b/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
index 25752d66..28ef10ff 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
@@ -312,14 +312,15 @@ public struct Uninstallation: Sendable {
/// page's total adds up. Rows Peel leaves alone are not counted.
public func movable(among leftovers: [Leftover], withApp: Bool) -> (count: Int, size: SizeTotal) {
guard !isPeel else { return (0, SizeTotal([])) }
- var sizes: [Int64?] = leftovers
- .filter { $0.match.heldBack?.cannotBeMoved != true }
- .map { $0.isMeasured ? $0.size : nil }
+ let movable = leftovers.filter { $0.match.heldBack?.cannotBeMoved != true }
+ var sizes = Dictionary(movable.map { ($0.url, $0.isMeasured ? $0.size : nil) }) { first, _ in first }
+ var count = movable.count
if withApp, !app.isSystemProtected, app.enclosingPackage == nil, !isExcluded, !isAppBeyondTheHelper,
!isAppInTheTrash {
- sizes.append(isAppMeasured ? appSize : nil)
+ sizes.updateValue(isAppMeasured ? appSize : nil, forKey: app.url)
+ count += 1
}
- return (sizes.count, SizeTotal(sizes))
+ return (count, SizeTotal(movingItemsAt: sizes))
}
public var privilegedURLs: Set<URL> {
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/SizeTotal.swift b/Packages/PeelCore/Sources/PeelCore/Support/SizeTotal.swift
index 7f802335..22169bee 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/SizeTotal.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/SizeTotal.swift
@@ -1,3 +1,6 @@
+public import Foundation
+internal import PeelPrivileged
+
/// A sum of sizes, some of which may be unknown. An unknown size is never counted as zero: it marks the
/// total incomplete.
public struct SizeTotal: Sendable, Hashable, Comparable, Codable {
@@ -21,6 +24,16 @@ public struct SizeTotal: Sendable, Hashable, Comparable, Codable {
self.init(known: known, isComplete: isComplete)
}
+ /// What moving the items in `sizes` frees. An item inside another of them goes with that one, so it frees
+ /// nothing more, and its own size, known or not, is left out.
+ public init(movingItemsAt sizes: [URL: Int64?]) {
+ let items = sizes.map { (names: PathComponents.of(PathPattern.comparablePath(of: $0.key)), size: $0.value) }
+ let folders = Set(items.map(\.names))
+ self.init(items.filter { item in
+ !(1..<max(item.names.count, 1)).contains { folders.contains(Array(item.names.prefix($0))) }
+ }.map(\.size))
+ }
+
/// Adds totals together. The sum is complete only when every part is.
public init(combining totals: some Sequence<SizeTotal>) {
self = totals.reduce(SizeTotal(known: 0, isComplete: true)) { $0.adding($1) }
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BulkUninstallationTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BulkUninstallationTests.swift
index cae74290..d1636d8f 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BulkUninstallationTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BulkUninstallationTests.swift
@@ -345,4 +345,16 @@ struct BulkUninstallationTests {
#expect(order == ["One.app", "Two.app", "com.example.two", "com.example.one"])
#expect(bulk.total == SizeTotal(known: 2030, isComplete: true))
}
+
+ /// Two apps chosen together can each list a folder inside the other's: its bytes are counted once.
+ @Test func theTotalCountsAFolderInsideAnotherOnce() {
+ let first = app("com.example.first", "First")
+ let second = app("com.example.second", "Second")
+ let bulk = BulkUninstallation(uninstallations: [
+ uninstallation(first, [leftover("/Users/x/Library/Application Support/Vendor", size: 100)]),
+ uninstallation(second, [leftover("/Users/x/Library/Application Support/Vendor/Second", size: 40)]),
+ ])
+
+ #expect(bulk.total == SizeTotal(known: 1_000 + 1_000 + 100, isComplete: true))
+ }
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift
index 813249e7..702574c4 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift
@@ -427,6 +427,20 @@ struct CommandLineTests {
#expect(json["appSize"] is NSNull)
}
+ /// What `peel uninstall` says it frees counts an item inside another moving item once: it goes with that one.
+ @Test func theUninstallTotalCountsAnItemInsideAnotherOnce() {
+ let app = URL(filePath: "/Applications/Steam.app", directoryHint: .isDirectory)
+ let support = URL(filePath: "/Users/me/Library/Application Support/Steam", directoryHint: .isDirectory)
+ let bundle = support.appending(path: "Steam.AppBundle", directoryHint: .isDirectory)
+ let plan = UninstallPlan(app: app, items: [
+ UninstallPlan.Item(url: app, size: 7, refusal: nil),
+ UninstallPlan.Item(url: support, size: 800, refusal: nil),
+ UninstallPlan.Item(url: bundle, size: 700, refusal: nil),
+ ], needsAdministrator: 0, needsReview: 0)
+
+ #expect(plan.total == SizeTotal(known: 807, isComplete: true))
+ }
+
/// `peel uninstall --json` says what stays and why, what moved and what failed, with every key in every record.
@Test func theUninstallReportSaysWhatMovedWhatStayedAndWhy() throws {
let app = URL(filePath: "/Applications/Editor.app", directoryHint: .isDirectory)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalQuestionTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalQuestionTests.swift
index c6c39192..9a556ac4 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RemovalQuestionTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalQuestionTests.swift
@@ -79,5 +79,11 @@ struct RemovalQuestionTests {
RemovalRequest(urls: [measured, unknown], sizes: [measured: 100]).total
== SizeTotal(known: 100, isComplete: false)
)
+ let inside = measured.appending(path: "inside")
+ #expect(
+ RemovalRequest(urls: [measured, inside], sizes: [measured: 100, inside: 60]).total
+ == SizeTotal(known: 100, isComplete: true),
+ "an item inside another moves with it and frees nothing more"
+ )
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SizeTotalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SizeTotalTests.swift
index 322a83c2..2714109a 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SizeTotalTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SizeTotalTests.swift
@@ -1,3 +1,4 @@
+import Foundation
@testable import PeelCore
import Testing
@@ -16,6 +17,20 @@ struct SizeTotalTests {
#expect(SizeTotal([Int64?]()) == SizeTotal(known: 0, isComplete: true))
}
+ /// Moving a folder takes what is inside it, so an item inside another of the same move frees nothing more, even
+ /// when its own size was not measured. A folder whose name only begins another's holds nothing of it.
+ @Test func countsAnItemInsideAnotherMovedItemOnce() {
+ let steam = URL(filePath: "/Users/me/Library/Application Support/Steam")
+ let bundle = steam.appending(path: "Steam.AppBundle")
+ let caches = URL(filePath: "/Users/me/Library/Caches/Steam")
+ #expect(
+ SizeTotal(movingItemsAt: [steam: 800, bundle: 700, caches: 5]) == SizeTotal(known: 805, isComplete: true)
+ )
+ #expect(SizeTotal(movingItemsAt: [steam: 800, bundle: nil]) == SizeTotal(known: 800, isComplete: true))
+ let beside = URL(filePath: "/Users/me/Library/Application Support/Steam 2")
+ #expect(SizeTotal(movingItemsAt: [steam: 800, beside: 2]) == SizeTotal(known: 802, isComplete: true))
+ }
+
/// Sizes can come from a sequence that is read once, as they are measured. An unknown one must still make
/// the total incomplete: reading the sequence a second time would find it empty.
@Test func readsTheSizesOnce() {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
index 90f5109a..c893fbb7 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UninstallationTests.swift
@@ -299,6 +299,15 @@ struct UninstallationTests {
#expect(total.size == SizeTotal(known: recommended.size.known + review.size.known, isComplete: false))
}
+ /// A Homebrew cask can name a folder inside one the scan found, such as Steam's bundle inside its Application
+ /// Support folder: both are listed, and the bytes they share are counted once.
+ @Test func theTotalCountsARowInsideAnotherRowOnce() {
+ let plan = uninstallation(leftovers: [leftover("Steam"), leftover("Steam/Steam.AppBundle")])
+
+ let total = plan.movable(among: plan.scan.leftovers, withApp: false)
+ #expect(total.size == SizeTotal(known: 1_000, isComplete: true))
+ }
+
/// An app bundle macOS will not let Peel read has no known size: its row reads "Unknown", never zero.
@Test(.permissionsHold) func anAppBundleThatCannotBeReadIsNotMeasured() async throws {
let directory = try TemporaryDirectory()