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.

peel orphans --remove never asks the helper, by its own word

Țugui Dragoș0a8c2699d744
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
index 0b138b36..601edbbd 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
@@ -164,7 +164,8 @@ struct OrphansCommand: AsyncParsableCommand {
items.filter { wanted.contains($0.url) },
installedApps: apps,
scanner: scanner,
- using: service
+ using: service,
+ mayUseHelper: false
)
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanRemoval.swift b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanRemoval.swift
index cca95fec..5879b2b6 100644
--- a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanRemoval.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanRemoval.swift
@@ -2,18 +2,24 @@ import Foundation
public enum OrphanRemoval {
/// Moves `items` to the Trash, but only those that are still orphaned when checked again. An app installed
- /// since the scan may own some of them, so those stay in place and fail with `claimedSinceScan`.
+ /// since the scan may own some of them, so those stay in place and fail with `claimedSinceScan`. Without
+ /// `mayUseHelper`, as for `peel`, what needs an administrator is not tried and fails with `needsHelper`.
@concurrent
public static func trash(
_ items: [OrphanItem],
installedApps: [InstalledApp],
scanner: OrphanScanner,
- using trashService: TrashService
+ using trashService: TrashService,
+ mayUseHelper: Bool
) async -> TrashResult {
let orphaned = await scanner.stillOrphaned(items, installedApps: installedApps)
let kept = Set(orphaned.map(\.url))
let privileged = Set(orphaned.filter(\.requiresPrivileges).map(\.url))
- var result = await trashService.trash(orphaned.map(\.url), usingHelperFor: privileged)
+ let moving = mayUseHelper ? orphaned.map(\.url) : orphaned.map(\.url).filter { !privileged.contains($0) }
+ var result = await trashService.trash(moving, usingHelperFor: mayUseHelper ? privileged : [])
+ if !mayUseHelper {
+ result.failures += orphaned.filter(\.requiresPrivileges).map { TrashFailure(url: $0.url, reason: .needsHelper) }
+ }
result.failures += items.filter { !kept.contains($0.url) }.map { TrashFailure(url: $0.url, reason: .claimedSinceScan) }
return result
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OrphanRemovalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OrphanRemovalTests.swift
index 89f38d0d..ac478a8f 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OrphanRemovalTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OrphanRemovalTests.swift
@@ -22,11 +22,41 @@ struct OrphanRemovalTests {
}
let back = InstalledApp(url: URL(filePath: "/Applications/Back.app"), bundleIdentifier: "com.back.again", name: "Back")
- let result = await OrphanRemoval.trash(listed, installedApps: [back], scanner: scanner, using: trash)
+ let result = await OrphanRemoval.trash(listed, installedApps: [back], scanner: scanner, using: trash, mayUseHelper: true)
#expect(moved.withLock { $0 } == ["com.gone.app"])
#expect(result.trashed.map(\.originalURL.lastPathComponent) == ["com.gone.app"])
#expect(result.failures.map(\.url.lastPathComponent) == ["com.back.again.plist"])
#expect(result.failures.first?.reason == .claimedSinceScan)
}
+
+ @Test(.permissionsHold) func whatNeedsAnAdministratorStaysWhenTheHelperMayNotBeUsed() async throws {
+ let directory = try TemporaryDirectory()
+ try directory.file("home/Library/Caches/com.gone.app/cache.db")
+ let served = try directory.directory("root/Library/Application Support/com.gone.app")
+ try directory.setPermissions(0o555, of: served)
+ defer { try? FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: served.path(percentEncoded: false)) }
+ let environment = SearchEnvironment(
+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),
+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)
+ )
+ let scanner = OrphanScanner(environment: environment) { _ in false }
+ let listed = await scanner.scan(installedApps: []).groups.flatMap(\.items)
+ let helperWasAsked = Mutex<[URL]>([])
+ let trash = TrashService(environment: environment, moveThroughHelper: { urls in
+ helperWasAsked.withLock { $0 += urls }
+ return TrashResult()
+ }) { url in url }
+ let privileged = listed.filter(\.requiresPrivileges).map(\.url)
+ #expect(!privileged.isEmpty, "the fixture needs an item only an administrator can move")
+
+ let result = await OrphanRemoval.trash(listed, installedApps: [], scanner: scanner, using: trash, mayUseHelper: false)
+
+ #expect(helperWasAsked.withLock { $0 }.isEmpty)
+ #expect(result.failures.map(\.url) == privileged)
+ #expect(result.failures.allSatisfy { $0.reason == .needsHelper })
+
+ _ = await OrphanRemoval.trash(listed, installedApps: [], scanner: scanner, using: trash, mayUseHelper: true)
+ #expect(helperWasAsked.withLock { $0 } == privileged)
+ }
}
diff --git a/Peel/Model/OrphanLibrary.swift b/Peel/Model/OrphanLibrary.swift
index e80257f2..11bc6018 100644
--- a/Peel/Model/OrphanLibrary.swift
+++ b/Peel/Model/OrphanLibrary.swift
@@ -91,7 +91,8 @@ extension OrphanLibrary: CarriesSelection {
selected(in: group).filter { part.sizes.keys.contains($0.url) },
installedApps: apps.apps,
scanner: OrphanScanner(exclusions: exclusions),
- using: TrashService(exclusions: exclusions)
+ using: TrashService(exclusions: exclusions),
+ mayUseHelper: true
)
}