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.

Measure an area's places a few at a time, and keep a refusal apart from time

Țugui Dragoș0ffee3cff2b6
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/SpaceReport.swift b/Packages/PeelCore/Sources/PeelCore/Space/SpaceReport.swift
index aae510b5..f2321739 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/SpaceReport.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/SpaceReport.swift
@@ -430,39 +430,36 @@ public enum SpaceInventory {
}
}
- /// The total size of `urls` less what is excluded inside them. A folder read only in part counts what was seen; one
- /// that ran out of time (a file provider can stall a read for minutes), or holds an excluded place that did, is
- /// unknown.
+ /// The total size of `urls` less what is excluded inside them, a few at a time, since one that doesn't answer would
+ /// delay the rest by its whole budget. A folder read only in part counts what was seen; one that ran out of time (a
+ /// file provider can stall a read for minutes), or holds an excluded place that did, is unknown, and so is one
+ /// holding an excluded place macOS refused, though no time ran out.
private static func size(
of urls: [URL],
leaving exclusions: Exclusions,
measure: @escaping LeftoverScanner.Measure
) async -> Measured {
- var parts: [SizeTotal] = []
- var ranOutOfTime = false
- for url in urls {
- // A stopped scan asks about nothing more.
- guard !Task.isCancelled else {
- return Measured(size: SizeTotal(known: 0, isComplete: false), ranOutOfTime: true)
- }
- guard let contents = await measure(url) else {
- parts.append(SizeTotal(known: 0, isComplete: false))
- ranOutOfTime = true
- continue
+ let unknown = SizeTotal(known: 0, isComplete: false)
+ let measured = await urls.concurrentMap(width: LeftoverScanner.concurrentMeasurements) { url -> Measured in
+ guard !Task.isCancelled, let contents = await measure(url) else {
+ return Measured(size: unknown, ranOutOfTime: true)
}
var known = contents.size
- var isComplete = !contents.couldNotBeRead
for place in exclusions.places(inside: url) where !place.isMissing {
- guard let excluded = await measure(place), !excluded.couldNotBeRead else {
- (known, isComplete) = (0, false)
- ranOutOfTime = true
- break
- }
+ guard let excluded = await measure(place) else { return Measured(size: unknown, ranOutOfTime: true) }
+ guard !excluded.couldNotBeRead else { return Measured(size: unknown, ranOutOfTime: false) }
known -= excluded.size
}
- parts.append(SizeTotal(known: max(0, known), isComplete: isComplete))
+ return Measured(
+ size: SizeTotal(known: max(0, known), isComplete: !contents.couldNotBeRead), ranOutOfTime: false
+ )
}
- return Measured(size: SizeTotal(combining: parts), ranOutOfTime: ranOutOfTime)
+ // A stopped scan starts nothing more, so it answers for fewer places than it was given.
+ let isWhole = measured.count == urls.count
+ return Measured(
+ size: SizeTotal(combining: measured.map(\.size) + (isWhole ? [] : [unknown])),
+ ranOutOfTime: !isWhole || measured.contains(where: \.ranOutOfTime)
+ )
}
/// The folders Space empties. Each stays itself, since Space moves what is inside, never the folder.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SpaceInventoryTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SpaceInventoryTests.swift
index 9331be62..21ddc550 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SpaceInventoryTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SpaceInventoryTests.swift
@@ -1,6 +1,7 @@
import Foundation
@testable import PeelCore
import PeelPrivileged
+import Synchronization
import Testing
struct SpaceInventoryTests {
@@ -234,6 +235,29 @@ struct SpaceInventoryTests {
#expect(logs.size.known < 400_000)
}
+ @Test func measuresAFewPlacesOfAnAreaAtATime() async throws {
+ let directory = try TemporaryDirectory()
+ for index in 0..<8 {
+ try directory.file("home/Library/Containers/org.example.app\(index)/Data/Library/Logs/run.log", bytes: 16)
+ }
+ let running = Mutex((now: 0, most: 0))
+
+ let report = await SpaceInventory.scan(
+ home: directory.url.appending(path: "home", directoryHint: .isDirectory),
+ root: directory.url.appending(path: "root", directoryHint: .isDirectory),
+ minimumSize: 1
+ ) { url in
+ guard url.path(percentEncoded: false).contains("/Data/Library/Logs") else { return await LeftoverScanner.walk(url) }
+ running.withLock { $0.now += 1; $0.most = max($0.most, $0.now) }
+ try? await Task.sleep(for: .milliseconds(100))
+ running.withLock { $0.now -= 1 }
+ return FolderContents(size: 4_000, holdsRepository: false)
+ }
+
+ #expect(report.items.first { $0.id == "logs" }?.size.known == 32_000)
+ #expect(running.withLock { $0.most } == LeftoverScanner.concurrentMeasurements)
+ }
+
@Test func anExcludedPlaceThatCannotBeMeasuredLeavesTheAreaUnknown() async throws {
let directory = try TemporaryDirectory()
try directory.file("Library/Caches/org.example.kept/blob", bytes: 300_000)
@@ -249,6 +273,25 @@ struct SpaceInventoryTests {
#expect(report.items.first { $0.id == "caches" }?.size.reading == .unknown)
}
+ @Test(.permissionsHold) func anExcludedPlaceMacOSRefusesSaysNothingOfTime() async throws {
+ let directory = try TemporaryDirectory()
+ try directory.file("home/Library/Caches/org.example.kept/blob", bytes: 300_000)
+ try directory.file("home/Library/Logs/org.example.big/blob", bytes: 900_000)
+ try directory.file("home/Library/Caches/org.example.closed/blob", bytes: 16)
+ try directory.setPermissions(0, of: "home/Library/Caches/org.example.closed")
+ defer { try? directory.setPermissions(0o755, of: "home/Library/Caches/org.example.closed") }
+ let excluded = directory.url.appending(path: "home/Library/Caches/org.example.closed")
+
+ let report = await SpaceInventory.scan(
+ home: directory.url.appending(path: "home", directoryHint: .isDirectory),
+ root: directory.url.appending(path: "root", directoryHint: .isDirectory),
+ minimumSize: 1, exclusions: Exclusions(paths: [excluded]), measure: LeftoverScanner.walk
+ )
+
+ #expect(report.items.first { $0.id == "caches" }?.size.reading == .unknown)
+ #expect(report.items.first?.id == "logs", "what ran out of time comes first, and this didn't")
+ }
+
@Test func leavesWhatOthersOwnToThem() async throws {
let directory = try TemporaryDirectory()
try directory.file(".colima/machine/disk.img", bytes: 4096)