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
Give the helper's ledger to the person only once it is in the Trash
Țugui Dragoșb734f6c6b29a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex cd180092..9ed37a3e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -139,7 +139,7 @@ its changes the way [Keep a Changelog](https://keepachangelog.com/) suggests.#### Settings- Open at Login reads and changes the login item away from the main thread, so the window no longer waits for macOS.-- Remove Peel hands you the helper's ledger before the helper moves it to the Trash, so emptying the Trash needs no+- Remove Peel gives you the helper's ledger as the helper moves it to the Trash, so emptying the Trash needs noadministrator's password for it.#### Applicationsdiff --git a/Packages/PeelCore/Sources/PeelPrivileged/DirectoryHandle.swift b/Packages/PeelCore/Sources/PeelPrivileged/DirectoryHandle.swiftindex decd1c43..bfa1f01e 100644--- a/Packages/PeelCore/Sources/PeelPrivileged/DirectoryHandle.swift+++ b/Packages/PeelCore/Sources/PeelPrivileged/DirectoryHandle.swift@@ -102,6 +102,11 @@ public final class DirectoryHandle: Sendable {name.withCString { fchownat(descriptor, $0, user, gid_t(bitPattern: -1), AT_SYMLINK_NOFOLLOW) == 0 }}+ /// Makes this folder itself belong to `user`, wherever it is now and whatever names it there; its group stays.+ public func giveItself(to user: uid_t) -> Bool {+ fchown(descriptor, user, gid_t(bitPattern: -1)) == 0+ }+/// The owner of the directory itself, read through the descriptor.public func ownerIdentifier() -> uid_t? {var info = stat()diff --git a/Packages/PeelCore/Sources/PeelPrivileged/HelperLedger.swift b/Packages/PeelCore/Sources/PeelPrivileged/HelperLedger.swiftindex 01c9a785..336666ca 100644--- a/Packages/PeelCore/Sources/PeelPrivileged/HelperLedger.swift+++ b/Packages/PeelCore/Sources/PeelPrivileged/HelperLedger.swift@@ -82,6 +82,19 @@ public final class HelperLedger: Sendable {_ folder: String = defaultFolder,into trash: DirectoryHandle,givingTo user: uid_t+ ) -> Result<String?, POSIXError> {+ moveFolder(folder, into: trash, givingTo: user) { folder, entry, owner in+ entry.map { folder.give($0, to: owner) } ?? folder.giveItself(to: owner)+ }+ }++ /// `moveFolder(_:into:givingTo:)`, where `changingOwner` gives `folder` itself, or the entry named inside it, to+ /// an account.+ static func moveFolder(+ _ folder: String,+ into trash: DirectoryHandle,+ givingTo user: uid_t,+ changingOwner: (_ folder: DirectoryHandle, _ entry: String?, _ owner: uid_t) -> Bool) -> Result<String?, POSIXError> {lock.withLock {switch OpenItem.at(folder) {@@ -91,28 +104,29 @@ public final class HelperLedger: Sendable {guard let mode = item.mode, mode & S_IFMT == S_IFDIR, mode & 0o077 == 0, item.owner == geteuid() else {return .failure(POSIXError(.EPERM))}- // Given to `user` before it moves, so emptying the Trash asks for no password: in the Trash its- // entries could no longer be listed without Full Disk Access. Taken back if the move fails, so the- // ledger is never the user's where the helper reads it.- give(item, to: user)+ // Given to `user` so emptying the Trash asks for no password: the files while the folder is still+ // root's and closed to everyone else, since in the Trash they could no longer be listed without Full+ // Disk Access, and the folder itself only once it is there, through the descriptor held here, so the+ // ledger is never the user's where the helper reads it. A failed move takes the files back.+ let inside = item.parent.child(item.name)+ let entries = (try? FileManager.default.contentsOfDirectory(atPath: item.path)) ?? []+ func giveEntries(to owner: uid_t) {+ guard let inside else { return }+ for entry in entries {+ _ = changingOwner(inside, entry, owner)+ }+ }+ giveEntries(to: user)let moved = TrashMover.move(item, into: trash)- if case .failure = moved {- give(item, to: geteuid())+ switch moved {+ case .success: _ = inside.map { changingOwner($0, nil, user) }+ case .failure: giveEntries(to: geteuid())}return moved.map { $0 }}}}- private static func give(_ item: OpenItem, to user: uid_t) {- if let inside = item.parent.child(item.name) {- for entry in (try? FileManager.default.contentsOfDirectory(atPath: item.path)) ?? [] {- _ = inside.give(entry, to: user)- }- }- _ = item.parent.give(item.name, to: user)- }-public struct Unreadable: Error {}/// The place `item` was taken from, when this helper took it on behalf of `user`.diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HelperLedgerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HelperLedgerTests.swiftindex ecb54d30..d76ebb0c 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/HelperLedgerTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/HelperLedgerTests.swift@@ -172,6 +172,48 @@ struct HelperLedgerTests {#expect(FileManager.default.fileExists(atPath: path("root/Library/Caches", in: directory)))}+ @Test func givesTheLedgersFolderToThePersonOnlyOnceItIsInTheTrash() throws {+ let directory = try TemporaryDirectory()+ let policy = try policy(in: directory)+ let trash = try #require(policy.openTrash(ownedBy: getuid()))+ let trashPath = try #require(trash.currentPath)+ _ = try move("root/Library/Caches/com.example.plist", with: try ledger(in: directory), policy: policy, in: directory)+ var given: [(entry: String?, inTheTrash: Bool)] = []++ let moved = try HelperLedger.moveFolder(path("private", in: directory), into: trash, givingTo: 4_242) {+ folder, entry, _ in+ given.append((entry, folder.currentPath?.hasPrefix(trashPath + "/") == true))+ return true+ }.get()++ #expect(moved != nil)+ #expect(given.map(\.entry) == ["moved.plist", nil])+ #expect(given.map(\.inTheTrash) == [false, true])+ }++ @Test func takesTheLedgersFilesBackWhenItsFolderCannotMove() throws {+ let directory = try TemporaryDirectory()+ let policy = try policy(in: directory)+ _ = try move("root/Library/Caches/com.example.plist", with: try ledger(in: directory), policy: policy, in: directory)+ try directory.directory("closed")+ try directory.setPermissions(0o555, of: "closed")+ defer { try? directory.setPermissions(0o755, of: "closed") }+ let closed = try DirectoryHandle.at(path("closed", in: directory)).get()+ var given: [(entry: String?, owner: uid_t)] = []++ #expect(throws: POSIXError.self) {+ try HelperLedger.moveFolder(path("private", in: directory), into: closed, givingTo: 4_242) {+ _, entry, owner in+ given.append((entry, owner))+ return true+ }.get()+ }++ #expect(given.map(\.entry) == ["moved.plist", "moved.plist"])+ #expect(given.map(\.owner) == [4_242, geteuid()])+ #expect(FileManager.default.fileExists(atPath: path("private/moved.plist", in: directory)))+ }+@Test func setsAsideALedgerItCannotReadAsOneAndStartsAnother() throws {let directory = try TemporaryDirectory()let policy = try policy(in: directory)