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.

Remove iCloud downloads on a thread of their own

Țugui Dragoș21bcdca820f3
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift b/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
index 3307f0f2..6398773a 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
@@ -452,14 +452,45 @@ public enum CloudStorage {
static func free(
_ files: [CloudFile],
exclusions: Exclusions,
- isSafe: (URLResourceValues) -> Bool,
- evict: (URL) throws -> Void = { try FileManager.default.evictUbiquitousItem(at: $0) },
- isPinned: (URL) -> Bool = isPinned(_:)
+ isSafe: @escaping @Sendable (URLResourceValues) -> Bool,
+ evict: @escaping @Sendable (URL) throws -> Void = { try FileManager.default.evictUbiquitousItem(at: $0) },
+ isPinned: @escaping @Sendable (URL) -> Bool = isPinned(_:)
) async -> CloudFreeing {
+ let isStopped = Atomic(false)
+ let counted = MoveCount.current
+ let quality = QualityOfService(Task.currentPriority)
+ return await withTaskCancellationHandler {
+ await withCheckedContinuation { continuation in
+ // iCloud can keep one file for minutes, and a thread of Swift's pool held that long holds up every
+ // other scan, so this runs on a thread of its own, as a walk does.
+ let thread = Thread {
+ continuation.resume(returning: freeInTurn(
+ files, exclusions: exclusions, isSafe: isSafe, evict: evict, isPinned: isPinned,
+ counted: counted, isStopped: { isStopped.load(ordering: .relaxed) }
+ ))
+ }
+ thread.qualityOfService = quality
+ thread.stackSize = 512 * 1_024
+ thread.start()
+ }
+ } onCancel: {
+ isStopped.store(true, ordering: .relaxed)
+ }
+ }
+
+ private static func freeInTurn(
+ _ files: [CloudFile],
+ exclusions: Exclusions,
+ isSafe: (URLResourceValues) -> Bool,
+ evict: (URL) throws -> Void,
+ isPinned: (URL) -> Bool,
+ counted: MoveCount?,
+ isStopped: () -> Bool
+ ) -> CloudFreeing {
var done = CloudFreeing()
for file in files {
// A stop lets the file iCloud is answering for finish, and nothing after it starts.
- if Task.isCancelled { break }
+ if isStopped() { break }
guard exclusions.isKnown, !exclusions.excludes(file.url) else {
done.refused.append(CloudRefusal(url: file.url, reason: .excluded))
continue
@@ -494,7 +525,7 @@ public enum CloudStorage {
do {
try evict(file.url)
done.freed.append(file)
- MoveCount.current?.add(1)
+ counted?.add(1)
} catch {
done.refused.append(CloudRefusal(url: file.url, reason: reason(for: error)))
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift b/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift
index 7a88ce4b..18cef65d 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift
@@ -136,12 +136,7 @@ public enum FileSize {
let contents = walking(folder, scan) { answer.isStopped }
Walks.finish(folder, with: contents, into: answer)
}
- thread.qualityOfService = switch priority {
- case .high...: .userInitiated
- case .medium..<(.high): .default
- case .low..<(.medium): .utility
- default: .background
- }
+ thread.qualityOfService = QualityOfService(priority)
thread.stackSize = 512 * 1_024
thread.start()
}
@@ -455,6 +450,18 @@ private enum Walks {
}
/// One answer, written by a thread that may never come back and read by everyone who is waiting for it.
+extension QualityOfService {
+ /// What a thread of its own takes to run at the priority of the task that asked.
+ init(_ priority: TaskPriority) {
+ self = switch priority {
+ case .high...: .userInitiated
+ case .medium..<(.high): .default
+ case .low..<(.medium): .utility
+ default: .background
+ }
+ }
+}
+
private final class Answer: Sendable {
private struct State {
var contents: FolderContents?
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
index 9bc3942e..74eb6d5f 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/CloudStorageTests.swift
@@ -1,5 +1,6 @@
import Foundation
@testable import PeelCore
+import Synchronization
import Testing
struct CloudStorageTests {
@@ -819,29 +820,39 @@ struct CloudStorageTests {
url: scanned.url, name: scanned.name, container: scanned.container, size: 1, modified: nil
)
let excluded = Exclusions(paths: [inside])
- var evicted: [URL] = []
+ let evicted = Mutex<[URL]>([])
- let refused = await CloudStorage.free([scanned], exclusions: excluded, isSafe: { _ in true }) {
- evicted.append($0)
+ let refused = await CloudStorage.free([scanned], exclusions: excluded, isSafe: { _ in true }) { url in
+ evicted.withLock { $0.append(url) }
}.refused
- let undatedRefused = await CloudStorage.free([undated], exclusions: excluded, isSafe: { _ in true }) {
- evicted.append($0)
+ let undatedRefused = await CloudStorage.free([undated], exclusions: excluded, isSafe: { _ in true }) { url in
+ evicted.withLock { $0.append(url) }
}.refused
#expect(refused.map(\.reason) == [.excluded])
#expect(undatedRefused.map(\.reason) == [.excluded])
- #expect(evicted.isEmpty)
+ let nothingWasEvicted = evicted.withLock { $0.isEmpty }
+ #expect(nothingWasEvicted)
}
@Test func stopsBetweenFilesOnceItsTaskIsCancelled() async throws {
let directory = try TemporaryDirectory()
let files = try downloads(3, in: directory)
- let done = await Task {
+ let (entered, enters) = AsyncStream.makeStream(of: Void.self)
+ let goesOn = DispatchSemaphore(value: 0)
+
+ let removal = Task {
await CloudStorage.free(files, exclusions: .none, isSafe: { _ in true }) { _ in
- withUnsafeCurrentTask { $0?.cancel() }
+ enters.yield()
+ goesOn.wait()
}
- }.value
+ }
+ var waiting = entered.makeAsyncIterator()
+ await waiting.next()
+ removal.cancel()
+ goesOn.signal()
+ let done = await removal.value
#expect(done.freed.map(\.url) == [files[0].url])
#expect(done.refused.isEmpty)
@@ -874,6 +885,37 @@ struct CloudStorageTests {
#expect(Set(done.refused.map(\.reason)) == [.keptDownloaded])
}
+ @Test func removingDownloadsKeepsNoThreadOfSwiftsPool() async throws {
+ let directory = try TemporaryDirectory()
+ let count = ProcessInfo.processInfo.activeProcessorCount + 1
+ let files = try downloads(count, in: directory)
+ let arrived = Mutex(0)
+ let everyoneIsIn = DispatchSemaphore(value: 0)
+
+ // Each waits for all the others to be in theirs, which more removals than Swift's pool has threads can't do
+ // on them.
+ let freed = await withTaskGroup(of: Int.self) { group in
+ for file in files {
+ group.addTask {
+ await CloudStorage.free([file], exclusions: .none, isSafe: { _ in true }) { _ in
+ let here = arrived.withLock { count in
+ count += 1
+ return count
+ }
+ if here == count {
+ for _ in 1..<count { everyoneIsIn.signal() }
+ } else if everyoneIsIn.wait(timeout: .now() + 30) == .timedOut {
+ throw CocoaError(.fileLocking)
+ }
+ }.freed.count
+ }
+ }
+ return await group.reduce(0, +)
+ }
+
+ #expect(freed == count)
+ }
+
@Test func countsEachFileItFrees() async throws {
let directory = try TemporaryDirectory()
let files = try downloads(3, in: directory)