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.

List the iPhone and iPad apps installed on a Mac

Țugui Dragoșcd528223dc10
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppBundleLayout.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppBundleLayout.swift
new file mode 100644
index 00000000..a60c5085
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppBundleLayout.swift
@@ -0,0 +1,36 @@
+import Foundation
+internal import PeelPrivileged
+
+/// Where an app bundle keeps the Info.plist and the executable that describe the app. A Mac app keeps them in
+/// `Contents`. An iPhone or iPad app installed on a Mac is a wrapper: its own bundle sits in `Wrapper`, where the
+/// wrapper's `WrappedBundle` link leads, with both at its top and signed on its own.
+struct AppBundleLayout {
+ let infoFolder: URL
+ let executableFolder: URL
+ let signedBundle: URL
+ let isWrapped: Bool
+
+ init(of bundle: URL) {
+ if let wrapped = Self.wrappedBundle(in: bundle) {
+ (infoFolder, executableFolder, signedBundle, isWrapped) = (wrapped, wrapped, wrapped, true)
+ } else {
+ let contents = bundle.appending(path: "Contents", directoryHint: .isDirectory)
+ infoFolder = contents
+ executableFolder = contents.appending(path: "MacOS", directoryHint: .isDirectory)
+ (signedBundle, isWrapped) = (bundle, false)
+ }
+ }
+
+ /// The bundle `WrappedBundle` leads to, only when it names a bundle directly in `Wrapper`, so that a link
+ /// leading anywhere else names nothing.
+ static func wrappedBundle(in bundle: URL) -> URL? {
+ let link = bundle.appending(path: "WrappedBundle").path(percentEncoded: false)
+ guard let destination = try? FileManager.default.destinationOfSymbolicLink(atPath: link) else { return nil }
+ let names = PathComponents.of(destination)
+ guard names.count == 2, names[0] == "Wrapper", names[1].hasSuffix(".app"), names[1] != ".app" else {
+ return nil
+ }
+ return bundle.appending(path: "Wrapper", directoryHint: .isDirectory)
+ .appending(path: names[1], directoryHint: .isDirectory)
+ }
+}
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
index c69539d0..2d4bb032 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
@@ -152,7 +152,7 @@ public enum AppCatalog {
}
private static func identity(of bundle: URL) -> Identity {
- let contents = bundle.appending(path: "Contents", directoryHint: .isDirectory)
+ let contents = AppBundleLayout(of: bundle).infoFolder
let info = contents.appending(path: "Info.plist")
let values = try? info.resourceValues(forKeys: [.contentModificationDateKey, .fileSizeKey])
return Identity(
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
index a24d1428..93f251ca 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
@@ -58,20 +58,20 @@ public enum AppInspector {
private static let spotlightBudget: TimeInterval = 2
public static func inspect(_ url: URL) -> InstalledApp? {
- let contents = url.appending(path: "Contents", directoryHint: .isDirectory)
+ let layout = AppBundleLayout(of: url)
+ let contents = layout.infoFolder
guard
let info = infoDictionary(in: contents),
let bundleIdentifier = info["CFBundleIdentifier"] as? String,
!bundleIdentifier.isEmpty
else { return nil }
- let signing = signingInformation(for: url)
- let executable = (info["CFBundleExecutable"] as? String).map {
- contents.appending(path: "MacOS", directoryHint: .isDirectory).appending(path: $0)
- }
- let isFromAppStore = FileManager.default.fileExists(
- atPath: contents.appending(path: "_MASReceipt/receipt").path(percentEncoded: false)
- )
+ let signing = signingInformation(for: layout.signedBundle)
+ let executable = (info["CFBundleExecutable"] as? String).map { layout.executableFolder.appending(path: $0) }
+ // The App Store leaves its metadata beside a wrapped app, as it leaves a receipt inside a Mac app.
+ let receipt = layout.isWrapped
+ ? url.appending(path: "Wrapper/iTunesMetadata.plist") : contents.appending(path: "_MASReceipt/receipt")
+ let isFromAppStore = FileManager.default.fileExists(atPath: receipt.path(percentEncoded: false))
let isSystemProtected = isSystemProtected(url)
let use = use(of: url)
@@ -86,7 +86,7 @@ public enum AppInspector {
teamIdentifier: signing.teamIdentifier,
developer: signing.developer,
applicationGroups: signing.applicationGroups,
- embeddedBundleIdentifiers: embeddedBundleIdentifiers(in: contents),
+ embeddedBundleIdentifiers: embeddedBundleIdentifiers(in: contents, isWrapped: layout.isWrapped),
architectures: executable.map(MachOHeader.architectures(ofExecutableAt:)) ?? [],
isFromAppStore: isFromAppStore,
isSystemProtected: isSystemProtected,
@@ -123,7 +123,7 @@ public enum AppInspector {
/// Whether the bundle's Info.plist is there and macOS will not let Peel read it, so nothing can be told about
/// the app.
static func isClosed(_ bundle: URL) -> Bool {
- let info = bundle.appending(path: "Contents/Info.plist")
+ let info = AppBundleLayout(of: bundle).infoFolder.appending(path: "Info.plist")
return !info.isMissing && BoundedRead.data(at: info) == nil
}
@@ -158,7 +158,7 @@ public enum AppInspector {
return entitlements?["com.apple.security.application-groups"] as? [String] ?? []
}
- private static func embeddedBundleIdentifiers(in contents: URL) -> [String] {
+ private static func embeddedBundleIdentifiers(in contents: URL, isWrapped: Bool) -> [String] {
let fileManager = FileManager.default
var identifiers: [String] = []
@@ -169,7 +169,8 @@ public enum AppInspector {
continue
}
for child in children where embeddedBundleExtensions.contains(child.pathExtension.lowercased()) {
- let childContents = child.appending(path: "Contents", directoryHint: .isDirectory)
+ // An iOS bundle keeps its Info.plist at its top, so those inside a wrapped app do too.
+ let childContents = isWrapped ? child : child.appending(path: "Contents", directoryHint: .isDirectory)
if let identifier = infoDictionary(in: childContents)?["CFBundleIdentifier"] as? String {
identifiers.append(identifier)
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/DefaultApps.swift b/Packages/PeelCore/Sources/PeelCore/Apps/DefaultApps.swift
index 241620bc..1ac180cd 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/DefaultApps.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/DefaultApps.swift
@@ -26,8 +26,7 @@ public enum DefaultApps {
/// Returns the kinds of files and links `app` is currently the default for, sorted by name.
@concurrent
public static func roles(of app: InstalledApp) async -> [DefaultRole] {
- let contents = app.url.appending(path: "Contents", directoryHint: .isDirectory)
- guard let info = AppInspector.infoDictionary(in: contents) else { return [] }
+ guard let info = AppInspector.infoDictionary(in: AppBundleLayout(of: app.url).infoFolder) else { return [] }
return (fileKinds(declaredIn: info, app: app) + links(declaredIn: info, app: app))
.sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending }
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
index fedf2c3d..a8e94bbf 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
@@ -301,4 +301,49 @@ struct AppInspectorTests {
try data.write(to: url)
return url
}
+
+ /// An iPhone or iPad app on a Mac keeps its own bundle, with no `Contents`, where `WrappedBundle` leads.
+ @Test func readsAnIPhoneAppFromTheBundleItWraps() throws {
+ let directory = try TemporaryDirectory()
+ let inner = try directory.directory("Example.app/Wrapper/example.app")
+ let info: [String: Any] = [
+ "CFBundleIdentifier": "org.example.wrapped", "CFBundleName": "example", "CFBundleExecutable": "example",
+ "CFBundleShortVersionString": "3.4", "CFBundleVersion": "189",
+ ]
+ try PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0)
+ .write(to: inner.appending(path: "Info.plist"))
+ try directory.file("Example.app/Wrapper/example.app/example")
+ try directory.file("Example.app/Wrapper/iTunesMetadata.plist")
+ let bundle = directory.url.appending(path: "Example.app")
+ try FileManager.default.createSymbolicLink(
+ atPath: bundle.appending(path: "WrappedBundle").path(percentEncoded: false),
+ withDestinationPath: "Wrapper/example.app"
+ )
+
+ let share = try directory.directory("Example.app/Wrapper/example.app/PlugIns/Share.appex")
+ try PropertyListSerialization.data(fromPropertyList: ["CFBundleIdentifier": "org.example.wrapped.share"], format: .xml, options: 0)
+ .write(to: share.appending(path: "Info.plist"))
+
+ let app = try #require(AppInspector.inspect(bundle))
+ #expect(app.bundleIdentifier == "org.example.wrapped")
+ #expect(app.embeddedBundleIdentifiers == ["org.example.wrapped.share"])
+ #expect(app.name == "Example")
+ #expect(app.version == "3.4")
+ #expect(app.isFromAppStore)
+ }
+
+ @Test func aWrappedBundleLinkThatLeadsOutOfTheWrapperIsNoApp() throws {
+ let directory = try TemporaryDirectory()
+ let elsewhere = try directory.directory("Elsewhere/example.app")
+ try PropertyListSerialization.data(fromPropertyList: ["CFBundleIdentifier": "org.example.elsewhere"], format: .xml, options: 0)
+ .write(to: elsewhere.appending(path: "Info.plist"))
+ let bundle = try directory.directory("Example.app")
+ try directory.directory("Example.app/Wrapper")
+ try FileManager.default.createSymbolicLink(
+ atPath: bundle.appending(path: "WrappedBundle").path(percentEncoded: false),
+ withDestinationPath: "../Elsewhere/example.app"
+ )
+
+ #expect(AppInspector.inspect(bundle) == nil)
+ }
}