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 alone what macOS marks so that nothing can remove it

Țugui Dragoșc4a4e367c6e6
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
index 91d1682b..8fe1f944 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
@@ -205,7 +205,9 @@ public struct LeftoverScanner: Sendable {
let path = url.path(percentEncoded: false)
if let match = claim(name, at: url, kind: location.kind, matcher: matcher, bundle: bundle),
!isSharedWithTheWholeMac(url, home: home) {
- guard !refuses(path, home), !exclusions.excludes(url) else { continue }
+ guard !refuses(path, home), !exclusions.excludes(url), !FileAccess.cannotBeRemoved(url) else {
+ continue
+ }
toMeasure.append(Found(url: url, match: match, parent: parent, isInsideAnotherAppsFolder: false))
} else if NestedSearch.kinds.contains(location.kind), url.isRealFolder, !refuses(path, home),
!exclusions.excludes(url) {
diff --git a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
index 183c522d..549fc016 100644
--- a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
@@ -394,7 +394,7 @@ public struct OrphanScanner: Sendable {
for name in entries where location.considers(fileName: name) {
guard !Task.isCancelled else { break }
let url = location.url.appending(path: name)
- guard isAFileAFolderOrALink(url) else { continue }
+ guard isAFileAFolderOrALink(url), !FileAccess.cannotBeRemoved(url) else { continue }
// What the guard refuses outright is never listed, whether an installed app claims it or not.
guard !ProtectedData.refuses(url.path(percentEncoded: false), home: home) else { continue }
var namedAfter: String?
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
index 792b0e7e..50bea59f 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
@@ -171,8 +171,11 @@ struct RemovalGuard: Sendable {
// A sandboxed app's own documents sit inside its container, beside the settings.
guard !ProtectedData.isInAContainersDocuments(spelling) else { return .protectedLocation }
}
+ // System Integrity Protection guards an item marked restricted, and nothing may remove or rename one marked
+ // no-unlink (`sys/stat.h`).
var info = stat()
- guard lstat(path, &info) != 0 || info.st_flags & UInt32(SF_RESTRICTED) == 0 else { return .protectedLocation }
+ let protectedFlags = UInt32(SF_RESTRICTED) | UInt32(SF_NOUNLINK)
+ guard lstat(path, &info) != 0 || info.st_flags & protectedFlags == 0 else { return .protectedLocation }
// Last, since each reads what is a level or two inside the folder, and a name refused above needs none of it.
for name in names {
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/FileAccess.swift b/Packages/PeelCore/Sources/PeelCore/Support/FileAccess.swift
index 7b8c1144..49a5f6dc 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/FileAccess.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/FileAccess.swift
@@ -32,6 +32,14 @@ public enum FileAccess {
return info.st_flags & (UInt32(UF_IMMUTABLE) | UInt32(SF_IMMUTABLE)) != 0
}
+ /// Whether `url` is marked so that nothing removes or renames it (`SF_NOUNLINK`, `sys/stat.h`), so it can't go to
+ /// the Trash, whoever asks.
+ static func cannotBeRemoved(_ url: URL) -> Bool {
+ var info = stat()
+ guard lstat(url.path(percentEncoded: false), &info) == 0 else { return false }
+ return info.st_flags & UInt32(SF_NOUNLINK) != 0
+ }
+
/// True when `url` itself is locked as Finder's Get Info locks it (`UF_IMMUTABLE`, `chflags(2)`): the file
/// may not be changed until its owner takes the lock off.
static func isLockedInFinder(_ url: URL) -> Bool {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
index e6f699a1..93eb6cb6 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
@@ -90,6 +90,22 @@ struct AttackRemovalGuardTests {
}
}
+ @Test func whatMacOSMarkedSoNothingRemovesItIsRefused() throws {
+ let temporary = try #require(SearchEnvironment.current.userTemporaryDirectory)
+ let entries = try FileManager.default.contentsOfDirectory(at: temporary, includingPropertiesForKeys: nil)
+ let marked = try #require(entries.first(where: FileAccess.cannotBeRemoved), "nothing in \(temporary.path())")
+ 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 plain = try directory.directory("plain")
+
+ #expect(!FileAccess.cannotBeRemoved(plain))
+ #expect(guardian.refusal(of: marked) == .protectedLocation, "\(marked.lastPathComponent)")
+ #expect(guardian.refusal(of: plain) == nil)
+ }
+
/// 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.md
index 3445b1c6..c2f1a964 100644
--- a/SAFETY.md
+++ b/SAFETY.md
@@ -50,7 +50,9 @@ 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`, which
is looked inside and never moved whole. Anything System Integrity Protection guards, which macOS marks as
-restricted, is refused as well.
+restricted, is refused as well, and so is anything macOS marks so that nothing can remove or rename it, such as
+the private folders it makes in your temporary folder for the processes it runs in a sandbox; Peel doesn't list
+those.
Everywhere on the Mac: `/Library/Keychains`, and any `.photoslibrary`, `.photolibrary`, `.migratedphotolibrary`,
`.musiclibrary`, `.tvlibrary`, `.imovielibrary`, `.fcpbundle`, or `.aplibrary`: somebody's whole photo, music, or