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.

The guard refuses by name before it reads what is inside a folder

Țugui Dragoșc3a1a829731d
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
index 299401c1..1d4fd661 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
@@ -52,10 +52,6 @@ struct RemovalGuard: Sendable {
for name in names {
let named = URL(filePath: name)
guard !exclusions.excludes(named), !exclusions.holds(named) else { return false }
- // An uninstall lists a whole container, and the documents inside would go with it. Space lists a
- // vendor's whole cache folder, and work kept only there (an IDE's local history) would go with it.
- guard !ProtectedData.holdsAContainersDocuments(name), !ProtectedData.holdsWorkKeptInACache(name) else { return false }
- guard !ProtectedData.holdsALibrary(name), !ProtectedData.holdsABrowserWallet(name) else { return false }
guard protectedObjects.allows(name) else { return false }
// The rules the helper follows. They protect every account's keychain and mail, not only those of the
// account Peel runs in.
@@ -77,7 +73,16 @@ struct RemovalGuard: Sendable {
guard !ProtectedData.isInAContainersDocuments(spelling) else { return false }
}
var info = stat()
- return lstat(path, &info) != 0 || info.st_flags & UInt32(SF_RESTRICTED) == 0
+ guard lstat(path, &info) != 0 || info.st_flags & UInt32(SF_RESTRICTED) == 0 else { return false }
+
+ // 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 {
+ // An uninstall lists a whole container, and the documents inside would go with it. Space lists a
+ // vendor's whole cache folder, and work kept only there (an IDE's local history) would go with it.
+ guard !ProtectedData.holdsAContainersDocuments(name), !ProtectedData.holdsWorkKeptInACache(name) else { return false }
+ guard !ProtectedData.holdsALibrary(name), !ProtectedData.holdsABrowserWallet(name) else { return false }
+ }
+ return true
}
/// True for anything at or inside a photo, music or video library package. The path arrives lower-cased.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
index adffe833..fb2f8164 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
@@ -504,6 +504,26 @@ struct TrashServiceTests {
}
}
+ /// A folder refused by its name is refused before anything inside it is read. The checks for a library, a
+ /// browser wallet and work kept in a cache look a level or two down, which costs a read of every folder there.
+ @Test func refusesByNameBeforeReadingWhatIsInside() throws {
+ let directory = try TemporaryDirectory()
+ for index in 0..<300 {
+ try directory.directory("home/Library/Keychains/\(index)/inner")
+ }
+ let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
+ let guardian = RemovalGuard(environment: SearchEnvironment(homeDirectory: home, rootDirectory: directory.url.appending(path: "root")))
+ let keychains = home.appending(path: "Library/Keychains", directoryHint: .isDirectory)
+
+ let took = ContinuousClock().measure {
+ for _ in 0..<10 {
+ #expect(!guardian.allowsRemoval(of: keychains))
+ }
+ }
+
+ #expect(took < .milliseconds(250))
+ }
+
/// These hold work nothing can bring back. A file removed from iCloud Drive, for example, is removed from
/// every device the user owns.
@Test func refusesTheFoldersThatHoldWorkNothingCanBringBack() throws {