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.

Watch the Trash leaves alone what peel moved there

Țugui Dragoș4cc476be3024
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift
index d59801eb..26221539 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalLog.swift
@@ -49,6 +49,13 @@ public actor RemovalLog {
whileNoOtherProcessWrites { RemovalLogOutcome(records: current(), problem: problem) }
}
+ /// The places in the Trash where an item Peel moved still is, whichever process moved it: what History records
+ /// and what a removal under way has written down so far. Spelled as `PathPattern.comparablePath` spells them.
+ public func placesInTheTrash() -> Set<String> {
+ let items = (load().records ?? []).map(\.trashedItem) + journal.entries().map(\.item)
+ return Set(items.filter(\.isInTheTrash).map { PathPattern.comparablePath(of: $0.trashedURL) })
+ }
+
public func add(_ records: [RemovalRecord]) -> RemovalLogOutcome {
whileNoOtherProcessWrites {
guard !records.isEmpty else { return RemovalLogOutcome(records: current(), problem: problem) }
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift
index a635d242..5d5d5891 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalJournalTests.swift
@@ -221,4 +221,29 @@ struct RemovalJournalTests {
#expect(!FileManager.default.fileExists(atPath: folder.path(percentEncoded: false)))
}
+
+ /// Every place in the Trash Peel put something, from any process: what History records and what a removal under
+ /// way has written down so far, as long as that item is still there.
+ @Test func knowsWhatPeelPutInTheTrashFromAnyProcess() async throws {
+ let directory = try TemporaryDirectory()
+ let log = RemovalLog(url: directory.url.appending(path: "Peel/removals.json"))
+ let recorded = try directory.directory("Trash/Recorded.app")
+ let underWay = try directory.directory("Trash/UnderWay.app")
+ let emptied = directory.url.appending(path: "Trash/Emptied.app")
+ try directory.directory("Trash/Thrown.app")
+ func item(_ trashed: URL) -> TrashedItem {
+ let original = URL(filePath: "/Applications/\(trashed.lastPathComponent)")
+ return TrashedItem(originalURL: original, trashedURL: trashed, date: .now)
+ }
+ _ = await log.add([
+ RemovalRecord(batch: UUID(), item: item(recorded), size: 1, source: "Recorded", tool: "applications"),
+ RemovalRecord(batch: UUID(), item: item(emptied), size: 1, source: "Emptied", tool: "applications"),
+ ])
+ RemovalJournal(beside: log.url).note([item(underWay)], batch: UUID(), by: .current)
+
+ let places = await log.placesInTheTrash()
+
+ #expect(places == Set([recorded, underWay].map(PathPattern.comparablePath(of:))))
+ }
+
}
diff --git a/Peel/Model/TrashMonitor.swift b/Peel/Model/TrashMonitor.swift
index 49d38c9b..47cdb86c 100644
--- a/Peel/Model/TrashMonitor.swift
+++ b/Peel/Model/TrashMonitor.swift
@@ -105,8 +105,12 @@ final class TrashMonitor {
let arrived = OwnTrashMoves.shared.arrivals(applications, known: knownApplications, in: trashURL, since: look)
else { return }
knownApplications = applications
+ // What Peel put there, from this process or from `peel` in Terminal, is no app the person threw away.
+ let placedByPeel = arrived.isEmpty ? [] : await RemovalLog().placesInTheTrash()
for name in arrived {
- onApplicationTrashed?(trashURL.appending(path: name, directoryHint: .isDirectory))
+ let url = trashURL.appending(path: name, directoryHint: .isDirectory)
+ guard !placedByPeel.contains(PathPattern.comparablePath(of: url)) else { continue }
+ onApplicationTrashed?(url)
}
}
}