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.

Bring back a removal History couldn't record as what it was

Țugui Dragoșf3119fe23d7d
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md
index 627e18d2..24db275c 100644
--- a/ARCHITECTURE.md
+++ b/ARCHITECTURE.md
@@ -164,7 +164,8 @@ the Homebrew page's uninstall and cleanups are Homebrew's own.
`RemovalTotals`), so the app and `peel` count alike.
- 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 the next time History is read, as an interrupted removal, or as what it was when History couldn't write
+ it, since its line then keeps the record (`RemovalJournal.keep`). 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, and while it cannot be written, its folder
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalJournal.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalJournal.swift
index ab6c7ed2..eb0beb62 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalJournal.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalJournal.swift
@@ -33,11 +33,15 @@ struct RemovalJournal: Sendable {
/// The move it was part of, so an interrupted one comes back as one entry of History.
let batch: UUID
let item: TrashedItem
-
- /// History's record of the item, when the removal stopped before it recorded the item in its own words.
- /// It names no tool, since which tool moved the item is not known.
- var interruptedRecord: RemovalRecord {
- RemovalRecord(batch: batch, item: item, size: nil, source: "Interrupted removal", sourceKey: "interrupted", tool: "")
+ /// The record History couldn't take, kept here so the item comes back as what it was.
+ var record: RemovalRecord? = nil
+
+ /// History's record of the item: the one kept here, or else that of an interrupted removal, which names no
+ /// tool, since which tool moved the item is not known.
+ var recordToTakeIn: RemovalRecord {
+ record ?? RemovalRecord(
+ batch: batch, item: item, size: nil, source: "Interrupted removal", sourceKey: "interrupted", tool: ""
+ )
}
}
@@ -127,17 +131,38 @@ struct RemovalJournal: Sendable {
forget { entries.contains($0) }
}
+ /// Keeps with each item's line the record History couldn't write.
+ func keep(_ records: [RemovalRecord]) {
+ let byItem = Dictionary(records.map { (Key($0.trashedItem), $0) }, uniquingKeysWith: { first, _ in first })
+ guard !url.isMissing else { return }
+ FileLock.whileHeld(beside: url) {
+ var entries = read()
+ var changed = false
+ for index in entries.indices {
+ guard let record = byItem[Key(entries[index].item)] else { continue }
+ entries[index].record = record
+ changed = true
+ }
+ guard changed else { return }
+ write(entries)
+ }
+ }
+
private func forget(where isForgotten: (Entry) -> Bool) {
guard !url.isMissing else { return }
FileLock.whileHeld(beside: url) {
let entries = read()
let kept = entries.filter { !isForgotten($0) }
guard kept.count != entries.count else { return }
- let data = kept.compactMap { try? Self.encoder.encode($0) }.reduce(into: Data()) { $0 += $1 + Data("\n".utf8) }
- try? data.write(to: url, options: .atomic)
+ write(kept)
}
}
+ private func write(_ entries: [Entry]) {
+ let data = entries.compactMap { try? Self.encoder.encode($0) }.reduce(into: Data()) { $0 += $1 + Data("\n".utf8) }
+ try? data.write(to: url, options: .atomic)
+ }
+
private func read() -> [Entry] {
guard let data = BoundedRead.data(at: url, maximum: 64 * 1_024 * 1_024) else { return [] }
return data.split(separator: UInt8(ascii: "\n")).compactMap { try? Self.decoder.decode(Entry.self, from: $0) }
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift
index 35f73c44..6a554420 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift
@@ -70,6 +70,7 @@ public actor RemovalLog {
guard let existing = current() else { return RemovalLogOutcome(records: nil, problem: problem) }
let updated = Self.trimmed(existing + records, keeping: Set(records.map(\.batch)))
guard write(updated, orNote: .couldNotRecord) else {
+ journal.keep(records)
return RemovalLogOutcome(records: nil, problem: problem)
}
RemovalTotals.change(at: totals) { $0.add(records) }
@@ -165,7 +166,7 @@ public actor RemovalLog {
let left = journal.left()
guard !left.isEmpty else { return records }
let recorded = Set(records.map { RemovalJournal.Key($0.trashedItem) })
- let interrupted = left.filter { !recorded.contains(RemovalJournal.Key($0.item)) }.map(\.interruptedRecord)
+ let interrupted = left.filter { !recorded.contains(RemovalJournal.Key($0.item)) }.map(\.recordToTakeIn)
var updated = records
if !interrupted.isEmpty {
updated = Self.trimmed(records + interrupted, keeping: Set(interrupted.map(\.batch)))
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
index 2575e6e7..8e2b4749 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
@@ -158,7 +158,7 @@ struct HistoryCommandTests {
date: .now
)
let writer = RemovalJournal.Writer(pid: 0, started: 0)
- _ = await logs.removals.add([RemovalJournal.Entry(writer: writer, batch: UUID(), item: item).interruptedRecord])
+ _ = await logs.removals.add([RemovalJournal.Entry(writer: writer, batch: UUID(), item: item).recordToTakeIn])
let removal = try #require(try listed(try await printed(["history", "--json"], from: logs)).first as? [String: Any])
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift
index dea85d13..4eff4508 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift
@@ -210,6 +210,24 @@ struct RemovalJournalTests {
#expect(await service.trashOwnFiles([item]).trashed.count == 1, "Peel's own files waited for History")
}
+ @Test func aRemovalHistoryCouldNotRecordComesBackAsWhatItWas() async throws {
+ let directory = try TemporaryDirectory()
+ let history = try directory.file("home/Library/Application Support/Peel/removals.json", contents: Data("[]".utf8))
+ let journal = RemovalJournal(beside: history)
+ let moved = item("com.example.app", in: directory)
+ journal.note([moved], batch: UUID(), by: try endedProcess())
+ let record = RemovalRecord(batch: UUID(), item: moved, size: 512, source: "Xcode", tool: "developer")
+ let path = history.path(percentEncoded: false)
+ #expect(chflags(path, UInt32(UF_IMMUTABLE)) == 0)
+ defer { chflags(path, 0) }
+
+ #expect(await RemovalLog(url: history).add([record]).records == nil)
+ chflags(path, 0)
+ let taken = try #require(await RemovalLog(url: history).load().records?.first)
+
+ #expect(taken.source == "Xcode" && taken.tool == "developer" && taken.size == 512)
+ }
+
@Test(.permissionsHold) func nothingMovesWhileHistoryCannotBeWritten() async throws {
let directory = try TemporaryDirectory()
let history = try directory.file("home/Library/Application Support/Peel/removals.json", contents: Data("[]".utf8))