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.

Say before putting a removal back which items cannot go back, and why

Țugui Dragoș748cf448d52b
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
index dc504b21..6fe7d4b5 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
@@ -326,33 +326,43 @@ struct RestoreCommand: AsyncParsableCommand {
throw CommandFailure("Peel couldn't read its History.\(outcome.problem.map { " \($0.summary)" } ?? "")")
}
let batch = try Self.batch(matching: id, in: Batch.all(in: records))
- let going = batch.restorable
+ let inTheTrash = batch.restorable
+ let refused = inTheTrash.compactMap { record in
+ service.refusal(ofPuttingBack: record.trashedItem).map { (record: record, failure: $0) }
+ }
+ let going = inTheTrash.filter { record in !refused.contains { $0.record.id == record.id } }
// An item in the Trash of a disk that isn't connected is still there, once the disk is.
let waiting = batch.records.filter { !$0.isStillInTrash && !$0.isOnAConnectedDisk }
let items = Output.count(waiting.count, "item is", "items are")
let elsewhere = waiting.isEmpty
? nil
: "\(items) on a disk that isn't connected. Connect it and run the command again."
- guard !going.isEmpty else {
+ guard !inTheTrash.isEmpty else {
let nothing = "Nothing of that removal is in the Trash anymore, so there is nothing to put back."
throw CommandFailure(elsewhere ?? nothing)
}
- Output.table(going.map { [Output.size($0.size), Output.path($0.originalURL)] })
- Output.line("Total: \(Output.size(going.totalSize))")
- let gone = batch.records.count - going.count - waiting.count
+ if !going.isEmpty {
+ Output.table(going.map { [Output.size($0.size), Output.path($0.originalURL)] })
+ Output.line("Total: \(Output.size(going.totalSize))")
+ }
+ let gone = batch.records.count - inTheTrash.count - waiting.count
if gone > 0 {
let items = Output.count(gone, "item is", "items are")
- Output.note("\(items) no longer in the Trash and will stay as they are.")
+ Output.note("\(items) no longer in the Trash and can't be put back.")
}
if let elsewhere {
Output.note(elsewhere)
}
+ for (record, failure) in refused {
+ Output.note("\(Output.plain(Output.path(record.originalURL))) stays: \(failure.summary)")
+ }
guard !dryRun else {
Output.line("Dry run: nothing was moved.")
return
}
+ guard !going.isEmpty else { throw ExitCode.failure }
if !yes {
try Output.confirm("Put \(Output.count(going.count, "item", "items")) back?")
}
@@ -378,6 +388,6 @@ struct RestoreCommand: AsyncParsableCommand {
for (record, failure) in failures {
Output.note("Couldn't put \(Output.plain(Output.path(record.originalURL))) back: \(failure.summary)")
}
- try Cleanup.end(failed: !failures.isEmpty, recorded: forgotten)
+ try Cleanup.end(failed: !failures.isEmpty || !refused.isEmpty, recorded: forgotten)
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index 552392e9..36eeb2de 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -528,16 +528,22 @@ public struct TrashService: Sendable {
return failure
}
- private func moveBack(_ item: TrashedItem, canUseHelper: Bool) async -> RestoreFailure? {
+ /// Why `item` cannot go back as things stand, or nil when it can.
+ public func refusal(ofPuttingBack item: TrashedItem) -> RestoreFailure? {
// The record comes from a file any process of the user can rewrite, so it is a request, not a fact.
// Putting an item back must not write where removal is refused, or move a file that is not in a Trash.
guard removalGuard.allowsPuttingBack(item.trashedURL, at: item.originalURL), isInATrash(item.trashedURL) else {
return .notAllowed
}
-
- let fileManager = FileManager.default
guard item.isInTheTrash else { return .missingFromTrash }
guard !item.originalURL.isThere else { return .alreadyThere }
+ return nil
+ }
+
+ private func moveBack(_ item: TrashedItem, canUseHelper: Bool) async -> RestoreFailure? {
+ if let refusal = refusal(ofPuttingBack: item) { return refusal }
+
+ let fileManager = FileManager.default
let parent = item.originalURL.deletingLastPathComponent()
// Missing folders are created again, so write access is checked on the nearest folder that exists.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
index 2a26cd16..2575e6e7 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
@@ -457,6 +457,37 @@ struct HistoryCommandTests {
#expect(await logs.removals.load().records?.isEmpty == true)
}
+ @Test func aDryRunNamesWhatCannotGoBackAndWhy() async throws {
+ let directory = try TemporaryDirectory()
+ let logs = logs(in: directory)
+ let taken = try removal(in: directory)
+ try directory.file("home/Documents/report.pdf", bytes: 1)
+ func record(_ name: String) -> RemovalRecord {
+ RemovalRecord(
+ batch: taken.record.batch,
+ item: TrashedItem(
+ originalURL: directory.url.appending(path: "home/Documents/\(name)"),
+ trashedURL: directory.url.appending(path: "home/.Trash/\(name)"),
+ date: .now
+ ),
+ size: 10, source: "Editor", tool: "applications"
+ )
+ }
+ try directory.file("home/.Trash/letter.txt", bytes: 10)
+ _ = await logs.removals.add([taken.record, record("letter.txt"), record("notes.txt")])
+ let collected = Output.Collected()
+
+ try await Output.$collected.withValue(collected) {
+ try await (command(["restore", String(taken.record.batch.uuidString.prefix(8)), "--dry-run"]) as RestoreCommand)
+ .run(in: logs.removals, using: service(in: directory))
+ }
+
+ #expect(collected.output.contains("letter.txt"))
+ #expect(!collected.output.contains("report.pdf"))
+ #expect(collected.notes.contains("report.pdf stays: something is there already"))
+ #expect(collected.notes.contains("1 item is no longer in the Trash and can't be put back."))
+ }
+
/// A file already sits where the item would go back, so the restore cannot finish. It exits 1 and keeps the
/// record.
@Test func aRestoreThatCouldNotFinishExitsOne() async throws {