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.

Reading the system apps no longer forgets the installed ones

Țugui Dragoș4b59cd22c5f4
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
index 64f35c64..78349ba6 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
@@ -65,10 +65,15 @@ public enum AppCatalog {
}
}
- Reading.forget(except: bundles)
+ Reading.forget(inside: directories, except: bundles)
return sorted(apps)
}
+ /// Whether what was read about `bundle` is kept, so the next reading of its folder does not read it again.
+ static func remembers(_ bundle: URL) -> Bool {
+ Reading.remembers(bundle)
+ }
+
/// A cache of what was read about each bundle. Reading a bundle costs its `Info.plist`, its signature, its
/// Mach-O header, and a Spotlight query, and the catalog is read again when Peel comes forward and on every
/// change in the app folders. Three `stat` calls tell whether a bundle changed. When it did not, only the
@@ -94,14 +99,22 @@ public enum AppCatalog {
return app
}
- /// Forgets every bundle not in `bundles`, such as an app that was removed or moved.
- static func forget(except bundles: Set<URL>) {
+ /// Forgets every bundle in `directories` that is not in `bundles`, such as an app that was removed or moved.
+ /// What other folders hold stays: the system apps are read on their own.
+ static func forget(inside directories: [URL], except bundles: Set<URL>) {
let paths = Set(bundles.map(PathPattern.comparablePath))
+ let folders = directories.map(PathPattern.comparablePath)
known.withLock { known in
- known = known.filter { paths.contains($0.key) }
+ known = known.filter { path, _ in
+ paths.contains(path) || !folders.contains { PathComponents.isPath(path, atOrInside: $0) }
+ }
}
}
+ static func remembers(_ bundle: URL) -> Bool {
+ known.withLock { $0[PathPattern.comparablePath(of: bundle)] != nil }
+ }
+
private static func identity(of bundle: URL) -> Identity {
let contents = bundle.appending(path: "Contents", directoryHint: .isDirectory)
let info = contents.appending(path: "Info.plist")
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
index 3f9bbcbb..c285426a 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
@@ -103,6 +103,23 @@ struct AppInspectorTests {
#expect(third == second)
}
+ /// The system apps are read on their own, and reading them must not forget what the Applications folders hold.
+ @Test func readingSomeFoldersForgetsOnlyWhatLeftThem() async throws {
+ let directory = try TemporaryDirectory()
+ try plist(["CFBundleIdentifier": "org.example.one", "CFBundleName": "One"], at: "One/One.app/Contents/Info.plist", in: directory)
+ try plist(["CFBundleIdentifier": "org.example.two", "CFBundleName": "Two"], at: "Two/Two.app/Contents/Info.plist", in: directory)
+ let (one, two) = (directory.url.appending(path: "One"), directory.url.appending(path: "Two"))
+
+ _ = await AppCatalog.installedApps(in: [one])
+ _ = await AppCatalog.installedApps(in: [two])
+ #expect(AppCatalog.remembers(one.appending(path: "One.app")))
+
+ try FileManager.default.moveItem(at: one.appending(path: "One.app"), to: directory.url.appending(path: "One.app"))
+ _ = await AppCatalog.installedApps(in: [one])
+ #expect(!AppCatalog.remembers(one.appending(path: "One.app")))
+ #expect(AppCatalog.remembers(two.appending(path: "Two.app")))
+ }
+
/// A launchd plist too large to be a job is not read, and neither is a link to something that is not a file
/// (reading `/dev/zero` never ends). A link to a real job is followed.
@Test func readsThroughALinkedLaunchdJobAndRefusesWhatIsNoJob() throws {