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.

Move a maker's folder an uninstall empties through the helper when its files went that way

Țugui Dragoșb12b5f9222eb
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index 19954349..95644745 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -468,12 +468,19 @@ public struct TrashService: Sendable {
let filesMoved = Set(more.trashed.map(\.originalURL))
result.note(stopped.compactMap(\.job).filter { filesMoved.contains($0.plist) })
await startAgain(stopped, unlessMoved: filesMoved)
- // Nobody asked for these folders, so one that stays is no failure, and they are not counted with the rest.
+ // Nobody asked for these folders, so one that stays is no failure, and they are not counted with the rest. A
+ // folder goes through the helper when what left it did.
+ let privileged = Set(privilegedURLs.map(PathPattern.comparablePath(of:)))
+ var throughHelper = more.trashed.map { PathPattern.comparablePath(of: $0.originalURL) }
+ .filter(privileged.contains)
for folder in MakersFolders(environment: environment, names: names).above(more.trashed.map(\.originalURL))
where MakersFolders.isEmpty(folder) {
+ let path = PathPattern.comparablePath(of: folder)
+ let helper: Set<URL> = throughHelper.contains { PathComponents.isPath($0, inside: path) } ? [folder] : []
let moved = await MoveCount.$current.withValue(nil) {
- await trash([folder], usingHelperFor: [], removal: removal, lettingTheirProgramsRun: []).trashed
+ await trash([folder], usingHelperFor: helper, removal: removal, lettingTheirProgramsRun: []).trashed
}
+ if !helper.isEmpty, !moved.isEmpty { throughHelper.append(path) }
result.trashed += moved
result.emptied.formUnion(moved.map(\.originalURL))
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
index eb372164..86903371 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
@@ -1169,4 +1169,54 @@ struct TrashServiceTests {
#expect(result.trashed.isEmpty)
#expect(result.failures.map(\.url) == [stayed])
}
+
+ @Test func aMakersFolderEmptiedThroughTheHelperGoesThroughIt() async throws {
+ let directory = try TemporaryDirectory()
+ let trash = try directory.directory("home/.Trash")
+ let root = PathPattern.comparablePath(of: try directory.directory("root"))
+ let app = try directory.directory("Applications/Notes.app")
+ let product = try directory.directory("root/Library/Application Support/Example/Notes")
+ let own = try directory.directory("home/Library/Application Support/Example/Notes")
+ let environment = SearchEnvironment(
+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),
+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)
+ )
+ let move: @Sendable (URL) throws -> URL = { url in
+ let destination = trash.appending(path: UUID().uuidString)
+ try FileManager.default.moveItem(at: url, to: destination)
+ return destination
+ }
+ let throughHelper = Mutex<[URL]>([])
+ let service = TrashService(
+ environment: environment,
+ moveThroughHelper: { urls in
+ throughHelper.withLock { $0 += urls }
+ var result = TrashResult()
+ for url in urls {
+ guard let trashed = try? move(url) else { continue }
+ result.trashed.append(TrashedItem(originalURL: url, trashedURL: trashed, date: .now))
+ }
+ return result
+ },
+ moveToTrash: { url in
+ guard !PathComponents.isPath(PathPattern.comparablePath(of: url), inside: root) else {
+ throw CocoaError(.fileWriteNoPermission)
+ }
+ return try move(url)
+ }
+ )
+ let names = MakersFolders.names(of: [InstalledApp(url: app, bundleIdentifier: "org.example.notes", name: "Notes")])
+
+ let result = await service.trash(
+ apps: [app], thenFiles: { _ in [product, own] }, usingHelperFor: [product], lettingTheirProgramsRun: [],
+ emptiedFoldersNamed: names
+ )
+
+ let makers = [product, own].map { $0.deletingLastPathComponent() }
+ #expect(makers.allSatisfy { $0.isMissing })
+ let emptied = Set(result.emptied.map(PathPattern.comparablePath(of:)))
+ #expect(emptied == Set(makers.map(PathPattern.comparablePath(of:))))
+ #expect(throughHelper.withLock { $0 } == [product, makers[0]])
+ #expect(result.failures.isEmpty)
+ }
}