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.

Keep the sizes a page measured for a removal cut short

Țugui Dragoșf15142da46a2
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
index 49066523..1e6d775c 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift
@@ -753,7 +753,9 @@ struct UninstallCommand: AsyncParsableCommand {
let (reset, result, recorded) = await Uninterrupted.run {
// The privacy reset comes before the move, because `tccutil` only finds an app that is still in place.
let reset = resetPrivacy ? await PrivacyReset.reset([target], beforeMovingWith: service).first?.result : nil
- let result = await plan.move(using: service)
+ let result = await RecordedRemoval.$current.withValue(plan.recordedAs(target.name)) {
+ await plan.move(using: service)
+ }
let recorded = await plan.record(result, from: target.name)
await LeftForAnAdministrator().record(uninstallation.leftForAnAdministrator(after: result))
return (reset, result, recorded)
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift
index 981d98d5..48671432 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift
@@ -107,8 +107,10 @@ struct Cleanup {
try await checkingAgain()
let (result, recorded) = await Uninterrupted.run {
- let result = await move(service, moving.map(\.url))
let sizes = [URL: Int64](measured: items.map { ($0.url, $0.size) })
+ let part = RemovalPart(source: source, sourceKey: sourceKey, tool: tool)
+ let recordedAs = RecordedRemoval(batch: UUID(), part: part, sizes: sizes)
+ let result = await RecordedRemoval.$current.withValue(recordedAs) { await move(service, moving.map(\.url)) }
let recorded = await Removals.record(
TrashResult(trashed: result.trashed, failures: result.failures + refused),
from: source,
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/UninstallPlan.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/UninstallPlan.swift
index fc350976..e841444b 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/UninstallPlan.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/UninstallPlan.swift
@@ -84,6 +84,19 @@ struct UninstallPlan {
)
}
+ /// The removal `move(using:)` goes into, with what was measured, so one cut short comes back into History named and
+ /// sized as `record(_:from:)` would have written it.
+ func recordedAs(_ source: String) -> RecordedRemoval {
+ RecordedRemoval(
+ batch: UUID(), part: RemovalPart(source: source, sourceKey: nil, tool: "applications"), sizes: sizes,
+ sharingStorage: appSharesStorage ? [app] : []
+ )
+ }
+
+ private var sizes: [URL: Int64] {
+ [URL: Int64](measured: items.map { ($0.url, $0.size) })
+ }
+
/// Writes `result` into History, with what the removal guard refused before the move beside what the move
/// reports, as `Cleanup` does, so `peel history --refused` and the app's Not Moved list it. False when History
/// could not be written.
@@ -98,7 +111,7 @@ struct UninstallPlan {
return await Removals.record(
result,
from: source,
- sizes: [URL: Int64](measured: items.map { ($0.url, $0.size) }),
+ sizes: sizes,
sharingStorage: appSharesStorage ? [app] : [],
tool: "applications",
in: log,
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalJournal.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalJournal.swift
index 0b131dad..08942b1c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalJournal.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalJournal.swift
@@ -7,14 +7,26 @@ internal import PeelPrivileged
public struct RecordedRemoval: Sendable {
public let batch: UUID
public let part: RemovalPart
+ /// What the page measured of each item, and the items that share most of their storage, as History records them.
+ public let sizes: [URL: Int64]
+ public let sharingStorage: Set<URL>
- public init(batch: UUID, part: RemovalPart) {
+ public init(batch: UUID, part: RemovalPart, sizes: [URL: Int64] = [:], sharingStorage: Set<URL> = []) {
self.batch = batch
self.part = part
+ self.sizes = sizes
+ self.sharingStorage = sharingStorage
}
/// The removal the moves in the current task go into, set by whoever records them.
@TaskLocal public static var current: RecordedRemoval?
+
+ /// The same removal, with the sizes of `more` known too.
+ public func measuring(_ more: [URL: Int64]) -> RecordedRemoval {
+ RecordedRemoval(
+ batch: batch, part: part, sizes: sizes.merging(more) { _, new in new }, sharingStorage: sharingStorage
+ )
+ }
}
/// What a removal moved, written down item by item as it moves, beside History. History takes each item over once
@@ -54,15 +66,17 @@ struct RemovalJournal: Sendable {
var part: RemovalPart? = nil
/// The disk the item moved on, read then: that disk may be gone when the line is taken in.
var disk: TrashedDisk? = nil
+ var size: Int64? = nil
+ var sharedStorage: Bool? = nil
- /// History's record of the item: the one kept here, or else one of its part, its size not known, or else
- /// that of an interrupted removal, which names no tool, since which tool moved the item is not known.
+ /// History's record of the item: the one kept here, or else one of its part, with the size its page measured,
+ /// or else that of an interrupted removal, which names no tool, since which tool moved the item is not known.
var recordToTakeIn: RemovalRecord {
if let record { return record }
let part = part ?? RemovalPart(source: "Interrupted removal", sourceKey: "interrupted", tool: "")
return RemovalRecord(
- batch: batch, item: item, size: nil, source: part.source, sourceKey: part.sourceKey, tool: part.tool,
- disk: disk
+ batch: batch, item: item, size: size, source: part.source, sourceKey: part.sourceKey, tool: part.tool,
+ sharedStorage: sharedStorage, disk: disk
)
}
}
@@ -132,10 +146,11 @@ struct RemovalJournal: Sendable {
by writer: Writer = .current
) {
let journal = url.path(percentEncoded: false)
- let entry = { item in
+ let entry = { (item: TrashedItem) in
Entry(
writer: writer, batch: recorded?.batch ?? batch, item: item, part: recorded?.part,
- disk: TrashedDisk(of: item)
+ disk: TrashedDisk(of: item), size: recorded?.sizes[item.originalURL],
+ sharedStorage: recorded?.sharingStorage.contains(item.originalURL) == true ? true : nil
)
}
let lines = items
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index 17467436..22a1beb2 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -658,8 +658,11 @@ public struct TrashService: Sendable {
where MakersFolders.isEmpty(folder) {
let path = PathPattern.comparablePath(of: folder)
let helper: Set<URL> = throughHelper.contains { PathComponents.isPath($0, inside: path) } ? [folder] : []
+ // What the files leave empty frees nothing, as History records it.
let moved = await MoveCount.$current.withValue(nil) {
- await trash([folder], usingHelperFor: helper, removal: removal, lettingTheirProgramsRun: []).trashed
+ await RecordedRemoval.$current.withValue(RecordedRemoval.current?.measuring([folder: 0])) {
+ await trash([folder], usingHelperFor: helper, removal: removal, lettingTheirProgramsRun: []).trashed
+ }
}
if !helper.isEmpty, !moved.isEmpty { throughHelper.append(path) }
result.trashed += moved
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift
index 063844f9..3d53d4ef 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift
@@ -95,6 +95,64 @@ struct RemovalJournalTests {
#expect(removals.first?.parts == [developer, space])
}
+ @Test func aRemovalCutShortKeepsTheSizesItsPageMeasured() async throws {
+ let directory = try TemporaryDirectory()
+ let history = directory.url.appending(path: "Peel/removals.json")
+ let journal = RemovalJournal(beside: history)
+ let measured = try directory.file("home/Library/Caches/com.example.one/cache.db").deletingLastPathComponent()
+ let shared = try directory.file("home/Library/Caches/com.example.two/cache.db").deletingLastPathComponent()
+ let service = service(in: directory, journal: journal) { url in
+ directory.url.appending(path: "home/.Trash/\(url.lastPathComponent)")
+ }
+ let part = RemovalPart(source: "App Caches", sourceKey: "space.appCaches", tool: "space")
+ let recorded = RecordedRemoval(batch: UUID(), part: part, sizes: [measured: 4_000], sharingStorage: [shared])
+ _ = await RecordedRemoval.$current.withValue(recorded) { await service.trash([measured, shared]) }
+
+ // The process stops before it records the part.
+ let left = journal.entries()
+ journal.forget(Set(left))
+ for entry in left {
+ journal.note([entry.item], batch: entry.batch, recordedAs: recorded, by: try endedProcess())
+ }
+
+ let records = try #require(await RemovalLog(url: history).load().records)
+ let byPlace = Dictionary(uniqueKeysWithValues: records.map { ($0.originalURL.lastPathComponent, $0) })
+ #expect(byPlace["com.example.one"]?.size == 4_000)
+ #expect(byPlace["com.example.two"]?.size == nil)
+ #expect(byPlace["com.example.two"]?.sharedStorage == true)
+ #expect(byPlace["com.example.one"]?.sharedStorage == nil)
+ }
+
+ @Test func aMakersFolderLeftEmptyIsJournaledAsFreeingNothing() async throws {
+ let directory = try TemporaryDirectory()
+ let journal = RemovalJournal(beside: directory.url.appending(path: "Peel/removals.json"))
+ let product = try directory.file("home/Library/Application Support/Example/Notes/notes.db")
+ .deletingLastPathComponent()
+ let trash = try directory.directory("home/.Trash")
+ let service = service(in: directory, journal: journal) { url in
+ let target = trash.appending(path: url.lastPathComponent)
+ try FileManager.default.moveItem(at: url, to: target)
+ return target
+ }
+ let app = InstalledApp(url: URL(filePath: "/Applications/Notes.app"), bundleIdentifier: "org.example.notes", name: "Notes")
+ let part = RemovalPart(source: "Notes", sourceKey: nil, tool: "applications")
+ let recorded = RecordedRemoval(batch: UUID(), part: part, sizes: [product: 2_000])
+
+ let result = await RecordedRemoval.$current.withValue(recorded) {
+ await service.trash(
+ apps: [], thenFiles: { _ in [product] }, usingHelperFor: [], lettingTheirProgramsRun: [],
+ emptiedFoldersNamed: MakersFolders.names(of: [app])
+ )
+ }
+
+ #expect(result.emptied.map(\.lastPathComponent) == ["Example"])
+ let sizes = Dictionary(
+ uniqueKeysWithValues: journal.entries().map { ($0.item.originalURL.lastPathComponent, $0.size) }
+ )
+ #expect(sizes["Notes"] == .some(2_000))
+ #expect(sizes["Example"] == .some(0))
+ }
+
/// While the process that moved an item still runs, it records the item itself, in its own words: a read of
/// History meanwhile, from the app or from `peel`, leaves the journal alone.
@Test func aRemovalStillUnderWayIsLeftToRecordItself() async throws {
diff --git a/Peel/Model/RemovalHistoryStore.swift b/Peel/Model/RemovalHistoryStore.swift
index f5db3da3..82778e94 100644
--- a/Peel/Model/RemovalHistoryStore.swift
+++ b/Peel/Model/RemovalHistoryStore.swift
@@ -369,14 +369,23 @@ final class RemovalHistoryStore {
/// Runs `move`, whose items the journal writes down under this name, so a removal a stop leaves unrecorded comes
/// back in History as what it was and not as an interrupted removal.
- func moving<Answer>(_ part: RemovalPart, _ move: () async -> Answer) async -> Answer {
- await RecordedRemoval.$current.withValue(RecordedRemoval(batch: UUID(), part: part)) { await move() }
+ /// Runs `move` as one removal of `part`, whose items the journal keeps with the `sizes` the page measured, so a
+ /// removal cut short comes back into History as `record` would have written it.
+ func moving<Answer>(
+ _ part: RemovalPart, sizes: [URL: Int64] = [:], sharingStorage: Set<URL> = [], _ move: () async -> Answer
+ ) async -> Answer {
+ let recorded = RecordedRemoval(batch: UUID(), part: part, sizes: sizes, sharingStorage: sharingStorage)
+ return await RecordedRemoval.$current.withValue(recorded) { await move() }
}
func moving<Answer>(
- tool: Tool, source: String, sourceKey: String? = nil, _ move: () async -> Answer
+ tool: Tool, source: String, sourceKey: String? = nil, sizes: [URL: Int64] = [:],
+ sharingStorage: Set<URL> = [], _ move: () async -> Answer
) async -> Answer {
- await moving(RemovalPart(source: source, sourceKey: sourceKey, tool: tool.rawValue), move)
+ await moving(
+ RemovalPart(source: source, sourceKey: sourceKey, tool: tool.rawValue), sizes: sizes,
+ sharingStorage: sharingStorage, move
+ )
}
/// Records in History what one part of `removal` moved, and what it refused.
diff --git a/Peel/Model/SelectionCarrier.swift b/Peel/Model/SelectionCarrier.swift
index 755da7dc..50cd3a60 100644
--- a/Peel/Model/SelectionCarrier.swift
+++ b/Peel/Model/SelectionCarrier.swift
@@ -114,7 +114,9 @@ final class SelectionCarrier {
await CarriedSelection.pass(parts) { part in
// What a part moves is written down with the removal and the part, so a stop before the part is
// recorded leaves one removal in History, not two.
- let recorded = RecordedRemoval(batch: removal.batch, part: part.removalPart)
+ let recorded = RecordedRemoval(
+ batch: removal.batch, part: part.removalPart, sizes: part.measuredSizes
+ )
guard let tool = tool(of: part),
let result = await RecordedRemoval.$current.withValue(recorded, operation: {
await tool.move(part, apps: apps)
diff --git a/Peel/Model/TerminalLibrary.swift b/Peel/Model/TerminalLibrary.swift
index 5d9e305a..2183268c 100644
--- a/Peel/Model/TerminalLibrary.swift
+++ b/Peel/Model/TerminalLibrary.swift
@@ -117,7 +117,9 @@ final class TerminalLibrary {
let file = HushLogin.url(in: home)
let sizes = [URL: Int64](measured: [(file, await FileSize.reclaimableSize(of: file))])
let result = await QuitGuard.shared.run {
- let result = await TrashService(exclusions: ExclusionsStore.shared.exclusions).trash([file])
+ let result = await RecordedRemoval.$current.withValue(RecordedRemoval.current?.measuring(sizes)) {
+ await TrashService(exclusions: ExclusionsStore.shared.exclusions).trash([file])
+ }
await record(result, sizes)
return result
}
diff --git a/Peel/Views/AppDetailView.swift b/Peel/Views/AppDetailView.swift
index 8be31e3f..b67dbe4d 100644
--- a/Peel/Views/AppDetailView.swift
+++ b/Peel/Views/AppDetailView.swift
@@ -528,7 +528,10 @@ struct AppDetailView: View {
// The one step that goes before the move: `tccutil` only finds an app that is still in its place.
let service = TrashService(exclusions: ExclusionsStore.shared.exclusions)
let privacy = await PrivacyReset.reset(resetting(request.urls), beforeMovingWith: service)
- let result = await history.moving(tool: .applications, source: plan.app.name) {
+ let result = await history.moving(
+ tool: .applications, source: plan.app.name, sizes: request.sizes,
+ sharingStorage: request.sharingStorage
+ ) {
await plan.move(request)
}
let keptItsFiles = plan.uninstallation?.keptItsFiles(after: result, selection: request.urls) == true
diff --git a/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift b/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
index f3f787a9..d821dd50 100644
--- a/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
+++ b/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
@@ -320,7 +320,7 @@ struct BackgroundItemDetailView: View {
if let plist = item.plistURL {
sizes[plist] = await FileSize.reclaimableSize(of: plist)
}
- await history.moving(tool: .backgroundItems, source: item.label) {
+ await history.moving(tool: .backgroundItems, source: item.label, sizes: sizes) {
await backgroundItems.perform(.moveToTrash, on: item) { result in
await history.record(result, tool: .backgroundItems, source: item.label, sizes: sizes)
}
diff --git a/Peel/Views/BackgroundItems/LaunchAgentEditor.swift b/Peel/Views/BackgroundItems/LaunchAgentEditor.swift
index 64a46b07..0b5b73d5 100644
--- a/Peel/Views/BackgroundItems/LaunchAgentEditor.swift
+++ b/Peel/Views/BackgroundItems/LaunchAgentEditor.swift
@@ -107,7 +107,7 @@ struct LaunchAgentEditor: View {
// Read before the move: afterwards nothing is at that path to measure.
var sizes: [URL: Int64] = [:]
sizes[file.url] = await FileSize.reclaimableSize(of: file.url)
- let outcome = await history.moving(tool: .backgroundItems, source: item.label) {
+ let outcome = await history.moving(tool: .backgroundItems, source: item.label, sizes: sizes) {
await backgroundItems.save(text, editing: file, of: item) { moved in
await history.record(moved, tool: .backgroundItems, source: item.label, sizes: sizes)
}
diff --git a/Peel/Views/MultipleAppsView.swift b/Peel/Views/MultipleAppsView.swift
index 38cec225..540a2e2a 100644
--- a/Peel/Views/MultipleAppsView.swift
+++ b/Peel/Views/MultipleAppsView.swift
@@ -401,7 +401,9 @@ struct MultipleAppsView: View {
// place.
let service = TrashService(exclusions: ExclusionsStore.shared.exclusions)
let privacy = await PrivacyReset.reset(resetting(request.urls), beforeMovingWith: service)
- let result = await history.moving(plan.part(naming: [])) { await plan.move(request) }
+ let result = await history.moving(
+ plan.part(naming: []), sizes: request.sizes, sharingStorage: request.sharingStorage
+ ) { await plan.move(request) }
let kept = plan.bulk?.appsThatKeptTheirFiles(after: result, selection: request.urls) ?? []
let left = plan.bulk?.leftForAnAdministrator(after: result) ?? []
await LeftForAnAdministrator().record(left)
diff --git a/Peel/Views/Packages/PackageDetailView.swift b/Peel/Views/Packages/PackageDetailView.swift
index 3e9142fc..5dfa8692 100644
--- a/Peel/Views/Packages/PackageDetailView.swift
+++ b/Peel/Views/Packages/PackageDetailView.swift
@@ -23,13 +23,14 @@ struct PackageDetailView: View {
}
private func moveSelection() async {
- let result = await history.moving(tool: .packages, source: receipt.identifier) {
+ let sizes = [URL: Int64](measured: receipt.items.map { ($0.url, $0.size) })
+ let result = await history.moving(tool: .packages, source: receipt.identifier, sizes: sizes) {
await packages.removeSelectedItems { result in
await history.record(
result,
tool: .packages,
source: receipt.identifier,
- sizes: [URL: Int64](measured: receipt.items.map { ($0.url, $0.size) })
+ sizes: sizes
)
}
}
diff --git a/Peel/Views/Plugins/PluginDetailView.swift b/Peel/Views/Plugins/PluginDetailView.swift
index deb464ed..f6fe5a88 100644
--- a/Peel/Views/Plugins/PluginDetailView.swift
+++ b/Peel/Views/Plugins/PluginDetailView.swift
@@ -113,14 +113,10 @@ struct PluginDetailView: View {
) {
Button("Move to Trash") {
Task {
- let result = await history.moving(tool: .plugins, source: plugin.name) {
+ let sizes = [URL: Int64](measured: [(plugin.url, plugin.size)])
+ let result = await history.moving(tool: .plugins, source: plugin.name, sizes: sizes) {
await plugins.moveToTrash(plugin) { result in
- await history.record(
- result,
- tool: .plugins,
- source: plugin.name,
- sizes: [URL: Int64](measured: [(plugin.url, plugin.size)])
- )
+ await history.record(result, tool: .plugins, source: plugin.name, sizes: sizes)
}
}
outcome.report(result)
diff --git a/Peel/Views/ResetSheet.swift b/Peel/Views/ResetSheet.swift
index 1ed87a81..e43864bf 100644
--- a/Peel/Views/ResetSheet.swift
+++ b/Peel/Views/ResetSheet.swift
@@ -42,7 +42,8 @@ struct ResetSheet: View {
Task {
await QuitGuard.shared.run {
let result = await history.moving(
- tool: .applications, source: "Reset of \(plan.app.name)", sourceKey: "reset.\(plan.app.name)"
+ tool: .applications, source: "Reset of \(plan.app.name)", sourceKey: "reset.\(plan.app.name)",
+ sizes: sizes
) {
await plan.performReset()
}
diff --git a/Peel/Views/Settings/SavedSettingsSection.swift b/Peel/Views/Settings/SavedSettingsSection.swift
index 157c9530..5bcf1b19 100644
--- a/Peel/Views/Settings/SavedSettingsSection.swift
+++ b/Peel/Views/Settings/SavedSettingsSection.swift
@@ -185,7 +185,10 @@ struct SavedSettingsSection: View {
let size = await FileSize.reclaimableSize(of: copy.folder)
await QuitGuard.shared.run {
let source = "Settings Saved for \(name)"
- let result = await history.moving(tool: .applications, source: source, sourceKey: "savedSettings.\(name)") {
+ let measured = [URL: Int64](measured: [(copy.folder, size)])
+ let result = await history.moving(
+ tool: .applications, source: source, sourceKey: "savedSettings.\(name)", sizes: measured
+ ) {
await TrashService(exclusions: ExclusionsStore.shared.exclusions).trash([copy.folder])
}
if let reason = result.failures.first?.reason {
@@ -196,7 +199,7 @@ struct SavedSettingsSection: View {
tool: .applications,
source: source,
sourceKey: "savedSettings.\(name)",
- sizes: [URL: Int64](measured: [(copy.folder, size)])
+ sizes: measured
)
}
SavedSettings.shared.reload()