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 files that belong to an app couldn't be saved or read

Țugui Dragoș9f62b4c65e8e
diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstrings
index 30b13258..1cfbd47a 100644
--- a/Localization/Peel/Localizable.xcstrings
+++ b/Localization/Peel/Localizable.xcstrings
@@ -123873,6 +123873,112 @@
}
}
},
+ "Nothing changed." : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Nic se nezměnilo."
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Es wurde nichts geändert."
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "No ha cambiado nada."
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Rien n’a changé."
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Non è cambiato nulla."
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "何も変更されていません。"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "아무것도 변경되지 않았습니다."
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Er is niets gewijzigd."
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Nic się nie zmieniło."
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Nada mudou."
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Nu s‑a schimbat nimic."
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Ничего не изменилось."
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Inget ändrades."
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Hiçbir şey değişmedi."
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Нічого не змінено."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "未做任何更改。"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "沒有任何變更。"
+ }
+ }
+ }
+ },
"Nothing changed. Check that Peel can write to its folder in Application Support, then try again." : {
"localizations" : {
"cs" : {
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppFolders.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppFolders.swift
index e9ea2802..28e0efe8 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppFolders.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppFolders.swift
@@ -57,14 +57,6 @@ public struct AppFolders: Sendable {
FileLock.whileReading(beside: url) { url.isMissing ? nil : readProblem() }
}
- /// What a change of the list did. A list that is there and can't be read is never written over, since that would
- /// lose the folders it holds.
- public enum Change: Sendable, Equatable {
- case saved
- /// And why, when Peel knows.
- case notSaved(FileProblem?)
- }
-
/// The folders the list holds that are no longer read, and why. One Peel reads anyway, as part of an Applications
/// folder, loses nothing.
public func leftOut() -> [LeftOut] {
@@ -79,7 +71,7 @@ public struct AppFolders: Sendable {
}
@discardableResult
- public func add(_ folders: [URL]) -> Change {
+ public func add(_ folders: [URL]) -> ListChange {
change { paths in
for path in folders.map({ PathPattern.comparablePath(of: $0) }) where !paths.contains(path) {
paths.append(path)
@@ -97,7 +89,7 @@ public struct AppFolders: Sendable {
}
@discardableResult
- public func remove(_ folders: [URL]) -> Change {
+ public func remove(_ folders: [URL]) -> ListChange {
let removed = Set(folders.map { PathPattern.comparablePath(of: $0) })
return change { paths in
paths.removeAll { removed.contains(PathPattern.comparablePath(of: URL(filePath: $0))) }
@@ -153,7 +145,7 @@ public struct AppFolders: Sendable {
PathPattern.comparablePath(of: url).lowercased()
}
- private func change(_ edit: (inout [String]) -> Void) -> Change {
+ private func change(_ edit: (inout [String]) -> Void) -> ListChange {
FileLock.whileHeld(beside: url) {
guard var paths = saved() ?? (url.isMissing ? [] : nil) else { return .notSaved(readProblem()) }
edit(&paths)
diff --git a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanOwners.swift b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanOwners.swift
index 3e8323fe..66f57dcd 100644
--- a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanOwners.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanOwners.swift
@@ -22,6 +22,12 @@ public struct OrphanOwners: Sendable {
return FileLock.whileHeld(beside: url) { read() }
}
+ /// Why the list can't be read, asked again once `load()` found it couldn't: nil when it can, or isn't there.
+ public func whyUnreadable() -> FileProblem? {
+ guard !url.isMissing else { return nil }
+ return FileLock.whileHeld(beside: url) { readProblem() }
+ }
+
/// Sets a list that cannot be read aside, under another name, so the person can say again what belongs to what.
@discardableResult
public func startOver() -> StartOver {
@@ -31,30 +37,42 @@ public struct OrphanOwners: Sendable {
}
}
- /// Records that `group` belongs to the app `bundleIdentifier`. False when the choice could not be saved, or when
- /// the file is there and cannot be read, since writing over it would lose the choices it holds.
+ /// Records that `group` belongs to the app `bundleIdentifier`.
@discardableResult
- public func give(_ group: String, to bundleIdentifier: String) -> Bool {
+ public func give(_ group: String, to bundleIdentifier: String) -> ListChange {
change { $0[group.lowercased()] = bundleIdentifier }
}
/// Forgets which app each of `groups` belongs to, so they are listed again.
@discardableResult
- public func take(_ groups: [String]) -> Bool {
+ public func take(_ groups: [String]) -> ListChange {
change { owners in groups.forEach { owners[$0.lowercased()] = nil } }
}
- private func change(_ edit: (inout [String: String]) -> Void) -> Bool {
+ private func change(_ edit: (inout [String: String]) -> Void) -> ListChange {
FileLock.whileHeld(beside: url) {
- guard var owners = read() ?? (url.isMissing ? [:] : nil) else { return false }
+ guard var owners = read() ?? (url.isMissing ? [:] : nil) else { return .notSaved(readProblem()) }
edit(&owners)
- guard let data = try? JSONEncoder().encode(owners) else { return false }
- return (try? PeelFolder.write(data, to: url)) != nil
+ do {
+ try PeelFolder.write(try JSONEncoder().encode(owners), to: url)
+ return .saved
+ } catch {
+ return .notSaved(.failed(SystemError(error)))
+ }
}
}
private func read() -> [String: String]? {
- guard let data = BoundedRead.data(at: url) else { return nil }
- return try? JSONDecoder().decode([String: String].self, from: data)
+ BoundedRead.data(at: url).flatMap(decoded)
+ }
+
+ /// Why the list there can't be read, or nil when it can, for a caller that holds the lock.
+ private func readProblem() -> 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: String]? {
+ try? JSONDecoder().decode([String: String].self, from: data)
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/ListChange.swift b/Packages/PeelCore/Sources/PeelCore/Support/ListChange.swift
new file mode 100644
index 00000000..0adead59
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Support/ListChange.swift
@@ -0,0 +1,7 @@
+/// What a change of one of Peel's own lists did. A list that is there and can't be read is never written over, since
+/// that would lose what it holds.
+public enum ListChange: Sendable, Equatable {
+ case saved
+ /// And why, when Peel knows.
+ case notSaved(FileProblem?)
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift
index dadc848a..bb83e7c8 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OrphanScannerTests.swift
@@ -865,11 +865,11 @@ struct OrphanScannerTests {
let owners = OrphanOwners(url: directory.url.appending(path: "orphan-owners.json"))
#expect(owners.load() == [:])
- #expect(owners.give("org.example.Helper", to: "com.installed.app"))
- #expect(owners.give("org.example.other", to: "com.microsoft.Word"))
+ #expect(owners.give("org.example.Helper", to: "com.installed.app") == .saved)
+ #expect(owners.give("org.example.other", to: "com.microsoft.Word") == .saved)
#expect(owners.load() == ["org.example.helper": "com.installed.app", "org.example.other": "com.microsoft.Word"])
- #expect(owners.take(["org.example.helper"]))
+ #expect(owners.take(["org.example.helper"]) == .saved)
#expect(owners.load() == ["org.example.other": "com.microsoft.Word"])
}
@@ -883,11 +883,24 @@ struct OrphanScannerTests {
try directory.file("Peel/orphan-owners.json", bytes: 16)
#expect(owners.load() == nil, "a list that can't be read read as no choices")
- #expect(!owners.give("org.example.helper", to: "com.installed.app"))
+ #expect(owners.whyUnreadable() == .damaged)
+ #expect(owners.give("org.example.helper", to: "com.installed.app") == .notSaved(.damaged))
#expect(owners.startOver() == .started)
#expect(owners.load() == [:])
+ #expect(owners.whyUnreadable() == nil)
#expect(owners.startOver() == .notNeeded)
- #expect(owners.give("org.example.helper", to: "com.installed.app"))
+ #expect(owners.give("org.example.helper", to: "com.installed.app") == .saved)
+ }
+
+ @Test(.permissionsHold) func saysWhyWhatBelongsToAnAppCouldNotBeSaved() throws {
+ let directory = try TemporaryDirectory()
+ let folder = try directory.directory("Peel")
+ try directory.setPermissions(0o500, of: folder)
+ defer { try? directory.setPermissions(0o755, of: folder) }
+
+ let refusedWrite = SystemError(domain: NSCocoaErrorDomain, code: NSFileWriteNoPermissionError)
+ let owners = OrphanOwners(url: folder.appending(path: "orphan-owners.json"))
+ #expect(owners.give("org.example.helper", to: "com.installed.app") == .notSaved(.failed(refusedWrite)))
}
/// An app installed since the scan owns what the list still calls orphaned.
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index 056e61ac..aab142ac 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -314,7 +314,7 @@ final class AppLibrary {
var foldersFile: URL { appFolders.url }
- private func changeFolders(_ change: (AppFolders) -> AppFolders.Change) {
+ private func changeFolders(_ change: (AppFolders) -> ListChange) {
switch change(appFolders) {
case .saved:
couldNotSaveFolders = false
diff --git a/Peel/Model/OrphanLibrary.swift b/Peel/Model/OrphanLibrary.swift
index f4b391e7..2b5774bc 100644
--- a/Peel/Model/OrphanLibrary.swift
+++ b/Peel/Model/OrphanLibrary.swift
@@ -23,7 +23,13 @@ final class OrphanLibrary {
private(set) var scannedAgainst: AppLibrary.Listing?
/// The groups the person said belong to an app, by identifier, with that app's bundle identifier, or nil when that
/// list can't be read.
- private(set) var owners = OrphanOwners().load()
+ private(set) var owners: [String: String]?
+ /// Why that list can't be read, when Peel knows.
+ private(set) var whyOwnersUnreadable: FileProblem?
+
+ init() {
+ readOwners()
+ }
var selectedGroup: OrphanGroup? {
guard case .group(let id) = selection else { return nil }
@@ -67,8 +73,8 @@ final class OrphanLibrary {
// check this too; checking here keeps a new caller from getting it wrong.
guard !library.apps.isEmpty else { return }
let installedApps = library.apps
- let owners = OrphanOwners().load()
- self.owners = owners
+ readOwners()
+ let owners = owners
guard let found = await scanRun.run({
let remembered = await AppMemory().remember(installedApps)
// What is currently running is better evidence than anything worked out from what is installed: a
@@ -103,34 +109,41 @@ final class OrphanLibrary {
}
/// Takes `group` out of the list, as the person said it belongs to `app`. Nothing moves, and the group stays
- /// listed when the choice could not be saved, which answers false.
- func give(_ group: OrphanGroup, to app: InstalledApp) -> Bool {
- let saved = app.bundleIdentifier.map { OrphanOwners().give(group.identifier, to: $0) } ?? false
- owners = OrphanOwners().load()
- guard saved else { return false }
+ /// listed when the choice could not be saved.
+ func give(_ group: OrphanGroup, to app: InstalledApp) -> ListChange {
+ let change = app.bundleIdentifier.map { OrphanOwners().give(group.identifier, to: $0) } ?? .notSaved(nil)
+ readOwners()
+ guard change == .saved else { return change }
if let scan {
self.scan = scan.without(group.id)
}
selectedURLs.subtract(group.items.map(\.url))
if selection == .group(group.id) { selection = nil }
- return true
+ return .saved
}
- /// Lists `groups` again, forgetting the app each was given to. False when that could not be saved.
- func forget(_ groups: [String], from library: AppLibrary) async -> Bool {
- guard OrphanOwners().take(groups) else {
- owners = OrphanOwners().load()
- return false
+ /// Lists `groups` again, forgetting the app each was given to.
+ func forget(_ groups: [String], from library: AppLibrary) async -> ListChange {
+ let change = OrphanOwners().take(groups)
+ guard change == .saved else {
+ readOwners()
+ return change
}
await refresh(from: library)
- return true
+ return .saved
}
func startOwnersOver() -> StartOver {
let answer = OrphanOwners().startOver()
- owners = OrphanOwners().load()
+ readOwners()
return answer
}
+
+ private func readOwners() {
+ let list = OrphanOwners()
+ owners = list.load()
+ whyOwnersUnreadable = owners == nil ? list.whyUnreadable() : nil
+ }
}
extension OrphanGroup {
diff --git a/Peel/Views/Orphans/OrphanList.swift b/Peel/Views/Orphans/OrphanList.swift
index 5edffdf1..11d456fe 100644
--- a/Peel/Views/Orphans/OrphanList.swift
+++ b/Peel/Views/Orphans/OrphanList.swift
@@ -8,7 +8,7 @@ struct OrphanList: View {
@Environment(\.openSettings) private var openSettings
@State private var searchText = ""
@State private var isRescanning = false
- @State private var notGivenTo: String?
+ @State private var notGiven: (app: String, why: FileProblem?)?
@State private var groupToGive: OrphanGroup?
var body: some View {
@@ -157,7 +157,7 @@ struct OrphanList: View {
// What a group belongs to is kept by the app's identifier.
AppChooser(apps: library.apps.filter { $0.bundleIdentifier != nil }) { app in
groupToGive = nil
- if !orphans.give(group, to: app) { notGivenTo = app.name }
+ if case .notSaved(let why) = orphans.give(group, to: app) { notGiven = (app.name, why) }
}
HStack {
Spacer()
@@ -169,8 +169,8 @@ struct OrphanList: View {
.padding(20)
}
.alert(
- "Peel couldn’t save that these files belong to \(notGivenTo ?? "")",
- isPresented: Binding(get: { notGivenTo != nil }, set: { if !$0 { notGivenTo = nil } })
+ "Peel couldn’t save that these files belong to \(notGiven?.app ?? "")",
+ isPresented: Binding(get: { notGiven != nil }, set: { if !$0 { notGiven = nil } })
) {
if orphans.owners == nil {
Button("Open Peel Settings") { SettingsPane.exclusions.open(with: openSettings) }
@@ -178,9 +178,7 @@ struct OrphanList: View {
Button("OK", role: .cancel) {}
.keyboardShortcut(.defaultAction)
} message: {
- orphans.owners == nil
- ? Text("Peel can’t read its list of the orphaned files you said belong to an app, so it lists them all. Start the list over in Settings > Exclusions.")
- : Text("Nothing changed. Check that Peel can write to its folder in Application Support, then try again.")
+ Text(verbatim: orphans.ownersNotSaved(notGiven?.why))
}
}
diff --git a/Peel/Views/Presentation.swift b/Peel/Views/Presentation.swift
index b21d4c15..30ee7784 100644
--- a/Peel/Views/Presentation.swift
+++ b/Peel/Views/Presentation.swift
@@ -298,6 +298,16 @@ extension AppLibrary {
}
}
+extension OrphanLibrary {
+ /// What an alert says when the list of orphaned files that belong to an app couldn't be changed, and why.
+ func ownersNotSaved(_ why: FileProblem?) -> String {
+ let said = owners == nil
+ ? String(localized: "Peel can’t read its list of the orphaned files you said belong to an app, so it lists them all. Start the list over in Settings > Exclusions.")
+ : String(localized: "Nothing changed.")
+ return why.said(after: said, about: OrphanOwners.defaultURL)
+ }
+}
+
extension ExclusionsStore {
/// That nothing moves until the list starts over, then why it couldn't be read, when Peel knows.
var nothingMovesUntilStartedOver: String {
diff --git a/Peel/Views/Settings/ExclusionsSettingsView.swift b/Peel/Views/Settings/ExclusionsSettingsView.swift
index 3735a378..2278ef6e 100644
--- a/Peel/Views/Settings/ExclusionsSettingsView.swift
+++ b/Peel/Views/Settings/ExclusionsSettingsView.swift
@@ -12,7 +12,7 @@ struct ExclusionsSettingsView: View {
@State private var selectedPaths: Set<URL> = []
@State private var selectedApps: Set<String> = []
@State private var selectedGroups: Set<String> = []
- @State private var couldNotForget = false
+ @State private var notForgotten: ListChange?
private var paths: [URL] {
exclusions.exclusions.paths.sorted { $0.path(percentEncoded: false) < $1.path(percentEncoded: false) }
@@ -43,6 +43,10 @@ struct ExclusionsSettingsView: View {
exclusions.whyNotSaved.said(after: String(localized: "Your latest changes work in Peel until it quits. When it opens again, and in the peel command, the list is as it was last saved."), about: exclusions.file)
}
+ private var ownersUnreadable: String {
+ orphans.whyOwnersUnreadable.said(after: String(localized: "Peel can’t read this list, so Orphaned Files lists these files again. Starting over keeps the old list beside a new one."), about: OrphanOwners.defaultURL)
+ }
+
private var isShowingRefusal: Binding<Bool> {
Binding(get: { !refused.isEmpty }, set: { if !$0 { refused = [] } })
}
@@ -212,7 +216,7 @@ struct ExclusionsSettingsView: View {
}
} else {
HStack(spacing: 8) {
- Text("Peel can’t read this list, so Orphaned Files lists these files again. Starting over keeps the old list beside a new one.")
+ Text(verbatim: ownersUnreadable)
.foregroundStyle(.secondary)
Spacer(minLength: 8)
StartOverButton(
@@ -244,12 +248,13 @@ struct ExclusionsSettingsView: View {
guard !keeping.isEmpty else { return }
Task { await exclusions.add(paths: keeping) }
}
- .alert("Peel couldn’t list these files again", isPresented: $couldNotForget) {
+ .alert(
+ "Peel couldn’t list these files again",
+ isPresented: Binding(get: { notForgotten != nil }, set: { if !$0 { notForgotten = nil } })
+ ) {
Button("OK", role: .cancel) {}
} message: {
- orphans.owners == nil
- ? Text("Peel can’t read its list of the orphaned files you said belong to an app, so it lists them all. Start the list over in Settings > Exclusions.")
- : Text("Nothing changed. Check that Peel can write to its folder in Application Support, then try again.")
+ if case .notSaved(let why) = notForgotten { Text(verbatim: orphans.ownersNotSaved(why)) }
}
.alert(refused.count == 1 ? "That folder can’t be excluded." : "These folders can’t be excluded.", isPresented: isShowingRefusal) {
Button("OK", role: .cancel) {}
@@ -266,6 +271,9 @@ extension ExclusionsSettingsView {
private func forgetSelectedGroups() {
let forgotten = Array(selectedGroups)
selectedGroups = []
- Task { couldNotForget = await !orphans.forget(forgotten, from: library) }
+ Task {
+ let change = await orphans.forget(forgotten, from: library)
+ if change != .saved { notForgotten = change }
+ }
}
}