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.

Look in the Trash again after a Put Back History couldn't write down

Țugui Dragoșba692e61e066
diff --git a/Peel/Model/RemovalHistoryStore.swift b/Peel/Model/RemovalHistoryStore.swift
index 7362b3c0..c97d4a60 100644
--- a/Peel/Model/RemovalHistoryStore.swift
+++ b/Peel/Model/RemovalHistoryStore.swift
@@ -170,9 +170,9 @@ final class RemovalHistoryStore {
var failures: [RemovalRecord.ID: RestoreFailure] = [:]
/// Counts the Put Backs History's page tells, so one that ends as the one before is told again.
private(set) var toldPutBacks = 0
- /// Counts the times History was read again, by Reload or as Peel came forward, so a removal's page then looks in
- /// the Trash again with the records as they are now.
- private(set) var rereads = 0
+ /// Counts the times a removal's page has to look in the Trash again: History read again, by Reload or as Peel came
+ /// forward, and a Put Back that brought something back, whatever History could write of it.
+ private(set) var trashLooks = 0
/// Counts, for each tool, the Put Backs that brought back what it had moved, so its page lists them again.
private(set) var putBack: [Tool: Int] = [:]
/// The count each page last listed under, kept here so a page off screen at a Put Back lists it when it returns.
@@ -234,7 +234,7 @@ final class RemovalHistoryStore {
/// Reads History before a removal's page looks in the Trash, so the page never looks with records `peel` changed.
private func readAgain() async {
await load()
- rereads += 1
+ trashLooks += 1
}
/// Whether History was read and lists nothing. One that can't be read now isn't known to be empty.
@@ -486,6 +486,7 @@ final class RemovalHistoryStore {
putBack[tool, default: 0] += 1
}
await apply(await log.remove(restored))
+ trashLooks += 1
// A move is announced, and so is its undoing: the rows only vanish from the list.
announcing(AttributedString(localized: "Put back ^[\(restored.count) item](inflect: true)."))
}
diff --git a/Peel/Views/History/HistoryDetailView.swift b/Peel/Views/History/HistoryDetailView.swift
index 200bf6a5..ee3b29b6 100644
--- a/Peel/Views/History/HistoryDetailView.swift
+++ b/Peel/Views/History/HistoryDetailView.swift
@@ -22,7 +22,7 @@ struct HistoryDetailView: View {
/// reads the disk again, since the Trash may have been emptied in the meantime.
private struct Look: Hashable {
let batch: RemovalBatch
- let rereads: Int
+ let trashLooks: Int
}
/// The size of what can still be put back. Until the disk has answered, it is the batch's own total.
@@ -64,7 +64,7 @@ struct HistoryDetailView: View {
}
.navigationTitle(Text(verbatim: batch.title))
.toolbar(removing: .title)
- .task(id: Look(batch: batch, rereads: history.rereads)) {
+ .task(id: Look(batch: batch, trashLooks: history.trashLooks)) {
let standing = await RemovalStanding.of(batch.records)
// A look that a newer one replaced leaves its answer.
guard !Task.isCancelled else { return }
diff --git a/PeelTests/RemovalHistoryStoreTests.swift b/PeelTests/RemovalHistoryStoreTests.swift
index 3922bf7d..4d9f9171 100644
--- a/PeelTests/RemovalHistoryStoreTests.swift
+++ b/PeelTests/RemovalHistoryStoreTests.swift
@@ -31,7 +31,7 @@ struct RemovalHistoryStoreTests {
_ = await RemovalLog(url: url).remove([records[0].id])
let seen = Mutex<Int?>(nil)
- withObservationTracking({ _ = history.rereads }, onChange: {
+ withObservationTracking({ _ = history.trashLooks }, onChange: {
MainActor.assumeIsolated { seen.withLock { $0 = history.batches.first?.records.count } }
})
await history.readAgainIfInUse()
@@ -108,6 +108,35 @@ struct RemovalHistoryStoreTests {
#expect(heard.words == ["Started History over.", "Started the Not Moved list over."])
}
+ @Test func looksInTheTrashAgainAfterAPutBackHistoryCouldNotWrite() async throws {
+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
+ let path = folder.path(percentEncoded: false)
+ defer {
+ try? FileManager.default.setAttributes([.posixPermissions: 0o700], ofItemAtPath: path)
+ try? FileManager.default.removeItem(at: folder)
+ }
+ let history = RemovalHistoryStore(
+ log: RemovalLog(url: folder.appending(path: "removals.json")),
+ refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
+ knowsTheExclusions: { true },
+ restoring: { items, _ in
+ // Back in place, and History's folder closed before History is told.
+ try? FileManager.default.setAttributes([.posixPermissions: 0o500], ofItemAtPath: path)
+ return items.map { _ in nil }
+ }
+ )
+ var removal = RemovalInProgress()
+ let part = RemovalPart(source: "Example", sourceKey: nil, tool: Tool.developer.rawValue)
+ await history.record(try moved("Cache"), part: part, sizes: [:], in: &removal)
+ history.finish(removal)
+ let looks = history.trashLooks
+
+ await history.restore(history.records, canUseHelper: false)
+
+ #expect(history.problem == .couldNotUpdate)
+ #expect(history.trashLooks == looks + 1, "the page kept saying it is in the Trash")
+ }
+
@Test func tellsAPutBackThatEndsAsTheOneBefore() async throws {
let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
defer { try? FileManager.default.removeItem(at: folder) }