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.

A folder measured late is not left unknown until Peel is relaunched

Țugui Dragoș3214c9a56357
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift b/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift
index b71fd242..c622e50e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/FileSize.swift
@@ -285,7 +285,9 @@ private enum Walks {
let path = PathPattern.comparablePath(of: folder)
let isLastToLeave = answer.leave()
if let found = answer.taken() {
- if case .walking(let current) = entries[path], current === answer { entries[path] = nil }
+ // Handed over once: the question after this walks the folder again, even if an earlier waiter
+ // had given up on this walk.
+ if isOf(entries[path], answer) { entries[path] = nil }
return found
}
if isWithdrawn {
@@ -297,13 +299,18 @@ private enum Walks {
}
}
+ /// Whether `entry` is the walk `answer` belongs to, still going or given up on.
+ private static func isOf(_ entry: Entry?, _ answer: Answer) -> Bool {
+ switch entry {
+ case .walking(let current), .abandoned(let current): current === answer
+ case .landed, nil: false
+ }
+ }
+
static func finish(_ folder: URL, with contents: FolderContents?, into answer: Answer) {
entries.withLock { entries in
let path = PathPattern.comparablePath(of: folder)
- let isCurrent = switch entries[path] {
- case .walking(let current), .abandoned(let current): current === answer
- case .landed, nil: false
- }
+ let isCurrent = isOf(entries[path], answer)
guard let contents else {
// Stopped partway: the next question walks the folder again.
if isCurrent { entries[path] = nil }
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift
index 435ea438..3776e4b0 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileSizeTests.swift
@@ -1,5 +1,6 @@
import Foundation
@testable import PeelCore
+import Synchronization
import Testing
struct FileSizeTests {
@@ -197,6 +198,43 @@ struct FileSizeTests {
#expect(try #require(later) >= 8_192)
}
+ /// Someone may still be waiting when a late walk finishes: that one takes the answer, and the question after it
+ /// walks the folder again, so the folder is still unknown only once. The first waiter starts the walk and counts
+ /// what it reads, the second joins with no budget and gives up, and the first is then still waiting.
+ @Test func aLateWalkTakenBySomeoneStillWaitingLeavesTheFolderKnowable() async throws {
+ var isSetUp = false
+ for attempt in 1...3 where !isSetUp {
+ let directory = try TemporaryDirectory()
+ let folder = try directory.directory("big")
+ for index in 0..<(1_000 * attempt) {
+ try directory.file("big/folder\(index)/file.bin", bytes: 16)
+ }
+ let read = ScanCount()
+ let isAnswered = Mutex(false)
+ let patient = Task {
+ let contents = await ScanCount.$current.withValue(read) { await FileSize.contents(of: folder, within: 60) }
+ isAnswered.withLock { $0 = true }
+ return contents
+ }
+ while read.value == 0 { await Task.yield() }
+ #expect(await FileSize.contents(of: folder, within: 0) == nil)
+ guard !isAnswered.withLock({ $0 }) else {
+ _ = await patient.value
+ continue
+ }
+ isSetUp = true
+ #expect(await patient.value != nil)
+
+ var later: Int64?
+ for _ in 0..<200 where later == nil {
+ try await Task.sleep(for: .milliseconds(20))
+ later = await FileSize.allocatedSize(of: folder, within: 0)
+ }
+ #expect(later != nil, "the folder stayed unknown once the walk was taken")
+ }
+ #expect(isSetUp, "the walk always finished before the second question joined it")
+ }
+
/// A canceled question comes back at once with nothing. That says nothing about the folder, so it is not
/// marked as one that never answers, and the next question gets the real size.
@Test func aQuestionThatWasWithdrawnBlamesNothingOnTheFolder() async throws {