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.

Judge a gone app's files from when Peel saw it go

Țugui Dragoș1de48d4f57ae
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppMemory.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppMemory.swift
index 43bee084..bcdc74d1 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppMemory.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppMemory.swift
@@ -12,6 +12,9 @@ public struct RememberedApp: Sendable, Hashable, Codable, Identifiable {
public let lastPath: String
/// The folders its own product file named for its data (`InstalledApp.productFolders`).
public var productFolders: [ProductFolder]? = nil
+ /// When Peel, running, read the apps again and found it gone from where it was. Nil when it left while Peel was
+ /// closed, since a launch weeks later can't say when.
+ public var goneSince: Date? = nil
public var id: String { bundleIdentifier }
}
@@ -53,19 +56,30 @@ public struct AppMemory: Sendable {
/// Records the apps currently installed, keeping every app seen before. Reading, merging, and writing happen
/// under one lock, so two refreshes that overlap cannot lose each other's apps.
+ /// `gone` holds the identifiers a reading of the apps no longer found, against the reading before it while Peel
+ /// ran: each is noted as gone from then, once its place can be looked at and it isn't there.
@discardableResult
@concurrent
- public func remember(_ apps: [InstalledApp], now: Date = .now) async -> [RememberedApp] {
- FileLock.whileHeld(beside: url) { remember(apps, now: now, onTopOf: read()) }
+ public func remember(_ apps: [InstalledApp], gone: Set<String> = [], now: Date = .now) async -> [RememberedApp] {
+ FileLock.whileHeld(beside: url) { remember(apps, gone: gone, now: now, onTopOf: read()) }
}
/// `known` is nil when the file cannot be read or reached. Its contents are then unknown, so it is never
/// written over, and the apps currently installed are still returned for this run.
- private func remember(_ apps: [InstalledApp], now: Date, onTopOf known: [RememberedApp]?) -> [RememberedApp] {
+ private func remember(
+ _ apps: [InstalledApp], gone: Set<String>, now: Date, onTopOf known: [RememberedApp]?
+ ) -> [RememberedApp] {
var byIdentifier = Dictionary(
(known ?? []).map { ($0.bundleIdentifier, $0) },
uniquingKeysWith: { first, _ in first }
)
+ for identifier in gone {
+ guard var app = byIdentifier[identifier], app.goneSince == nil, app.stillInstalled() == nil else {
+ continue
+ }
+ app.goneSince = now
+ byIdentifier[identifier] = app
+ }
for app in apps where !app.isSystemProtected {
guard let identifier = app.bundleIdentifier else { continue }
byIdentifier[identifier] = RememberedApp(
diff --git a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanConfidence.swift b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanConfidence.swift
index cdf19e50..a6844806 100644
--- a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanConfidence.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanConfidence.swift
@@ -93,10 +93,10 @@ public struct OrphanConfidence: Sendable, Hashable {
}
let makerIsStillHere = group.rememberedApp?.teamIdentifier.map(installedTeams.contains) ?? false
// A folder's own date changes when files are taken out of it, so the last write on a leftover is often
- // the uninstall itself. If nothing was written later than `settling` after Peel last saw the app, a
- // recent date is the removal, not a sign that something still uses the folder.
+ // the uninstall itself. If nothing was written later than `settling` after Peel saw the app go, or else after
+ // it last saw the app, a recent date is the removal, not a sign that something still uses the folder.
if !makerIsStillHere, let app = group.rememberedApp, let written = lastWrite(in: group),
- written <= app.lastSeen.addingTimeInterval(settling) {
+ written <= (app.goneSince ?? app.lastSeen).addingTimeInterval(settling) {
return OrphanConfidence(level: .certain, reasons: [.appLeft(name: app.name, lastSeen: app.lastSeen)])
}
if let written = group.lastModified, now.timeIntervalSince(written) < recentlyWritten, written <= now {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppMemoryTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppMemoryTests.swift
index e5abad3d..c2380157 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppMemoryTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppMemoryTests.swift
@@ -25,6 +25,34 @@ struct AppMemoryTests {
#expect(Set(second.map(\.bundleIdentifier)) == ["net.shinyfrog.bear", "dev.zed.Zed"], "an app it saw before was forgotten")
}
+ @Test func notesWhenItSawAnAppGoOnlyWhereTheAppIsNoLongerThere() async throws {
+ let directory = try TemporaryDirectory()
+ let memory = AppMemory(url: directory.url.appending(path: "apps.json"))
+ try directory.file(
+ "Apps/Tunewell.app/Contents/Info.plist",
+ contents: PropertyListSerialization.data(
+ fromPropertyList: ["CFBundleIdentifier": "org.example.tunewell", "CFBundleName": "Tunewell"],
+ format: .xml, options: 0
+ )
+ )
+ let still = InstalledApp(
+ url: directory.url.appending(path: "Apps/Tunewell.app", directoryHint: .isDirectory),
+ bundleIdentifier: "org.example.tunewell", name: "Tunewell", teamIdentifier: nil
+ )
+ let seen = Date(timeIntervalSince1970: 1_800_000_000)
+ await memory.remember([still, app("Gone", "org.example.gone")], now: seen)
+
+ let left = seen.addingTimeInterval(3_600)
+ let after = await memory.remember([], gone: ["org.example.tunewell", "org.example.gone"], now: left)
+ #expect(after.first { $0.bundleIdentifier == "org.example.gone" }?.goneSince == left)
+ #expect(after.first { $0.bundleIdentifier == "org.example.tunewell" }?.goneSince == nil, "an app still in place")
+
+ let later = await memory.remember([], gone: ["org.example.gone"], now: left.addingTimeInterval(3_600))
+ #expect(later.first { $0.bundleIdentifier == "org.example.gone" }?.goneSince == left, "the first time stays")
+ let back = await memory.remember([app("Gone", "org.example.gone")], now: left.addingTimeInterval(7_200))
+ #expect(back.first { $0.bundleIdentifier == "org.example.gone" }?.goneSince == nil, "installed again")
+ }
+
/// A memory that leads nowhere cannot be read, so it is left as it is rather than replaced by a new file.
@Test func leavesAMemoryItCannotReachAsItIs() async throws {
let directory = try TemporaryDirectory()
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OrphanConfidenceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OrphanConfidenceTests.swift
index eacb1542..80cabb6f 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OrphanConfidenceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OrphanConfidenceTests.swift
@@ -62,6 +62,14 @@ struct OrphanConfidenceTests {
#expect(judged.reasons.contains { if case .appLeft = $0 { true } else { false } })
}
+ @Test("What the app wrote after Peel last listed it, before Peel saw it go, is no sign of something still running")
+ func writingBeforePeelSawItGo() {
+ var removed = left("Example", daysAgo: 7)
+ removed.goneSince = now.addingTimeInterval(-2 * 24 * 60 * 60)
+ let judged = OrphanConfidence.judge(group(written: 3 * 24 * 60 * 60, remembered: removed), now: now)
+ #expect(judged.level == .certain)
+ }
+
@Test("A write after the app was last seen is still something running")
func writingAfterwardsStillCounts() {
let removed = left("Example", daysAgo: 6)
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index cebd40ee..527d7d62 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -286,10 +286,11 @@ final class AppLibrary {
lastOpenedRevision += 1
return []
}
+ let gone = Set(apps.compactMap(\.bundleIdentifier)).subtracting(listed.compactMap(\.bundleIdentifier))
let changed = adopt(found)
// Remembered whenever the list changes, so an app installed and removed between two visits to Orphaned
// Files is still known. Done last, so nothing above is left half done while this waits.
- await AppMemory().remember(found)
+ await AppMemory().remember(found, gone: gone)
return changed
}