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.

Remove Peel takes the helper's ledger to the Trash too

Țugui Dragoș3e3f0c2b935f
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md
index e75255ce..98ec535e 100644
--- a/ARCHITECTURE.md
+++ b/ARCHITECTURE.md
@@ -165,8 +165,8 @@ where a Homebrew install takes them from.
| `/private/var/db/com.tuguidragos.Peel.Helper/` | The helper's ledger of what it moved. |
| Peel's preferences | Settings, the totals Home shows, what each tool found the last time it looked, and the last answer of each update check. |
-Remove Peel, in Settings, takes all of it to the Trash except the helper's ledger, which only the helper could
-move, and it unregisters the helper first. It is the only way Peel is removed: Peel's own page in Applications, and
+Remove Peel, in Settings, takes all of it to the Trash. The helper moves its own ledger there, since nothing else
+can, just before Peel unregisters it. It is the only way Peel is removed: Peel's own page in Applications, and
Peel among several chosen apps, list it and select nothing of it (`Uninstallation.isPeel`), and `peel uninstall`
refuses it.
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
index 1caaf094..a90a2bd2 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
@@ -132,6 +132,20 @@ public enum PrivilegedHelper {
}
}
+ /// Has the helper move its own ledger to this user's Trash, since nothing else can move it. Returns why the ledger
+ /// stayed, or nil once it is there or when there is none.
+ @concurrent
+ public static func moveLedgerToTrash() async -> TrashFailure? {
+ let folder = URL(filePath: HelperLedger.defaultFolder, directoryHint: .isDirectory)
+ guard folder.isThere else { return nil }
+ let failure: String? = await withHelper(fallback: Self.unavailable) { helper, finish in
+ helper.moveLedgerToTrash(version: HelperIdentity.protocolVersion) { failure in
+ finish(failure)
+ }
+ }
+ return failure.map { TrashFailure(url: folder, reason: .failed($0)) }
+ }
+
/// Returns nil on success, or a failure description.
@concurrent
static func restore(_ item: TrashedItem) async -> String? {
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/HelperInterface.swift b/Packages/PeelCore/Sources/PeelPrivileged/HelperInterface.swift
index 41f7dc24..dac34ded 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/HelperInterface.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/HelperInterface.swift
@@ -6,7 +6,7 @@ public enum HelperIdentity {
public static let machServiceName = "com.tuguidragos.Peel.Helper"
public static let launchdPlistName = "com.tuguidragos.Peel.Helper.plist"
/// Raise it whenever `PeelHelperProtocol` changes. The helper refuses requests that carry any other version.
- public static let protocolVersion = 5
+ public static let protocolVersion = 6
}
/// The complete set of privileged operations. The helper validates every argument itself.
@@ -28,6 +28,10 @@ public enum HelperIdentity {
/// Moves an item back from the user's Trash, when the helper's own ledger says it moved that item from
/// exactly there. Replies with nil on success, or a failure description.
func restoreItem(version: Int, fromTrashPath trashPath: String, toPath destination: String, withReply reply: @escaping @Sendable (String?) -> Void)
+
+ /// Moves the helper's own ledger to the user's Trash, for Remove Peel just before it unregisters the helper.
+ /// Replies with nil once it is there or when there is none, or a failure description.
+ func moveLedgerToTrash(version: Int, withReply reply: @escaping @Sendable (String?) -> Void)
}
public enum DaemonCommand: String, Sendable {
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/HelperLedger.swift b/Packages/PeelCore/Sources/PeelPrivileged/HelperLedger.swift
index cbcab9a6..ad89e56e 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/HelperLedger.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/HelperLedger.swift
@@ -13,7 +13,8 @@ public final class HelperLedger: @unchecked Sendable {
let date: Date
}
- public static let defaultURL = URL(filePath: "/private/var/db/\(HelperIdentity.helperIdentifier)/moved.plist")
+ public static let defaultFolder = "/private/var/db/\(HelperIdentity.helperIdentifier)"
+ public static let defaultURL = URL(filePath: defaultFolder + "/moved.plist")
private let url: URL
private let maximumEntries: Int
@@ -56,6 +57,23 @@ public final class HelperLedger: @unchecked Sendable {
}
}
+ /// Moves the folder the ledger lives in to `trash` and returns where it went, or nil when there is none. It is
+ /// for Remove Peel, just before the helper goes, since nothing else can move it, and it moves only a folder that
+ /// is the helper's own and closed to everyone else, as the ledger keeps its folder.
+ public static func moveFolder(_ folder: String = defaultFolder, into trash: DirectoryHandle) -> Result<String?, POSIXError> {
+ lock.withLock {
+ switch OpenItem.at(folder) {
+ case .failure(let error):
+ return error.code == .ENOENT ? .success(nil) : .failure(error)
+ case .success(let item):
+ guard let mode = item.mode, mode & S_IFMT == S_IFDIR, mode & 0o077 == 0, item.owner == geteuid() else {
+ return .failure(POSIXError(.EPERM))
+ }
+ return TrashMover.move(item, into: trash).map { $0 }
+ }
+ }
+ }
+
/// The place `item` was taken from, when this helper took it on behalf of `user`.
public func origin(of item: OpenItem, movedBy user: uid_t) -> String? {
Self.lock.withLock {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HelperLedgerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HelperLedgerTests.swift
index 27ee174f..af54b1a1 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HelperLedgerTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HelperLedgerTests.swift
@@ -113,6 +113,35 @@ struct HelperLedgerTests {
#expect(items.filter { ledger.origin(of: $0, movedBy: getuid()) == nil }.isEmpty, "a record was lost")
}
+ /// Remove Peel has the helper move its ledger to the Trash before it goes, since nothing else can move it. It
+ /// moves only the ledger's own folder, private to the helper, and nothing when there is none.
+ @Test func movesItsOwnFolderToTheTrashAndNothingElse() throws {
+ let directory = try TemporaryDirectory()
+ let policy = try policy(in: directory)
+ let trash = try #require(policy.openTrash(ownedBy: getuid()))
+ let folder = path("private", in: directory)
+
+ #expect(try HelperLedger.moveFolder(folder, into: trash).get() == nil, "there was no folder to move")
+
+ let ledger = try ledger(in: directory)
+ _ = try move("root/Library/Caches/com.example.plist", with: ledger, policy: policy, in: directory)
+ let moved = try #require(try HelperLedger.moveFolder(folder, into: trash).get())
+ #expect(moved.hasSuffix("/home/.Trash/private"))
+ #expect(FileManager.default.fileExists(atPath: moved + "/moved.plist"))
+ #expect(!FileManager.default.fileExists(atPath: folder))
+
+ try directory.directory("shared")
+ try directory.setPermissions(0o755, of: "shared")
+ #expect(throws: POSIXError.self, "a folder others can read into was moved") {
+ try HelperLedger.moveFolder(path("shared", in: directory), into: trash).get()
+ }
+ try FileManager.default.createSymbolicLink(atPath: folder, withDestinationPath: path("root/Library/Caches", in: directory))
+ #expect(throws: POSIXError.self, "a link in the ledger's place was followed") {
+ try HelperLedger.moveFolder(folder, into: trash).get()
+ }
+ #expect(FileManager.default.fileExists(atPath: path("root/Library/Caches", in: directory)))
+ }
+
@Test func keepsTheNewestEntriesAndOnlyInAFolderOfItsOwn() throws {
let directory = try TemporaryDirectory()
let policy = try policy(in: directory)
diff --git a/Peel/Model/SelfUninstall.swift b/Peel/Model/SelfUninstall.swift
index cb285be5..5a1667f4 100644
--- a/Peel/Model/SelfUninstall.swift
+++ b/Peel/Model/SelfUninstall.swift
@@ -42,7 +42,9 @@ final class SelfUninstall {
}
let isRegistered = { [helper] in helper.status == .enabled || helper.status == .requiresApproval }
+ var ledgerStayed: TrashFailure?
if isRegistered() {
+ ledgerStayed = await PrivilegedHelper.moveLedgerToTrash()
await helper.uninstall()
// A helper still registered would point into the Trash, so Peel moves only once the helper is gone.
guard !isRegistered() else {
@@ -57,8 +59,9 @@ final class SelfUninstall {
failure = result.failures.map { "\($0.url.abbreviatedPath)\n\($0.reason.explanation)" }.joined(separator: "\n\n")
return
}
- guard result.failures.isEmpty else {
- leftBehind = result.failures.map { "\($0.url.abbreviatedPath)\n\($0.reason.explanation)" }.joined(separator: "\n\n")
+ let stayed = result.failures + [ledgerStayed].compactMap(\.self)
+ guard stayed.isEmpty else {
+ leftBehind = stayed.map { "\($0.url.abbreviatedPath)\n\($0.reason.explanation)" }.joined(separator: "\n\n")
return
}
quit()
diff --git a/PeelHelper/HelperService.swift b/PeelHelper/HelperService.swift
index c361441a..85b20bc4 100644
--- a/PeelHelper/HelperService.swift
+++ b/PeelHelper/HelperService.swift
@@ -118,6 +118,20 @@ final class HelperService: NSObject, PeelHelperProtocol {
}
}
+ func moveLedgerToTrash(version: Int, withReply finish: @escaping @Sendable (String?) -> Void) {
+ lifetime.requestStarted()
+ let reply = tracked(finish)
+ guard Self.speaksThisVersion(version) else { return reply(HelperRefusal.outOfDate.rawValue) }
+ guard let caller = Self.caller() else { return reply(HelperRefusal.notAllowed.rawValue) }
+ guard let trash = PrivilegedPathPolicy(homeDirectory: caller.homeDirectory).openTrash(ownedBy: caller.user) else {
+ return reply(HelperRefusal.noTrash.rawValue)
+ }
+ switch HelperLedger.moveFolder(into: trash) {
+ case .success: reply(nil)
+ case .failure(let error): reply(error.localizedDescription)
+ }
+ }
+
func runDaemonCommand(version: Int, command: String, label: String, withReply finish: @escaping @Sendable (String?) -> Void) {
lifetime.requestStarted()
let reply = tracked(finish)
diff --git a/README.md b/README.md
index e0765afb..79515db6 100644
--- a/README.md
+++ b/README.md
@@ -345,8 +345,9 @@ too.
## Uninstall Peel
-In Settings > General, choose Remove Peel. It removes its helper and its login item, moves itself, the files that
-are certainly its own, and its folder in Application Support to the Trash, clears its settings, and quits. That
+In Settings > General, choose Remove Peel. It has its helper move its record of what it moved to the Trash, removes
+its helper and its login item, moves itself, the files that are certainly its own, and its folder in Application
+Support to the Trash, clears its settings, and quits. That
folder holds History, your exclusions, and the settings Peel saved when you reset an app.
If you installed Peel with Homebrew, Settings shows this command in its place, with a button that copies it. Run it