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.

Leave the Trash of every disk alone, as the home's Trash is

Țugui Dragoș016296dcd99a
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift b/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift
index ba0c8f4a..0a05ba54 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift
@@ -521,7 +521,7 @@ public enum ProtectedData: Sendable {
public static func refuses(spellings: Set<String>, home: String) -> Bool {
spellings.contains { spelling in
let names = PathComponents.of(spelling)
- if names.contains(where: isALibrary) { return true }
+ if names.contains(where: isALibrary) || isInADisksTrash(names) { return true }
if isGlobalPreferences(spelling) || isInAnApplesGroupContainer(names) { return true }
if isInsideAWalletExtension(names) { return true }
@@ -532,6 +532,12 @@ public enum ProtectedData: Sendable {
}
}
+ /// True at or inside the Trash macOS keeps at the top of a disk, `.Trashes`, as `~/.Trash` is the home's: what is
+ /// there is on its way out. The names arrive lower-cased.
+ private static func isInADisksTrash(_ names: [String]) -> Bool {
+ names.first == ".trashes" || (names.count > 2 && names[0] == "volumes" && names[2] == ".trashes")
+ }
+
/// The home the caller named, plus whichever account the path, given by its names, sits in.
private static func homes(of names: [String], given home: String) -> [String] {
var homes = [((home as NSString).standardizingPath).lowercased()]
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
index 594e6a13..42601b7e 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
@@ -40,6 +40,23 @@ struct AttackRemovalGuardTests {
/// Under `/usr` a link may go only directly in a folder command-line tools or their shell completions are linked
/// into, and only a link may go back there. A file, a name with nothing under it yet, and anything deeper stay
/// refused.
+ @Test func theTrashOfEveryDiskIsRefused() throws {
+ let directory = try TemporaryDirectory()
+ let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
+ let guardian = RemovalGuard(environment: SearchEnvironment(
+ homeDirectory: home,
+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)
+ ))
+
+ for path in ["/Volumes/Backup/.Trashes/501/report.pdf", "/Volumes/Backup/.Trashes", "/.Trashes/501/report.pdf",
+ "/System/Volumes/Data/.Trashes/501/report.pdf"] {
+ #expect(guardian.refusal(of: URL(filePath: path)) == .protectedLocation, "\(path) would move")
+ }
+ for path in ["/Volumes/Backup/Work/.Trashes/report.pdf", home.path(percentEncoded: false) + "Projects/.Trashes"] {
+ #expect(!ProtectedData.refuses(path, home: home.path(percentEncoded: false)), "\(path) refused")
+ }
+ }
+
@Test func underUsrALinkMayGoOrComeBackOnlyInTheToolFolders() throws {
let directory = try TemporaryDirectory()
let guardian = RemovalGuard(environment: SearchEnvironment(
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
index 1592f066..c5694b52 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
@@ -460,6 +460,17 @@ struct DuplicateFinderTests {
#expect(!finder.canScan(URL(filePath: "/Volumes/")))
}
+ @Test func refusesTheTrashOfEveryDisk() throws {
+ let directory = try TemporaryDirectory()
+ let home = try directory.directory("home")
+ let finder = DuplicateFinder(homeDirectory: home)
+
+ #expect(!finder.canScan(URL(filePath: "/Volumes/Backup/.Trashes/501", directoryHint: .isDirectory)))
+ #expect(!finder.canScan(URL(filePath: "/Volumes/Backup/.Trashes", directoryHint: .isDirectory)))
+ #expect(!finder.canScan(home.appending(path: ".Trash", directoryHint: .isDirectory)))
+ #expect(finder.canScan(URL(filePath: "/Volumes/Backup/Work", directoryHint: .isDirectory)))
+ }
+
@Test func refusesAFolderASyncAppKeepsInStep() throws {
let directory = try TemporaryDirectory()