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.

Space and Installers never select what may exist nowhere else

Țugui Dragoș34511195ed7f
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/Installers.swift b/Packages/PeelCore/Sources/PeelCore/Space/Installers.swift
index 0649099f..143aef93 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/Installers.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/Installers.swift
@@ -24,6 +24,9 @@ public struct InstallerItem: Sendable, Hashable, Identifiable {
/// True when moving the item needs administrator access, so the move goes through the helper. A macOS
/// installer that Software Update downloaded is one: it belongs to root.
public var requiresPrivileges = false
+ /// Why the item is left for the person to choose, from what measuring it saw: it was not measured or not read,
+ /// or a wallet or a repository is inside.
+ public var heldBack: HoldBack?
public var id: URL { url }
@@ -64,7 +67,7 @@ public enum Installers {
exclusions: Exclusions = .none,
minimumSize: Int64 = Installers.minimumSize
) async -> InstallerScan {
- await scan(installedApps: installedApps, home: home, root: root, exclusions: exclusions, minimumSize: minimumSize, measure: FileSize.measure)
+ await scan(installedApps: installedApps, home: home, root: root, exclusions: exclusions, minimumSize: minimumSize, measure: LeftoverScanner.walk)
}
@concurrent
@@ -74,7 +77,7 @@ public enum Installers {
root: URL,
exclusions: Exclusions,
minimumSize: Int64,
- measure: FileSize.Measure
+ measure: LeftoverScanner.Measure
) async -> InstallerScan {
var items: [InstallerItem] = []
let downloads = ["Downloads", "Desktop", "Documents"].map { home.appending(path: $0, directoryHint: .isDirectory) }
@@ -86,13 +89,17 @@ public enum Installers {
let isInstaller = installerExtensions.contains(suffix)
let isArchive = suffix == "zip"
guard isInstaller || isArchive || firmwareExtensions.contains(suffix), !exclusions.excludes(url), !Task.isCancelled else { continue }
- let size = await measure(url)
+ let (size, heldBack) = await measured(url, by: measure)
guard isWorthARow(size, minimumSize) else { continue }
if isArchive {
guard let app = appInside(zip: url) else { continue }
- items.append(appInstaller(at: url, size: size, installedApps: installedApps, named: app))
+ items.append(appInstaller(at: url, size: size, heldBack: heldBack, installedApps: installedApps, named: app))
} else {
- items.append(isInstaller ? appInstaller(at: url, size: size, installedApps: installedApps) : firmware(at: url, size: size))
+ items.append(
+ isInstaller
+ ? appInstaller(at: url, size: size, heldBack: heldBack, installedApps: installedApps)
+ : firmware(at: url, size: size, heldBack: heldBack)
+ )
}
}
}
@@ -118,6 +125,12 @@ public enum Installers {
)
}
+ /// What measuring `url` gave: its size, and why what it saw leaves the item for the person to choose.
+ private static func measured(_ url: URL, by measure: LeftoverScanner.Measure) async -> (size: Int64?, heldBack: HoldBack?) {
+ let contents = await measure(url)
+ return (contents.flatMap { $0.couldNotBeRead ? nil : $0.size }, HoldBack.seen(in: contents))
+ }
+
/// True when `size` reaches `minimumSize`. An unknown size could be any size, so it is kept.
private static func isWorthARow(_ size: Int64?, _ minimumSize: Int64) -> Bool {
size.map { $0 >= minimumSize } ?? true
@@ -125,7 +138,7 @@ public enum Installers {
/// Makes the item for an app installer. For an archive, `name` is the app inside it, which is matched
/// instead of the file name: a download can be called anything, such as `download (3).zip`.
- static func appInstaller(at url: URL, size: Int64?, installedApps: [InstalledApp], named name: String? = nil) -> InstallerItem {
+ static func appInstaller(at url: URL, size: Int64?, heldBack: HoldBack?, installedApps: [InstalledApp], named name: String? = nil) -> InstallerItem {
let match = installedApp(named: name ?? url.deletingPathExtension().lastPathComponent, in: installedApps)
return InstallerItem(
url: url,
@@ -136,7 +149,8 @@ public enum Installers {
installedApp: match?.name,
isReadOnly: false,
notes: [],
- requiresPrivileges: FileAccess.requiresPrivilegesToRemove(url)
+ requiresPrivileges: FileAccess.requiresPrivilegesToRemove(url),
+ heldBack: heldBack
)
}
@@ -192,13 +206,13 @@ public enum Installers {
return afterTheName.contains { rest.hasPrefix($0) }
}
- static func macOSInstallers(in folders: [URL], exclusions: Exclusions, minimumSize: Int64, measure: FileSize.Measure) async -> [InstallerItem] {
+ static func macOSInstallers(in folders: [URL], exclusions: Exclusions, minimumSize: Int64, measure: LeftoverScanner.Measure) async -> [InstallerItem] {
var items: [InstallerItem] = []
for folder in folders {
for url in files(in: folder) where url.pathExtension.lowercased() == "app" && url.lastPathComponent.hasPrefix("Install macOS") {
guard !Task.isCancelled else { return items }
guard !exclusions.excludes(url) else { continue }
- let size = await measure(url)
+ let (size, heldBack) = await measured(url, by: measure)
guard isWorthARow(size, minimumSize) else { continue }
let version = AppInspector.infoDictionary(in: url.appending(path: "Contents", directoryHint: .isDirectory))?["CFBundleShortVersionString"] as? String
items.append(InstallerItem(
@@ -210,14 +224,15 @@ public enum Installers {
installedApp: nil,
isReadOnly: false,
notes: version.map { [.version($0)] } ?? [],
- requiresPrivileges: FileAccess.requiresPrivilegesToRemove(url)
+ requiresPrivileges: FileAccess.requiresPrivilegesToRemove(url),
+ heldBack: heldBack
))
}
}
return items
}
- static func firmwareFiles(home: URL, exclusions: Exclusions, minimumSize: Int64, measure: FileSize.Measure) async -> [InstallerItem] {
+ static func firmwareFiles(home: URL, exclusions: Exclusions, minimumSize: Int64, measure: LeftoverScanner.Measure) async -> [InstallerItem] {
let folders = ["iPhone", "iPad", "iPod", "Apple TV", "HomePod", "Watch"].map {
home.appending(path: "Library/iTunes/\($0) Software Updates", directoryHint: .isDirectory)
}
@@ -226,15 +241,15 @@ public enum Installers {
for url in files(in: folder) where firmwareExtensions.contains(url.pathExtension.lowercased()) {
guard !Task.isCancelled else { return items }
guard !exclusions.excludes(url) else { continue }
- let size = await measure(url)
+ let (size, heldBack) = await measured(url, by: measure)
guard isWorthARow(size, minimumSize) else { continue }
- items.append(firmware(at: url, size: size))
+ items.append(firmware(at: url, size: size, heldBack: heldBack))
}
}
return items
}
- static func firmware(at url: URL, size: Int64?) -> InstallerItem {
+ static func firmware(at url: URL, size: Int64?, heldBack: HoldBack?) -> InstallerItem {
InstallerItem(
url: url,
kind: .firmware,
@@ -243,13 +258,14 @@ public enum Installers {
date: creationDate(of: url),
installedApp: nil,
isReadOnly: false,
- notes: []
+ notes: [],
+ heldBack: heldBack
)
}
/// Lists the device backups in `folder`, with what each says about itself: the device, its system, the
/// date of the last backup, and whether it is encrypted.
- static func backups(in folder: URL, measure: FileSize.Measure) async -> [InstallerItem] {
+ static func backups(in folder: URL, measure: LeftoverScanner.Measure) async -> [InstallerItem] {
var items: [InstallerItem] = []
for url in files(in: folder) {
guard (try? url.resourceValues(forKeys: [.isDirectoryKey]).isDirectory) == true, !Task.isCancelled else { continue }
@@ -271,7 +287,7 @@ public enum Installers {
url: url,
kind: .deviceBackup,
name: info?["Device Name"] as? String ?? url.lastPathComponent,
- size: await measure(url),
+ size: await measured(url, by: measure).size,
date: info?["Last Backup Date"] as? Date ?? creationDate(of: url),
installedApp: nil,
isReadOnly: true,
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift b/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
index 95871b63..5dd9b09d 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
@@ -16,6 +16,9 @@ public enum SpaceRemoval {
/// The size of each removable child, which is what emptying frees. A child whose size is not known is
/// missing here but stays in `removable`.
public let sizes: [URL: Int64]
+ /// Why a removable child is left for the person to choose, from what measuring it saw. A child missing here
+ /// holds nothing that keeps it from being selected.
+ public var heldBack: [URL: HoldBack] = [:]
/// The open apps whose folders are left alone, each named once and sorted, by the names the user knows, not
/// the folder names. One app can write to several folders, by its name and by its identifier.
@@ -23,9 +26,9 @@ public enum SpaceRemoval {
Set(inUse.map(\.name)).sorted()
}
- /// What Peel selects of this plan for the person: every child it could measure.
+ /// What Peel selects of this plan for the person: every child it could measure that nothing holds back.
public var suggested: Set<URL> {
- Set(sizes.keys)
+ Set(sizes.keys).subtracting(heldBack.keys)
}
}
@@ -36,7 +39,7 @@ public enum SpaceRemoval {
exclusions: Exclusions = .none,
running: [String: String] = [:]
) async -> Plan {
- await plan(for: item, environment: environment, exclusions: exclusions, running: running, measure: FileSize.measure)
+ await plan(for: item, environment: environment, exclusions: exclusions, running: running, measure: LeftoverScanner.walk)
}
@concurrent
@@ -45,14 +48,20 @@ public enum SpaceRemoval {
environment: SearchEnvironment,
exclusions: Exclusions,
running: [String: String],
- measure: FileSize.Measure
+ measure: LeftoverScanner.Measure
) async -> Plan {
let children = children(of: item, environment: environment, exclusions: exclusions, running: running)
var sizes: [URL: Int64] = [:]
+ var heldBack: [URL: HoldBack] = [:]
for child in children.removable where !Task.isCancelled {
- sizes[child] = await measure(child)
+ let contents = await measure(child)
+ sizes[child] = contents.flatMap { $0.couldNotBeRead ? nil : $0.size }
+ heldBack[child] = HoldBack.seen(in: contents)
}
- return Plan(removable: children.removable, inUse: children.inUse, leftToDeveloper: children.leftToDeveloper, sizes: sizes)
+ return Plan(
+ removable: children.removable, inUse: children.inUse, leftToDeveloper: children.leftToDeveloper, sizes: sizes,
+ heldBack: heldBack
+ )
}
/// What of `item` can move now, measuring nothing: the check a move makes just before it moves, since an app
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/InstallersTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/InstallersTests.swift
index 15da799e..3f4b4e57 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/InstallersTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/InstallersTests.swift
@@ -18,7 +18,7 @@ struct InstallersTests {
let unanswered = Unanswered()
let stop = try await unanswered.stop {
- _ = await Installers.scan(installedApps: [], home: directory.url, root: directory.url, exclusions: .none, minimumSize: 0, measure: unanswered.measure)
+ _ = await Installers.scan(installedApps: [], home: directory.url, root: directory.url, exclusions: .none, minimumSize: 0, measure: unanswered.walk)
}
#expect(stop.took < .seconds(1))
@@ -139,7 +139,7 @@ struct InstallersTests {
let scan = await Installers.scan(installedApps: [], home: directory.url, root: directory.url, exclusions: .none, minimumSize: 100_000) { url in
asked.withLock { $0.append(url.lastPathComponent) }
- return await FileSize.allocatedSize(of: url, within: FileSize.budget)
+ return await FileSize.contents(of: url)
}
#expect(asked.withLock { $0 } == ["Thing-1.dmg"])
@@ -160,7 +160,7 @@ struct InstallersTests {
await Installers.scan(installedApps: [], home: home, root: home, exclusions: .none, minimumSize: 100_000) { _ in
measured.withLock { $0 += 1 }
while !Task.isCancelled { await Task.yield() }
- return 400_000
+ return FolderContents(size: 400_000, holdsRepository: false)
}
}
while measured.withLock({ $0 }) == 0 { await Task.yield() }
@@ -180,7 +180,7 @@ struct InstallersTests {
try directory.directory("Applications")
let scan = await Installers.scan(installedApps: [], home: directory.url, root: directory.url, exclusions: .none, minimumSize: 100_000) { url in
- url.lastPathComponent == "slow" ? nil : await FileSize.allocatedSize(of: url, within: FileSize.budget)
+ url.lastPathComponent == "slow" ? nil : await FileSize.contents(of: url)
}
let backups = scan.items(in: .deviceBackup)
@@ -189,7 +189,8 @@ struct InstallersTests {
#expect(try #require(backups.last?.size) >= 400_000)
}
- /// A package can be a folder. One that did not answer may be any size, so the floor does not drop it.
+ /// A package can be a folder. One that did not answer may be any size, so the floor does not drop it, and what
+ /// is inside is not known, so it is left for the person to choose.
@Test func keepsAnInstallerWhoseSizeIsNotKnown() async throws {
let directory = try TemporaryDirectory()
try directory.file("Downloads/Suite.mpkg/Contents/Packages/one.pkg", bytes: 16)
@@ -199,6 +200,7 @@ struct InstallersTests {
#expect(scan.items(in: .appInstaller).map(\.name) == ["Suite.mpkg"])
#expect(scan.items.first?.size == nil)
+ #expect(scan.items.first?.heldBack == .notMeasured)
}
/// A backup is the only thing here Peel won't move: it says what is in it and leaves it to Finder.
@@ -216,7 +218,7 @@ struct InstallersTests {
try (PropertyListSerialization.data(fromPropertyList: ["IsEncrypted": true], format: .xml, options: 0))
.write(to: backup.appending(path: "Manifest.plist"))
- let items = await Installers.backups(in: directory.url.appending(path: "Backup", directoryHint: .isDirectory), measure: FileSize.measure)
+ let items = await Installers.backups(in: directory.url.appending(path: "Backup", directoryHint: .isDirectory), measure: LeftoverScanner.walk)
let item = try #require(items.first)
#expect(item.name == "Zoë's iPhone")
@@ -226,7 +228,7 @@ struct InstallersTests {
let tablet = try directory.directory("Backup/00008112-00AB")
try (PropertyListSerialization.data(fromPropertyList: ["Product Type": "iPad13,4", "Product Version": "26.1"], format: .xml, options: 0))
.write(to: tablet.appending(path: "Info.plist"))
- let tablets = await Installers.backups(in: directory.url.appending(path: "Backup", directoryHint: .isDirectory), measure: FileSize.measure)
+ let tablets = await Installers.backups(in: directory.url.appending(path: "Backup", directoryHint: .isDirectory), measure: LeftoverScanner.walk)
#expect(tablets.first { $0.url.lastPathComponent == "00008112-00AB" }?.notes == [.name("iPad13,4"), .name("iPadOS 26.1")])
#expect(item.date == Date(timeIntervalSince1970: 1_700_000_000))
#expect(item.isReadOnly)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift
index 354fcf5f..56292efb 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift
@@ -172,6 +172,21 @@ struct SpaceRemovalTests {
#expect(try #require(plan.sizes.values.first) >= 4_000)
}
+ /// What may exist nowhere else is never selected for the person: a child with a wallet or a repository inside
+ /// is left for them to choose, with its reason.
+ @Test func aChildHoldingAWalletOrARepositoryIsLeftToChoose() async throws {
+ let directory = try TemporaryDirectory()
+ try directory.file("home/Library/Caches/com.coin.app/wallets/default", bytes: 100)
+ try directory.file("home/Library/Caches/com.tool.app/mirror/.git/HEAD", bytes: 100)
+ try directory.file("home/Library/Caches/com.gone.app/old.db", bytes: 100)
+
+ let plan = await SpaceRemoval.plan(for: item(directory), environment: environment(directory), running: [:])
+
+ let reasons = plan.removable.map { "\($0.lastPathComponent): \(plan.heldBack[$0].map(\.rawValue) ?? "none")" }.sorted()
+ #expect(reasons == ["com.coin.app: holdsAWallet", "com.gone.app: none", "com.tool.app: holdsRepository"])
+ #expect(plan.suggested.map(\.lastPathComponent) == ["com.gone.app"])
+ }
+
/// Each removed item is recorded with its size, so History shows what a cleanup freed. A child that did not
/// answer in time still goes, and only its size is unknown.
@Test func aChildThatDidNotAnswerStillGoesAndHasNoSize() async throws {
@@ -180,11 +195,12 @@ struct SpaceRemovalTests {
try directory.file("home/Library/Caches/com.gone.app/small.db", bytes: 4_000)
let plan = await SpaceRemoval.plan(for: item(directory), environment: environment(directory), exclusions: .none, running: [:]) { url in
- url.lastPathComponent == "com.slow.app" ? nil : await FileSize.allocatedSize(of: url, within: FileSize.budget)
+ url.lastPathComponent == "com.slow.app" ? nil : await FileSize.contents(of: url)
}
#expect(Set(plan.removable.map(\.lastPathComponent)) == ["com.slow.app", "com.gone.app"])
#expect(plan.sizes.keys.map(\.lastPathComponent) == ["com.gone.app"])
+ #expect(plan.heldBack.map { "\($0.key.lastPathComponent): \($0.value.rawValue)" } == ["com.slow.app: notMeasured"])
}
/// An area's plan is made again when the disk changes, and what the person chose in it stays chosen: a child
diff --git a/Peel/Views/Installers/InstallerDetailView.swift b/Peel/Views/Installers/InstallerDetailView.swift
index 2499cfd1..14d3d450 100644
--- a/Peel/Views/Installers/InstallerDetailView.swift
+++ b/Peel/Views/Installers/InstallerDetailView.swift
@@ -9,7 +9,7 @@ struct InstallerDetailView: View {
var body: some View {
let rows = items
- let hasNoteColumn = rows.contains { !$0.isReadOnly && detail(for: $0) != nil }
+ let hasNoteColumn = rows.contains { !$0.isReadOnly && (detail(for: $0) != nil || $0.heldBack != nil) }
List {
header(rows)
@@ -29,6 +29,7 @@ struct InstallerDetailView: View {
url: item.url,
icon: .file(item.url),
detail: detail(for: item),
+ warning: item.heldBack.map { String(localized: $0.explanation) },
size: item.size ?? 0,
isMeasured: item.size != nil,
isLocked: item.requiresPrivileges && !helper.canAct,
@@ -44,7 +45,7 @@ struct InstallerDetailView: View {
heading(kind.title, kind.explanation)
} actions: {
SelectAllButton(
- selectable: rows.filter { !$0.isReadOnly && !($0.requiresPrivileges && !helper.canAct) }.map(\.url),
+ selectable: rows.filter { !$0.isReadOnly && $0.heldBack == nil && !($0.requiresPrivileges && !helper.canAct) }.map(\.url),
selection: Bindable(installers).selectedURLs
)
}
diff --git a/Peel/Views/Space/SpaceDetailView.swift b/Peel/Views/Space/SpaceDetailView.swift
index b69db305..a5a3f344 100644
--- a/Peel/Views/Space/SpaceDetailView.swift
+++ b/Peel/Views/Space/SpaceDetailView.swift
@@ -101,10 +101,11 @@ struct SpaceDetailView: View {
url: url,
icon: .file(url),
detail: nil,
+ warning: plan.heldBack[url].map { String(localized: $0.explanation) },
size: plan.sizes[url] ?? 0,
isMeasured: plan.sizes[url] != nil,
isFirst: index == 0,
- hasNoteColumn: false,
+ hasNoteColumn: !plan.heldBack.isEmpty,
selection: space, isSelected: space.isSelected(url)
)
}
@@ -113,7 +114,7 @@ struct SpaceDetailView: View {
SectionHeaderLine {
heading("Inside", "What is selected goes to the Trash, and History can put it back. The folders it sits in stay, since macOS expects to find them.")
} actions: {
- SelectAllButton(selectable: rows, selection: Bindable(space).selectedURLs)
+ SelectAllButton(selectable: rows.filter { plan.heldBack[$0] == nil }, selection: Bindable(space).selectedURLs)
}
} footer: {
if !plan.appsToQuit.isEmpty || !plan.leftToDeveloper.isEmpty {