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
Offer a package's folder whole only when all of it is the package's
Țugui Dragoș397c74cbb9ad
diff --git a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swiftindex 116297c3..52223d25 100644--- a/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift+++ b/Packages/PeelCore/Sources/PeelCore/Packages/PackageReceipts.swift@@ -372,7 +372,7 @@ public enum PackageReceipts {files: [String],installLocation: String,home: String = URL.homeDirectory.path(percentEncoded: false),- namesInside: (String) -> [String]? = { try? FileManager.default.contentsOfDirectory(atPath: $0) },+ namesInside: (String) -> [String]? = PackageReceipts.namesInside,exists: (String) -> Bool = { URL(filePath: $0).isThere }) -> (onDisk: [String], offered: [String], othersFilled: Set<String>) {let root = (installLocation as NSString).standardizingPath@@ -396,38 +396,63 @@ public enum PackageReceipts {var othersFilled: Set<String> = []let onDisk = outermost.flatMap {- onlyWhatThisPackageWrote($0, listed: folded, namesInside: namesInside, othersFilled: &othersFilled)+ onlyWhatThisPackageWrote(+ $0, listed: folded, namesInside: namesInside, exists: exists, othersFilled: &othersFilled+ )}.sorted()return (onDisk, onDisk.filter { !ProtectedData.refuses($0, home: home) }, othersFilled)}- /// Returns `path` when this package installed everything inside it, and otherwise the parts inside it that- /// the package did install, so a vendor folder that two of its products share is never offered whole. A- /// bundle is always one item: an app that updated itself does not match its receipt file by file, and+ /// Returns `path` when this package installed everything inside it, at any depth, and otherwise the parts+ /// inside it that the package did install, so a vendor folder that two of its products share is never offered+ /// whole. A bundle is always one item: an app that updated itself does not match its receipt file by file, and/// its parts are never listed on their own. `listed` holds the package's paths in lowercase.static func onlyWhatThisPackageWrote(_ path: String,listed: Set<String>,namesInside: (String) -> [String]?,+ exists: (String) -> Bool,depth: Int = 0,othersFilled: inout Set<String>) -> [String] {- guard (path as NSString).pathExtension.isEmpty else { return [path] }- guard let names = namesInside(path) else { return [path] }+ guard let names = namesInside(path), !isBundle(path, exists: exists) else { return [path] }let inside = names.map { (path as NSString).appendingPathComponent($0) }- guard inside.contains(where: { !listed.contains($0.lowercased()) }) else { return [path] }let own = inside.filter { listed.contains($0.lowercased()) }+ func parts(_ depth: Int) -> [String] {+ own.flatMap {+ onlyWhatThisPackageWrote(+ $0, listed: listed, namesInside: namesInside, exists: exists, depth: depth,+ othersFilled: &othersFilled+ )+ }+ }+ if own.count == inside.count {+ let found = parts(depth)+ return found == own && !own.contains(where: othersFilled.contains) ? [path] : found+ }// Nothing inside is listed, or the search went as deep as it goes: the folder is still the only thing this// receipt can name, and what else is in it is not this package's.guard !own.isEmpty, depth < deepestDescent else {othersFilled.insert(path)return [path]}- return own.flatMap {- onlyWhatThisPackageWrote(- $0, listed: listed, namesInside: namesInside, depth: depth + 1, othersFilled: &othersFilled- )+ return parts(depth + 1)+ }++ /// What is inside a folder, or nil for a file or a link, which is one item whatever it leads to.+ static func namesInside(_ path: String) -> [String]? {+ var info = stat()+ guard lstat(path, &info) == 0, info.st_mode & S_IFMT == S_IFDIR else { return nil }+ return try? FileManager.default.contentsOfDirectory(atPath: path)+ }++ /// A folder with an extension and an `Info.plist` in `Contents`, or in `Resources` for a framework. A dot alone+ /// is no bundle: `Logitech.localized` is a plain folder.+ private static func isBundle(_ path: String, exists: (String) -> Bool) -> Bool {+ guard !(path as NSString).pathExtension.isEmpty else { return false }+ return ["Contents/Info.plist", "Resources/Info.plist"].contains {+ exists((path as NSString).appendingPathComponent($0))}}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swiftindex af264680..e1342fdf 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/PackageReceiptsTests.swift@@ -64,6 +64,66 @@ struct PackageReceiptsTests {#expect(items == ["/Applications/Example.app"])}+ @Test func offersAFolderWholeOnlyWhenNothingInsideItAtAnyDepthIsAnotherOnes() {+ let support = "/Library/Application Support"+ let tree: [String: [String]] = [+ "\(support)/ZeroTier": ["One"],+ "\(support)/ZeroTier/One": ["zerotier-one", "uninstall.sh", "identity.secret"],+ "\(support)/org.pqrs": ["Karabiner-DriverKit-VirtualHIDDevice", "config", "tmp"],+ "\(support)/org.pqrs/Karabiner-DriverKit-VirtualHIDDevice": ["scripts"],+ "\(support)/org.pqrs/Karabiner-DriverKit-VirtualHIDDevice/scripts": ["uninstall.sh"],+ "\(support)/Logitech.localized": ["LogiOptionsPlus", "LogiRightSightForWebcams"],+ "\(support)/Logitech.localized/LogiRightSightForWebcams": ["agent"],+ "\(support)/Vendor": ["Product"],+ "\(support)/Vendor/Product": ["Resources"],+ "\(support)/Vendor/Product/Resources": ["data.bin"],+ ]+ let files = [+ "ZeroTier", "ZeroTier/One", "ZeroTier/One/zerotier-one", "ZeroTier/One/uninstall.sh",+ "org.pqrs", "org.pqrs/Karabiner-DriverKit-VirtualHIDDevice",+ "org.pqrs/Karabiner-DriverKit-VirtualHIDDevice/scripts",+ "org.pqrs/Karabiner-DriverKit-VirtualHIDDevice/scripts/uninstall.sh",+ "Logitech.localized", "Logitech.localized/LogiRightSightForWebcams",+ "Logitech.localized/LogiRightSightForWebcams/agent",+ "Vendor", "Vendor/Product", "Vendor/Product/Resources", "Vendor/Product/Resources/data.bin",+ ]++ let found = PackageReceipts.topLevel(+ files: files,+ installLocation: support,+ namesInside: { tree[$0] },+ exists: { path in tree[path] != nil || files.contains { "\(support)/\($0)" == path } }+ )++ #expect(found.offered == [+ "\(support)/Logitech.localized/LogiRightSightForWebcams", "\(support)/Vendor",+ "\(support)/ZeroTier/One/uninstall.sh", "\(support)/ZeroTier/One/zerotier-one",+ "\(support)/org.pqrs/Karabiner-DriverKit-VirtualHIDDevice",+ ])+ #expect(found.othersFilled.isEmpty)+ }++ @Test func offersALinkThePackageInstalledAsOneItemWithoutLookingThrough() throws {+ let directory = try TemporaryDirectory()+ let root = directory.url.path(percentEncoded: false)+ try directory.file("Library/Application Support/Vendor/Versions/2/tool", bytes: 16)+ try directory.file("Library/Application Support/Vendor/Versions/2/settings.json", bytes: 16)+ try FileManager.default.createSymbolicLink(+ atPath: "\(root)/Library/Application Support/Vendor/Current", withDestinationPath: "Versions/2"+ )+ let files = [+ "Library/Application Support/Vendor", "Library/Application Support/Vendor/Current",+ "Library/Application Support/Vendor/Versions", "Library/Application Support/Vendor/Versions/2",+ "Library/Application Support/Vendor/Versions/2/tool",+ "Library/Application Support/Vendor/Versions/2/settings.json",+ ]++ let found = PackageReceipts.topLevel(files: files, installLocation: root)++ #expect(found.offered.map { ($0 as NSString).lastPathComponent } == ["Vendor"])+ #expect(found.othersFilled.isEmpty)+ }+@Test func resolvesPathsAgainstTheInstallLocationAndSkipsMissingFiles() {let files = ["Example.app", "Example.app/Contents", "Gone.app"]let items = PackageReceipts.topLevel(files: files, installLocation: "/Applications", exists: { $0 != "/Applications/Gone.app" }).offered@@ -115,6 +175,44 @@ struct PackageReceiptsTests {])}+ @Test func offersOnlyWhatThePackageWroteInFoldersOthersWroteInToo() async throws {+ let directory = try TemporaryDirectory()+ let volume = PathPattern.canonical(directory.url)+ try directory.file("Library/Application Support/Vendor Node/One/vendor-node", bytes: 16)+ try directory.file("Library/Application Support/Vendor Node/One/identity.secret", bytes: 16)+ try directory.file("Library/Application Support/org.example/Driver/driver.sh", bytes: 16)+ try directory.file("Library/Application Support/org.example/config/profile.json", bytes: 16)+ try directory.file("Library/Application Support/Example.plugin/Contents/Info.plist", bytes: 16)+ try directory.file("Library/Application Support/Example.plugin/Contents/Updated", bytes: 16)+ let listing = [+ "Vendor Node", "Vendor Node/One", "Vendor Node/One/vendor-node",+ "org.example", "org.example/Driver", "org.example/Driver/driver.sh",+ "Example.plugin", "Example.plugin/Contents", "Example.plugin/Contents/Info.plist",+ ].joined(separator: "\n")+ let pkgutil: PackageReceipts.Pkgutil = { arguments in+ switch arguments.first {+ case "--pkgs-plist":+ return PkgutilAnswer.packages("com.example.node")+ case "--pkg-info-plist":+ let info = ["volume": volume.path(percentEncoded: false), "install-location": "Library/Application Support"]+ let data = try? PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0)+ return data.flatMap { String(data: $0, encoding: .utf8) }+ case "--files":+ return listing+ default:+ return nil+ }+ }+ let environment = SearchEnvironment(homeDirectory: volume.appending(path: "home"), rootDirectory: volume)++ let scan = await PackageReceipts.list(exclusions: .none, pkgutil: pkgutil, environment: environment)++ let items = scan.receipts.flatMap(\.items).map {+ $0.url.path(percentEncoded: false).components(separatedBy: "Library/Application Support/").last ?? ""+ }+ #expect(items.sorted() == ["Example.plugin", "Vendor Node/One/vendor-node", "org.example/Driver"])+ }+@Test func leavesAnAppToFinderWhenMacOSInstalledItsSystemExtension() async throws {let directory = try TemporaryDirectory()let volume = PathPattern.canonical(directory.url)