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
A late walk's answer is handed over only while it is recent
Țugui Dragoș54c1707b9b89
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift b/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swiftindex 9b3c8082..f02ea861 100644--- a/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift+++ b/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift@@ -35,6 +35,11 @@ public enum FileSize {/// large folder. A folder that never answers costs this wait once and is not asked again.public static let budget: TimeInterval = 8+ /// How long an answer that landed after its question gave up is handed to the next one. Long enough for a+ /// question asked again soon after, as Applications asks again for an app it could not measure, and short+ /// enough that no scan is fed a folder as it was long before.+ static let lateAnswerLife = Duration.seconds(60)+/// How a scanner asks for a size. Tests hand in their own, to stand in for a folder that does not answer.typealias Measure = @Sendable (URL) async -> Int64?@@ -71,12 +76,15 @@ public enum FileSize {) -> FolderContents?@concurrent- static func contents(of url: URL, within budget: TimeInterval, walking: @escaping Walker) async -> FolderContents? {+ static func contents(+ of url: URL, within budget: TimeInterval, walking: @escaping Walker,+ keepingLateAnswersFor life: Duration = lateAnswerLife+ ) async -> FolderContents? {guard let folder = folderToWalk(url) else { return immediateContents(of: url) }// A canceled task starts no walk.guard !Task.isCancelled else { return nil }let answer: Answer- switch Walks.ask(about: folder) {+ switch Walks.ask(about: folder, keepingLateAnswersFor: life) {case .answered(let contents):return contentscase .wait(let shared, let isNew):@@ -271,7 +279,7 @@ private enum Walks {enum Entry {case walking(Answer)case abandoned(Answer)- case landed(FolderContents)+ case landed(FolderContents, at: ContinuousClock.Instant)}enum Asked {@@ -282,12 +290,13 @@ private enum Walks {private static let entries = Mutex<[String: Entry]>([:])/// Returns a known answer, or the walk to wait for (`isNew` when the caller has to start it). An answer- /// that landed late is handed over once, and the question after that walks the folder again.- static func ask(about folder: URL) -> Asked {+ /// that landed late is handed over once, and only within `life` of landing; the question after that walks+ /// the folder again.+ static func ask(about folder: URL, keepingLateAnswersFor life: Duration) -> Asked {entries.withLock { entries inlet path = PathPattern.comparablePath(of: folder)switch entries[path] {- case .landed(let contents):+ case .landed(let contents, let landed) where ContinuousClock.now - landed <= life:entries[path] = nilreturn .answered(contents)case .abandoned:@@ -295,7 +304,7 @@ private enum Walks {case .walking(let answer):answer.join()return .wait(answer, isNew: false)- case nil:+ case .landed, nil:let answer = Answer()answer.join()entries[path] = .walking(answer)@@ -351,7 +360,7 @@ private enum Walks {}answer.give(contents)// With nobody left to take it, it is kept for whoever asks next.- if isCurrent, !answer.isAwaited { entries[path] = .landed(contents) }+ if isCurrent, !answer.isAwaited { entries[path] = .landed(contents, at: .now) }}}}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swiftindex c19ae4d0..897fb69c 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift@@ -312,6 +312,37 @@ struct FileSizeTests {#expect(ContinuousClock.now - asked < .seconds(4), "the question waited on the walk that was stopped")}+ /// A late answer describes the folder as it was when the walk ended. It is handed over while that is recent,+ /// and a question after that walks the folder again: a rescan an hour later is never fed the old size and date.+ @Test func aLateAnswerIsHandedOverOnlyWhileItIsRecent() async throws {+ let directory = try TemporaryDirectory()+ let folder = try directory.directory("late")+ try directory.file("late/a.bin", bytes: 4_096)+ let walks = Mutex(0)+ let walking: FileSize.Walker = { url, scan, isStopped in+ defer { walks.withLock { $0 += 1 } }+ return FileSize.walk(url, countingFor: scan, unless: isStopped)+ }+ func ask(within budget: TimeInterval, keepingLateAnswersFor life: Duration) async -> FolderContents? {+ await FileSize.contents(of: folder, within: budget, walking: walking, keepingLateAnswersFor: life)+ }+ func landALateAnswer() async throws {+ let before = walks.withLock { $0 }+ #expect(await ask(within: 0, keepingLateAnswersFor: .zero) == nil)+ while walks.withLock({ $0 }) == before { try await Task.sleep(for: .milliseconds(5)) }+ try await Task.sleep(for: .milliseconds(50))+ }++ try await landALateAnswer()+ #expect(await ask(within: 5, keepingLateAnswersFor: .seconds(60)) != nil)+ #expect(walks.withLock { $0 } == 1, "a recent answer was walked for again")++ try await landALateAnswer()+ try await Task.sleep(for: .milliseconds(100))+ #expect(await ask(within: 5, keepingLateAnswersFor: .milliseconds(50)) != nil)+ #expect(walks.withLock { $0 } == 3, "an old answer was handed over")+ }+/// A walk nobody wants anymore stops at the next entry instead of reading the folder to its end.@Test func aWalkNobodyWantsStopsWhereItIs() throws {let directory = try TemporaryDirectory()