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
Every item is written down as it moves, so History never misses one
Țugui Dragoșe56046b8a545
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.mdindex 93064675..fdfcf0fc 100644--- a/ARCHITECTURE.md+++ b/ARCHITECTURE.md@@ -87,8 +87,10 @@ Take the most common case, uninstalling an app. Every other page follows the samforget the preference domains whose files went.7. **Record.** Every removal goes into History (`removals.json`, through `RemovalLog`), with where each item camefrom and where it went, so it can be put back; History keeps the most recent 20,000 items. A removal is one- entry, even when it moved what was selected on several pages. What Peel refused to move is recorded too- (`refusals.json`), and History lists it under Not Moved, one removal to an entry.+ entry, even when it moved what was selected on several pages. Each item is also written down the moment it+ moves (`removals.journal`, through `RemovalJournal`), so what a removal cut short moved reaches History the+ next time it is read, as an interrupted removal. What Peel refused to move is recorded too (`refusals.json`),+ and History lists it under Not Moved, one removal to an entry.8. **Put back.** History's Put Back reads its record as a request, not as a fact: an item returns only from areal Trash, only to a place the guard allows, and, through the helper, only if the helper's own ledger saysit moved that very item from that very place.@@ -171,6 +173,7 @@ where a Homebrew install takes them from.| Where | What ||---|---|| `~/Library/Application Support/Peel/removals.json` | History: every removal, so it can be put back. |+| `~/Library/Application Support/Peel/removals.journal` | What a removal has moved so far, item by item, until History has it. || `~/Library/Application Support/Peel/refusals.json` | What Peel was asked to move and refused, and why. || `~/Library/Application Support/Peel/exclusions.json` | The user's exclusions. || `~/Library/Application Support/Peel/Preference Backups/` | The settings Peel saved before resetting an app. |diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstringsindex 2bcc451c..ee8de022 100644--- a/Localization/Peel/Localizable.xcstrings+++ b/Localization/Peel/Localizable.xcstrings@@ -44109,6 +44109,112 @@}}},+ "Interrupted Removal" : {+ "localizations" : {+ "cs" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Přerušené odstranění"+ }+ },+ "de" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Unterbrochenes Entfernen"+ }+ },+ "es" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Eliminación interrumpida"+ }+ },+ "fr" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Suppression interrompue"+ }+ },+ "it" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Rimozione interrotta"+ }+ },+ "ja" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "中断されたゴミ箱への移動"+ }+ },+ "ko" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "중단된 제거 작업"+ }+ },+ "nl" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Onderbroken verwijdering"+ }+ },+ "pl" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Przerwane przeniesienie do Kosza"+ }+ },+ "pt" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Remoção Interrompida"+ }+ },+ "ro" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Eliminare întreruptă"+ }+ },+ "ru" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Прерванное удаление"+ }+ },+ "sv" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Avbruten borttagning"+ }+ },+ "tr" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Kesintiye Uğrayan Kaldırma İşlemi"+ }+ },+ "uk" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Перерване вилучення"+ }+ },+ "zh-Hans" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "已中断的移除"+ }+ },+ "zh-Hant" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "中斷的移除作業"+ }+ }+ }+ },"It came inside an app, and it goes when that app goes." : {"localizations" : {"cs" : {diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swiftindex 063de972..a8c03d90 100644--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift@@ -2,6 +2,11 @@ import ArgumentParserimport Foundationimport PeelCore+extension RemovalPart {+ /// The tool that moved the part, or nil for a removal that was interrupted, which names none.+ fileprivate var namedTool: String? { tool.isEmpty ? nil : tool }+}+/// One removal, as History shows it: everything that went in one go.struct Batch {let id: UUID@@ -11,8 +16,8 @@ struct Batch {/// Where the removal moved from: one part, or one for each tool whose selection it moved.var parts: [RemovalPart] { RemovalPart.of(records.map { ($0.date, $0.part) }) }var source: String { Output.list(parts.map(\.source)) }- /// The tool the removal moved from, or nil for one that moved from several.- var tool: String? { parts.count == 1 ? parts[0].tool : nil }+ /// The tool the removal moved from, or nil for one that moved from several or was interrupted.+ var tool: String? { parts.count == 1 ? parts[0].namedTool : nil }var size: SizeTotal { records.totalSize }/// The records whose items are still in the Trash. An item emptied from the Trash can't be put back.var restorable: [RemovalRecord] { records.filter(\.isStillInTrash) }@@ -54,7 +59,18 @@ struct HistoryCommand: AsyncParsableCommand {private struct Record: Encodable {struct Part: Encodable {let source: String- let tool: String+ let tool: String?++ private enum CodingKeys: String, CodingKey {+ case source, tool+ }++ /// Written by hand so an interrupted part says `"tool": null` rather than leaving the key out.+ func encode(to encoder: any Encoder) throws {+ var container = encoder.container(keyedBy: CodingKeys.self)+ try container.encode(source, forKey: .source)+ try container.encode(tool, forKey: .tool)+ }}let batch: String@@ -110,7 +126,7 @@ struct HistoryCommand: AsyncParsableCommand {date: batch.date,source: batch.source,tool: batch.tool,- parts: batch.parts.map { Record.Part(source: $0.source, tool: $0.tool) },+ parts: batch.parts.map { Record.Part(source: $0.source, tool: $0.namedTool) },size: MeasuredSize(batch.size),itemCount: batch.records.count,restorableCount: batch.restorable.count,diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateRemoval.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateRemoval.swiftindex 132cc78b..5e83c39f 100644--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateRemoval.swift+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateRemoval.swift@@ -9,7 +9,7 @@ public enum DuplicateRemoval {_ selection: Set<URL>,folders: Set<URL> = [],from scan: DuplicateScan,- using trashService: TrashService = TrashService()+ using trashService: TrashService) async -> TrashResult {// The scan never offers a file on its own when it sits inside an offered folder, so the two selections// never ask for the same bytes.diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swiftindex 62518315..709a7da8 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift@@ -39,7 +39,7 @@ public enum PreferenceBackup {/// Exports every existing preference domain among `urls` into a new folder in `directory`. If any one/// cannot be copied, the result is `failed` and nothing may be cleared: the copy is what makes clearing safe.@concurrent- static func save(_ urls: [URL], for app: InstalledApp, in directory: URL, through service: TrashService = TrashService(), run: Run) async -> Saved {+ static func save(_ urls: [URL], for app: InstalledApp, in directory: URL, through service: TrashService, run: Run) async -> Saved {let domains = PreferenceCleanup.domains(for: urls, ownedBy: app.bundleIdentifier)return await save(domains, of: app.bundleIdentifier, in: directory, through: service, run: run)}@@ -76,7 +76,7 @@ public enum PreferenceBackup {// `rmdir` removes the folder only when it is empty. If copies were already written, the folder// goes to the Trash instead: nothing is deleted outright.if rmdir(folder.path(percentEncoded: false)) != 0 {- _ = await service.trash([folder])+ _ = await service.trashOwnFiles([folder])}return .failed}@@ -118,7 +118,7 @@ public enum PreferenceBackup {from folder: URL,of bundleIdentifier: String,in directory: URL,- through service: TrashService = TrashService(),+ through service: TrashService,run: Run) async -> Restored {let files = (try? FileManager.default.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil)) ?? []diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalJournal.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalJournal.swiftnew file mode 100644index 00000000..914f9fd7--- /dev/null+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalJournal.swift@@ -0,0 +1,146 @@+import Darwin+import Foundation+internal import PeelPrivileged++/// What a removal moved, written down item by item as it moves, beside History. History takes each item over once+/// it records it; what a process that stopped before then left here goes into History the next time History is read,+/// as an interrupted removal, so nothing Peel moved to the Trash is ever missing from it.+struct RemovalJournal: Sendable {+ /// The process that wrote a line: its id, and when it started, since macOS gives an ended process's id to a new+ /// one.+ struct Writer: Sendable, Codable, Hashable {+ let pid: Int32+ let started: Double++ static let current = Writer(pid: getpid(), started: startTime(of: getpid()) ?? 0)++ /// Whether the process that wrote the line still runs, and so will record the line itself.+ var isRunning: Bool {+ self == .current || Self.startTime(of: pid) == started+ }++ private static func startTime(of pid: Int32) -> Double? {+ var info = proc_bsdinfo()+ let size = Int32(MemoryLayout<proc_bsdinfo>.size)+ guard proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, size) == size else { return nil }+ return Double(info.pbi_start_tvsec) + Double(info.pbi_start_tvusec) / 1_000_000+ }+ }++ /// One item that moved.+ struct Entry: Sendable, Codable, Hashable {+ let writer: Writer+ /// 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: "")+ }+ }++ /// An item as History keeps it: where it was, where it went, and when, to the millisecond. It is the same+ /// for an item in memory and for that item read back from a file, which keeps its time to the millisecond.+ struct Key: Hashable {+ let originalURL: URL+ let trashedURL: URL+ let time: String++ init(_ item: TrashedItem) {+ originalURL = item.originalURL+ trashedURL = item.trashedURL+ time = LogTime.text(for: item.date)+ }+ }++ let url: URL++ /// The journal beside the History log at `history`.+ init(beside history: URL) {+ url = history.deletingLastPathComponent().appending(path: "removals.journal")+ }++ /// Writes down `items`, which moved together in `batch`, one line each. An item the journal was in, Peel's own+ /// folder as Peel removes itself, is left out: History went with it, and writing the line would make the+ /// folder again.+ func note(_ items: [TrashedItem], batch: UUID, by writer: Writer = .current) {+ let journal = url.path(percentEncoded: false)+ let lines = items+ .filter { !PathComponents.isPath(journal, atOrInside: $0.originalURL.path(percentEncoded: false)) }+ .compactMap { item in (try? Self.encoder.encode(Entry(writer: writer, batch: batch, item: item))) }+ guard !lines.isEmpty else { return }+ FileLock.whileHeld(beside: url) {+ let descriptor = open(journal, O_RDWR | O_APPEND | O_CREAT | O_CLOEXEC, 0o600)+ guard descriptor >= 0 else { return }+ defer { close(descriptor) }+ var data = Data()+ // A line cut short, as when the disk filled up, is ended first, so the next line is not read as part+ // of it.+ let end = lseek(descriptor, 0, SEEK_END)+ var last: UInt8 = 0+ if end > 0, pread(descriptor, &last, 1, end - 1) == 1, last != UInt8(ascii: "\n") {+ data.append(UInt8(ascii: "\n"))+ }+ for line in lines {+ data += line + Data("\n".utf8)+ }+ _ = data.withUnsafeBytes { Darwin.write(descriptor, $0.baseAddress, $0.count) }+ }+ }++ /// Every line the journal holds. A line that does not decode is left out. Reading makes nothing on the disk:+ /// with no journal there is nothing to take in.+ func entries() -> [Entry] {+ guard !url.isMissing else { return [] }+ return FileLock.whileHeld(beside: url) { read() }+ }++ /// The lines of processes that no longer run.+ func left() -> [Entry] {+ let entries = entries()+ let running = Set(entries.map(\.writer)).filter(\.isRunning)+ return entries.filter { !running.contains($0.writer) }+ }++ /// Takes out the lines of `items`, which History now has.+ func forget(_ items: [TrashedItem]) {+ let keys = Set(items.map(Key.init))+ forget { keys.contains(Key($0.item)) }+ }++ /// Takes out `entries`.+ func forget(_ entries: Set<Entry>) {+ forget { entries.contains($0) }+ }++ 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)+ }+ }++ 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) }+ }++ private static let encoder: JSONEncoder = {+ let encoder = JSONEncoder()+ encoder.dateEncodingStrategy = LogTime.encoding+ encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]+ return encoder+ }()++ private static let decoder: JSONDecoder = {+ let decoder = JSONDecoder()+ decoder.dateDecodingStrategy = LogTime.decoding+ return decoder+ }()+}diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swiftindex f4b7f748..8d3b8311 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift@@ -31,9 +31,12 @@ public actor RemovalLog {public let url: URLpublic private(set) var problem: RemovalLogProblem?+ /// What removals moved and have not recorded yet, item by item.+ private let journal: RemovalJournalpublic init(url: URL = RemovalHistory.defaultURL) {self.url = url+ journal = RemovalJournal(beside: url)}/// Reads the log. The read takes the lock too, because reading a damaged file sets it aside and writes back@@ -48,6 +51,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 { return RemovalLogOutcome(records: nil, problem: problem) }+ journal.forget(records.map(\.trashedItem))// A removal that is recorded clears an earlier `.couldNotRecord`.if problem == .couldNotRecord { problem = nil }return RemovalLogOutcome(records: updated, problem: problem)@@ -80,8 +84,32 @@ public actor RemovalLog {self.problem = problem}- /// The records on disk, or nil when the file must not be replaced because it could not be read.+ /// The records on disk with every removal that stopped before recording itself taken in, or nil when the file+ /// must not be replaced because it could not be read.private func current() -> [RemovalRecord]? {+ guard let records = stored() else { return nil }+ return takingInInterrupted(records)+ }++ /// Records what the journal holds for processes that no longer run, as interrupted removals, then lets those+ /// lines go. An item History already has is not recorded again: a process may have stopped after writing+ /// History and before letting its lines go.+ private func takingInInterrupted(_ records: [RemovalRecord]) -> [RemovalRecord] {+ 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)+ var updated = records+ if !interrupted.isEmpty {+ updated = Self.trimmed(records + interrupted, keeping: Set(interrupted.map(\.batch)))+ guard write(updated, orNote: .couldNotRecord) else { return records }+ }+ journal.forget(Set(left))+ return updated+ }++ /// The records on disk, or nil when the file must not be replaced because it could not be read.+ private func stored() -> [RemovalRecord]? {guard !url.isMissing else {note(nil)return []diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swiftindex 41eb58bd..4f667807 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift@@ -1,7 +1,7 @@public import Foundationinternal import PeelPrivileged-public struct TrashedItem: Sendable, Hashable {+public struct TrashedItem: Sendable, Hashable, Codable {public let originalURL: URLpublic let trashedURL: URLpublic let date: Date@@ -105,6 +105,8 @@ public struct TrashService: Sendable {private let moveThroughHelper: MoveThroughHelperprivate let moveToTrash: @Sendable (URL) throws -> URLprivate let ownMoves: OwnTrashMoves+ /// Where each item is written down as soon as it moves, so a removal cut short still reaches History.+ private let journal: RemovalJournal?public init(environment: SearchEnvironment = .current, exclusions: Exclusions = .none) {let removalGuard = RemovalGuard(environment: environment, exclusions: exclusions)@@ -115,12 +117,13 @@ public struct TrashService: Sendable {forgetDomains: { await PreferenceCleanup.forgetDomains(for: $0, ownedBy: $1) },moveThroughHelper: Self.moveThroughTheHelper,moveToTrash: { try Self.moveToSystemTrash($0, isAllowed: removalGuard.allowsRemoval(of:)) },- ownMoves: .shared+ ownMoves: .shared,+ journal: RemovalJournal(beside: RemovalHistory.defaultURL))}- /// For tests. Unless a test passes its own, it stops no launchd job, forgets no preference domain, and acts- /// as if there were no helper.+ /// For tests. Unless a test passes its own, it stops no launchd job, forgets no preference domain, acts as if+ /// there were no helper, and writes no journal.init(environment: SearchEnvironment,exclusions: Exclusions = .none,@@ -128,6 +131,7 @@ public struct TrashService: Sendable {forgetDomains: @escaping ForgetDomains = { _, _ in },moveThroughHelper: @escaping MoveThroughHelper = Self.asIfThereWereNoHelper,ownMoves: OwnTrashMoves = OwnTrashMoves(),+ journal: RemovalJournal? = nil,moveToTrash: @escaping @Sendable (URL) throws -> URL) {self.init(@@ -137,7 +141,8 @@ public struct TrashService: Sendable {forgetDomains: forgetDomains,moveThroughHelper: moveThroughHelper,moveToTrash: moveToTrash,- ownMoves: ownMoves+ ownMoves: ownMoves,+ journal: journal)}@@ -148,7 +153,8 @@ public struct TrashService: Sendable {forgetDomains: @escaping ForgetDomains,moveThroughHelper: @escaping MoveThroughHelper,moveToTrash: @escaping @Sendable (URL) throws -> URL,- ownMoves: OwnTrashMoves+ ownMoves: OwnTrashMoves,+ journal: RemovalJournal?) {self.environment = environmentself.removalGuard = removalGuard@@ -157,6 +163,7 @@ public struct TrashService: Sendable {self.moveThroughHelper = moveThroughHelperself.moveToTrash = moveToTrashself.ownMoves = ownMoves+ self.journal = journal}/// Why the guard would refuse to move `url`, or nil when it would move. A plan can show this before anything@@ -170,6 +177,18 @@ public struct TrashService: Sendable {/// is forgotten even when Apple wrote the app.@concurrentpublic func trash(_ urls: [URL], ownedBy owner: String? = nil) async -> TrashResult {+ await trash(urls, ownedBy: owner, removal: UUID())+ }++ /// Moves files Peel made for itself, which History never lists, such as a copy of settings it could not+ /// finish: to the Trash like anything else, with nothing written down for History.+ @concurrent+ func trashOwnFiles(_ urls: [URL]) async -> TrashResult {+ await trash(urls, ownedBy: nil, removal: nil)+ }++ /// Moves `urls` as the current user, as part of `removal`, the move the journal groups them by, or of none.+ private func trash(_ urls: [URL], ownedBy owner: String?, removal: UUID?) async -> TrashResult {// The guard reads the disk, so it is asked once for each item.let allowed = urls.filter(removalGuard.allowsRemoval(of:))let isAllowed = Set(allowed)@@ -188,8 +207,11 @@ public struct TrashService: Sendable {let path = PathPattern.comparablePath(of: url)guard !moved.contains(where: { PathComponents.isPath(path, inside: $0) }) else { continue }do {- let trashedURL = try moveToTrash(url)- result.trashed.append(TrashedItem(originalURL: url, trashedURL: trashedURL, date: .now))+ let item = TrashedItem(originalURL: url, trashedURL: try moveToTrash(url), date: .now)+ if let removal {+ journal?.note([item], batch: removal)+ }+ result.trashed.append(item)moved.append(path)} catch {result.failures.append(TrashFailure(url: url, reason: Self.reason(for: error)))@@ -203,7 +225,8 @@ public struct TrashService: Sendable {/// Moves `privilegedURLs` through the privileged helper and everything else as the current user.@concurrentpublic func trash(_ urls: [URL], usingHelperFor privilegedURLs: Set<URL>) async -> TrashResult {- var result = await trash(urls.filter { !privilegedURLs.contains($0) })+ let removal = UUID()+ var result = await trash(urls.filter { !privilegedURLs.contains($0) }, ownedBy: nil, removal: removal)let helperURLs = urls.filter(privilegedURLs.contains)guard !helperURLs.isEmpty else { return result }@@ -226,6 +249,7 @@ public struct TrashService: Sendable {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)helperResult.trashed += moved.trashedhelperResult.failures += moved.failures}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swiftindex c5fb34fc..684424b8 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift@@ -137,6 +137,28 @@ struct HistoryCommandTests {#expect(listedParts.compactMap { $0["tool"] as? String } == ["developer", "projects", "duplicates"])}+ /// A removal cut short comes back into History with no tool, since which tool moved it is not known, and+ /// `--json` says so with null rather than a name.+ @Test func anInterruptedRemovalNamesNoTool() async throws {+ let directory = try TemporaryDirectory()+ let logs = logs(in: directory)+ let item = TrashedItem(+ originalURL: directory.url.appending(path: "home/Library/Caches/com.example.app"),+ trashedURL: directory.url.appending(path: "home/.Trash/com.example.app"),+ date: .now+ )+ let writer = RemovalJournal.Writer(pid: 0, started: 0)+ _ = await logs.removals.add([RemovalJournal.Entry(writer: writer, batch: UUID(), item: item).interruptedRecord])++ let removal = try #require(try listed(try await printed(["history", "--json"], from: logs)).first as? [String: Any])++ #expect(removal["source"] as? String == "Interrupted removal")+ #expect(removal["tool"] is NSNull)+ let parts = try #require(removal["parts"] as? [[String: Any]])+ #expect(parts.count == 1)+ #expect(parts.first?["tool"] is NSNull)+ }+/// With nothing recorded, the command prints a sentence rather than an empty table, and `--json` prints an/// empty list.@Test func saysSoWhenThereIsNothingToList() async throws {diff --git a/Packages/PeelCore/Tests/PeelCoreTests/PeelFolderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/PeelFolderTests.swiftindex 23392971..e1c84185 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/PeelFolderTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/PeelFolderTests.swift@@ -8,7 +8,8 @@ struct PeelFolderTests {let folder = PathPattern.comparablePath(of: PeelFolder.url)let written = [TeamRegistry.defaultURL, ExclusionStore.defaultURL, AppMemory.defaultURL,- RemovalHistory.defaultURL, RemovalHistory.refusalsURL, PreferenceBackup.defaultDirectory,+ RemovalHistory.defaultURL, RemovalHistory.refusalsURL, RemovalJournal(beside: RemovalHistory.defaultURL).url,+ PreferenceBackup.defaultDirectory, DigestMemory.defaultURL,]for url in written {#expect(PathPattern.comparablePath(of: url.deletingLastPathComponent()) == folder, "\(url.path(percentEncoded: false))")diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swiftindex f498e6bc..f80b83d1 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift@@ -4,6 +4,11 @@ import Synchronizationimport Testingstruct RemovalHygieneTests {+ /// A service for the saves and restores below, which move nothing of their own: one that did would be refused.+ private static let service = TrashService(+ environment: SearchEnvironment(homeDirectory: URL(filePath: "/nonexistent/home"), rootDirectory: URL(filePath: "/nonexistent/root"))+ ) { _ in throw CocoaError(.fileWriteNoPermission) }+private let home = URL(filePath: "/Users/x", directoryHint: .isDirectory)/// The host UUID of the Mac these tests pretend to run on, as its ByHost file names carry it.private let host = "0A1B2C3D-4E5F-6071-8293-A4B5C6D7E8F9"@@ -207,9 +212,9 @@ struct RemovalHygieneTests {return .no}- #expect(await PreferenceBackup.save([URL.homeDirectory.appending(path: "Library/Caches/com.example.app")], for: app, in: backups, run: notThere) == .nothingToSave)+ #expect(await PreferenceBackup.save([URL.homeDirectory.appending(path: "Library/Caches/com.example.app")], for: app, in: backups, through: Self.service, run: notThere) == .nothingToSave)#expect(asked.withLock { $0 }.isEmpty, "a folder that names no domain was asked about")- #expect(await PreferenceBackup.save([plist], for: app, in: backups, run: notThere) == .nothingToSave)+ #expect(await PreferenceBackup.save([plist], for: app, in: backups, through: Self.service, run: notThere) == .nothingToSave)#expect(asked.withLock { $0 } == [["read", "com.example.app"]])#expect(try FileManager.default.contentsOfDirectory(atPath: backups.path(percentEncoded: false)).isEmpty)}@@ -230,7 +235,7 @@ struct RemovalHygieneTests {return Self.exporting(arguments)}- let restored = await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, run: run)+ let restored = await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, through: Self.service, run: run)#expect(restored == .incomplete(clearedSome: false), "a copy that could not be read was reported as put back, or as cleared")let commands = asked.withLock { $0 }@@ -239,7 +244,7 @@ struct RemovalHygieneTests {// An import that fails after the delete has cleared that domain, and the result says so.let failingImport: PreferenceBackup.Run = { arguments in arguments.first == "import" ? .no : Self.exporting(arguments) }- #expect(await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, run: failingImport) == .incomplete(clearedSome: true))+ #expect(await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, through: Self.service, run: failingImport) == .incomplete(clearedSome: true))}/// Put Back replaces what the app set since the reset, which may be a license entered again, so those settings@@ -256,7 +261,7 @@ struct RemovalHygieneTests {return Self.exporting(arguments)}- let restored = await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, run: run)+ let restored = await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, through: Self.service, run: run)#expect(restored == .complete)let copies = PreferenceBackup.copies(in: backups)@@ -280,7 +285,7 @@ struct RemovalHygieneTests {return arguments.first == "export" ? .no : .yes}- #expect(await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, run: run) == .notSaved)+ #expect(await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, through: Self.service, run: run) == .notSaved)#expect(!asked.withLock { $0 }.contains { $0.first == "delete" || $0.first == "import" })}@@ -300,11 +305,11 @@ struct RemovalHygieneTests {let app = InstalledApp(url: URL(filePath: "/Applications/Example.app"), bundleIdentifier: "com.example.app", name: "Example")let plist = URL.homeDirectory.appending(path: "Library/Preferences/com.example.app.plist")- let refused = await PreferenceBackup.save([plist], for: app, in: backups) { arguments in arguments.contains("export") ? .no : .yes }+ let refused = await PreferenceBackup.save([plist], for: app, in: backups, through: Self.service) { arguments in arguments.contains("export") ? .no : .yes }#expect(refused == .failed)#expect(try FileManager.default.contentsOfDirectory(atPath: backups.path(percentEncoded: false)).isEmpty, "an empty copy was left behind")- let nothing = await PreferenceBackup.save([URL(filePath: "/Users/x/Library/Caches/com.example.app")], for: app, in: backups) { _ in .yes }+ let nothing = await PreferenceBackup.save([URL(filePath: "/Users/x/Library/Caches/com.example.app")], for: app, in: backups, through: Self.service) { _ in .yes }#expect(nothing == .nothingToSave)}@@ -316,7 +321,7 @@ struct RemovalHygieneTests {let app = InstalledApp(url: URL(filePath: "/Applications/Example.app"), bundleIdentifier: "com.example.app", name: "Example")let plist = URL.homeDirectory.appending(path: "Library/Preferences/com.example.app.plist")- let saved = await PreferenceBackup.save([plist], for: app, in: backups) { arguments in+ let saved = await PreferenceBackup.save([plist], for: app, in: backups, through: Self.service) { arguments inarguments.first == "read" ? .noAnswer : .yes}@@ -338,8 +343,8 @@ struct RemovalHygieneTests {return .yes}- let first = await PreferenceBackup.save([plist], for: app, in: backups, run: exporting)- let second = await PreferenceBackup.save([plist], for: app, in: backups, run: exporting)+ let first = await PreferenceBackup.save([plist], for: app, in: backups, through: Self.service, run: exporting)+ let second = await PreferenceBackup.save([plist], for: app, in: backups, through: Self.service, run: exporting)guard case .saved(let one) = first, case .saved(let two) = second else {Issue.record("a copy was not made: \(first), \(second)")diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swiftnew file mode 100644index 00000000..72013268--- /dev/null+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift@@ -0,0 +1,198 @@+import Foundation+@testable import PeelCore+import Synchronization+import Testing++struct RemovalJournalTests {+ /// An item that moved at a time with more than milliseconds in it, as every real move has.+ private func item(_ name: String, in directory: borrowing TemporaryDirectory, at date: Date = Date(timeIntervalSince1970: 1_800_000_000.002_07)) -> TrashedItem {+ TrashedItem(+ originalURL: directory.url.appending(path: "home/Library/Caches/\(name)"),+ trashedURL: directory.url.appending(path: "home/.Trash/\(name)"),+ date: date+ )+ }++ /// A process that has ended, which no running process can be taken for.+ private func endedProcess() throws -> RemovalJournal.Writer {+ let process = Process()+ process.executableURL = URL(filePath: "/usr/bin/true")+ try process.run()+ process.waitUntilExit()+ return RemovalJournal.Writer(pid: process.processIdentifier, started: 1)+ }++ private func service(in directory: borrowing TemporaryDirectory, journal: RemovalJournal, moveToTrash: @escaping @Sendable (URL) throws -> URL) -> TrashService {+ TrashService(+ environment: SearchEnvironment(homeDirectory: directory.url.appending(path: "home"), rootDirectory: directory.url.appending(path: "root")),+ journal: journal,+ moveToTrash: moveToTrash+ )+ }++ /// A removal cut short by a quit or a crash left what it moved in the journal: the next read of History takes it+ /// in, as an interrupted removal, and the journal lets it go.+ @Test func aRemovalCutShortIsInHistoryTheNextTimeItIsRead() async throws {+ let directory = try TemporaryDirectory()+ let history = directory.url.appending(path: "Peel/removals.json")+ let journal = RemovalJournal(beside: history)+ let moved = item("com.example.app", in: directory)+ journal.note([moved], batch: UUID(), by: try endedProcess())++ let records = try #require(await RemovalLog(url: history).load().records)++ #expect(records.map(\.trashedURL) == [moved.trashedURL])+ #expect(records.first?.sourceKey == "interrupted")+ #expect(records.first?.size == nil)+ #expect(journal.entries().isEmpty)+ }++ /// 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 {+ let directory = try TemporaryDirectory()+ let history = directory.url.appending(path: "Peel/removals.json")+ let journal = RemovalJournal(beside: history)+ let moved = item("com.example.app", in: directory)+ journal.note([moved], batch: UUID(), by: .current)++ #expect(await RemovalLog(url: history).load().records?.isEmpty == true)+ #expect(journal.entries().count == 1)++ let record = RemovalRecord(batch: UUID(), item: moved, size: 10, source: "Editor", tool: "applications")+ _ = await RemovalLog(url: history).add([record])++ #expect(journal.entries().isEmpty, "History recorded the item and the journal kept it")+ }++ /// An item History already has is never recorded twice, as when a process wrote History and stopped before it+ /// could let the journal go.+ @Test func anItemHistoryHasIsNotRecordedAgain() async throws {+ let directory = try TemporaryDirectory()+ let history = directory.url.appending(path: "Peel/removals.json")+ let journal = RemovalJournal(beside: history)+ let moved = item("com.example.app", in: directory)+ let record = RemovalRecord(batch: UUID(), item: moved, size: 10, source: "Editor", tool: "applications")+ _ = await RemovalLog(url: history).add([record])+ journal.note([moved], batch: UUID(), by: try endedProcess())++ let records = try #require(await RemovalLog(url: history).load().records)++ #expect(records.map(\.id) == [record.id])+ #expect(journal.entries().isEmpty)+ }++ /// An older record at the same places, an item that moved there before and has left the Trash since, is+ /// another item: the one that moved now is still recorded.+ @Test func anOlderItemAtTheSamePlacesIsAnotherItem() async throws {+ let directory = try TemporaryDirectory()+ let history = directory.url.appending(path: "Peel/removals.json")+ let journal = RemovalJournal(beside: history)+ let older = item("com.example.app", in: directory)+ _ = await RemovalLog(url: history).add([RemovalRecord(batch: UUID(), item: older, size: 10, source: "Editor", tool: "applications")])+ let newer = item("com.example.app", in: directory, at: older.date.addingTimeInterval(86_400))+ journal.note([newer], batch: UUID(), by: try endedProcess())++ let records = try #require(await RemovalLog(url: history).load().records)++ #expect(records.count == 2)+ #expect(records.contains { $0.sourceKey == "interrupted" && LogTime.text(for: $0.date) == LogTime.text(for: newer.date) })+ }++ /// Each item is written down as soon as it has moved, before the next one moves and before the service returns,+ /// so a stop at any point loses no more than the item moving then.+ @Test func theServiceWritesDownEachItemAsItMoves() async throws {+ let directory = try TemporaryDirectory()+ let first = try directory.file("home/Library/Caches/com.example.one/cache.db").deletingLastPathComponent()+ let second = try directory.file("home/Library/Caches/com.example.two/cache.db").deletingLastPathComponent()+ let journal = RemovalJournal(beside: directory.url.appending(path: "Peel/removals.json"))+ let written = Mutex<[Int]>([])+ let service = service(in: directory, journal: journal) { url in+ written.withLock { $0.append(journal.entries().count) }+ return directory.url.appending(path: "home/.Trash/\(url.lastPathComponent)")+ }++ let result = await service.trash([first, second])++ #expect(result.trashed.count == 2)+ #expect(written.withLock { $0 } == [0, 1], "the first item was not written down before the second moved")+ #expect(Set(journal.entries().map(\.item.trashedURL)) == Set(result.trashed.map(\.trashedURL)))+ }++ /// Peel's folder holds the journal, and Peel moves it last as it removes itself: that move is not written+ /// down, since History went with the folder and writing the line would make the folder again.+ @Test func theMoveOfTheFolderThatHoldsTheJournalMakesNothingAgain() async throws {+ let directory = try TemporaryDirectory()+ let folder = try directory.directory("home/Library/Application Support/Peel")+ let trash = try directory.directory("home/.Trash")+ let journal = RemovalJournal(beside: folder.appending(path: "removals.json"))+ let service = service(in: directory, journal: journal) { url in+ let landed = trash.appending(path: url.lastPathComponent)+ try FileManager.default.moveItem(at: url, to: landed)+ return landed+ }++ let result = await service.trash([folder])++ #expect(result.trashed.map(\.originalURL) == [folder])+ #expect(!FileManager.default.fileExists(atPath: folder.path(percentEncoded: false)), "the folder was made again")+ }++ /// A copy of settings Peel could not finish goes to the Trash as its own file, which History never lists, so+ /// nothing is written down that would come back later as an interrupted removal.+ @Test func aCopyPeelCouldNotFinishIsNotWrittenDown() async throws {+ let directory = try TemporaryDirectory()+ let backups = try directory.directory("home/Library/Application Support/Peel/Preference Backups")+ let journal = RemovalJournal(beside: directory.url.appending(path: "home/Library/Application Support/Peel/removals.json"))+ let moved = Mutex<[URL]>([])+ let service = service(in: directory, journal: journal) { url in+ moved.withLock { $0.append(url) }+ return directory.url.appending(path: "home/.Trash/\(url.lastPathComponent)")+ }+ let app = InstalledApp(url: URL(filePath: "/Applications/Example.app"), bundleIdentifier: "com.example.app", name: "Example")+ let preferences = URL.homeDirectory.appending(path: "Library/Preferences")+ let exports = Mutex(0)++ // The first domain is copied, the second is not, so the copy is left half made.+ let saved = await PreferenceBackup.save(+ [preferences.appending(path: "com.example.app.plist"), preferences.appending(path: "com.example.app.helper.plist")],+ for: app,+ in: backups,+ through: service+ ) { arguments in+ guard arguments.first == "export", let file = arguments.last else { return .yes }+ guard exports.withLock({ $0 += 1; return $0 }) == 1 else { return .no }+ FileManager.default.createFile(atPath: file, contents: Data())+ return .yes+ }++ #expect(saved == .failed)+ #expect(moved.withLock { $0.count } == 1, "the half made copy did not go to the Trash")+ #expect(journal.entries().isEmpty)+ }++ /// A line cut short, as when the disk filled up, costs only itself: the next line starts on a line of its own.+ @Test func aLineCutShortCostsOnlyItself() throws {+ let directory = try TemporaryDirectory()+ let journal = RemovalJournal(beside: directory.url.appending(path: "Peel/removals.json"))+ let moved = item("com.example.app", in: directory)+ try FileManager.default.createDirectory(at: journal.url.deletingLastPathComponent(), withIntermediateDirectories: true)+ try Data(#"{"batch":"#.utf8).write(to: journal.url)++ journal.note([moved], batch: UUID())++ #expect(journal.entries().map(\.item.trashedURL) == [moved.trashedURL])+ }++ /// With no journal there is nothing to take in: reading it or letting lines go makes nothing on the disk.+ @Test func withNoJournalNothingIsMade() throws {+ let directory = try TemporaryDirectory()+ let folder = directory.url.appending(path: "Peel")+ let journal = RemovalJournal(beside: folder.appending(path: "removals.json"))++ #expect(journal.entries().isEmpty)+ journal.forget([item("com.example.app", in: directory)])++ #expect(!FileManager.default.fileExists(atPath: folder.path(percentEncoded: false)))+ }+}diff --git a/Peel/Model/RemovalHistoryStore.swift b/Peel/Model/RemovalHistoryStore.swiftindex 70b235e1..824e567e 100644--- a/Peel/Model/RemovalHistoryStore.swift+++ b/Peel/Model/RemovalHistoryStore.swift@@ -40,6 +40,8 @@ struct RemovalBatch: Identifiable, Hashable {static func title(source: String, key: String?, tool: Tool?) -> String {guard let key else { return source }if key == "tool", let tool { return String(localized: tool.title) }+ // What a removal moved before Peel quit or stopped, taken into History the next time it was read.+ if key == "interrupted" { return String(localized: "Interrupted Removal") }if key.hasPrefix("space."), let words = SpaceItem.words(for: String(key.dropFirst("space.".count))) {return String(localized: words.title)}diff --git a/SAFETY.md b/SAFETY.mdindex 44fd54cb..3a69d3bd 100644--- a/SAFETY.md+++ b/SAFETY.md@@ -107,8 +107,10 @@ link moves the link.Peel never deletes anything of yours. Everything it removes goes to the Trash, and everything is recorded soHistory can put it back; History keeps the most recent 20,000 items, and what it forgets is still in the Trash.That includes an installer receipt you ask Peel to forget: the two files that make it up go to the Trash-through the helper, where `pkgutil --forget` would have discarded them for good. The space is freed when *you*-empty the Trash: Peel never does that for you.+through the helper, where `pkgutil --forget` would have discarded them for good. Each item is written down the+moment it moves, so a removal cut short by a quit or a crash still reaches History, as an interrupted removal,+the next time Peel or `peel` opens it. The space is freed when *you* empty the Trash: Peel never does+that for you.The one file Peel deletes outright is its own list of refusals, and only when `peel history --refused --clear` isasked to forget it.