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.

Say nothing of an item that was already gone when its move came

Țugui Dragoșd033c7e4318e
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
index 1c687188..4c02d61e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
@@ -218,6 +218,9 @@ public enum PrivilegedHelper {
result.trashed.append(
TrashedItem(originalURL: url, trashedURL: found, date: .now, identity: .init(ofItemAt: found))
)
+ } else if (links[url] ?? nil) == nil, url.isMissing {
+ // Nothing was there when the request went, nor is now: neither moved nor a failure.
+ continue
} else {
result.failures.append(TrashFailure(url: url, reason: .failed(reply?.failed[path] ?? Self.unavailable)))
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index afd406b5..19954349 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -418,6 +418,8 @@ public struct TrashService: Sendable {
moved.add(path)
MoveCount.current?.add(1)
} catch {
+ // Nothing was there to move: like an item inside a folder that moved, neither moved nor a failure.
+ if Self.foundNothing(error), url.isMissing { continue }
let reason = Self.reason(for: error)
let isLocked = reason == .notPermitted && FileAccess.isLockedInFinder(url)
result.failures.append(TrashFailure(url: url, reason: isLocked ? .locked : reason))
@@ -741,6 +743,15 @@ public struct TrashService: Sendable {
return .failed(error.localizedDescription)
}
+ /// Whether a move failed because nothing was at the item's path.
+ static func foundNothing(_ error: any Error) -> Bool {
+ let error = error as NSError
+ return ([error] + error.underlyingErrors.map { $0 as NSError }).contains {
+ ($0.domain == NSPOSIXErrorDomain && $0.code == Int(ENOENT))
+ || ($0.domain == NSCocoaErrorDomain && $0.code == NSFileNoSuchFileError)
+ }
+ }
+
/// The item moved, but macOS did not say where and Peel could not find it.
struct MovedWithoutATrace: Error {}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
index eb7ab635..eb372164 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
@@ -1121,4 +1121,52 @@ struct TrashServiceTests {
#expect(result.trashed.first?.trashedURL.lastPathComponent == "moved 12.34.56.db")
#expect(result.failures.map(\.url) == [stayed])
}
+
+ @Test func anItemAlreadyGoneIsNeitherMovedNorAFailure() async throws {
+ let directory = try TemporaryDirectory()
+ let environment = SearchEnvironment(
+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),
+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)
+ )
+ let removalGuard = RemovalGuard(environment: environment)
+ let service = TrashService(environment: environment) {
+ try TrashService.moveToSystemTrash($0, refusal: removalGuard.refusal(of:))
+ }
+ let gone = try directory.directory("home/Library/Caches").appending(path: "org.example.gone")
+
+ let result = await service.trash([gone])
+
+ #expect(result.trashed.isEmpty)
+ #expect(result.failures.isEmpty)
+ }
+
+ @Test func movesTheRestWhenOneItemIsAlreadyGone() async throws {
+ let directory = try TemporaryDirectory()
+ let service = try service(in: directory)
+ let first = try directory.file("home/Library/Caches/org.example.first/first.db")
+ let last = try directory.file("home/Library/Caches/org.example.last/last.db")
+ let gone = directory.url.appending(path: "home/Library/Caches/org.example.gone")
+
+ let result = await service.trash([first, gone, last])
+
+ #expect(result.trashed.map(\.originalURL) == [first, last])
+ #expect(result.failures.isEmpty)
+ }
+
+ @Test func dropsTheHelpersFailureForAnItemThatWasNeverThere() throws {
+ let directory = try TemporaryDirectory()
+ let trash = try directory.directory("home/.Trash")
+ let gone = directory.url.appending(path: "root/Library/Application Support/Vendor/gone.db")
+ let stayed = try directory.file("root/Library/Application Support/Vendor/stayed.db")
+ let links = [gone: FileIdentity.Link.of(gone), stayed: FileIdentity.Link.of(stayed)]
+ let reply = HelperTrashReply(
+ moved: [:],
+ failed: [gone.path(percentEncoded: false): "No such file or directory", stayed.path(percentEncoded: false): "x"]
+ )
+
+ let result = PrivilegedHelper.result(for: [gone, stayed], reply: reply, links: links, trash: trash)
+
+ #expect(result.trashed.isEmpty)
+ #expect(result.failures.map(\.url) == [stayed])
+ }
}