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 why the record of who signs your apps couldn't be read or saved

Țugui Dragoș4069ba1a2dcf
diff --git a/Packages/PeelCore/Sources/PeelCore/Updates/TeamRegistry.swift b/Packages/PeelCore/Sources/PeelCore/Updates/TeamRegistry.swift
index 9c2815cb..00b8d1d1 100644
--- a/Packages/PeelCore/Sources/PeelCore/Updates/TeamRegistry.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Updates/TeamRegistry.swift
@@ -11,10 +11,11 @@ public struct TeamRegistry: Sendable {
/// Why the registry is not keeping its record, which the person is told, since no warning comes meanwhile.
public enum Problem: Sendable, Equatable {
- /// The file cannot be read, so nothing is compared or recorded until the person starts it over.
- case unreadable
+ /// The file cannot be read, so nothing is compared or recorded until the person starts it over; and why, when
+ /// Peel knows.
+ case unreadable(FileProblem?)
/// The file could not be written, so what the last check learned is not kept.
- case unsaved
+ case unsaved(FileProblem)
}
public struct Outcome: Sendable, Equatable {
@@ -58,7 +59,7 @@ public struct TeamRegistry: Sendable {
@concurrent
public func check(_ apps: [InstalledApp], now: Date = .now) async -> Outcome {
FileLock.whileHeld(beside: url) {
- guard let stored = load() else { return Outcome(changes: [], problem: .unreadable) }
+ guard let stored = load() else { return Outcome(changes: [], problem: .unreadable(whyUnreadable())) }
var entries = stored
var changes: [Change] = []
@@ -82,8 +83,8 @@ public struct TeamRegistry: Sendable {
if let pending = entry.pending { changes.append(pending) }
}
- let isSaved = entries == stored || save(entries)
- return Outcome(changes: changes, problem: isSaved ? nil : .unsaved)
+ let failure = entries == stored ? nil : save(entries)
+ return Outcome(changes: changes, problem: failure.map(Problem.unsaved))
}
}
@@ -91,12 +92,12 @@ public struct TeamRegistry: Sendable {
@concurrent
public func acknowledge(_ bundleIdentifier: String) async -> Problem? {
FileLock.whileHeld(beside: url) {
- guard var entries = load() else { return .unreadable }
+ guard var entries = load() else { return .unreadable(whyUnreadable()) }
guard var entry = entries[bundleIdentifier], let pending = entry.pending else { return nil }
entry.acknowledged = (entry.acknowledged ?? []) + [pending.current]
entry.pending = nil
entries[bundleIdentifier] = entry
- return save(entries) ? nil : .unsaved
+ return save(entries).map(Problem.unsaved)
}
}
@@ -114,22 +115,31 @@ public struct TeamRegistry: Sendable {
/// registry would silently accept any change of team made since it was last read.
private func load() -> [String: Entry]? {
guard !url.isMissing else { return [:] }
- guard let data = BoundedRead.data(at: url) else { return nil }
+ return BoundedRead.data(at: url).flatMap(decoded)
+ }
+
+ /// Why `load()` found nothing, asked again by a caller that holds the lock.
+ private func whyUnreadable() -> FileProblem? {
+ guard let data = BoundedRead.data(at: url) else { return BoundedRead.whyUnreadable(url) }
+ return decoded(data) == nil ? .damaged : nil
+ }
+
+ private func decoded(_ data: Data) -> [String: Entry]? {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
return try? decoder.decode([String: Entry].self, from: data)
}
- private func save(_ entries: [String: Entry]) -> Bool {
+ /// Writes `entries`, and answers why it couldn't, or nil once it did.
+ private func save(_ entries: [String: Entry]) -> FileProblem? {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .iso8601
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
do {
- let data = try encoder.encode(entries)
- try PeelFolder.write(data, to: url)
- return true
+ try PeelFolder.write(try encoder.encode(entries), to: url)
+ return nil
} catch {
- return false
+ return .failed(SystemError(error))
}
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
index 306134fc..24d6abc3 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
@@ -609,7 +609,9 @@ struct TeamRegistryTests {
let path = url.path(percentEncoded: false)
try FileManager.default.createSymbolicLink(atPath: path, withDestinationPath: "/nowhere/teams.json")
- #expect(await TeamRegistry(url: url).check([app("com.example.app", team: "AAAA111111")]) == .init(changes: [], problem: .unreadable))
+ let nothingThere = FileProblem.failed(SystemError(domain: NSPOSIXErrorDomain, code: Int(ENOENT)))
+ let outcome = await TeamRegistry(url: url).check([app("com.example.app", team: "AAAA111111")])
+ #expect(outcome == .init(changes: [], problem: .unreadable(nothingThere)))
let kind = try FileManager.default.attributesOfItem(atPath: path)[.type] as? FileAttributeType
#expect(kind == .typeSymbolicLink, "the registry was written over")
@@ -707,7 +709,8 @@ struct TeamRegistryTests {
try directory.setPermissions(0o000, of: "teams.json")
defer { try? directory.setPermissions(0o644, of: "teams.json") }
- #expect(await TeamRegistry(url: url).check([app("com.example.app", team: "BBBB222222")]) == .init(changes: [], problem: .unreadable))
+ let closed = FileProblem.failed(SystemError(domain: NSPOSIXErrorDomain, code: Int(EACCES)))
+ #expect(await TeamRegistry(url: url).check([app("com.example.app", team: "BBBB222222")]) == .init(changes: [], problem: .unreadable(closed)))
try directory.setPermissions(0o644, of: "teams.json")
#expect(try Data(contentsOf: url) == before)
@@ -721,8 +724,8 @@ struct TeamRegistryTests {
let url = try directory.file("teams.json", contents: Data("{".utf8))
let registry = TeamRegistry(url: url)
- #expect(await registry.check([app("com.example.app", team: "AAAA111111")]) == .init(changes: [], problem: .unreadable))
- #expect(await registry.acknowledge("com.example.app") == .unreadable)
+ #expect(await registry.check([app("com.example.app", team: "AAAA111111")]) == .init(changes: [], problem: .unreadable(.damaged)))
+ #expect(await registry.acknowledge("com.example.app") == .unreadable(.damaged))
#expect(try Data(contentsOf: url) == Data("{".utf8))
#expect(await registry.startOver() == .started)
@@ -755,7 +758,9 @@ struct TeamRegistryTests {
try directory.setPermissions(0o555, of: "Peel")
defer { try? directory.setPermissions(0o755, of: "Peel") }
- #expect(await TeamRegistry(url: url).check([app("com.example.app", team: "AAAA111111")]) == .init(changes: [], problem: .unsaved))
+ let refusedWrite = SystemError(domain: NSCocoaErrorDomain, code: NSFileWriteNoPermissionError)
+ let outcome = await TeamRegistry(url: url).check([app("com.example.app", team: "AAAA111111")])
+ #expect(outcome == .init(changes: [], problem: .unsaved(.failed(refusedWrite))))
#expect(url.isMissing)
try directory.setPermissions(0o755, of: "Peel")
diff --git a/Peel/Views/AppList.swift b/Peel/Views/AppList.swift
index 2a125828..999eee23 100644
--- a/Peel/Views/AppList.swift
+++ b/Peel/Views/AppList.swift
@@ -136,19 +136,19 @@ private struct TeamRecordNotice: View {
var body: some View {
Group {
switch problem {
- case .unreadable:
+ case .unreadable(let why):
Notice(
title: Text("Peel can’t read its record of who signs your apps"),
- detail: Text("Until you start the record over, Peel can’t warn you when an app’s signer changes. Starting over keeps the old file beside a new one.")
+ detail: Text(verbatim: why.said(after: String(localized: "Until you start the record over, Peel can’t warn you when an app’s signer changes. Starting over keeps the old file beside a new one."), about: library.teamRecordFile))
) {
StartOverButton(file: library.teamRecordFile, startedOver: "Started the record of who signs your apps over.") {
await library.startTeamRecordOver()
}
}
- case .unsaved:
+ case .unsaved(let why):
Notice(
title: Text("Peel couldn’t save its record of who signs your apps"),
- detail: Text("Until it can, Peel may not warn you when an app’s signer changes.")
+ detail: Text(verbatim: why.said(after: String(localized: "Until it can, Peel may not warn you when an app’s signer changes."), about: library.teamRecordFile))
) { EmptyView() }
}
}