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
Write down what the helper moves after each request
Țugui Dragoș986a01572932
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.mdindex 8bddfeef..b9a05976 100644--- a/ARCHITECTURE.md+++ b/ARCHITECTURE.md@@ -152,10 +152,10 @@ the Homebrew page's uninstall and cleanups are Homebrew's own.even when it moved what was selected on several pages.- As it records, History adds what moved to the totals Home and the menu bar panel show (`totals.json`, through`RemovalTotals`), so the app and `peel` count alike.- - Each item is also written down the moment it moves (`removals.journal`, through `RemovalJournal`), so what a- removal moved before it was cut short reaches History the next time History is read, as an interrupted- removal. Quitting the app waits for a removal until History has it (`QuitGuard`), as `peel` carries on- through Control-C (`Uninterrupted`).+ - Each item is also written down the moment it moves, and what the helper moves as soon as it answers each+ request (`removals.journal`, through `RemovalJournal`), so what a removal moved before it was cut short reaches+ History the next time History is read, as an interrupted removal. Quitting the app waits for a removal until+ History has it (`QuitGuard`), as `peel` carries on through Control-C (`Uninterrupted`).- While History cannot be read nothing moves (`RemovalLog.canBeRead`, asked by `TrashService` before a removal)until Start Over, on the History page, sets the unreadable file aside.- What Peel refused to move is recorded too (`refusals.json`), and History lists it under Not Moved, onediff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swiftindex 6df81460..3894a9f7 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift@@ -182,30 +182,20 @@ public enum PrivilegedHelper {/// At utility quality these timers would wait behind any utility work queued before them.static let timers = DispatchQueue.global(qos: .userInitiated)+ /// Moves `urls` to the Trash in one request, so at most `HelperRequest.maximumItems` of them: the helper refuses+ /// more.@concurrentpublic static func moveToTrash(_ urls: [URL]) async -> TrashResult {- var result = TrashResult()let trash = URL.homeDirectory.appending(path: ".Trash", directoryHint: .isDirectory)- // The helper's own limit also bounds how many items an answer that never comes can affect.- let limit = HelperRequest.maximumItems- for start in stride(from: 0, to: urls.count, by: limit) {- let batch = Array(urls[start..<min(start + limit, urls.count)])- // Read before the move, so that if the answer is lost, moved items can be found in the Trash by identity.- let links = Dictionary(- batch.map { ($0, FileIdentity.Link.of($0)) },- uniquingKeysWith: { first, _ in first }- )- let paths = batch.map { $0.path(percentEncoded: false) }- let reply: HelperTrashReply? = await withHelper(fallback: nil) { helper, finish in- helper.moveItemsToTrash(version: HelperIdentity.protocolVersion, atPaths: paths) { moved, failed in- finish(HelperTrashReply(moved: moved, failed: failed))- }+ // Read before the move, so that if the answer is lost, moved items can be found in the Trash by identity.+ let links = Dictionary(urls.map { ($0, FileIdentity.Link.of($0)) }, uniquingKeysWith: { first, _ in first })+ let paths = urls.map { $0.path(percentEncoded: false) }+ let reply: HelperTrashReply? = await withHelper(fallback: nil) { helper, finish in+ helper.moveItemsToTrash(version: HelperIdentity.protocolVersion, atPaths: paths) { moved, failed in+ finish(HelperTrashReply(moved: moved, failed: failed))}- let answered = Self.result(for: batch, reply: reply, links: links, trash: trash)- result.trashed += answered.trashed- result.failures += answered.failures}- return result+ return Self.result(for: urls, reply: reply, links: links, trash: trash)}/// Builds the result of one request from the helper's reply. The helper moves items one by one and answersdiff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swiftindex ce7cb80b..7062d8b8 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift@@ -624,12 +624,18 @@ public struct TrashService: Sendable {let links = Set(allowed.filter(reach.takesOnlyALink))ownMoves.began()var helperResult = TrashResult()- for batch in [allowed.filter { !links.contains($0) }, allowed.filter(links.contains)] where !batch.isEmpty {- let moved = await moveThroughHelper(batch)- journal?.note(moved.trashed, batch: removal)- MoveCount.current?.add(moved.trashed.count)- helperResult.trashed += moved.trashed- helperResult.failures += moved.failures+ // One request at a time, each answer written down before the next request goes, so a stop while the helper+ // works loses no more than the request under way. The helper's own limit also bounds how many items an+ // answer that never comes can affect.+ let limit = HelperRequest.maximumItems+ for batch in [allowed.filter { !links.contains($0) }, allowed.filter(links.contains)] {+ for start in stride(from: 0, to: batch.count, by: limit) {+ let moved = await moveThroughHelper(Array(batch[start..<min(start + limit, batch.count)]))+ journal?.note(moved.trashed, batch: removal)+ MoveCount.current?.add(moved.trashed.count)+ helperResult.trashed += moved.trashed+ helperResult.failures += moved.failures+ }}ownMoves.ended(landedAt: helperResult.trashed.map(\.trashedURL))result.trashed += helperResult.trasheddiff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swiftindex 7a7a181b..6a8c78c1 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift@@ -1,5 +1,6 @@import Foundation@testable import PeelCore+import PeelPrivilegedimport Synchronizationimport Testing@@ -150,6 +151,39 @@ struct RemovalJournalTests {#expect(Set(journal.entries().map(\.item.trashedURL)) == Set(result.trashed.map(\.trashedURL)))}+ @Test func whatTheHelperMovesIsWrittenDownAfterEachRequest() async throws {+ let directory = try TemporaryDirectory()+ let journal = RemovalJournal(beside: directory.url.appending(path: "Peel/removals.json"))+ let items = try (0...HelperRequest.maximumItems).map {+ try directory.file("home/Library/Caches/com.example.app\($0)/cache.db").deletingLastPathComponent()+ }+ let written = Mutex<[Int]>([])+ let service = TrashService(+ environment: SearchEnvironment(+ homeDirectory: directory.url.appending(path: "home"),+ rootDirectory: directory.url.appending(path: "root")+ ),+ moveThroughHelper: { urls in+ written.withLock { $0.append(journal.entries().count) }+ return TrashResult(trashed: urls.map {+ TrashedItem(+ originalURL: $0,+ trashedURL: directory.url.appending(path: "home/.Trash/\($0.lastPathComponent)"),+ date: .now+ )+ })+ },+ journal: journal,+ moveToTrash: { _ in throw CocoaError(.fileWriteNoPermission) }+ )++ let result = await service.trash(items, usingHelperFor: Set(items))++ #expect(result.trashed.count == items.count)+ #expect(written.withLock { $0 } == [0, HelperRequest.maximumItems], "a request went before the last answer was written down")+ #expect(journal.entries().count == items.count)+ }+/// While History cannot be read nothing moves, since what moved could not be listed for Put Back. An item the/// guard refuses keeps that reason, and Peel's own files, which History never lists, still go.@Test(.permissionsHold) func nothingMovesWhileHistoryCannotBeRead() async throws {diff --git a/SAFETY.md b/SAFETY.mdindex e7812e1b..de22acc2 100644--- a/SAFETY.md+++ b/SAFETY.md@@ -143,9 +143,10 @@ That includes what `pkgutil` and Homebrew would delete:or into its gone apps, to the Trash.A removal reaches History even when something cuts it short. Quitting Peel waits until History has it, `peel`-carries on through Control-C or a closed terminal until it has written History, and each item is written down the-moment it moves, so one cut short by a crash or Force Quit still reaches History, as an interrupted removal, the-next time Peel or `peel` opens it.+carries on through Control-C or a closed terminal until it has written History, and each item Peel moves itself is+written down the moment it moves, and what its helper moves as soon as the helper answers each request, so one cut+short by a crash or Force Quit still reaches History, as an interrupted removal, the next time Peel or `peel` opens+it.Right before anything moves, Peel looks at the files other programs of yours hold open and at the program eachprocess runs, and leaves where it is any file or folder with one of them inside, naming the program and how to free