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.

Hold back a macOS installer or firmware that just arrived, as any installer

Țugui Dragoș470db438f050
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/Installers.swift b/Packages/PeelCore/Sources/PeelCore/Space/Installers.swift
index 7cb2f155..2bd0b2cd 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/Installers.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/Installers.swift
@@ -207,7 +207,10 @@ public enum Installers {
let installer = appInstaller(at: url, size: size, heldBack: heldBack, installedApps: installedApps)
items.append(unlessItMayStillBeNeeded(installer, origins: origins))
} else {
- items.append(firmware(at: url, size: size, heldBack: heldBack))
+ items.append(firmware(
+ at: url, size: size,
+ heldBack: heldBack ?? (arrivedRecently(url, newestInside: nil) ? .arrivedRecently : nil)
+ ))
}
}
}
@@ -228,7 +231,14 @@ public enum Installers {
syncKeeper: syncKeeper,
openFiles: openFiles
)
- items += await firmwareFiles(home: home, exclusions: exclusions, minimumSize: minimumSize, measure: measure)
+ items += await firmwareFiles(
+ home: home,
+ exclusions: exclusions,
+ minimumSize: minimumSize,
+ measure: measure,
+ syncKeeper: syncKeeper,
+ openFiles: openFiles
+ )
items += await updateDownloads(
home: home,
installedApps: installedApps,
@@ -315,16 +325,18 @@ public enum Installers {
installer.heldBack = .encryptedImage
} else if installer.installedApp == nil, origins.address(of: installer.url) == nil {
installer.heldBack = .mayBeTheOnlyCopy
- } else if arrivedRecently(installer.url) {
+ } else if arrivedRecently(installer.url, newestInside: nil) {
installer.heldBack = .arrivedRecently
}
return installer
}
- /// Whether `url` arrived in its folder or changed within `justArrived`, or when that can't be told.
- private static func arrivedRecently(_ url: URL) -> Bool {
+ /// Whether `url` arrived in its folder or changed within `justArrived`, or when that can't be told. A package
+ /// changes when what is inside it does (`newestInside`).
+ private static func arrivedRecently(_ url: URL, newestInside: Date?) -> Bool {
let values = try? url.resourceValues(forKeys: [.contentModificationDateKey, .addedToDirectoryDateKey])
- let latest = [values?.contentModificationDate, values?.addedToDirectoryDate].compactMap(\.self).max()
+ let latest = [values?.contentModificationDate, values?.addedToDirectoryDate, newestInside]
+ .compactMap(\.self).max()
return latest.map { Date.now.timeIntervalSince($0) < justArrived } ?? true
}
@@ -476,7 +488,7 @@ public enum Installers {
for url in files(in: folder.url) where url.pathExtension.lowercased() == "app" && isMacOSInstaller(url) {
guard !Task.isCancelled else { return items }
guard !exclusions.excludes(url), !exclusions.holds(url) else { continue }
- let (size, seen, _) = await measured(url, by: measure)
+ let (size, seen, newest) = await measured(url, by: measure)
guard isWorthARow(size, minimumSize) else { continue }
let info = AppInspector.infoDictionary(in: url.appending(path: "Contents", directoryHint: .isDirectory))
let version = AppInspector.stated(info?["CFBundleShortVersionString"])
@@ -492,7 +504,7 @@ public enum Installers {
requiresPrivileges: FileAccess.requiresPrivilegesToRemove(url),
heldBack: heldBack(
url, seen: seen, in: folder, syncKeeper: syncKeeper, openFiles: openFiles
- )
+ ) ?? (arrivedRecently(url, newestInside: newest) ? .arrivedRecently : nil)
))
}
}
@@ -505,13 +517,16 @@ public enum Installers {
["Install macOS", "Install OS X"].contains { url.lastPathComponent.hasPrefix($0) }
}
+ /// The folders macOS names for its iOS Files in storage (`iOSFilesStorageExtension`).
static func firmwareFiles(
home: URL,
exclusions: Exclusions,
minimumSize: Int64,
- measure: LeftoverScanner.Measure
+ measure: LeftoverScanner.Measure,
+ syncKeeper: (URL) -> SyncKeeper?,
+ openFiles: OpenFiles
) async -> [InstallerItem] {
- let folders = ["iPhone", "iPad", "iPod", "Apple TV", "HomePod", "Watch"].map {
+ let folders = ["iPhone", "iPad", "iPod", "Apple TV"].map {
home.appending(path: "Library/iTunes/\($0) Software Updates", directoryHint: .isDirectory)
}
var items: [InstallerItem] = []
@@ -519,9 +534,14 @@ 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), !exclusions.holds(url) else { continue }
- let (size, heldBack, _) = await measured(url, by: measure)
+ let (size, seen, _) = await measured(url, by: measure)
guard isWorthARow(size, minimumSize) else { continue }
- items.append(firmware(at: url, size: size, heldBack: heldBack))
+ let place = Folder(url: folder, isSharedWithEveryone: false)
+ let heldBack = heldBack(url, seen: seen, in: place, syncKeeper: syncKeeper, openFiles: openFiles)
+ items.append(firmware(
+ at: url, size: size,
+ heldBack: heldBack ?? (arrivedRecently(url, newestInside: nil) ? .arrivedRecently : nil)
+ ))
}
}
return items
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/InstallersTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/InstallersTests.swift
index 142ab152..00f1e744 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/InstallersTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/InstallersTests.swift
@@ -253,6 +253,14 @@ struct InstallersTests {
try #require(result == 0)
}
+ private func ageWhole(_ top: URL) throws {
+ let inside = FileManager.default.enumerator(at: top, includingPropertiesForKeys: nil)?
+ .compactMap { $0 as? URL } ?? []
+ for url in inside + [top] {
+ try age(url, changedHoursAgo: 2, addedHoursAgo: 2)
+ }
+ }
+
/// Dates `url` as changed and as added to its folder so many hours ago (`setattrlist(2)` sets the second).
private func age(_ url: URL, changedHoursAgo changed: Double, addedHoursAgo added: Double) throws {
let path = url.path(percentEncoded: false)
@@ -449,6 +457,9 @@ struct InstallersTests {
try directory.file("home/Downloads/Tool-1.dmg", bytes: 400_000)
try directory.file("home/Library/iTunes/iPhone Software Updates/iPhone17,1_26.1.ipsw", bytes: 400_000)
try directory.file("root/Applications/Install macOS Tahoe.app/Contents/MacOS/app", bytes: 400_000)
+ for path in ["home/Library/iTunes/iPhone Software Updates/iPhone17,1_26.1.ipsw", "root/Applications/Install macOS Tahoe.app"] {
+ try ageWhole(directory.url.appending(path: path))
+ }
let locked = ["home/Downloads", "home/Library/iTunes/iPhone Software Updates", "root/Applications"]
for folder in locked { try directory.setPermissions(0o555, of: folder) }
defer { for folder in locked { try? directory.setPermissions(0o755, of: folder) } }
@@ -693,6 +704,49 @@ struct InstallersTests {
#expect(scan.items(in: .firmware).map(\.name) == ["iPhone17,1_26.1.ipsw"])
}
+ @Test func holdsBackAMacOSInstallerOrFirmwareThatJustArrived() async throws {
+ let directory = try TemporaryDirectory()
+ for path in [
+ "Applications/Install macOS Tahoe.app/Contents/SharedSupport/SharedSupport.dmg",
+ "Applications/Install macOS Sequoia.app/Contents/SharedSupport/SharedSupport.dmg",
+ "Downloads/Fresh.ipsw", "Downloads/Old.ipsw", "Library/iTunes/iPhone Software Updates/Kept.ipsw",
+ ] {
+ try directory.file(path, bytes: 400_000)
+ }
+ for path in ["Applications/Install macOS Sequoia.app", "Downloads/Old.ipsw"] {
+ try ageWhole(directory.url.appending(path: path))
+ }
+
+ let scan = await Installers.scan(
+ installedApps: [], home: directory.url, root: directory.url, minimumSize: 100_000
+ )
+
+ let heldBack = Dictionary(uniqueKeysWithValues: scan.items.map { ($0.name, $0.heldBack) })
+ #expect(heldBack["Install macOS Tahoe"] == .arrivedRecently)
+ #expect(heldBack["Install macOS Sequoia"] == .some(nil))
+ #expect(heldBack["Fresh.ipsw"] == .arrivedRecently)
+ #expect(heldBack["Old.ipsw"] == .some(nil))
+ #expect(heldBack["Kept.ipsw"] == .arrivedRecently)
+ }
+
+ @Test func readsTheFirmwareFoldersMacOSNamesAndWhatHoldsThem() async throws {
+ let directory = try TemporaryDirectory()
+ try directory.file("Library/iTunes/HomePod Software Updates/HomePod.ipsw", bytes: 400_000)
+ try directory.file("Library/iTunes/Watch Software Updates/Watch.ipsw", bytes: 400_000)
+ let held = try directory.file("Library/iTunes/iPad Software Updates/iPad.ipsw", bytes: 400_000)
+ let handle = try FileHandle(forReadingFrom: held)
+ defer { try? handle.close() }
+ try directory.directory("Applications")
+
+ let scan = await Installers.scan(
+ installedApps: [], home: directory.url, root: directory.url, exclusions: .none, minimumSize: 100_000,
+ measure: LeftoverScanner.walk, openFiles: OpenFiles(excluding: nil)
+ )
+
+ #expect(scan.items(in: .firmware).map(\.name) == ["iPad.ipsw"])
+ #expect(scan.items(in: .firmware).first?.heldBack == .openInAProgram)
+ }
+
@Test func aPlaceThatLeadsIntoAnotherListsWhatIsThereOnce() async throws {
let directory = try TemporaryDirectory()
let moved = try directory.directory("Documents/Downloads")