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.

Find the Intel updaters apps keep beside their frameworks and resources

Țugui Dragoș37078d6178a0
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift b/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift
index f446c7bd..5504b5b4 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift
@@ -79,6 +79,9 @@ public enum IntelInspector {
/// The extensions of bundles that run as a process of their own.
static let processExtensions: Set<String> = ["app", "xpc"]
+ /// Folders of an app made for resources, where some apps still keep a process of their own, an updater above all.
+ static let resourceDirectories = ["Contents/Resources", "Contents/SharedSupport"]
+
@concurrent
public static func scan(
installedApps: [InstalledApp],
@@ -207,7 +210,10 @@ public enum IntelInspector {
/// Helpers, plug-ins, and plain binaries inside an app that only have Intel code.
static func intelOnlyBundles(inside bundle: URL) -> [URL] {
- frameworkProcesses(inside: bundle).filter(isIntelOnly(bundle:))
+ let resources = resourceDirectories.flatMap { directory in
+ processes(in: bundle.appending(path: directory, directoryHint: .isDirectory))
+ }
+ return (frameworkProcesses(inside: bundle) + resources).filter(isIntelOnlyProcess)
+ embeddedDirectories.flatMap { directory -> [URL] in
let folder = bundle.appending(path: directory, directoryHint: .isDirectory)
let names =
@@ -226,9 +232,9 @@ public enum IntelInspector {
}
/// Returns the processes under `Contents/Frameworks`: apps and XPC services kept there directly, as every
- /// Electron app keeps its helpers, and those in each framework's `XPCServices` and `Helpers` folders.
- /// `Versions/Current` is skipped: it is a link to one of the versions beside it, so following it would
- /// report the same bundle twice.
+ /// Electron app keeps its helpers, and those in each framework's `XPCServices` and `Helpers` folders, at the top
+ /// of each version, as Sparkle keeps its updater, and in its `Resources`, as Squirrel does. `Versions/Current` is
+ /// skipped: it is a link to one of the versions beside it, so following it would report the same bundle twice.
static func frameworkProcesses(inside bundle: URL) -> [URL] {
let frameworks = bundle.appending(path: "Contents/Frameworks", directoryHint: .isDirectory)
return entries(of: frameworks).flatMap { url -> [URL] in
@@ -237,16 +243,40 @@ public enum IntelInspector {
let versions = entries(of: url.appending(path: "Versions", directoryHint: .isDirectory))
.filter { $0.lastPathComponent != "Current" }
return (versions + [url]).flatMap { version in
- ["XPCServices", "Helpers"].flatMap { name in
+ let bundles = ["XPCServices", "Helpers"].flatMap { name in
// At the top of a versioned framework these are links into `Versions/Current`, already read.
let folder = version.appending(path: name, directoryHint: .isDirectory)
guard version != url || folder.isRealFolder else { return [URL]() }
return entries(of: folder).filter { processExtensions.contains($0.pathExtension.lowercased()) }
}
+ let resources = version.appending(path: "Resources", directoryHint: .isDirectory)
+ let kept = version != url || resources.isRealFolder ? processes(in: resources) : []
+ return bundles + processes(in: version) + kept
+ }
+ }
+ }
+
+ /// What in `folder` can run as a process of its own: a bundle that runs as one, or a file that can be run. Links
+ /// are passed over, since at the top of a framework they lead into a version read on its own.
+ private static func processes(in folder: URL) -> [URL] {
+ entries(of: folder).filter { url in
+ var info = stat()
+ guard lstat(url.path(percentEncoded: false), &info) == 0 else { return false }
+ switch info.st_mode & S_IFMT {
+ case S_IFDIR: return processExtensions.contains(url.pathExtension.lowercased())
+ case S_IFREG: return info.st_mode & 0o111 != 0
+ default: return false
}
}
}
+ /// Whether `url`, a bundle that runs as a process or a file that can be run, has only Intel code, and a file is a
+ /// program rather than a library another program loads.
+ private static func isIntelOnlyProcess(_ url: URL) -> Bool {
+ url.hasDirectoryPath || url.isRealFolder
+ ? isIntelOnly(bundle: url) : isIntelOnly(executable: url) && MachOHeader.isProgram(at: url)
+ }
+
private static func entries(of directory: URL) -> [URL] {
let names = (try? FileManager.default.contentsOfDirectory(atPath: directory.path(percentEncoded: false))) ?? []
return names.map { directory.appending(path: $0) }
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift
index 44b5b5d3..1ee7dcb1 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift
@@ -278,6 +278,38 @@ struct IntelInspectorTests {
])
}
+ @Test func findsTheUpdatersAnAppKeepsBesideItsFrameworksAndResources() throws {
+ let directory = try TemporaryDirectory()
+ _ = try bundle(directory, "Universal.app", cpuTypes: [intel, appleSilicon])
+ let sparkle = "Universal.app/Contents/Frameworks/Sparkle.framework"
+ let squirrel = "Universal.app/Contents/Frameworks/Squirrel.framework"
+ _ = try bundle(directory, "\(sparkle)/Versions/B/Updater.app", cpuTypes: [intel])
+ try directory.setPermissions(0o755, of: try directory.file("\(sparkle)/Versions/B/Autoupdate", contents: intelHeader(fileType: 2)))
+ // The framework's own library loads into the app's process.
+ try directory.setPermissions(0o755, of: try directory.file("\(sparkle)/Versions/B/Sparkle", contents: intelHeader(fileType: 6)))
+ try directory.setPermissions(0o755, of: try directory.file("\(squirrel)/Versions/A/Resources/ShipIt", contents: intelHeader(fileType: 2)))
+ try directory.file("\(squirrel)/Versions/A/Resources/Info.plist", bytes: 16)
+ for (link, destination) in [
+ ("\(sparkle)/Versions/Current", "B"), ("\(sparkle)/Autoupdate", "Versions/Current/Autoupdate"),
+ ("\(sparkle)/Updater.app", "Versions/Current/Updater.app"), ("\(squirrel)/Versions/Current", "A"),
+ ("\(squirrel)/Resources", "Versions/Current/Resources"),
+ ] {
+ try FileManager.default.createSymbolicLink(
+ atPath: directory.url.appending(path: link).path(percentEncoded: false),
+ withDestinationPath: destination
+ )
+ }
+ try directory.setPermissions(0o755, of: try directory.file("Universal.app/Contents/Resources/helper", contents: intelHeader(fileType: 2)))
+ try directory.file("Universal.app/Contents/Resources/strings.pak", bytes: 16)
+ _ = try bundle(directory, "Universal.app/Contents/Resources/Loaded.bundle", cpuTypes: [intel])
+ _ = try bundle(directory, "Universal.app/Contents/SharedSupport/Installer.app", cpuTypes: [intel])
+
+ let app = directory.url.appending(path: "Universal.app", directoryHint: .isDirectory)
+ let found = IntelInspector.intelOnlyBundles(inside: app)
+
+ #expect(found.map(\.lastPathComponent).sorted() == ["Autoupdate", "Installer.app", "ShipIt", "Updater.app", "helper"])
+ }
+
/// A Homebrew installed for Intel keeps every command in `/usr/local/bin` as a link into its `Cellar`. The
/// program the link leads to is what needs Rosetta, listed under the command's name.
@Test func findsTheIntelProgramACommandLinkLeadsTo() throws {