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.

A settings copy fails when defaults gives no answer

Țugui Dragoșa9cff27659a5
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift
index fc7d774e..719f4e77 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift
@@ -19,7 +19,15 @@ public enum PreferenceBackup {
case failed
}
- typealias Run = @Sendable ([String]) async -> Bool
+ /// What `defaults` answered: yes, no (it ran and said no, as for a domain that is not there), or nothing at
+ /// all (it did not start, or ran out of time), which is never taken for a no.
+ enum Answer: Sendable, Equatable {
+ case yes
+ case no
+ case noAnswer
+ }
+
+ typealias Run = @Sendable ([String]) async -> Answer
@concurrent
public static func save(
@@ -36,8 +44,12 @@ public enum PreferenceBackup {
@concurrent
static func save(_ urls: [URL], for app: InstalledApp, in directory: URL, through service: TrashService = TrashService(), run: Run) async -> Saved {
var domains: [PreferenceCleanup.Domain] = []
- for domain in PreferenceCleanup.domains(for: urls, ownedBy: app.bundleIdentifier) where await run(domain.command("read")) {
- domains.append(domain)
+ for domain in PreferenceCleanup.domains(for: urls, ownedBy: app.bundleIdentifier) {
+ switch await run(domain.command("read")) {
+ case .yes: domains.append(domain)
+ case .no: continue
+ case .noAnswer: return .failed
+ }
}
guard !domains.isEmpty else { return .nothingToSave }
@@ -53,7 +65,7 @@ public enum PreferenceBackup {
for domain in domains {
let file = folder.appending(path: fileName(for: domain))
- guard await run(domain.command("export", file.path(percentEncoded: false))), file.isThere else {
+ guard await run(domain.command("export", file.path(percentEncoded: false))) == .yes, file.isThere else {
// `rmdir` removes the folder only when it is empty. If copies were already written, the folder
// goes to the Trash instead: nothing is deleted outright.
if rmdir(folder.path(percentEncoded: false)) != 0 {
@@ -95,7 +107,7 @@ public enum PreferenceBackup {
continue
}
_ = await run(domain.command("delete"))
- if await !run(domain.command("import", file.path(percentEncoded: false))) {
+ if await run(domain.command("import", file.path(percentEncoded: false))) != .yes {
isComplete = false
clearedSome = true
}
@@ -156,12 +168,14 @@ public enum PreferenceBackup {
Date.now.formatted(stampStyle)
}
- /// Runs `defaults` and returns true when it exits with status 0. `save` checks that a domain exists with
- /// `read`, because `export` succeeds even for a domain that does not exist.
- @discardableResult
- private static func run(_ arguments: [String]) async -> Bool {
- guard case .success(let output) = await Subprocess.run("/usr/bin/defaults", arguments, timeout: 10) else { return false }
- return output.status == 0
+ /// Runs `defaults`: yes when it exits with status 0, no for any other status, and no answer when it did not
+ /// run to its end. `save` checks that a domain exists with `read`, because `export` succeeds even for a domain
+ /// that does not exist.
+ private static func run(_ arguments: [String]) async -> Answer {
+ guard case .success(let output) = await Subprocess.run("/usr/bin/defaults", arguments, timeout: 10) else {
+ return .noAnswer
+ }
+ return output.status == 0 ? .yes : .no
}
/// Moves the folders beyond the newest `maximumBackups` to the Trash, never deleting them: once the Trash
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
index 0a36822a..86aa678d 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
@@ -204,7 +204,7 @@ struct RemovalHygieneTests {
let asked = Mutex<[[String]]>([])
let notThere: PreferenceBackup.Run = { arguments in
asked.withLock { $0.append(arguments) }
- return false
+ return .no
}
#expect(await PreferenceBackup.save([URL.homeDirectory.appending(path: "Library/Caches/com.example.app")], for: app, in: backups, run: notThere) == .nothingToSave)
@@ -226,7 +226,7 @@ struct RemovalHygieneTests {
let asked = Mutex<[[String]]>([])
let run: PreferenceBackup.Run = { arguments in
asked.withLock { $0.append(arguments) }
- return true
+ return .yes
}
let restored = await PreferenceBackup.restore(from: folder, run: run)
@@ -237,7 +237,7 @@ struct RemovalHygieneTests {
#expect(!commands.contains { $0.contains("com.example.helper") }, "the settings in use were cleared for a copy that could not go back")
// An import that fails after the delete has cleared that domain, and the result says so.
- let failingImport: PreferenceBackup.Run = { arguments in arguments.first != "import" }
+ let failingImport: PreferenceBackup.Run = { arguments in arguments.first == "import" ? .no : .yes }
#expect(await PreferenceBackup.restore(from: folder, run: failingImport) == PreferenceBackup.Restored(isComplete: false, clearedSome: true))
}
@@ -249,14 +249,30 @@ struct RemovalHygieneTests {
let app = InstalledApp(url: URL(filePath: "/Applications/Example.app"), bundleIdentifier: "com.example.app", name: "Example")
let plist = URL.homeDirectory.appending(path: "Library/Preferences/com.example.app.plist")
- let refused = await PreferenceBackup.save([plist], for: app, in: backups) { arguments in !arguments.contains("export") }
+ let refused = await PreferenceBackup.save([plist], for: app, in: backups) { arguments in arguments.contains("export") ? .no : .yes }
#expect(refused == .failed)
#expect(try FileManager.default.contentsOfDirectory(atPath: backups.path(percentEncoded: false)).isEmpty, "an empty copy was left behind")
- let nothing = await PreferenceBackup.save([URL(filePath: "/Users/x/Library/Caches/com.example.app")], for: app, in: backups) { _ in true }
+ let nothing = await PreferenceBackup.save([URL(filePath: "/Users/x/Library/Caches/com.example.app")], for: app, in: backups) { _ in .yes }
#expect(nothing == .nothingToSave)
}
+ /// `defaults` that did not start or ran out of time said nothing about the domain, so the copy fails and the
+ /// reset moves nothing, rather than reading it as a domain that is not there and clearing it with no copy.
+ @Test func aReadThatGetsNoAnswerMakesTheCopyFail() async throws {
+ let directory = try TemporaryDirectory()
+ let backups = try directory.directory("Backups")
+ let app = InstalledApp(url: URL(filePath: "/Applications/Example.app"), bundleIdentifier: "com.example.app", name: "Example")
+ let plist = URL.homeDirectory.appending(path: "Library/Preferences/com.example.app.plist")
+
+ let saved = await PreferenceBackup.save([plist], for: app, in: backups) { arguments in
+ arguments.first == "read" ? .noAnswer : .yes
+ }
+
+ #expect(saved == .failed)
+ #expect(try FileManager.default.contentsOfDirectory(atPath: backups.path(percentEncoded: false)).isEmpty)
+ }
+
/// Settings lists the saved copies, newest first, to put back or clear. A copy may hold a license key.
@Test func listsTheCopiesThatWereSaved() throws {
let directory = try TemporaryDirectory()
@@ -327,7 +343,7 @@ struct RemovalHygieneTests {
if arguments.contains("export"), let file = arguments.last {
FileManager.default.createFile(atPath: file, contents: Data())
}
- return true
+ return .yes
}
guard case .saved = saved else { Issue.record("the copy was not made: \(saved)"); return }