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.

Search a folder whose name only the other Library uses

Țugui Dragoșaaa58ce94fc4
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
index 850751a8..ce19cc6a 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
@@ -196,7 +196,7 @@ public struct LeftoverScanner: Sendable {
// The guard reads every spelling of a path from the disk, so it is asked only about what the scan would
// take or walk into, and what it refuses is left out of both, as is what the person excluded.
- for name in entries where location.kind.considers(fileName: name) {
+ for name in entries where location.considers(fileName: name) {
// A canceled scan stops here, since nobody will read what it finds.
guard !Task.isCancelled else { break }
let url = location.url.appending(path: name)
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift
index d0ecd8e6..9dea6c39 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift
@@ -47,10 +47,23 @@ public struct SearchLocation: Sendable, Hashable {
public let kind: Kind
public let url: URL
+ /// At the top of a Library, the names that same Library searches as places of their own, which it never offers
+ /// whole: `Preferences` is looked inside and never moved.
+ let skipped: Set<String>
public init(kind: Kind, url: URL) {
+ self.init(kind: kind, url: url, skipped: [])
+ }
+
+ init(kind: Kind, url: URL, skipped: Set<String>) {
self.kind = kind
self.url = url
+ self.skipped = skipped
+ }
+
+ /// True when this location looks at an entry with this name.
+ func considers(fileName: String) -> Bool {
+ kind.considers(fileName: fileName) && !skipped.contains(fileName)
}
}
@@ -90,13 +103,11 @@ extension SearchLocation.Kind {
}
/// True when this kind of location looks at an entry with this name. The home folder is split in two: hidden
- /// entries, and the rest except the folders every account starts with. At the top of a Library, a folder
- /// scanned as a location of its own is never offered whole.
+ /// entries, and the rest except the folders every account starts with.
func considers(fileName: String) -> Bool {
switch self {
case .hiddenHomeFiles: fileName.hasPrefix(".")
case .homeFolder: !fileName.hasPrefix(".") && !SearchEnvironment.accountFolderNames.contains(fileName.lowercased())
- case .library: !SearchEnvironment.libraryEntriesScannedElsewhere.contains(fileName)
default: true
}
}
@@ -144,14 +155,18 @@ public struct SearchEnvironment: Sendable {
/// Lowercased, because disks ignore case by default, so `desktop` is the Desktop folder.
static let accountFolderNames = Set(ProtectedData.accountFolders.map { $0.lowercased() })
- /// The first folder of every Library path scanned as a location of its own, such as `Preferences`. The top of
- /// a Library never offers one of these whole: they are looked inside, never moved.
- static let libraryEntriesScannedElsewhere = Set(
- ((userEntries + localEntries).map(\.1) + Plugins.folders.map(\.0) + systemCodeFolders).map { name in
- String(name.prefix { $0 != "/" })
- }
+ /// The first folder of every path the home's Library and the Mac's search as a location of their own, such as
+ /// `Preferences`. The top of a Library never offers one of its own whole: it is looked inside, never moved. One
+ /// only the other Library searches, such as `PrivilegedHelperTools` in the home's, is a folder like any other.
+ static let homeLibraryEntriesScannedElsewhere = firstFolders(of: userEntries.map(\.1) + Plugins.folders.map(\.0))
+ static let localLibraryEntriesScannedElsewhere = firstFolders(
+ of: localEntries.map(\.1) + Plugins.folders.map(\.0) + systemCodeFolders
)
+ private static func firstFolders(of paths: [String]) -> Set<String> {
+ Set(paths.map { String($0.prefix { $0 != "/" }) })
+ }
+
/// Where a driver's kernel extensions and a file system's bundles stay in `/Library`. They are searched like
/// plug-ins, named for what they do, but the helper does not serve these folders, so what is found there is
/// listed and never moved.
@@ -193,7 +208,10 @@ public struct SearchEnvironment: Sendable {
))
locations.append(SearchLocation(kind: .hiddenHomeFiles, url: homeDirectory))
locations.append(SearchLocation(kind: .homeFolder, url: homeDirectory))
- locations += [userLibrary, localLibrary].map { SearchLocation(kind: .library, url: $0) }
+ locations += [
+ SearchLocation(kind: .library, url: userLibrary, skipped: Self.homeLibraryEntriesScannedElsewhere),
+ SearchLocation(kind: .library, url: localLibrary, skipped: Self.localLibraryEntriesScannedElsewhere),
+ ]
return locations
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
index 6464a9bf..f7a17a47 100644
--- a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
@@ -385,7 +385,7 @@ public struct OrphanScanner: Sendable {
let parent = ParentAccess(location.url)
var candidates: [Candidate] = []
var nobodysFolders: [URL] = []
- for name in entries where location.kind.considers(fileName: name) {
+ 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 }
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift b/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
index 110096a9..c7b4629d 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/Uninstallation.swift
@@ -151,14 +151,16 @@ public struct Uninstallation: Sendable {
let path = PathPattern.comparablePath(of: url)
let names = PathComponents.of(path)
let inside = environment.locations
- .map { (kind: $0.kind, root: PathPattern.comparablePath(of: $0.url)) }
+ .map { location in
+ let root = PathPattern.comparablePath(of: location.url)
+ return (location: location, root: root, below: Array(names.dropFirst(PathComponents.of(root).count)))
+ }
.filter { PathComponents.isPath(path, inside: $0.root) }
- .map { (kind: $0.kind, root: $0.root, below: Array(names.dropFirst(PathComponents.of($0.root).count))) }
// Two locations can share a root, like the hidden and plain halves of the home folder. The one that
// considers this name is the one that would have found it.
- .filter { location in
- guard let first = location.below.first else { return false }
- return location.kind.considers(fileName: first)
+ .filter { found in
+ guard let first = found.below.first else { return false }
+ return found.location.considers(fileName: first)
}
.max { $0.root.count < $1.root.count }
guard let inside else { return (.elsewhere, [url.lastPathComponent]) }
@@ -167,7 +169,7 @@ public struct Uninstallation: Sendable {
guard inside.root != PathPattern.comparablePath(of: environment.homeDirectory) || components.count == 1 else {
return (.elsewhere, [url.lastPathComponent])
}
- return (inside.kind, components)
+ return (inside.location.kind, components)
}
/// True when the app itself would stay: excluded, Peel, kept by macOS, part of another package, beyond the
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift
index 2dced790..7a824fe8 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift
@@ -385,6 +385,36 @@ struct LeftoverScannerTests {
#expect(try #require(emptied.leftovers.first { $0.kind == .containers }).match.isRecommended)
}
+ /// macOS keeps privileged helpers only in the Mac's own Library, so a folder by that name in the home's Library is
+ /// no place macOS knows: it is searched like any other folder at the top of a Library.
+ @Test func searchesAFolderTheHomesLibraryHasOnlyByAnotherLibrarysName() async throws {
+ let directory = try TemporaryDirectory()
+ try directory.file("home/Library/PrivilegedHelperTools/net.example.client.helper")
+
+ let scanner = LeftoverScanner(environment: environment(in: directory))
+
+ let scan = await scanner.scan(tunewell, installedApps: [tunewell])
+
+ #expect(scan.leftovers.map(\.url.lastPathComponent) == ["net.example.client.helper"])
+ }
+
+ /// At the top of a Library, what is skipped is what that Library searches as a place of its own, and nothing the
+ /// other Library alone searches.
+ @Test func eachLibrarySkipsOnlyWhatItSearchesItself() throws {
+ let directory = try TemporaryDirectory()
+ let environment = environment(in: directory)
+ let libraries = environment.locations.filter { $0.kind == .library }
+ let home = try #require(libraries.first { $0.url.path(percentEncoded: false).contains("/home/") })
+ let mac = try #require(libraries.first { $0.url.path(percentEncoded: false).contains("/root/") })
+
+ #expect(!home.considers(fileName: "Preferences") && !mac.considers(fileName: "Preferences"))
+ #expect(!home.considers(fileName: "Containers") && mac.considers(fileName: "Containers"))
+ #expect(!home.considers(fileName: "Frameworks") && mac.considers(fileName: "Frameworks"))
+ #expect(home.considers(fileName: "PrivilegedHelperTools") && !mac.considers(fileName: "PrivilegedHelperTools"))
+ #expect(home.considers(fileName: "Extensions") && !mac.considers(fileName: "Extensions"))
+ #expect(!home.considers(fileName: "Audio") && !mac.considers(fileName: "Audio"))
+ }
+
@Test func aCacheThatKeepsAnEditorsLocalHistoryIsShownAndLeftAlone() async throws {
let directory = try TemporaryDirectory()
try directory.file("home/Library/Caches/net.example.client/LocalHistory/changes.storageData")