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.

Nothing moves before the exclusions are read

Țugui Dragoșc598a031ef07
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
index f88752ec..cbbe0d1f 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
@@ -64,7 +64,7 @@ struct RemovalGuard: Sendable {
}
private func allows(_ url: URL, isALink known: Bool?) -> Bool {
- guard url.isFileURL, !exclusions.isUnreadable else { return false }
+ guard url.isFileURL, exclusions.isKnown else { return false }
let path = Self.normalized(url.path(percentEncoded: false))
// A path can be written several ways (`/var` for `/private/var`, a different case), and a Put Back
// destination does not exist yet. `located` names the part that exists the way the kernel does.
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
index 30f04f4d..5006c179 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
@@ -15,6 +15,11 @@ public struct Exclusions: Sendable, Codable, Hashable {
/// True when the saved list exists but could not be read, so what the user excluded is unknown. Nothing is
/// moved while this is true.
public private(set) var isUnreadable = false
+ /// False until the saved list has been read. Nothing is moved before, as for a list that cannot be read.
+ public private(set) var hasBeenRead = true
+
+ /// True when what the user excluded is known: the saved list was read, and could be.
+ public var isKnown: Bool { hasBeenRead && !isUnreadable }
public static let none = Exclusions()
public static let unreadable: Exclusions = {
@@ -22,6 +27,12 @@ public struct Exclusions: Sendable, Codable, Hashable {
exclusions.isUnreadable = true
return exclusions
}()
+ /// The list before the saved one has been read.
+ public static let notYetRead: Exclusions = {
+ var exclusions = Exclusions()
+ exclusions.hasBeenRead = false
+ return exclusions
+ }()
private enum CodingKeys: String, CodingKey {
case paths
@@ -31,12 +42,14 @@ public struct Exclusions: Sendable, Codable, Hashable {
/// Compares what is excluded. `spellings` is left out, since it is worked out from `paths`.
public static func == (one: Exclusions, other: Exclusions) -> Bool {
one.paths == other.paths && one.bundleIdentifiers == other.bundleIdentifiers && one.isUnreadable == other.isUnreadable
+ && one.hasBeenRead == other.hasBeenRead
}
public func hash(into hasher: inout Hasher) {
hasher.combine(paths)
hasher.combine(bundleIdentifiers)
hasher.combine(isUnreadable)
+ hasher.combine(hasBeenRead)
}
/// One saved entry. `value` is nil when the entry cannot be decoded, so a bad entry is dropped, not the list.
@@ -111,9 +124,10 @@ public struct Exclusions: Sendable, Codable, Hashable {
/// True when `brew uninstall` would delete something excluded: an app the package installs (`apps` are the
/// installed apps known to be its), an app identifier it quits, or its own folder under `prefix`. An
- /// unreadable list counts as excluding every package, since `brew uninstall` cannot be undone.
+ /// unreadable list, or one not read yet, counts as excluding every package, since `brew uninstall` cannot be
+ /// undone.
public func excludes(_ package: HomebrewPackage, apps: [InstalledApp], prefix: URL?) -> Bool {
- guard !isUnreadable else { return true }
+ guard isKnown else { return true }
let targets = package.appTargets.map { URL(filePath: $0, directoryHint: .isDirectory) } + apps.map(\.url)
if targets.contains(where: { excludes($0) || holds($0) }) { return true }
if apps.contains(where: { excludes(bundleIdentifier: $0.bundleIdentifier) }) { return true }
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
index 349101ab..5b1b63b3 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
@@ -51,6 +51,7 @@ struct ExclusionsTests {
#expect(Exclusions(paths: [URL(filePath: "/opt/homebrew/Caskroom/example")]).excludes(cask, apps: [], prefix: prefix))
#expect(Exclusions(paths: [URL(filePath: "/opt/homebrew")]).excludes(formula, apps: [], prefix: prefix))
#expect(Exclusions.unreadable.excludes(formula, apps: [], prefix: prefix), "a list that cannot be read keeps everything")
+ #expect(Exclusions.notYetRead.excludes(formula, apps: [], prefix: prefix), "a list not read yet keeps everything")
let unrelated = Exclusions(paths: [URL(filePath: "/Applications/Other.app")], bundleIdentifiers: ["com.other.app"])
#expect(!unrelated.excludes(cask, apps: [app], prefix: prefix))
@@ -236,6 +237,21 @@ struct ExclusionsTests {
#expect(result.failures.map(\.reason) == [.protectedLocation])
}
+ /// Until the app has read the saved list, what the user excluded is not known, so nothing moves, as for a list
+ /// that cannot be read.
+ @Test func movesNothingBeforeTheListIsRead() async throws {
+ let directory = try TemporaryDirectory()
+ let item = try directory.file("home/Library/Caches/com.example.app/cache.db").deletingLastPathComponent()
+ let environment = SearchEnvironment(homeDirectory: directory.url.appending(path: "home"), rootDirectory: directory.url.appending(path: "root"))
+ let service = TrashService(environment: environment, exclusions: .notYetRead) { $0 }
+
+ let result = await service.trash([item])
+ #expect(result.trashed.isEmpty)
+ #expect(result.failures.map(\.reason) == [.protectedLocation])
+ #expect(!Exclusions.notYetRead.isKnown && !Exclusions.notYetRead.isUnreadable)
+ #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()
diff --git a/Peel/Model/ExclusionsStore.swift b/Peel/Model/ExclusionsStore.swift
index 129e2f76..fa43db02 100644
--- a/Peel/Model/ExclusionsStore.swift
+++ b/Peel/Model/ExclusionsStore.swift
@@ -8,7 +8,7 @@ final class ExclusionsStore {
static let shared = ExclusionsStore()
private let store = ExclusionStore()
- private(set) var exclusions = Exclusions.none
+ private(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.
private(set) var revision = 0
diff --git a/Peel/PeelApp.swift b/Peel/PeelApp.swift
index 74539699..80a2e8bb 100644
--- a/Peel/PeelApp.swift
+++ b/Peel/PeelApp.swift
@@ -251,13 +251,15 @@ struct PeelApp: App {
guard !hasLaunched else { return }
hasLaunched = true
history.stats = stats
+ // Read beside the rest and behind nothing: until the exclusions are read, nothing moves.
+ async let exclusionsRead: Void = Marks.interval("Exclusions") { await exclusions.load() }
// The apps and Homebrew don't depend on Home's checks, so they load in parallel with them.
async let homebrewPackages: Void = Marks.interval("Homebrew") { await homebrew.refresh() }
async let apps: Void = Marks.interval("Applications") { await library.load() }
// Home's checks run at launch, whichever page opens first. They ask the helper whether it can act,
// which every page reads to lock the rows that need it.
await Marks.interval("Home checks") { await home.refresh(helper: helper) }
- await Marks.interval("Exclusions") { await exclusions.load() }
+ await exclusionsRead
trashMonitor.onApplicationTrashed = { [notifications] url in
notifications.notify(applicationTrashed: url)
}
diff --git a/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift b/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
index ec71a14e..b74880b4 100644
--- a/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
+++ b/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
@@ -303,7 +303,7 @@ struct BackgroundItemDetailView: View {
isConfirmingTrash = true
}
.buttonStyle(.borderedProminent)
- .disabled(isBusy || item.removalRequiresPrivileges && !helper.canAct || exclusions.exclusions.isUnreadable)
+ .disabled(isBusy || item.removalRequiresPrivileges && !helper.canAct || !exclusions.exclusions.isKnown)
}
}
diff --git a/Peel/Views/History/HistoryDetailView.swift b/Peel/Views/History/HistoryDetailView.swift
index 77554f60..02886080 100644
--- a/Peel/Views/History/HistoryDetailView.swift
+++ b/Peel/Views/History/HistoryDetailView.swift
@@ -78,7 +78,7 @@ struct HistoryDetailView: View {
RestoreBar(
count: selected.count,
isRestoring: history.isRestoring,
- isEnabled: !selected.isEmpty && !history.isRestoring && !exclusions.exclusions.isUnreadable
+ isEnabled: !selected.isEmpty && !history.isRestoring && exclusions.exclusions.isKnown
) {
Task { await history.restore(selected, canUseHelper: helper.canAct) }
}
diff --git a/Peel/Views/Homebrew/HomebrewDetailView.swift b/Peel/Views/Homebrew/HomebrewDetailView.swift
index 85c97f97..d63b557e 100644
--- a/Peel/Views/Homebrew/HomebrewDetailView.swift
+++ b/Peel/Views/Homebrew/HomebrewDetailView.swift
@@ -128,6 +128,7 @@ struct HomebrewDetailView: View {
/// `TrashService` and `RemovalGuard`, so this is where the exclusions are checked.
private var uninstallRefusal: LocalizedStringResource? {
if exclusions.exclusions.isUnreadable { return "Exclusions can’t be read: see Settings" }
+ if !exclusions.exclusions.hasBeenRead { return "Checking…" }
let apps = library.apps.filter { library.cask(for: $0)?.id == package.id }
return exclusions.exclusions.excludes(package, apps: apps, prefix: homebrew.installation?.prefix) ? "Excluded in Settings" : nil
}
diff --git a/Peel/Views/Plugins/PluginDetailView.swift b/Peel/Views/Plugins/PluginDetailView.swift
index 720be92b..065e8ac9 100644
--- a/Peel/Views/Plugins/PluginDetailView.swift
+++ b/Peel/Views/Plugins/PluginDetailView.swift
@@ -86,7 +86,7 @@ struct PluginDetailView: View {
isConfirmingRemoval = true
}
.buttonStyle(.borderedProminent)
- .disabled(isLocked || plugin.isLeftAlone || plugins.isRemoving || plugins.isScanning || exclusions.exclusions.isUnreadable)
+ .disabled(isLocked || plugin.isLeftAlone || plugins.isRemoving || plugins.isScanning || !exclusions.exclusions.isKnown)
}
}
}
diff --git a/Peel/Views/RemovalBar.swift b/Peel/Views/RemovalBar.swift
index 13022b34..a8356f40 100644
--- a/Peel/Views/RemovalBar.swift
+++ b/Peel/Views/RemovalBar.swift
@@ -90,7 +90,7 @@ struct RemovalBar: View {
var body: some View {
let reading = reading
- let isEnabled = reading.isEnabled && !exclusions.exclusions.isUnreadable
+ let isEnabled = reading.isEnabled && exclusions.exclusions.isKnown
FloatingBar {
if let notice {
Text(verbatim: notice.figure)