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.

Find what the helper moved by the names it gives when its answer is lost

Țugui Dragoșf83df64887ca
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
index 3894a9f7..f333917a 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
@@ -222,7 +222,8 @@ public enum PrivilegedHelper {
)
)
} else if reply == nil, !url.isThere, let link = links[url] ?? nil,
- let found = TrashService.item(link, in: trash) {
+ let found = TrashService.item(link, in: trash)
+ ?? TrashService.item(link, named: url.lastPathComponent, in: trash) {
result.trashed.append(
TrashedItem(originalURL: url, trashedURL: found, date: .now, identity: .init(ofItemAt: found))
)
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index 849bed91..80d7eb80 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -957,6 +957,20 @@ public struct TrashService: Sendable {
let contents = (try? FileManager.default.contentsOfDirectory(at: trash, includingPropertiesForKeys: nil)) ?? []
return contents.first { FileIdentity.Link.of($0) == link }
}
+
+ /// The item in `trash` with the device and inode in `link`, under `name` or a name the helper gives when that one
+ /// is taken. Nothing is listed: without Full Disk Access macOS lets Peel search a Trash but not read it.
+ static func item(_ link: FileIdentity.Link, named name: String, in trash: URL) -> URL? {
+ guard let folder = try? DirectoryHandle.at(trash.path(percentEncoded: false)).get() else { return nil }
+ for attempt in TrashMover.attempts {
+ let candidate = TrashMover.name(name, attempt: attempt)
+ guard let info = folder.status(of: candidate) else { continue }
+ if FileIdentity.Link(device: info.st_dev, inode: info.st_ino) == link {
+ return trash.appending(path: candidate)
+ }
+ }
+ return nil
+ }
}
/// Folders a request takes, looked up by a path's own names, one level at a time, rather than compared one by one
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/DirectoryHandle.swift b/Packages/PeelCore/Sources/PeelPrivileged/DirectoryHandle.swift
index 0e905c80..5caa4135 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/DirectoryHandle.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/DirectoryHandle.swift
@@ -90,6 +90,13 @@ public final class DirectoryHandle: Sendable {
_ = name.withCString { unlinkat(descriptor, $0, AT_REMOVEDIR) }
}
+ /// What sits at `name` inside this folder, a link itself and never what it leads to, or nil when nothing does.
+ public func status(of name: String) -> stat? {
+ var info = stat()
+ guard name.withCString({ fstatat(descriptor, $0, &info, AT_SYMLINK_NOFOLLOW) }) == 0 else { return nil }
+ return info
+ }
+
/// 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/TrashMover.swift b/Packages/PeelCore/Sources/PeelPrivileged/TrashMover.swift
index 596eba85..52a92f00 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/TrashMover.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/TrashMover.swift
@@ -14,16 +14,8 @@ public enum TrashMover {
into trash: DirectoryHandle,
exclusively: ExclusiveRename
) -> Result<String, POSIXError> {
- let base = (item.name as NSString).deletingPathExtension
- let pathExtension = (item.name as NSString).pathExtension
-
- for attempt in 1...1_000 {
- let candidate: String
- if attempt == 1 {
- candidate = item.name
- } else {
- candidate = pathExtension.isEmpty ? "\(base) \(attempt)" : "\(base) \(attempt).\(pathExtension)"
- }
+ for attempt in attempts {
+ let candidate = name(item.name, attempt: attempt)
switch rename(item, to: candidate, in: trash, exclusively: exclusively) {
case .success:
return .success((trash.path as NSString).appendingPathComponent(candidate))
@@ -34,6 +26,16 @@ public enum TrashMover {
return .failure(POSIXError(.EEXIST))
}
+ public static let attempts = 1...1_000
+
+ /// The name `move` tries at `attempt`: the item's own, then "name 2", "name 3", and so on, as Finder names a copy.
+ public static func name(_ name: String, attempt: Int) -> String {
+ guard attempt > 1 else { return name }
+ let base = (name as NSString).deletingPathExtension
+ let pathExtension = (name as NSString).pathExtension
+ return pathExtension.isEmpty ? "\(base) \(attempt)" : "\(base) \(attempt).\(pathExtension)"
+ }
+
/// Renames `item` to `name` in `directory` through both descriptors, refusing to replace anything there.
public static func rename(
_ item: OpenItem,
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
index 243c5281..082c132e 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
@@ -1281,6 +1281,25 @@ struct TrashServiceTests {
#expect(result.failures.map(\.url) == [stayed])
}
+ /// Without Full Disk Access macOS lets Peel search the Trash but not list it, so what the helper moved is
+ /// looked for under the names the helper gives.
+ @Test(.permissionsHold) func whatTheHelperMovedIsFoundAgainInATrashPeelMayNotRead() throws {
+ let directory = try TemporaryDirectory()
+ let trash = try directory.directory("home/.Trash")
+ try directory.file("home/.Trash/moved.db")
+ let moved = try directory.file("root/Library/Application Support/Vendor/moved.db")
+ let links = [moved: FileIdentity.Link.of(moved)]
+ try FileManager.default.moveItem(at: moved, to: trash.appending(path: "moved 2.db"))
+ try directory.setPermissions(0o300, of: "home/.Trash")
+ defer { try? directory.setPermissions(0o755, of: "home/.Trash") }
+
+ let result = PrivilegedHelper.result(for: [moved], reply: nil, links: links, trash: trash)
+
+ #expect(result.trashed.map(\.originalURL) == [moved])
+ #expect(result.trashed.first?.trashedURL.lastPathComponent == "moved 2.db")
+ #expect(result.failures.isEmpty)
+ }
+
@Test func anItemAlreadyGoneIsNeitherMovedNorAFailure() async throws {
let directory = try TemporaryDirectory()
let environment = SearchEnvironment(