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.

Wait for macOS's question before giving up a read

Țugui Dragoș73c434811db6
diff --git a/Packages/PeelCore/Sources/PeelCore/Home/Access.swift b/Packages/PeelCore/Sources/PeelCore/Home/Access.swift
index da35379c..35a2bd99 100644
--- a/Packages/PeelCore/Sources/PeelCore/Home/Access.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Home/Access.swift
@@ -13,6 +13,11 @@ public enum FullDiskAccess {
/// Full Disk Access opens, one after another, and the first clear answer decides.
@concurrent
public static func state(home: URL = .homeDirectory) async -> AccessState {
+ currentState(home: home)
+ }
+
+ /// The same, read on the caller's thread.
+ static func currentState(home: URL = .homeDirectory) -> AccessState {
let probes = [
home.appending(path: ".Trash", directoryHint: .isDirectory),
home.appending(path: "Library/Containers/com.apple.Safari", directoryHint: .isDirectory),
diff --git a/Packages/PeelCore/Sources/PeelCore/Home/AppDataQuestion.swift b/Packages/PeelCore/Sources/PeelCore/Home/AppDataQuestion.swift
index e188b1f1..21e6296e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Home/AppDataQuestion.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Home/AppDataQuestion.swift
@@ -1,3 +1,5 @@
+import AppKit
+
/// Tells when macOS is asking the user whether Peel may read another app's data. On macOS 26 the question comes
/// in a dialog shown by UserNotificationCenter, and the scan that caused it waits for the answer. With Full Disk
/// Access, macOS doesn't ask.
@@ -7,4 +9,11 @@ public enum AppDataQuestion {
public static func isAsked(frontmost: String?, hasFullDiskAccess: Bool) -> Bool {
frontmost == asker && !hasFullDiskAccess
}
+
+ /// The same, read now on any thread.
+ static func isOnScreen() -> Bool {
+ let frontmost = NSWorkspace.shared.frontmostApplication?.bundleIdentifier
+ guard frontmost == asker else { return false }
+ return isAsked(frontmost: frontmost, hasFullDiskAccess: FullDiskAccess.currentState() == .granted)
+ }
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift b/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift
index 8ec93730..b74f250f 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift
@@ -499,13 +499,17 @@ private final class Answer: Sendable {
return true
}
guard goesOnWaiting else { return continuation.resume() }
- DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + budget) { self.wake(waiter) }
+ ReadBudget.end(after: budget, isWaiting: { self.isWaiting(waiter) }) { self.wake(waiter) }
}
} onCancel: {
wake(waiter)
}
}
+ private func isWaiting(_ waiter: Int) -> Bool {
+ state.withLock { $0.contents == nil && !$0.woken.contains(waiter) }
+ }
+
/// Each waiter is woken once, by whichever comes first.
private func wake(_ waiter: Int) {
let continuation = state.withLock { state in
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/ReadBudget.swift b/Packages/PeelCore/Sources/PeelCore/Support/ReadBudget.swift
new file mode 100644
index 00000000..e3ff0f16
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Support/ReadBudget.swift
@@ -0,0 +1,22 @@
+import Foundation
+
+/// The end of a read's budget. While macOS asks the person whether Peel may read another app's data, the read it
+/// holds waits for that answer, so the budget starts again rather than give up what the answer may allow.
+enum ReadBudget {
+ /// Calls `wake` once `budget` has run out, unless `isWaiting` says nobody waits anymore.
+ static func end(
+ after budget: TimeInterval,
+ while isAsked: @escaping @Sendable () -> Bool = AppDataQuestion.isOnScreen,
+ isWaiting: @escaping @Sendable () -> Bool,
+ then wake: @escaping @Sendable () -> Void
+ ) {
+ DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + budget) {
+ guard isWaiting() else { return }
+ if isAsked() {
+ end(after: budget, while: isAsked, isWaiting: isWaiting, then: wake)
+ } else {
+ wake()
+ }
+ }
+ }
+}
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/SlowRead.swift b/Packages/PeelCore/Sources/PeelCore/Support/SlowRead.swift
index 71c7cf22..5ec4176c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/SlowRead.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/SlowRead.swift
@@ -32,6 +32,7 @@ enum SlowRead {
/// or the task is canceled. A read that works in steps can check `isGivenUp` and stop once nobody waits.
static func answer<T: Sendable>(
within budget: TimeInterval,
+ while isAsked: @escaping @Sendable () -> Bool = AppDataQuestion.isOnScreen,
_ read: @escaping @Sendable (_ isGivenUp: @escaping @Sendable () -> Bool) -> T
) async -> T? {
guard !Task.isCancelled else { return nil }
@@ -39,7 +40,7 @@ enum SlowRead {
let thread = Thread { answer.give(read { answer.isGivenUp }) }
thread.stackSize = 512 * 1_024
thread.start()
- return await answer.wait(budget)
+ return await answer.wait(budget, while: isAsked)
}
/// Its one waiter is woken once: by the answer, by the end of the budget, or by its own cancellation.
@@ -68,7 +69,7 @@ enum SlowRead {
waiter?.resume()
}
- func wait(_ budget: TimeInterval) async -> T? {
+ func wait(_ budget: TimeInterval, while isAsked: @escaping @Sendable () -> Bool) async -> T? {
await withTaskCancellationHandler {
await withCheckedContinuation { continuation in
let goesOnWaiting = state.withLock { state in
@@ -77,7 +78,10 @@ enum SlowRead {
return true
}
guard goesOnWaiting else { return continuation.resume() }
- DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + budget) { self.wake() }
+ ReadBudget.end(
+ after: budget, while: isAsked,
+ isWaiting: { self.state.withLock { $0.value == nil && !$0.isWoken } }
+ ) { self.wake() }
}
} onCancel: {
wake()
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ReadBudgetTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ReadBudgetTests.swift
new file mode 100644
index 00000000..2cdd15f9
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ReadBudgetTests.swift
@@ -0,0 +1,32 @@
+import Foundation
+@testable import PeelCore
+import Synchronization
+import Testing
+
+struct ReadBudgetTests {
+ @Test func theBudgetStartsAgainWhileMacOSAsksAndEndsOnceItStopsAsking() async {
+ let asked = Mutex(0)
+ await withCheckedContinuation { (done: CheckedContinuation<Void, Never>) in
+ ReadBudget.end(
+ after: 0.01, while: { asked.withLock { $0 += 1; return $0 < 3 } }, isWaiting: { true }
+ ) { done.resume() }
+ }
+
+ #expect(asked.withLock { $0 } == 3)
+ }
+
+ @Test func aReadMacOSHoldsWhileItAsksIsWaitedFor() async {
+ let released = DispatchSemaphore(value: 0)
+ let read = { @Sendable (_: @escaping @Sendable () -> Bool) -> String in
+ released.wait()
+ return "answered"
+ }
+
+ let answer = await SlowRead.answer(within: 0.01, while: {
+ released.signal()
+ return true
+ }, read)
+
+ #expect(answer == "answered")
+ }
+}