Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Refuse to move an excluded app, whatever its bundle is named
Țugui Dragoșa9f3066c45d3
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swiftindex d0734d93..a15b1026 100644--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift@@ -154,8 +154,11 @@ public enum AppInspector {/// The name Finder shows for a bundle, in the user's language when the app translates its name. Asking Launch/// Services for it registers the bundle, so one it does not know yet is named by its file.public static func displayName(of url: URL) -> String {- let identifier = infoDictionary(in: AppBundleLayout(of: url).infoFolder)?["CFBundleIdentifier"] as? String- return displayName(of: url, identifier: identifier)+ displayName(of: url, identifier: bundleIdentifier(ofAppAt: url))+ }++ static func bundleIdentifier(ofAppAt url: URL) -> String? {+ infoDictionary(in: AppBundleLayout(of: url).infoFolder)?["CFBundleIdentifier"] as? String}private static func displayName(of url: URL, identifier: String?) -> String {diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swiftindex 012ce690..b1257749 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift@@ -194,6 +194,7 @@ struct RemovalGuard: Sendable {if item == nil, names.contains(where: { syncsToOtherDevices(URL(filePath: $0)) }) { return .inTheCloud }// Last, since each reads what is a level or two inside the folder, and a name refused above needs none of it.+ guard !exclusions.isAnExcludedApp(item ?? URL(filePath: path)) else { return .excluded }for name in names {guard exclusions.appFolders(inside: URL(filePath: name, directoryHint: .isDirectory)).isEmpty else {return .excludeddiff --git a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swiftindex 4be3a7ef..60b6875b 100644--- a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift+++ b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift@@ -128,7 +128,7 @@ public struct Exclusions: Sendable, Codable, Hashable {/// True when something excluded sits inside `url`, so moving `url` would take it along.public func holds(_ url: URL) -> Bool {- guard !spellings.isEmpty || !appFolderNames.isEmpty else { return false }+ guard !spellings.isEmpty || !bundleIdentifiers.isEmpty else { return false }return holds(spellings: Self.spellings(of: url)) || !appFolders(inside: url).isEmpty}@@ -140,17 +140,29 @@ public struct Exclusions: Sendable, Codable, Hashable {}}- /// The excluded apps' folders a level or two inside `folder`. It reads the disk.+ /// The excluded apps' folders, and the excluded apps themselves, a level or two inside `folder`. It reads the disk.func appFolders(inside folder: URL) -> [URL] {- guard !appFolderNames.isEmpty else { return [] }+ guard !bundleIdentifiers.isEmpty else { return [] }let list = { (folder: URL) in(try? FileManager.default.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil)) ?? []}+ let isExcluded = { (url: URL) in+ appFolderNames.contains(url.lastPathComponent.lowercased()) || isAnExcludedApp(url)+ }return list(folder).flatMap { child in- if appFolderNames.contains(child.lastPathComponent.lowercased()) { return [child] }+ if isExcluded(child) { return [child] }guard child.isRealFolder else { return [] }- return list(child).filter { appFolderNames.contains($0.lastPathComponent.lowercased()) }+ return list(child).filter(isExcluded)+ }+ }++ /// Whether `url` is an app whose own Info.plist declares an excluded identifier: its bundle is named for the app,+ /// so no name says so. It reads the disk.+ func isAnExcludedApp(_ url: URL) -> Bool {+ guard !bundleIdentifiers.isEmpty, url.pathExtension.caseInsensitiveCompare("app") == .orderedSame else {+ return false}+ return AppInspector.bundleIdentifier(ofAppAt: url).map(excludes(bundleIdentifier:)) == true}/// The excluded places inside `url`, leaving out one inside another, so what they hold is counted once.diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swiftindex 877e8129..b3846b84 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift@@ -190,6 +190,64 @@ struct ExclusionsTests {#expect(own.isThere && maker.isThere)}+ private func app(_ path: String, identifier: String, in directory: borrowing TemporaryDirectory) throws -> URL {+ let info = try PropertyListSerialization.data(+ fromPropertyList: ["CFBundleIdentifier": identifier], format: .xml, options: 0+ )+ return try directory.file("\(path)/Contents/Info.plist", contents: info)+ .deletingLastPathComponent().deletingLastPathComponent()+ }++ @Test func refusesToRemoveAnExcludedAppOrAFolderHoldingIt() async throws {+ let directory = try TemporaryDirectory()+ let excluded = try app("home/Applications/Notes.app", identifier: "org.example.Notes", in: directory)+ let holder = try app("home/Downloads/Old/Notes.app", identifier: "org.example.Notes", in: directory)+ .deletingLastPathComponent()+ let other = try app("home/Applications/Other.app", identifier: "org.example.Other", in: directory)+ let trash = try directory.directory("Trash")+ let service = TrashService(+ environment: SearchEnvironment(+ homeDirectory: directory.url.appending(path: "home"),+ rootDirectory: directory.url.appending(path: "root")+ ),+ exclusions: Exclusions(bundleIdentifiers: ["org.example.Notes"])+ ) { url in+ let destination = trash.appending(path: url.lastPathComponent)+ try FileManager.default.moveItem(at: url, to: destination)+ return destination+ }++ let result = await service.trash([excluded, holder, other])++ #expect(result.trashed.map(\.originalURL) == [other])+ #expect(result.failures.map(\.reason) == [.guarded(.excluded), .guarded(.excluded)])+ #expect(excluded.isThere && holder.isThere)+ }++ @Test func putsNoExcludedAppBack() throws {+ let directory = try TemporaryDirectory()+ let trashed = try app("home/.Trash/Notes.app", identifier: "org.example.Notes", in: directory)+ let other = try app("home/.Trash/Other.app", identifier: "org.example.Other", in: directory)+ let service = TrashService(+ environment: SearchEnvironment(+ homeDirectory: directory.url.appending(path: "home"),+ rootDirectory: directory.url.appending(path: "root")+ ),+ exclusions: Exclusions(bundleIdentifiers: ["org.example.Notes"])+ ) { $0 }+ let applications = directory.url.appending(path: "home/Applications", directoryHint: .isDirectory)++ let refusal = service.refusal(ofPuttingBack: TrashedItem(+ originalURL: applications.appending(path: "Notes.app"), trashedURL: trashed, date: .now+ ))+ let control = service.refusal(ofPuttingBack: TrashedItem(+ originalURL: applications.appending(path: "Other.app"), trashedURL: other, date: .now+ ))++ #expect(refusal == .notAllowed)+ #expect(control == nil)+ }+@Test func refusesToRemoveAFolderThatHoldsSomethingExcluded() async throws {let directory = try TemporaryDirectory()let kept = try directory.file("home/Library/Application Support/Example/keep.db")