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.

Leave what needs an administrator out of peel restore's plan

Țugui Dragoș4986bed0f358
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
index a1ebe19e..7528029c 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
@@ -338,7 +338,8 @@ struct RestoreCommand: AsyncParsableCommand {
let batch = try Self.batch(matching: id, in: Batch.all(in: records))
let inTheTrash = batch.restorable
let refused = inTheTrash.compactMap { record in
- service.refusal(ofPuttingBack: record.trashedItem).map { (record: record, failure: $0) }
+ service.refusal(ofPuttingBack: record.trashedItem, canUseHelper: false)
+ .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.
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index dd8d130f..dfadd9dd 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -748,8 +748,9 @@ public struct TrashService: Sendable {
return failure
}
- /// Why `item` cannot go back as things stand, or nil when it can.
- public func refusal(ofPuttingBack item: TrashedItem) -> RestoreFailure? {
+ /// Why `item` cannot go back as things stand, or nil when it can. A way back only the helper can take stays when
+ /// the helper can't be used.
+ public func refusal(ofPuttingBack item: TrashedItem, canUseHelper: Bool) -> 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 move a file that is not in a Trash, or write where removal is refused.
guard isInATrash(item.trashedURL) else { return .notInATrash }
@@ -758,10 +759,12 @@ public struct TrashService: Sendable {
}
guard item.isInTheTrash else { return .missingFromTrash }
guard !item.originalURL.isThere else { return .alreadyThere }
- if Self.needsTheHelper(toPutBack: item),
- let rejection = HelperReach(environment: environment)
- .refusal(ofPuttingBack: item.trashedURL, at: item.originalURL) {
- return .failed(rejection.explanation)
+ if Self.needsTheHelper(toPutBack: item) {
+ let reach = HelperReach(environment: environment)
+ if let rejection = reach.refusal(ofPuttingBack: item.trashedURL, at: item.originalURL) {
+ return .failed(rejection.explanation)
+ }
+ if !canUseHelper { return .needsHelper }
}
return nil
}
@@ -777,7 +780,7 @@ public struct TrashService: Sendable {
}
private func moveBack(_ item: TrashedItem, canUseHelper: Bool) async -> RestoreFailure? {
- if let refusal = refusal(ofPuttingBack: item) { return refusal }
+ if let refusal = refusal(ofPuttingBack: item, canUseHelper: canUseHelper) { return refusal }
if !Self.needsTheHelper(toPutBack: item) {
do {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
index 7a19c1b5..d8c0abc4 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
@@ -250,10 +250,10 @@ struct ExclusionsTests {
let refusal = service.refusal(ofPuttingBack: TrashedItem(
originalURL: applications.appending(path: "Notes.app"), trashedURL: trashed, date: .now
- ))
+ ), canUseHelper: true)
let control = service.refusal(ofPuttingBack: TrashedItem(
originalURL: applications.appending(path: "Other.app"), trashedURL: other, date: .now
- ))
+ ), canUseHelper: true)
#expect(refusal == .notAllowed(.excluded))
#expect(control == nil)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
index a5c1d255..03559a25 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
@@ -516,6 +516,36 @@ struct HistoryCommandTests {
#expect(collected.notes.contains("1 item is no longer in the Trash and can't be put back."))
}
+ @Test(.permissionsHold) func aDryRunLeavesWhatNeedsAnAdministratorOutOfThePlan() async throws {
+ let directory = try TemporaryDirectory()
+ let logs = logs(in: directory)
+ let taken = try removal(in: directory)
+ try directory.directory("home/Library/Caches/Locked")
+ try directory.file("home/.Trash/daemon.plist", bytes: 10)
+ let locked = RemovalRecord(
+ batch: taken.record.batch,
+ item: TrashedItem(
+ originalURL: directory.url.appending(path: "home/Library/Caches/Locked/daemon.plist"),
+ trashedURL: directory.url.appending(path: "home/.Trash/daemon.plist"),
+ date: .now
+ ),
+ size: 10, source: "Editor", tool: "applications"
+ )
+ _ = await logs.removals.add([taken.record, locked])
+ try directory.setPermissions(0o500, of: "home/Library/Caches/Locked")
+ defer { try? directory.setPermissions(0o755, of: "home/Library/Caches/Locked") }
+ 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("report.pdf"))
+ #expect(!collected.output.contains("daemon.plist"))
+ #expect(collected.notes.contains("daemon.plist stays: it needs administrator access, so put it back with the Peel app"))
+ }
+
/// 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 {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
index 2690d96f..6afd70c9 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
@@ -588,10 +588,10 @@ struct TrashServiceTests {
) { $0 }
let item = TrashedItem(originalURL: components.appending(path: "Synth.component"), trashedURL: trashed, date: .now)
- let byThePerson = service.refusal(ofPuttingBack: item)
+ let byThePerson = service.refusal(ofPuttingBack: item, canUseHelper: true)
try directory.setPermissions(0o555, of: components)
defer { try? directory.setPermissions(0o755, of: components) }
- let throughTheHelper = service.refusal(ofPuttingBack: item)
+ let throughTheHelper = service.refusal(ofPuttingBack: item, canUseHelper: true)
#expect(byThePerson == nil)
#expect(throughTheHelper == .failed(PrivilegedPathPolicy.Rejection.loadsCode.explanation))
@@ -612,7 +612,7 @@ struct TrashServiceTests {
defer { try? directory.setPermissions(0o755, of: locked) }
let outside = PrivilegedPathPolicy.Rejection.outsideAllowedLocations.explanation
- #expect(service.refusal(ofPuttingBack: item) == .failed(outside))
+ #expect(service.refusal(ofPuttingBack: item, canUseHelper: true) == .failed(outside))
}
@Test func aMoveRefusedForAnItemLockedInFinderSaysItIsLocked() async throws {