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.

peel moves and puts back nothing while the exclusions can't be read, and says why

Țugui Dragoș0faa69748308
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
index ec92ea66..376c694f 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
@@ -291,6 +291,9 @@ struct RestoreCommand: AsyncParsableCommand {
}
func run(in log: RemovalLog, using service: TrashService) async throws {
+ guard service.knowsTheExclusions else {
+ throw AppLookup.Failure.exclusionsUnreadable(ExclusionStore.defaultURL.path(percentEncoded: false))
+ }
let outcome = await log.load()
guard let records = outcome.records else {
throw CommandFailure("Peel couldn't read its History.\(outcome.problem.map { " \($0.summary)" } ?? "")")
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift
index 12443aa8..72302b75 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/Cleanup.swift
@@ -39,7 +39,10 @@ struct Cleanup {
Item(
url: $0.url,
size: $0.size,
- refusal: needingAdministrator.contains($0.url) ? .needsHelper : service.refusal(of: $0.url)
+ // While the exclusions can't be read the guard refuses everything, which says nothing of the
+ // item itself: `run` then moves nothing and names the list.
+ refusal: needingAdministrator.contains($0.url)
+ ? .needsHelper : service.knowsTheExclusions ? service.refusal(of: $0.url) : nil
)
},
source: source,
@@ -80,6 +83,9 @@ struct Cleanup {
Output.line(moving.isEmpty ? "Nothing here can be moved." : "Dry run: nothing was moved.")
return
}
+ guard service.knowsTheExclusions else {
+ throw AppLookup.Failure.exclusionsUnreadable(ExclusionStore.defaultURL.path(percentEncoded: false))
+ }
// Refused items never reach the move, so their failures are built here and recorded, just as the app
// records the refusals its service reports.
let refused = items.compactMap { item in item.refusal.map { TrashFailure(url: item.url, reason: $0) } }
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
index c0116960..ef4edfeb 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
@@ -14,6 +14,8 @@ struct RemovalGuard: Sendable {
private let protectedTrees: [[String]]
private let protectedObjects: ProtectedObjects
private let exclusions: Exclusions
+
+ var knowsTheExclusions: Bool { exclusions.isKnown }
private let home: String
init(environment: SearchEnvironment, exclusions: Exclusions = .none) {
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index 00d5176f..f9058953 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -236,6 +236,9 @@ public struct TrashService: Sendable {
/// Why the guard would refuse to move `url`, or nil when it would move. A plan can show this before anything
/// moves, since the move asks the same guard.
+ /// False while the saved exclusions are not read yet or cannot be read, when nothing moves.
+ public var knowsTheExclusions: Bool { removalGuard.knowsTheExclusions }
+
public func refusal(of url: URL) -> TrashFailure.Reason? {
guard removalGuard.allowsRemoval(of: url) else { return .protectedLocation }
return historyCanBeRead ? nil : .historyUnreadable
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CleanupTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CleanupTests.swift
index ee2fbc39..231ca212 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/CleanupTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/CleanupTests.swift
@@ -7,13 +7,16 @@ import Testing
/// Tests `Cleanup`, the one path every `peel` command that removes files goes through.
struct CleanupTests {
- private func service(in directory: borrowing TemporaryDirectory, onMove: @escaping @Sendable () -> Void = {}) throws -> TrashService {
+ private func service(
+ in directory: borrowing TemporaryDirectory, exclusions: Exclusions = .none,
+ onMove: @escaping @Sendable () -> Void = {}
+ ) throws -> TrashService {
let trash = try directory.directory("Trash")
let environment = SearchEnvironment(
homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),
rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)
)
- return TrashService(environment: environment) { url in
+ return TrashService(environment: environment, exclusions: exclusions) { url in
onMove()
let destination = trash.appending(path: UUID().uuidString)
try FileManager.default.moveItem(at: url, to: destination)
@@ -38,6 +41,29 @@ struct CleanupTests {
#expect(plan.total.known == 20)
}
+ /// While the saved exclusions can't be read nothing moves, and no item is blamed on a protected location or
+ /// recorded as refused: the cause is the list, which the command names.
+ @Test func movesNothingAndBlamesNoItemWhileTheExclusionsCannotBeRead() async throws {
+ let directory = try TemporaryDirectory()
+ let cache = try directory.file("home/Library/Caches/com.example.editor/blob", bytes: 20)
+ let service = try service(in: directory, exclusions: .unreadable)
+ let refusals = RefusalLog(url: directory.url.appending(path: "Peel/refusals.json"))
+ let removals = RemovalLog(url: directory.url.appending(path: "Peel/removals.json"))
+ let plan = cleanup(items: [(cache, 20)], service: service)
+
+ #expect(plan.items.allSatisfy { $0.refusal == nil })
+ await #expect(throws: AppLookup.Failure.self) {
+ try await Output.$collected.withValue(Output.Collected()) {
+ try await plan.run(
+ question: "?", dryRun: false, yes: true, using: service,
+ recordingIn: removals, refusals: refusals
+ )
+ }
+ }
+ #expect(FileManager.default.fileExists(atPath: cache.path(percentEncoded: false)))
+ #expect(await refusals.load().isEmpty)
+ }
+
/// While History cannot be read nothing moves, and the command says so above the list, with the way out.
@Test(.permissionsHold) func saysSoWhenHistoryCannotBeRead() async throws {
let directory = try TemporaryDirectory()
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
index 194a919e..6ec1e584 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
@@ -302,6 +302,18 @@ struct HistoryCommandTests {
}
}
+ @Test func putsNothingBackWhileTheExclusionsCannotBeRead() async throws {
+ let directory = try TemporaryDirectory()
+ let logs = logs(in: directory)
+ let removal = try removal(in: directory)
+ _ = await logs.removals.add([removal.record])
+
+ await #expect(throws: AppLookup.Failure.self) {
+ try await (command(["restore", String(removal.record.batch.uuidString.prefix(8)), "-y"]) as RestoreCommand)
+ .run(in: logs.removals, using: TrashService(exclusions: .unreadable))
+ }
+ }
+
@Test func refusesAnIdentifierThatNamesNothing() async throws {
let directory = try TemporaryDirectory()
let logs = logs(in: directory)