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.

Read the open files again before each request to the helper

Țugui Dragoșee64940e47d7
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index 4b197263..a32a0365 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -617,16 +617,7 @@ public struct TrashService: Sendable {
// find it gone and report a failure.
var paths = Folders()
permitted.forEach { paths.add(PathPattern.comparablePath(of: $0)) }
- var openFiles = OpenFiles.Fresh()
- var allowed: [URL] = []
- for url in permitted {
- guard !paths.hold(PathPattern.comparablePath(of: url)) else { continue }
- if let held = openFiles.refusal(of: url, lettingItsProgramsRun: apps.contains(url)) {
- result.failures.append(TrashFailure(url: url, reason: held))
- continue
- }
- allowed.append(url)
- }
+ let allowed = permitted.filter { !paths.hold(PathPattern.comparablePath(of: $0)) }
let jobs = LaunchdCleanup.jobs(for: allowed, environment: environment)
// A command-line tool's link leads into its app until the app has moved, and the helper takes such a link
// only once it leads nowhere, so the links go after everything else.
@@ -636,11 +627,22 @@ public struct TrashService: Sendable {
var helperResult = TrashResult()
// One request at a time, each answer written down before the next request goes, so a stop while the helper
// works loses no more than the request under way. The helper's own limit also bounds how many items an
- // answer that never comes can affect.
+ // answer that never comes can affect. The helper reads no open files, so they are read right before each
+ // request, and again once the last reading is older than `OpenFiles.lifetime`.
let limit = HelperRequest.maximumItems
+ var openFiles = OpenFiles.Fresh()
for batch in [allowed.filter { !links.contains($0) }, allowed.filter(links.contains)] {
for start in stride(from: 0, to: batch.count, by: limit) {
- let moved = await moveThroughHelper(Array(batch[start..<min(start + limit, batch.count)]))
+ var request: [URL] = []
+ for url in batch[start..<min(start + limit, batch.count)] {
+ if let held = openFiles.refusal(of: url, lettingItsProgramsRun: apps.contains(url)) {
+ result.failures.append(TrashFailure(url: url, reason: held))
+ } else {
+ request.append(url)
+ }
+ }
+ guard !request.isEmpty else { continue }
+ let moved = await moveThroughHelper(request)
journal?.note(moved.trashed, batch: removal)
MoveCount.current?.add(moved.trashed.count)
helperResult.trashed += moved.trashed
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
index 3ff10b00..b063a34b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
@@ -862,6 +862,40 @@ struct TrashServiceTests {
#expect(result.failures.map(\.reason) == [.heldOpen(by: ["cat"])])
}
+ @Test func readsTheOpenFilesAgainBeforeEachRequestToTheHelper() async throws {
+ let directory = try TemporaryDirectory()
+ let first = try (0..<HelperRequest.maximumItems).map {
+ try directory.file("home/Library/Caches/org.example.app/\($0).cache")
+ }
+ let last = try directory.file("home/Library/Caches/org.example.app/last.cache", bytes: 1_000_000)
+ let reader = Mutex<Process?>(nil)
+ defer { reader.withLock { $0?.terminate() } }
+ let service = TrashService(
+ environment: SearchEnvironment(
+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),
+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)
+ ),
+ moveThroughHelper: { urls in
+ if !urls.contains(last) {
+ let cat = Process()
+ cat.executableURL = URL(filePath: "/bin/cat")
+ cat.arguments = [last.path(percentEncoded: false)]
+ cat.standardOutput = Pipe()
+ try? cat.run()
+ reader.withLock { $0 = cat }
+ try? await Task.sleep(for: .milliseconds(1_050))
+ }
+ return TrashResult(trashed: urls.map { TrashedItem(originalURL: $0, trashedURL: $0, date: .now) })
+ },
+ moveToTrash: { _ in throw CocoaError(.fileWriteNoPermission) }
+ )
+
+ let result = await service.trash(first + [last], usingHelperFor: Set(first + [last]))
+
+ #expect(result.trashed.map(\.originalURL) == first)
+ #expect(result.failures.map(\.reason) == [.heldOpen(by: ["cat"])])
+ }
+
@Test func makesNoFolderForAPutBackTheGuardRefusesOnceTheFoldersAreHeld() throws {
let directory = try TemporaryDirectory()
let caches = try directory.directory("home/Library/Caches")