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.

Word what macOS reported in the reader's language, so peel stays English

Țugui Dragoș8df6424eb7f1
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
index 890643d3..c26fdcca 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/HistoryCommands.swift
@@ -234,7 +234,7 @@ struct HistoryCommand: AsyncParsableCommand {
/// Why an item stayed, in the words the rest of `peel` uses. A reason a newer Peel wrote reads as recorded.
static func why(_ record: RefusalRecord) -> String {
- TrashFailure.Reason(name: record.reason, detail: record.detail)?.summary ?? record.detail ?? record.reason
+ record.failureReason?.summary ?? record.detail ?? record.reason
}
private func listRefusals(in log: RefusalLog, trash: TrashService) async throws {
@@ -268,7 +268,8 @@ struct HistoryCommand: AsyncParsableCommand {
tool: removal.parts.count == 1 ? removal.parts[0].namedTool : nil,
items: removal.records.map { record in
RefusedRemoval.Item(
- path: Output.path(record.url), reason: record.reason, detail: record.detail,
+ path: Output.path(record.url), reason: record.reason,
+ detail: record.failureReason?.detail ?? record.detail,
why: Self.why(record)
)
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RefusalLog.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RefusalLog.swift
index 407bb8c6..d3acccee 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RefusalLog.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RefusalLog.swift
@@ -10,6 +10,7 @@ public struct RefusalRecord: Sendable, Codable, Hashable, Identifiable {
/// `TrashFailure.Reason.name`: a word, not the sentence shown on screen, so later versions can still read it.
public let reason: String
public let detail: String?
+ public let error: SystemError?
public let date: Date
public let source: String
/// A key for a source Peel names itself, as in `RemovalRecord.sourceKey`.
@@ -29,6 +30,7 @@ public struct RefusalRecord: Sendable, Codable, Hashable, Identifiable {
url = failure.url
reason = failure.reason.name
detail = failure.reason.detail
+ error = failure.error
self.date = date
self.source = source
self.sourceKey = sourceKey
@@ -36,6 +38,15 @@ public struct RefusalRecord: Sendable, Codable, Hashable, Identifiable {
}
}
+extension RefusalRecord {
+ /// Why the item stayed, with what macOS reported worded in the reader's language (`SystemError`), or nil for a
+ /// word this version doesn't know.
+ public var failureReason: TrashFailure.Reason? {
+ if reason == "failed", let error { return .failed(error.description(of: url)) }
+ return TrashFailure.Reason(name: reason, detail: detail)
+ }
+}
+
/// What went wrong reading or writing the refusal log, for History to say.
public enum RefusalLogProblem: Sendable, Equatable {
/// The file could not be decoded. It was set aside under this name, and the rows that could be read were kept.
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/SystemError.swift b/Packages/PeelCore/Sources/PeelCore/Removal/SystemError.swift
new file mode 100644
index 00000000..b8f77fff
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/SystemError.swift
@@ -0,0 +1,24 @@
+public import Foundation
+
+/// An error macOS reported, kept by its domain and code rather than its words, which are in the language of the
+/// process that met it: each reader words it again in its own.
+public struct SystemError: Sendable, Hashable, Codable {
+ public let domain: String
+ public let code: Int
+
+ public init(domain: String, code: Int) {
+ self.domain = domain
+ self.code = code
+ }
+
+ public init(_ error: any Error) {
+ let error = error as NSError
+ self.init(domain: error.domain, code: error.code)
+ }
+
+ /// What macOS says of this error for the item at `url`, in the language of the process asking.
+ public func description(of url: URL) -> String {
+ NSError(domain: domain, code: code, userInfo: [NSFilePathErrorKey: url.path(percentEncoded: false)])
+ .localizedDescription
+ }
+}
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index a53c5742..58e3288c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -105,10 +105,12 @@ public struct TrashFailure: Sendable, Hashable {
public let url: URL
public let reason: Reason
+ public let error: SystemError?
- public init(url: URL, reason: Reason) {
+ public init(url: URL, reason: Reason, error: SystemError? = nil) {
self.url = url
self.reason = reason
+ self.error = error
}
}
@@ -513,7 +515,8 @@ public struct TrashService: Sendable {
if Self.foundNothing(error), url.isMissing { continue }
let reason = Self.reason(for: error)
let isLocked = reason == .notPermitted && FileAccess.isLockedInFinder(url)
- result.failures.append(TrashFailure(url: url, reason: isLocked ? .locked : reason))
+ let code: SystemError? = if case .failed = reason { SystemError(error) } else { nil }
+ result.failures.append(TrashFailure(url: url, reason: isLocked ? .locked : reason, error: code))
}
}
ownMoves.ended(landedAt: result.trashed.map(\.trashedURL))
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
index 21218eae..2f31c0c4 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HistoryCommandTests.swift
@@ -353,6 +353,23 @@ struct HistoryCommandTests {
#expect(FileManager.default.fileExists(atPath: trash.appending(path: "refusals.json").path(percentEncoded: false)))
}
+ @Test func printsWhatMacOSReportedInEnglishWhateverLanguageWroteIt() async throws {
+ let directory = try TemporaryDirectory()
+ let logs = logs(in: directory)
+ let romanian = "Operația nu a putut fi efectuată. Sistem de fișiere doar citire"
+ let error = SystemError(domain: NSPOSIXErrorDomain, code: Int(EROFS))
+ await logs.refusals.add(
+ [TrashFailure(url: URL(filePath: "/Volumes/Old/report.pdf"), reason: .failed(romanian), error: error)],
+ source: "Editor", tool: "applications"
+ )
+
+ let text = try await printed(["history", "--refused"], from: logs)
+ let json = try await printed(["history", "--refused", "--json"], from: logs)
+
+ #expect(text.contains("Read-only file system") && !text.contains("Operația"))
+ #expect(json.contains("Read-only file system") && !json.contains("Operația"))
+ }
+
@Test func clearsACopySetAsideWhenNothingElseIsLeft() async throws {
let directory = try TemporaryDirectory()
let logs = logs(in: directory)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RefusalLogTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RefusalLogTests.swift
index 6f4349ae..11983d89 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RefusalLogTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RefusalLogTests.swift
@@ -171,6 +171,29 @@ struct RefusalLogTests {
#expect(second.problem == .damaged(setAside: setAside), "the copy set aside is the only trace of it")
}
+ @Test func aReasonMacOSGaveIsWordedInTheReadersLanguage() async throws {
+ let directory = try TemporaryDirectory()
+ let log = RefusalLog(url: directory.url.appending(path: "Peel/refusals.json"))
+ let item = URL(filePath: "/Volumes/Old/report.pdf")
+ let error = SystemError(domain: NSPOSIXErrorDomain, code: Int(EROFS))
+ let romanian = TrashFailure.Reason.failed("Operația nu a putut fi efectuată. Sistem de fișiere doar citire")
+ await log.add([TrashFailure(url: item, reason: romanian, error: error)], source: "Editor", tool: "applications")
+
+ let record = try #require(await RefusalLog(url: log.url).load().records?.first)
+
+ #expect(record.error == error)
+ #expect(record.failureReason == .failed(error.description(of: item)))
+ #expect(record.failureReason != romanian, "read in the words of the process that wrote it")
+ }
+
+ @Test func aReasonWrittenWithoutItsCodeReadsAsWritten() async throws {
+ let directory = try TemporaryDirectory()
+ let log = RefusalLog(url: directory.url.appending(path: "Peel/refusals.json"))
+ await log.add([failure("/Users/me/a", .failed("disk full"))], source: "Editor", tool: "applications")
+
+ #expect(try #require(await log.load().records?.first).failureReason == .failed("disk full"))
+ }
+
@Test func forgetsTheDamageOnceItsCopyIsGone() async throws {
let directory = try TemporaryDirectory()
let url = try directory.directory("Peel").appending(path: "refusals.json")
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
index fc0eabf0..296c40c4 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
@@ -282,6 +282,21 @@ struct TrashServiceTests {
#expect(batches.withLock { $0 } == [["Tool.app"], ["tool", "_tool"]])
}
+ @Test func keepsTheCodeOfAnErrorMacOSReported() async throws {
+ let directory = try TemporaryDirectory()
+ let file = try directory.file("home/Library/Caches/org.example.busy/cache.db")
+ let service = TrashService(
+ environment: SearchEnvironment(
+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),
+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)
+ )
+ ) { _ in throw POSIXError(.EBUSY) }
+
+ let result = await service.trashOwnFiles([file])
+
+ #expect(result.failures.map(\.error) == [SystemError(domain: NSPOSIXErrorDomain, code: Int(EBUSY))])
+ }
+
/// An item inside a folder that just moved went with it, whatever its name begins with: it is neither moved
/// again nor a failure. A slash and a combining mark after it are one `Character`, so to a comparison of
/// characters such an item was not inside its folder, and its move failed for a file already in the Trash.
diff --git a/Peel/Views/History/RefusalDetailView.swift b/Peel/Views/History/RefusalDetailView.swift
index d5438717..ee2a8935 100644
--- a/Peel/Views/History/RefusalDetailView.swift
+++ b/Peel/Views/History/RefusalDetailView.swift
@@ -71,6 +71,6 @@ private struct RefusalRecordRow: View {
/// Why the item stayed, in the words Peel shows when it refuses. A reason this version doesn't know is shown
/// as it was stored.
private var explanation: String {
- TrashFailure.Reason(name: record.reason, detail: record.detail)?.explanation ?? record.detail ?? record.reason
+ record.failureReason?.explanation ?? record.detail ?? record.reason
}
}