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
Let Homebrew's record of an app go on an Intel Mac too
Țugui Dragoș99ad5e07974b
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swiftindex f2a657b5..792b0e7e 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift@@ -45,10 +45,12 @@ struct RemovalGuard: Sendable {/// Nothing inside these may be removed. `/Library/Updates` is where Software Update stages macOS updates./// Lower-cased, because the spellings they are compared against are.private static let protectedPrefixes = ["/system", "/usr", "/bin", "/sbin", "/library/updates"]- /// The folders command-line tools and their shell completions are linked into. A link directly in one is the one- /// thing under `/usr` that may go: what an app's tool leaves there, which Peel's helper takes only once it leads- /// nowhere.+ /// The folders command-line tools and their shell completions are linked into. A link directly in one may go from+ /// under `/usr`: what an app's tool leaves there, which Peel's helper takes only once it leads nowhere.private static let toolLinkFolders = PrivilegedPathPolicy.linkLocations.map { PathComponents.of($0.lowercased()) }+ /// Homebrew's Caskroom where its prefix is `/usr/local`, as on an Intel Mac. A folder directly in it is Homebrew's+ /// record of a cask, which goes with the cask's app.+ private static let caskroom = PathComponents.of("/usr/local/caskroom")private let protectedPaths: Set<String>private let protectedTrees: [[String]]@@ -93,7 +95,7 @@ struct RemovalGuard: Sendable {}func refusal(of url: URL) -> GuardRefusal? {- refusal(of: url, isALink: nil)+ refusal(of: url, typeOf: nil)}/// Whether `trashed` may go back to `destination`, which is judged as a removal from there would be, for the@@ -103,7 +105,7 @@ struct RemovalGuard: Sendable {}func refusal(ofPuttingBack trashed: URL, at destination: URL) -> GuardRefusal? {- refusal(of: destination, isALink: Self.isALink(trashed.path(percentEncoded: false)))+ refusal(of: destination, typeOf: trashed)}/// True for the lower-cased spelling of a link directly in a folder tools or their completions are linked into.@@ -111,19 +113,24 @@ struct RemovalGuard: Sendable {isALink && toolLinkFolders.contains(Array(PathComponents.of(spelling).dropLast()))}- private static func isALink(_ path: String) -> Bool {+ private static func isACasksRecord(_ spelling: String, isAFolder: Bool) -> Bool {+ isAFolder && Array(PathComponents.of(spelling).dropLast()) == caskroom+ }++ private static func type(of path: String) -> mode_t? {var info = stat()- return lstat(path, &info) == 0 && info.st_mode & S_IFMT == S_IFLNK+ return lstat(path, &info) == 0 ? info.st_mode & S_IFMT : nil}- private func refusal(of url: URL, isALink known: Bool?) -> GuardRefusal? {+ /// `item`, when given, is what would sit at `url`: an item coming back from the Trash.+ private func refusal(of url: URL, typeOf item: URL?) -> GuardRefusal? {guard url.isFileURL else { return .protectedLocation }guard exclusions.isKnown else { return .exclusionsNotKnown }let path = Self.normalized(url.path(percentEncoded: false))// A path can be written several ways (`/var` for `/private/var`, a different case), and a Put Back// destination does not exist yet. `located` names the part that exists the way the kernel does.guard let located = PathPattern.located(path) else { return .protectedLocation }- let isALink = known ?? Self.isALink(located)+ let type = Self.type(of: item?.path(percentEncoded: false) ?? located)// The kernel's own name for the item is judged too: `/.vol/<device>/<inode>` names a file by its numbers// alone and shows none of the folders it sits in.let names = Set([path, located, PathPattern.kernelName(of: path, followingLinks: false)].compactMap(\.self))@@ -146,7 +153,9 @@ struct RemovalGuard: Sendable {for spelling in spelled.values.reduce(into: Set<String>(), { $0.formUnion($1) }) {let isUnderAPrefix = Self.protectedPrefixes.contains { PathComponents.isPath(spelling, inside: $0) }guard !protectedPaths.contains(spelling) else { return .staysItself }- guard !isUnderAPrefix || Self.isAToolsLink(spelling, isALink: isALink) else { return .protectedLocation }+ let mayGo = Self.isAToolsLink(spelling, isALink: type == S_IFLNK)+ || Self.isACasksRecord(spelling, isAFolder: type == S_IFDIR)+ guard !isUnderAPrefix || mayGo else { return .protectedLocation }let parts = PathComponents.of(spelling)let isATree = protectedTrees.contains { parts.starts(with: $0) }diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swiftindex 24c3ff37..e6f699a1 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift@@ -37,10 +37,10 @@ struct AttackRemovalGuardTests {#expect(guardian.allowsRemoval(of: home.appending(path: "Downloads/something.txt")))}- /// Under `/usr` only a link directly in a folder command-line tools or their shell completions are linked into- /// may go, and only a link may go back there. A file, a name with nothing under it yet, and anything deeper stay+ /// 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 underUsrOnlyAToolsLinkMayGoOrComeBack() throws {+ @Test func underUsrALinkMayGoOrComeBackOnlyInTheToolFolders() throws {let directory = try TemporaryDirectory()let guardian = RemovalGuard(environment: SearchEnvironment(homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),@@ -68,6 +68,28 @@ struct AttackRemovalGuardTests {#expect(!guardian.allowsRemoval(of: URL(filePath: "/usr/bin/true")))}+ @Test func underUsrAFolderMayGoOrComeBackOnlyInHomebrewsCaskroom() throws {+ let directory = try TemporaryDirectory()+ let guardian = RemovalGuard(environment: SearchEnvironment(+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)+ ))+ let trashedFolder = try directory.directory("home/.Trash/example")+ let trashedFile = try directory.file("home/.Trash/file")+ let trashedLink = directory.url.appending(path: "home/.Trash/link")+ try link("/Applications/Gone.app", at: trashedLink.path(percentEncoded: false))+ let name = "peel-test-\(UUID().uuidString)"+ let record = URL(filePath: "/usr/local/Caskroom/\(name)")++ #expect(guardian.allowsPuttingBack(trashedFolder, at: record), "Homebrew's record of a cask can't come back")+ #expect(!guardian.allowsPuttingBack(trashedFile, at: record), "ATTACK SUCCEEDED: a file can go to the Caskroom")+ #expect(!guardian.allowsPuttingBack(trashedLink, at: record), "ATTACK SUCCEEDED: a link can go to the Caskroom")+ #expect(!guardian.allowsRemoval(of: record), "a name with nothing there was allowed")+ for path in ["/usr/local/Caskroom", "/usr/local/Caskroom/\(name)/1.0", "/usr/local/\(name)", "/usr/Caskroom/\(name)"] {+ #expect(!guardian.allowsPuttingBack(trashedFolder, at: URL(filePath: path)), "ATTACK SUCCEEDED: \(path)")+ }+ }+/// Attack 1: write the protected folder in a different case. APFS is case-insensitive by default, so the/// path still names the same file, and comparing strings must not miss it.@Test func everySpellingOfAPathIsAsked() {diff --git a/SAFETY.md b/SAFETY.mdindex d5c38b85..3445b1c6 100644--- a/SAFETY.md+++ b/SAFETY.md@@ -41,10 +41,12 @@ In the Library of every account on the Mac and, for the last rows, in the home f| `.config`, `.cache`, `.local`, `.kube` and its `config`, `.CFUserTextEncoding`, your shell's settings and history (`.zshrc`, `.zsh_history`, `.zsh_sessions`, `.bash_history`, and the like), and the settings of git and other tools (`.gitconfig`, `.gitignore_global`, `.netrc`, `.npmrc`) | Shared by many tools and owned by no app. They are never removed themselves; what one tool keeps inside `.config`, `.cache`, or `.local` still can be. |Outside your home: everything inside `/System`, `/usr`, `/bin`, `/sbin`, and `/Library/Updates`, where macOS-stages its own updates, but for one thing: a link that leads nowhere, directly in `/usr/local/bin`,+stages its own updates, but for two things: a link that leads nowhere, directly in `/usr/local/bin`,`/usr/local/sbin`, or one of the four folders of `/usr/local` that shell completions are linked into, which anapp's tool or its completion leaves there once the app is gone and the helper may take (see [The helper that-runs as root](#the-helper-that-runs-as-root)). And these folders themselves, though not what is inside them:+runs as root](#the-helper-that-runs-as-root)); and a folder directly in `/usr/local/Caskroom`, the record+Homebrew keeps of an app it installed on an Intel Mac, which goes with the app. And these folders themselves,+though not what is inside them:`/`, `/Applications`, `/Library`, `/Users`, `/Users/Shared`, `/Volumes`, `/opt`, `/private` and its `var`,`tmp`, and `etc`, `/cores`, your home folder, and every folder Peel searches, such as `~/Library/Caches`, whichis looked inside and never moved whole. Anything System Integrity Protection guards, which macOS marks as