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.

Keep two copies of one system extension version apart

Țugui Dragoș8b43e85e94ca
diff --git a/Packages/PeelCore/Sources/PeelCore/Extensions/AppExtensions.swift b/Packages/PeelCore/Sources/PeelCore/Extensions/AppExtensions.swift
index 6b8aa2ef..fcda9b16 100644
--- a/Packages/PeelCore/Sources/PeelCore/Extensions/AppExtensions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Extensions/AppExtensions.swift
@@ -426,11 +426,12 @@ public enum AppExtensions {
guard count(in: lines, /^(\d+) extension\(s\)$/) == rows.count else { return nil }
let records = records()
return notingReplacements(rows.map { row, category in
- let record = record(for: row.identifier, version: row.version, among: records)
+ let record = record(for: row.identifier, version: row.version, state: row.state, among: records)
let installedFor = record.flatMap(installedFor)
return AppExtension(
- // During an update, one identifier is listed twice, once for the old version and once for the new.
- id: [row.identifier, row.version].compactMap(\.self).joined(separator: " "),
+ // During an update, one identifier is listed twice, once for the old version and once for the new,
+ // and in developer mode twice for one version: macOS's record names each copy.
+ id: record?.uniqueID ?? [row.identifier, row.version, row.state].compactMap(\.self).joined(separator: " "),
identifier: row.identifier,
name: row.name,
kind: .systemExtension,
@@ -518,6 +519,8 @@ public enum AppExtensions {
let container: String?
/// Where the extension was installed from, inside its app (`originPath`).
var origin: String? = nil
+ var state: String? = nil
+ var uniqueID: String? = nil
}
/// The path of the app a record says its extension was installed for: the app macOS holds it for, or, once
@@ -535,14 +538,20 @@ public enum AppExtensions {
/// The record of the extension `systemextensionsctl` listed: the one of its version, or the only one of its
/// identifier. During an update an identifier has a record per version, and the other version's copy is not
- /// this one: no path is better than a wrong one.
+ /// this one: no path is better than a wrong one. Two copies of one version, as developer mode allows, are told
+ /// apart by the state, which the record writes with underscores where the listing has spaces.
static func record(
for identifier: String,
version: String?,
+ state: String? = nil,
among records: [SystemExtensionRecord]
) -> SystemExtensionRecord? {
let named = records.filter { $0.identifier == identifier }
- return named.first { $0.version == version } ?? (named.count == 1 ? named.first : nil)
+ let ofTheVersion = named.filter { $0.version == version }
+ guard ofTheVersion.count < 2 else {
+ return ofTheVersion.first { $0.state != nil && $0.state == state?.replacing(" ", with: "_") }
+ }
+ return ofTheVersion.first ?? (named.count == 1 ? named.first : nil)
}
static func systemExtensionRecords(
@@ -560,7 +569,9 @@ public enum AppExtensions {
version: [short, build].compactMap(\.self).joined(separator: "/"),
staged: staged.flatMap(URL.init(string:)),
container: (entry["container"] as? [String: Any])?["bundlePath"] as? String,
- origin: entry["originPath"] as? String
+ origin: entry["originPath"] as? String,
+ state: entry["state"] as? String,
+ uniqueID: entry["uniqueID"] as? String
)
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppExtensionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppExtensionsTests.swift
index 2d00ab60..4ec28724 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppExtensionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppExtensionsTests.swift
@@ -283,6 +283,30 @@ struct AppExtensionsTests {
#expect(AppExtensions.record(for: "com.example.other", version: nil, among: [old, new]) == nil)
}
+ @Test func keepsTwoCopiesOfOneVersionApart() async throws {
+ func record(_ state: String, _ unique: String) -> AppExtensions.SystemExtensionRecord {
+ .init(
+ identifier: "com.example.filter", version: "2.0/200",
+ staged: URL(filePath: "/Library/SystemExtensions/\(unique)/com.example.filter.systemextension"),
+ container: "/Applications/Example.app", state: state, uniqueID: unique
+ )
+ }
+ let records = [record("terminated_waiting_to_uninstall_on_reboot", "OLD"), record("activated_enabled", "NEW")]
+ let listing = """
+ 2 extension(s)
+ --- com.apple.system_extension.network_extension (Go to 'System Settings' to modify these system extension(s))
+ enabled\tactive\tteamID\tbundleID (version)\tname\t[state]
+ *\t*\tABCDE12345\tcom.example.filter (2.0/200)\tExample Filter\t[activated enabled]
+ \t\tABCDE12345\tcom.example.filter (2.0/200)\tExample Filter\t[terminated waiting to uninstall on reboot]
+ """
+
+ let found = try #require(await AppExtensions.systemExtensions(run: { _, _ in listing }, records: records))
+
+ #expect(Set(found.map(\.id)).count == 2, "two rows of a list share an id")
+ #expect(found.first?.url?.pathComponents.contains("NEW") == true)
+ #expect(found.last?.url?.pathComponents.contains("OLD") == true)
+ }
+
/// An extension that waits for the user's approval has not been turned off. Calling it off would send the user
/// looking for a switch to turn back on.
@Test func tellsWaitingForApprovalApartFromTurnedOff() {