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 Intel Software's findings four at a time, and count what its walks read

Țugui Dragoș3eeff19c2b6d
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift b/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift
index 6caae63c..47c49f00 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift
@@ -19,7 +19,7 @@ public struct IntelFinding: Sendable, Hashable, Identifiable {
/// What it belongs to: the app it sits inside, or the launchd property list that starts it.
public let owner: String?
/// Nil when the size is not known because measuring it timed out or was refused. Unknown is not zero.
- public let size: Int64?
+ public var size: Int64?
/// For a plug-in, its kind, which says where its Intel code can load (`Plugin.Category.intelHost`).
public var pluginCategory: Plugin.Category?
/// The labels of the launchd jobs that run a program at it or inside it.
@@ -95,12 +95,14 @@ public enum IntelInspector {
@concurrent
public static func scan(
installedApps: [InstalledApp],
+ knownSizes: [URL: Int64] = [:],
backgroundItems: BackgroundItems.Scan = BackgroundItems.Scan(),
exclusions: Exclusions = .none,
environment: SearchEnvironment = .current
) async -> IntelScan {
await scan(
installedApps: installedApps,
+ knownSizes: knownSizes,
backgroundItems: backgroundItems,
exclusions: exclusions,
environment: environment,
@@ -111,10 +113,11 @@ public enum IntelInspector {
@concurrent
static func scan(
installedApps: [InstalledApp],
+ knownSizes: [URL: Int64] = [:],
backgroundItems: BackgroundItems.Scan = BackgroundItems.Scan(),
exclusions: Exclusions,
environment: SearchEnvironment = .current,
- measure: FileSize.Measure
+ measure: @escaping FileSize.Measure
) async -> IntelScan {
// A path can be found twice: `/Library/Audio/Plug-Ins/HAL` is a plug-in folder and sits inside a driver
// folder, and two launchd jobs often run the same program. The first kind to find a path keeps it.
@@ -136,7 +139,7 @@ public enum IntelInspector {
kind: .app,
name: app.name,
owner: nil,
- size: await measure(app.url),
+ size: nil,
))
continue
}
@@ -147,7 +150,7 @@ public enum IntelInspector {
kind: .insideApp,
name: url.lastPathComponent,
owner: app.name,
- size: await measure(url),
+ size: nil,
))
}
}
@@ -165,7 +168,7 @@ public enum IntelInspector {
kind: .plugin,
name: Plugins.name(of: plugin.url, info: info),
owner: nil,
- size: await measure(plugin.url),
+ size: nil,
pluginCategory: plugin.category,
))
}
@@ -179,7 +182,7 @@ public enum IntelInspector {
kind: kind,
name: url.lastPathComponent,
owner: nil,
- size: await measure(url),
+ size: nil,
))
}
}
@@ -193,7 +196,7 @@ public enum IntelInspector {
kind: .driver,
name: url.lastPathComponent,
owner: nil,
- size: await measure(url),
+ size: nil,
))
}
@@ -219,7 +222,7 @@ public enum IntelInspector {
kind: .backgroundItem,
name: item.label,
owner: item.plistURL?.lastPathComponent,
- size: await measure(program),
+ size: nil,
)
finding.job = item.id
findings.append(finding)
@@ -236,16 +239,28 @@ public enum IntelInspector {
kind: .commandLineTool,
name: tool.name,
owner: nil,
- size: await measure(tool.url),
+ size: nil,
))
}
+ // Measured once everything is found, a few at a time, as the other scans measure. The Applications list has
+ // measured the apps already, the same way.
+ let measured = await findings.concurrentMap(width: LeftoverScanner.concurrentMeasurements) { finding in
+ var finding = finding
+ if let known = knownSizes[finding.url] {
+ finding.size = known
+ } else {
+ finding.size = await measure(finding.url)
+ }
+ return finding
+ }
+
var named: Set<String> = []
let unread = unreadable.filter {
!exclusions.excludes($0) && named.insert(PathPattern.comparablePath(of: $0)).inserted
}
return IntelScan(
- findings: findings.sorted {
+ findings: measured.sorted {
$0.size != $1.size ? SizeTotal([$0.size]) > SizeTotal([$1.size]) : $0.name < $1.name
},
unreadable: unread,
@@ -343,6 +358,7 @@ public enum IntelInspector {
if let folder = FileAccess.refusedFolder(directory) { unreadable.append(folder) }
return []
}
+ ScanCount.current?.add(names.count)
return names.map { directory.appending(path: $0) }
}
@@ -414,6 +430,7 @@ public enum IntelInspector {
}
for case let item as URL in enumerator {
guard !Task.isCancelled else { break }
+ ScanCount.current?.add(1)
if driverExtensions.contains(item.pathExtension) { bundles.append(item) }
}
unreadable += refused.compactMap(FileAccess.refusedFolder)
@@ -441,6 +458,7 @@ public enum IntelInspector {
}
for case let item as URL in enumerator {
guard !Task.isCancelled else { break }
+ ScanCount.current?.add(1)
if driverExtensions.contains(item.pathExtension) {
// `skipDescendants()` after a file would skip the next folder.
if (try? item.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true {
diff --git a/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift b/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift
index 05769161..6236f388 100644
--- a/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift
@@ -242,6 +242,7 @@ public enum Plugins {
unreadable.append(directory)
return []
}
+ ScanCount.current?.add(names.count)
return names.map { directory.appending(path: $0) }
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift
index ecfb0527..c91ebfe1 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift
@@ -1,5 +1,6 @@
import Foundation
@testable import PeelCore
+import Synchronization
import Testing
struct IntelInspectorTests {
@@ -155,6 +156,62 @@ struct IntelInspectorTests {
)
}
+ @Test func countsWhatItReadsForThePagesFigure() async throws {
+ let directory = try TemporaryDirectory()
+ for index in 1...3 {
+ _ = try bundle(directory, "root/Library/Printers/Vendor/Driver \(index).plugin", cpuTypes: [appleSilicon])
+ }
+ let count = ScanCount()
+
+ _ = await ScanCount.$current.withValue(count) {
+ await IntelInspector.scan(
+ installedApps: [], exclusions: .none, environment: environment(in: directory), measure: { _ in 1 }
+ )
+ }
+
+ #expect(count.value > 0, "the page's figure stood still while Intel Software read")
+ }
+
+ @Test func measuresSeveralFindingsAtOnce() async throws {
+ let directory = try TemporaryDirectory()
+ for index in 1...6 {
+ _ = try bundle(directory, "root/Library/Printers/Driver \(index).plugin", cpuTypes: [intel])
+ }
+ let inFlight = Mutex((now: 0, most: 0))
+
+ let scan = await IntelInspector.scan(
+ installedApps: [], exclusions: .none, environment: environment(in: directory)
+ ) { _ in
+ inFlight.withLock {
+ $0.now += 1
+ $0.most = max($0.most, $0.now)
+ }
+ await Task.yield()
+ inFlight.withLock { $0.now -= 1 }
+ return 1
+ }
+
+ #expect(scan.findings.count == 6)
+ #expect(inFlight.withLock { $0.most } > 1, "each measurement waited for the one before")
+ }
+
+ @Test func takesAnAppsSizeFromTheApplicationsList() async throws {
+ let directory = try TemporaryDirectory()
+ let url = try bundle(directory, "Apps/Old.app", cpuTypes: [intel])
+ let app = InstalledApp(url: url, bundleIdentifier: "org.example.old", name: "Old", architectures: [.x86_64])
+ let measured = Mutex<[URL]>([])
+
+ let scan = await IntelInspector.scan(
+ installedApps: [app], knownSizes: [url: 42], exclusions: .none, environment: environment(in: directory)
+ ) { url in
+ measured.withLock { $0.append(url) }
+ return 1
+ }
+
+ #expect(scan.findings.map(\.size) == [42])
+ #expect(measured.withLock { $0 }.isEmpty, "Applications had measured it already")
+ }
+
@Test func tellsIntelOnlyBundlesFromUniversalOnes() throws {
let directory = try TemporaryDirectory()
let intelApp = try bundle(directory, "Intel.app", cpuTypes: [intel])
diff --git a/Peel/Model/IntelLibrary.swift b/Peel/Model/IntelLibrary.swift
index f91b3b9f..b440beab 100644
--- a/Peel/Model/IntelLibrary.swift
+++ b/Peel/Model/IntelLibrary.swift
@@ -16,14 +16,18 @@ final class IntelLibrary {
var findings: [IntelFinding] { scan?.findings ?? [] }
- private let read: @MainActor ([InstalledApp], Exclusions) async -> IntelScan
+ /// Reads the page from the installed apps, the sizes the Applications list measured, and the exclusions.
+ typealias Read = @MainActor ([InstalledApp], [URL: Int64], Exclusions) async -> IntelScan
+
+ private let read: Read
/// `read` scans everything the report covers, so it is complete even when the other pages were never opened.
init(
- read: @escaping @MainActor ([InstalledApp], Exclusions) async -> IntelScan = { installedApps, exclusions in
+ read: @escaping Read = { installedApps, sizes, exclusions in
async let backgroundItems = BackgroundItems.scan(exclusions: exclusions)
return await IntelInspector.scan(
installedApps: installedApps,
+ knownSizes: sizes,
backgroundItems: backgroundItems,
exclusions: exclusions
)
@@ -32,20 +36,21 @@ final class IntelLibrary {
self.read = read
}
- func refresh(installedApps: [InstalledApp]) async {
+ func refresh(installedApps: [InstalledApp], knownSizes: [URL: Int64]) async {
let read = read
- guard let result = await scanRun.run({ await read(installedApps, ExclusionsStore.shared.exclusions) }) else {
- return
- }
+ guard let result = await scanRun.run({
+ await read(installedApps, knownSizes, ExclusionsStore.shared.exclusions)
+ }) else { return }
show(result)
}
/// Reads the page again without showing a scan, once it has been read.
- func readAgain(installedApps: [InstalledApp]) async {
+ func readAgain(installedApps: [InstalledApp], knownSizes: [URL: Int64]) async {
guard scan != nil else { return }
let read = read
- guard let result = await scanRun.runQuietly({ await read(installedApps, ExclusionsStore.shared.exclusions) })
- else { return }
+ guard let result = await scanRun.runQuietly({
+ await read(installedApps, knownSizes, ExclusionsStore.shared.exclusions)
+ }) else { return }
show(result)
}
diff --git a/Peel/Views/Intel/IntelList.swift b/Peel/Views/Intel/IntelList.swift
index 57b80d87..abb620c1 100644
--- a/Peel/Views/Intel/IntelList.swift
+++ b/Peel/Views/Intel/IntelList.swift
@@ -86,7 +86,7 @@ struct IntelList: View {
.task(id: library.revision) {
guard library.hasLoaded else { return }
guard intel.scan == nil else {
- await intel.readAgain(installedApps: library.apps)
+ await intel.readAgain(installedApps: library.apps, knownSizes: library.sizes)
return
}
guard !intel.scanRun.wasStopped else { return }
@@ -94,7 +94,7 @@ struct IntelList: View {
}
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
guard library.hasLoaded else { return }
- Task { await intel.readAgain(installedApps: library.apps) }
+ Task { await intel.readAgain(installedApps: library.apps, knownSizes: library.sizes) }
}
.rescanOnExclusionChange("IntelList") { await scan() }
}
@@ -107,7 +107,7 @@ struct IntelList: View {
}
private func scan() async {
- await intel.refresh(installedApps: library.apps)
+ await intel.refresh(installedApps: library.apps, knownSizes: library.sizes)
}
/// What Peel couldn't read for this page: the apps the Applications list couldn't, and what the scan couldn't.
diff --git a/PeelTests/IntelLibraryTests.swift b/PeelTests/IntelLibraryTests.swift
index 2f7b3b8f..f87b7644 100644
--- a/PeelTests/IntelLibraryTests.swift
+++ b/PeelTests/IntelLibraryTests.swift
@@ -14,18 +14,18 @@ struct IntelLibraryTests {
@Test func readsAgainWithoutShowingAScan() async {
let reads = Reads()
let unread = URL(filePath: "/org.example/Vendor")
- let library = IntelLibrary { _, _ in
+ let library = IntelLibrary { _, _, _ in
reads.count += 1
reads.wereShownAsAScan.append(reads.library?.isScanning == true)
return IntelScan(findings: [], unreadable: reads.count > 1 ? [unread] : [])
}
reads.library = library
- await library.readAgain(installedApps: [])
+ await library.readAgain(installedApps: [], knownSizes: [:])
#expect(reads.count == 0, "nothing is read again before the page was read once")
- await library.refresh(installedApps: [])
- await library.readAgain(installedApps: [])
+ await library.refresh(installedApps: [], knownSizes: [:])
+ await library.readAgain(installedApps: [], knownSizes: [:])
#expect(library.scan?.unreadable == [unread])
#expect(reads.wereShownAsAScan == [true, false], "a read again is announced to nobody")