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
Say why an item that isn't the one the helper moved stays in the Trash
Țugui Dragoș9f549f57f300
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/HelperPutBack.swift b/Packages/PeelCore/Sources/PeelPrivileged/HelperPutBack.swiftindex f60d224c..f450aae1 100644--- a/Packages/PeelCore/Sources/PeelPrivileged/HelperPutBack.swift+++ b/Packages/PeelCore/Sources/PeelPrivileged/HelperPutBack.swift@@ -55,6 +55,8 @@ public enum HelperPutBack {case .success?:ledger.forget([trashed])return nil+ case .failure(.notTheItem)?:+ return HelperRefusal.notInRecord.rawValuecase .failure(let failure)?:return failure.explanation}diff --git a/Packages/PeelCore/Sources/PeelPrivileged/TrashMover.swift b/Packages/PeelCore/Sources/PeelPrivileged/TrashMover.swiftindex fff30deb..00088f3c 100644--- a/Packages/PeelCore/Sources/PeelPrivileged/TrashMover.swift+++ b/Packages/PeelCore/Sources/PeelPrivileged/TrashMover.swift@@ -58,7 +58,11 @@ public enum TrashMover {in directory: DirectoryHandle,exclusively: ExclusiveRename) -> Result<Void, POSIXError> {- if case .failure(let error) = stillThere(item) { return .failure(error) }+ switch presence(of: item) {+ case .there: break+ case .another: return .failure(POSIXError(.ENOENT))+ case .gone(let error): return .failure(error)+ }switch exclusively(item, name, directory) {case .failure(let error) where error.code == .ENOTSUP:return renameOntoPlaceholder(item, to: name, in: directory)@@ -133,14 +137,19 @@ public enum TrashMover {_ = name.withCString { unlinkat(directory.descriptor, $0, isFolder ? AT_REMOVEDIR : 0) }}- /// The identity `item` was opened with, while its name still leads to it.- private static func stillThere(_ item: OpenItem) -> Result<ItemIdentity, POSIXError> {+ private enum Presence {+ case there(ItemIdentity)+ case another+ case gone(POSIXError)+ }++ private static func presence(of item: OpenItem) -> Presence {var info = stat()guard item.name.withCString({ fstatat(item.parent.descriptor, $0, &info, AT_SYMLINK_NOFOLLOW) }) == 0 else {- return .failure(.last)+ return .gone(.last)}- guard let identity = item.identity, ItemIdentity(info) == identity else { return .failure(POSIXError(.ENOENT)) }- return .success(identity)+ guard let identity = item.identity, ItemIdentity(info) == identity else { return .another }+ return .there(identity)}static func renameExclusively(@@ -230,11 +239,13 @@ extension TrashMover {enum StagedFailure: Error {case failed(POSIXError)case refused(PrivilegedPathPolicy.Rejection)+ case notTheItemvar explanation: String {switch self {case .failed(let error): error.localizedDescriptioncase .refused(let rejection): rejection.explanation+ case .notTheItem: PrivilegedPathPolicy.Rejection.missing.explanation}}}@@ -252,9 +263,10 @@ extension TrashMover {exclusively: ExclusiveRename = renameExclusively) -> Result<String, StagedFailure> {let identity: ItemIdentity- switch stillThere(item) {- case .success(let opened): identity = opened- case .failure(let error): return .failure(.failed(error))+ switch presence(of: item) {+ case .there(let opened): identity = opened+ case .another: return .failure(.notTheItem)+ case .gone(let error): return .failure(.failed(error))}var staged = "\(user).\(UUID().uuidString).\(item.name)"while staged.utf8.count > Int(NAME_MAX) { staged.removeLast() }@@ -271,7 +283,7 @@ extension TrashMover {}}} else {- failure = .failed(POSIXError(.ENOENT))+ failure = .notTheItem}_ = place(staged, from: staging, in: item.parent, as: finderNames(of: item.name))return .failure(failure)diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AttackHelperPolicyTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AttackHelperPolicyTests.swiftindex 753f32ba..e3abffd8 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/AttackHelperPolicyTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/AttackHelperPolicyTests.swift@@ -213,7 +213,10 @@ extension AttackHelperPolicyTests {trashed, to: original, for: getuid(), policy: policy, ledger: { ledger }, exclusively: swappingIn("payload"))- #expect(refusal != nil, "ATTACK SUCCEEDED: root put the user's own object back where launchd reads daemons")+ #expect(+ refusal == HelperRefusal.notInRecord.rawValue,+ "ATTACK SUCCEEDED: root put the user's own object back where launchd reads daemons"+ )#expect(!FileManager.default.fileExists(atPath: original))let inTheTrash = Set(try FileManager.default.contentsOfDirectory(atPath: path("home/.Trash", in: directory)))#expect(inTheTrash.count == 2, "both objects stay in the Trash")@@ -239,7 +242,10 @@ extension AttackHelperPolicyTests {trashed, to: original, for: getuid(), policy: policy, ledger: { ledger }, exclusively: openedToEveryone)- #expect(refusal != nil, "ATTACK SUCCEEDED: a file anyone could rewrite went back where launchd reads daemons")+ #expect(+ refusal == PrivilegedPathPolicy.Rejection.loadsCode.explanation,+ "ATTACK SUCCEEDED: a file anyone could rewrite went back where launchd reads daemons"+ )#expect(!FileManager.default.fileExists(atPath: original))#expect(FileManager.default.fileExists(atPath: trashed))}@@ -260,7 +266,10 @@ extension AttackHelperPolicyTests {trashed, to: original, for: getuid(), policy: policy, ledger: { ledger }, exclusively: swappingIn("payload"))- #expect(refusal != nil, "ATTACK SUCCEEDED: root put back an object the helper never moved")+ #expect(+ refusal == HelperRefusal.notInRecord.rawValue,+ "ATTACK SUCCEEDED: root put back an object the helper never moved"+ )#expect(!FileManager.default.fileExists(atPath: original))}@@ -280,6 +289,7 @@ extension AttackHelperPolicyTests {))#expect(result.moved.isEmpty, "ATTACK SUCCEEDED: root moved an object it never judged")+ #expect(result.failed[original] == PrivilegedPathPolicy.Rejection.missing.explanation)#expect(try FileManager.default.contentsOfDirectory(atPath: path("home/.Trash", in: directory)).isEmpty)#expect(try String(contentsOfFile: original, encoding: .utf8) == "the user's")#expect(try ledger.origin(of: item, movedBy: getuid()) == nil)@@ -308,10 +318,10 @@ extension AttackHelperPolicyTests {exclusively: unsupported))- #expect(refusal != nil)+ #expect(refusal == POSIXError(.ENOTSUP).localizedDescription)#expect(!FileManager.default.fileExists(atPath: original), "the helper replaced a placeholder by name")#expect(FileManager.default.fileExists(atPath: trashed))- #expect(result.moved.isEmpty)+ #expect(result.failed[other] == POSIXError(.ENOTSUP).localizedDescription)#expect(FileManager.default.fileExists(atPath: other))}@@ -334,7 +344,7 @@ extension AttackHelperPolicyTests {trashed, to: original, for: getuid(), policy: policy, ledger: { ledger }, exclusively: takingTheName)- #expect(refusal != nil)+ #expect(refusal == POSIXError(.EEXIST).localizedDescription)#expect(try String(contentsOfFile: original, encoding: .utf8) == "another")#expect(try String(contentsOfFile: trashed, encoding: .utf8) == "the app's")let trash = try #require(policy.openTrash(ownedBy: getuid()))@@ -358,7 +368,10 @@ extension AttackHelperPolicyTests {let refusal = HelperPutBack.run(trashed, to: original, for: getuid(), policy: policy, ledger: { ledger })- #expect(refusal != nil, "the helper moved an item by name with nowhere to check it first")+ #expect(+ refusal == POSIXError(.EXDEV).localizedDescription,+ "the helper moved an item by name with nowhere to check it first"+ )#expect(FileManager.default.fileExists(atPath: trashed))#expect(!FileManager.default.fileExists(atPath: original))}