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
History is written before the refusals
Țugui Dragoșc0d2c54b89a0
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/Removals.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/Removals.swiftindex 11c3b1ff..9860d7d2 100644--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/Removals.swift+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/Removals.swift@@ -13,9 +13,13 @@ enum Removals {in log: RemovalLog = RemovalLog(),refusals: RefusalLog = RefusalLog()) async -> Bool {+ // History first: it is the way back for what moved, and the refusal log may have to wait for another writer.+ var isRecorded = true+ if !result.trashed.isEmpty {+ let part = RemovalPart(source: source, sourceKey: sourceKey, tool: tool)+ isRecorded = await log.add(part.records(of: result, sizes: sizes, batch: UUID())).records != nil+ }await refusals.add(result.failures, source: source, sourceKey: sourceKey, tool: tool)- guard !result.trashed.isEmpty else { return true }- let records = RemovalPart(source: source, sourceKey: sourceKey, tool: tool).records(of: result, sizes: sizes, batch: UUID())- return await log.add(records).records != nil+ return isRecorded}}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RefusalLogTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RefusalLogTests.swiftindex f8333bfa..9217acf6 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/RefusalLogTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/RefusalLogTests.swift@@ -40,6 +40,40 @@ struct RefusalLogTests {#expect(await RefusalLog(url: refusals.url).load().map(\.reason) == ["protected-location"])}+ /// History is the way back for what just moved, so it is written before the refusals: while the refusal log+ /// waits for another writer, what moved is already in History.+ @Test func writesHistoryBeforeTheRefusals() async throws {+ let directory = try TemporaryDirectory()+ let folder = try directory.directory("Peel")+ let history = folder.appending(path: "removals.json")+ let log = RemovalLog(url: history)+ let refusals = RefusalLog(url: folder.appending(path: "refusals.json"))+ let moved = TrashedItem(+ originalURL: URL(filePath: "/Users/me/Library/Caches/com.example.app"),+ trashedURL: URL(filePath: "/Users/me/.Trash/com.example.app"),+ date: .now+ )+ let result = TrashResult(trashed: [moved], failures: [failure("/Users/me/Library/Mail", .protectedLocation)])+ // Holds the refusal log's lock, as another process writing it would.+ let lockFile = folder.appending(path: "refusals.json.lock").path(percentEncoded: false)+ let lock = open(lockFile, O_CREAT | O_RDWR | O_CLOEXEC, 0o600)+ defer { close(lock) }+ flock(lock, LOCK_EX)++ let recording = Task {+ await Removals.record(result, from: "Editor", sizes: [:], tool: "applications", in: log, refusals: refusals)+ }+ var isInHistory = false+ for _ in 0..<100 where !isInHistory {+ try await Task.sleep(for: .milliseconds(20))+ isInHistory = FileManager.default.fileExists(atPath: history.path(percentEncoded: false))+ }+ flock(lock, LOCK_UN)++ #expect(await recording.value)+ #expect(isInHistory, "History waited for the refusals")+ }+@Test func nothingIsWrittenWhenNothingWasRefused() async throws {let directory = try TemporaryDirectory()let url = directory.url.appending(path: "Peel/refusals.json")diff --git a/Peel/Model/RemovalHistoryStore.swift b/Peel/Model/RemovalHistoryStore.swiftindex 5ee6286d..70b235e1 100644--- a/Peel/Model/RemovalHistoryStore.swift+++ b/Peel/Model/RemovalHistoryStore.swift@@ -157,17 +157,18 @@ final class RemovalHistoryStore {/// Records in History what one part of `removal` moved, and what it refused.func record(_ result: TrashResult, part: RemovalPart, sizes: [URL: Int64], in removal: inout RemovalInProgress) async {- // Refusals are logged before the early return below: a removal where nothing moved is the one most- // worth a record.+ // History first: it is the way back for what just moved, and nothing that follows may keep it unwritten.+ if !result.trashed.isEmpty {+ let new = part.records(of: result, sizes: sizes, batch: removal.batch)+ removal.records += new+ apply(await log.add(new))+ }+ // Refusals are logged whether or not anything moved: a removal where nothing moved is the one most worth a+ // record.await refusals.add(result.failures, source: part.source, sourceKey: part.sourceKey, tool: part.tool, batch: removal.refusals)if !result.failures.isEmpty {await loadRefusals()}- guard !result.trashed.isEmpty else { return }- let new = part.records(of: result, sizes: sizes, batch: removal.batch)- removal.records += new- // History first: it is the way back for what just moved, and nothing that follows may keep it unwritten.- apply(await log.add(new))}/// Tells what `removal` moved once every part of it is recorded: the bar's "Moved", VoiceOver, the lifetime