Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Never write an empty list of exclusions over one that reads
Țugui Dragoșf0954c392524
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swiftindex 438cef02..f7211a60 100644--- a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift+++ b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift@@ -354,12 +354,13 @@ public struct ExclusionStore: Sendable {read()}- /// Saves `exclusions` in place of the list, and returns false when they could not be written. A saved file that- /// cannot be read is first renamed with `DamagedFile`, never overwritten.- @concurrent- @discardableResult- public func save(_ exclusions: Exclusions) async -> Bool {- FileLock.whileHeld(beside: url) { write(exclusions) }+ /// Keeps a list that cannot be read beside a new, empty one. A list that reads stays as it is: starting it over+ /// would throw away what the person chose.+ public func startOver() async -> StartOver {+ FileLock.whileHeld(beside: url) {+ guard read().isUnreadable else { return .notNeeded }+ return DamagedFile.setAside(url) == nil ? .couldNotSetAside : .started+ }}/// Changes the saved list with `transform`. The list is read again and written under the file's lock, so whatdiff --git a/Packages/PeelCore/Sources/PeelCore/Support/StartOver.swift b/Packages/PeelCore/Sources/PeelCore/Support/StartOver.swiftnew file mode 100644index 00000000..73769775--- /dev/null+++ b/Packages/PeelCore/Sources/PeelCore/Support/StartOver.swift@@ -0,0 +1,9 @@+/// What starting over one of Peel's own files did.+public enum StartOver: Sendable, Equatable {+ /// The file that couldn't be read is kept beside a new one, under another name.+ case started+ /// The file can be read, or isn't there, so it stays as it is.+ case notNeeded+ /// The file couldn't be set aside, so nothing changed.+ case couldNotSetAside+}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionCommandTests.swiftindex 212b02d2..f8524c35 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionCommandTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionCommandTests.swift@@ -105,7 +105,7 @@ struct ExclusionCommandTests {let directory = try TemporaryDirectory()let store = store(in: directory)let note = try directory.file("home/Documents/note.txt")- #expect(await store.save(Exclusions(paths: [note])))+ #expect(await store.change { $0 = Exclusions(paths: [note]) } == .saved(Exclusions(paths: [note])))try await add([note.path(percentEncoded: false).uppercased()], in: store)#expect(await saved(store).paths.count == 1)diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swiftindex d8c0abc4..086bb860 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift@@ -299,7 +299,7 @@ struct ExclusionsTests {#expect(await store.load() == .none)let exclusions = Exclusions(paths: [URL(filePath: "/Users/x/Keep")], bundleIdentifiers: ["com.example.app"])- await store.save(exclusions)+ _ = await store.change { $0 = exclusions }#expect(await store.load() == exclusions)}@@ -311,7 +311,7 @@ struct ExclusionsTests {let (app, terminal) = (ExclusionStore(url: file), ExclusionStore(url: file))let (kept, added) = (URL(filePath: "/Users/x/Kept"), URL(filePath: "/Users/x/Added"))_ = await app.load()- await terminal.save(Exclusions(paths: [kept]))+ _ = await terminal.change { $0 = Exclusions(paths: [kept]) }let outcome = await app.change { $0.paths.insert(added) }@@ -333,6 +333,26 @@ struct ExclusionsTests {#expect(await store.load().paths.count == 16)}+ @Test func startingOverLeavesAListItCanReadAsItIs() async throws {+ let directory = try TemporaryDirectory()+ let store = ExclusionStore(url: directory.url.appending(path: "exclusions.json"))+ let chosen = Exclusions(paths: [URL(filePath: "/Users/x/Keep")], bundleIdentifiers: ["com.example.app"])+ _ = await store.change { $0 = chosen }++ #expect(await store.startOver() == .notNeeded)+ #expect(await store.load() == chosen, "what the person excluded was thrown away")+ }++ @Test func startingOverKeepsAListItCannotReadAside() async throws {+ let directory = try TemporaryDirectory()+ let file = try directory.file("exclusions.json", contents: Data("not json".utf8))++ #expect(await ExclusionStore(url: file).startOver() == .started)+ #expect(await ExclusionStore(url: file).load() == .none)+ let kept = try FileManager.default.contentsOfDirectory(atPath: directory.url.path(percentEncoded: false))+ #expect(kept.contains { $0.hasPrefix("exclusions-damaged-") })+ }+/// A list that cannot be read is left as it is: changing it would throw away what the user chose.@Test func aChangeLeavesAListItCannotReadAsItIs() async throws {let directory = try TemporaryDirectory()@@ -367,7 +387,7 @@ struct ExclusionsTests {let folder = directory.url.appending(path: "Peel", directoryHint: .isDirectory)try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)let store = ExclusionStore(url: folder.appending(path: "exclusions.json"))- await store.save(Exclusions(paths: [URL(filePath: "/Users/x/Kept")]))+ _ = await store.change { $0 = Exclusions(paths: [URL(filePath: "/Users/x/Kept")]) }try directory.setPermissions(0o500, of: folder)defer {try? FileManager.default.setAttributes(@@ -454,21 +474,6 @@ struct ExclusionsTests {#expect(Exclusions.none.isKnown && !Exclusions.unreadable.isKnown)}- /// Saving over a list that could not be read keeps it under another name instead of erasing it.- @Test func keepsAListItCouldNotReadWhenANewOneIsSaved() async throws {- let directory = try TemporaryDirectory()- let file = try directory.file("exclusions.json", contents: Data("not json".utf8))- let store = ExclusionStore(url: file)-- #expect(await store.save(Exclusions(bundleIdentifiers: ["com.example.app"])))-- #expect(await store.load().bundleIdentifiers == ["com.example.app"])- let kept = try FileManager.default.contentsOfDirectory(atPath: directory.url.path(percentEncoded: false)).filter- { $0.contains("damaged") }- #expect(kept.count == 1)- #expect(try String(contentsOf: directory.url.appending(path: try #require(kept.first)), encoding: .utf8) == "not json")- }-@Test func refusesToRemoveAnExcludedItem() async throws {let directory = try TemporaryDirectory()let kept = try directory.file("home/Library/Caches/com.example.keep/cache.db").deletingLastPathComponent()diff --git a/Peel/Model/ExclusionsStore.swift b/Peel/Model/ExclusionsStore.swiftindex 894159e6..0917ae51 100644--- a/Peel/Model/ExclusionsStore.swift+++ b/Peel/Model/ExclusionsStore.swift@@ -7,7 +7,7 @@ import PeelCorefinal class ExclusionsStore {static let shared = ExclusionsStore()- private let store = ExclusionStore()+ private let store: ExclusionStoreprivate(set) var exclusions = Exclusions.notYetRead/// Incremented whenever the list changes. Pages scan again, or narrow their list, when it does, because/// what they show was filtered by the exclusions as they stood when the scan ran.@@ -23,6 +23,10 @@ final class ExclusionsStore {/// takes the whole pane, no page is on screen to notice a change.@ObservationIgnored private var scanned = ScannedRevisions()+ init(store: ExclusionStore = ExclusionStore()) {+ self.store = store+ }+func needsRescan(_ page: String) -> Bool {scanned.needsRescan(page, at: revision)}@@ -54,13 +58,22 @@ final class ExclusionsStore {await change { $0.bundleIdentifiers.subtract(bundleIdentifiers) }}- /// Replaces a list that could not be read, which the store keeps under another name.- func startOver() async {+ /// Replaces a list that could not be read, which the store keeps under another name, and reads the list again.+ @discardableResult+ func startOver() async -> StartOver {await inTurn { [self] in- couldNotSave = await !store.save(.none)- guard !couldNotSave else { return }- unsaved = []- show(.none)+ let answer = await store.startOver()+ switch answer {+ case .started:+ unsaved = []+ couldNotSave = false+ case .couldNotSetAside:+ couldNotSave = true+ case .notNeeded:+ break+ }+ show(withUnsaved(await store.load()))+ return answer}}@@ -85,14 +98,14 @@ final class ExclusionsStore {}/// Runs `operation` once the read or change before it has landed.- private func inTurn(_ operation: @escaping () async -> Void) async {+ private func inTurn<Answer: Sendable>(_ operation: @escaping () async -> Answer) async -> Answer {let previous = lastlet current = Task {await previous?.value- await operation()+ return await operation()}- last = current- await current.value+ last = Task { _ = await current.value }+ return await current.value}/// `list` with the changes that could not be written laid over it. They started a new list if the saved onediff --git a/PeelTests/ExclusionsStoreTests.swift b/PeelTests/ExclusionsStoreTests.swiftnew file mode 100644index 00000000..286f96cd--- /dev/null+++ b/PeelTests/ExclusionsStoreTests.swift@@ -0,0 +1,27 @@+import Foundation+@testable import Peel+import PeelCore+import Testing++@MainActor+struct ExclusionsStoreTests {+ @Test func startingOverAListItCanReadKeepsSayingWhatCouldNotBeSaved() async throws {+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")+ let path = folder.path(percentEncoded: false)+ defer {+ try? FileManager.default.setAttributes([.posixPermissions: 0o700], ofItemAtPath: path)+ try? FileManager.default.removeItem(at: folder)+ }+ let exclusions = ExclusionsStore(store: ExclusionStore(url: folder.appending(path: "exclusions.json")))+ await exclusions.add(paths: [URL(filePath: "/Users/x/Kept")])+ try FileManager.default.setAttributes([.posixPermissions: 0o500], ofItemAtPath: path)+ await exclusions.add(paths: [URL(filePath: "/Users/x/Unsaved")])+ #expect(exclusions.couldNotSave)++ let answer = await exclusions.startOver()++ #expect(answer == .notNeeded)+ #expect(exclusions.couldNotSave, "a change still unsaved was no longer said")+ #expect(exclusions.exclusions.paths.count == 2)+ }+}