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.

Take a name found inside another folder only from the app's or its maker's

Țugui Dragoșf5deaea7eeec
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift
index 2218a8cc..82a8e3be 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverMatcher.swift
@@ -113,6 +113,14 @@ struct LeftoverMatcher: Sendable {
return PathPattern.comparablePath(of: URL(filePath: program))
}
+ /// Whether a folder with this name is the app's or its maker's. A folder named `X.localized` is named `X`
+ /// (Apple, "Localizing the Name of a Directory").
+ func namesTheAppOrItsMaker(folder name: String) -> Bool {
+ let suffix = ".localized"
+ let name = name.lowercased().hasSuffix(suffix) ? String(name.dropLast(suffix.count)) : name
+ return target.ownFolderNames.contains(Naming.normalized(name))
+ }
+
/// The other installed apps with any claim on `fileName`, by bundle identifier, and the other copies of this app
/// with one. A path a Homebrew cask names is checked this way too, so one shared with another app or another
/// copy is never selected for this app.
@@ -292,6 +300,7 @@ extension LeftoverMatcher {
let vendorPrefix: String?
let names: [String]
let normalizedNames: Set<String>
+ let ownFolderNames: Set<String>
let isApplesOwn: Bool
let isASafariWebApp: Bool
@@ -315,10 +324,24 @@ extension LeftoverMatcher {
vendorPrefix = vendor.map { $0 + "." }
names = app.matchingNames.filter(Naming.isSignificant).map { $0.lowercased() }
normalizedNames = Set(names.map(Naming.normalized))
+ ownFolderNames = Self.folderNames(names: normalizedNames, vendor: vendor, developer: app.developer)
isApplesOwn = app.isSystemProtected || teamPrefix == Self.appleTeam
isASafariWebApp = app.isASafariWebApp
}
+ /// What a folder of the app's or its maker's is called, normalized: the app's names, the maker's part of its
+ /// identifier and that part's last name, and the leading words of the developer its signature names.
+ private static func folderNames(names: Set<String>, vendor: String?, developer: String?) -> Set<String> {
+ var folders = names
+ if let vendor {
+ folders.insert(Naming.normalized(vendor))
+ folders.insert(Naming.normalized(String(vendor.split(separator: ".").last ?? "")))
+ }
+ let words = (developer ?? "").split { !$0.isLetter && !$0.isNumber }.map { $0.lowercased() }
+ for end in words.indices { folders.insert(words[...end].joined()) }
+ return folders.filter { !$0.isEmpty }
+ }
+
func isSibling(of other: Profile) -> Bool {
if let vendorPrefix, vendorPrefix == other.vendorPrefix { return true }
if let teamPrefix, teamPrefix == other.teamPrefix { return true }
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
index 4e62ef7d..bbdafef9 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/LeftoverScanner.swift
@@ -218,7 +218,7 @@ public struct LeftoverScanner: Sendable {
guard !refuses(path, home), !exclusions.excludes(url), !FileAccess.cannotBeRemoved(url) else {
continue
}
- toMeasure.append(Found(url: url, match: match, parent: parent, isInsideAnotherAppsFolder: false))
+ toMeasure.append(Found(url: url, match: match, parent: parent, foldersAbove: .itsOwn))
} else if NestedSearch.kinds.contains(location.kind), url.isRealFolder, !refuses(path, home),
!exclusions.excludes(url) {
nobodysFolders.append(url)
@@ -256,11 +256,11 @@ public struct LeftoverScanner: Sendable {
if let match = Self.match(name, at: url, kind: kind, matcher: matcher), match.confidence >= .likely,
!isSharedWithTheWholeMac(url, home: home) {
guard !refuses(path, home), !exclusions.excludes(url) else { return .pass }
- // Whether it sits inside a folder of somebody else's decides what its name is worth.
+ // Whose the folders on the way are decides what its name is worth.
let between = url.deletingLastPathComponent().pathComponents.dropFirst(locationLength)
return .take(Found(
url: url, match: match, parent: parent,
- isInsideAnotherAppsFolder: between.contains { isSomebodyElses($0, kind: kind, matcher: matcher) }
+ foldersAbove: foldersAbove(between, kind: kind, matcher: matcher)
))
}
guard canLookInside, url.isRealFolder, !refuses(path, home), !exclusions.excludes(url) else { return .pass }
@@ -275,7 +275,24 @@ public struct LeftoverScanner: Sendable {
let url: URL
let match: LeftoverMatch
let parent: ParentAccess
- let isInsideAnotherAppsFolder: Bool
+ let foldersAbove: FoldersAbove
+ }
+
+ /// Whose the folders between a search location and an item found inside them are.
+ enum FoldersAbove: Sendable {
+ /// None, or each named for the app or its maker.
+ case itsOwn
+ /// One is Apple's or another installed app's.
+ case anotherApps
+ /// One is named for neither the app nor its maker, so it may be any program's.
+ case unknown
+ }
+
+ private static func foldersAbove(
+ _ folders: some Collection<String>, kind: SearchLocation.Kind, matcher: LeftoverMatcher
+ ) -> FoldersAbove {
+ if folders.contains(where: { isSomebodyElses($0, kind: kind, matcher: matcher) }) { return .anotherApps }
+ return folders.allSatisfy(matcher.namesTheAppOrItsMaker(folder:)) ? .itsOwn : .unknown
}
/// How many items of one location are measured at once. One after another, a few folders that never answer
@@ -289,7 +306,7 @@ public struct LeftoverScanner: Sendable {
await found.concurrentMap(width: concurrentMeasurements) { item in
await leftover(
at: item.url, kind: kind, match: item.match, parent: item.parent, home: home,
- isInsideAnotherAppsFolder: item.isInsideAnotherAppsFolder, measure: measure
+ foldersAbove: item.foldersAbove, measure: measure
)
}
}
@@ -302,7 +319,7 @@ public struct LeftoverScanner: Sendable {
match: LeftoverMatch,
parent: ParentAccess,
home: String,
- isInsideAnotherAppsFolder: Bool = false,
+ foldersAbove: FoldersAbove = .itsOwn,
measure: Measure = LeftoverScanner.walk
) async -> Leftover {
// The walk that measures the folder also reports whether it saw a repository or a wallet, so neither
@@ -328,7 +345,7 @@ public struct LeftoverScanner: Sendable {
} else if contents?.couldNotBeRead == true {
.couldNotBeRead
} else if let contents {
- heldBack(match, at: url, in: kind, contents: contents, isInsideAnotherAppsFolder: isInsideAnotherAppsFolder)
+ heldBack(match, at: url, in: kind, contents: contents, foldersAbove: foldersAbove)
} else {
.notMeasured
}
@@ -356,7 +373,7 @@ public struct LeftoverScanner: Sendable {
at url: URL,
in kind: SearchLocation.Kind,
contents: FolderContents,
- isInsideAnotherAppsFolder: Bool
+ foldersAbove: FoldersAbove
) -> HoldBack? {
// A wallet comes first: a cask's `zap` can name a coin app's whole data folder, which is where its keys
// are. Holding it back costs a checkmark, and the row says what is inside.
@@ -366,7 +383,8 @@ public struct LeftoverScanner: Sendable {
if kind == .sharedFolder { return .sharedWithEveryone }
if Self.namedGroundOfSomebodyElse.contains(kind), match.restsOnAName { return .namedLikeTheApp }
// An identifier names the app wherever it sits. Its name, inside somebody else's folder, does not.
- if isInsideAnotherAppsFolder, match.restsOnAName { return .insideAnotherAppsFolder }
+ if match.restsOnAName, foldersAbove == .anotherApps { return .insideAnotherAppsFolder }
+ if match.restsOnAName, foldersAbove == .unknown { return .namedLikeTheApp }
return nil
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift
index 00c2eb9e..a9d86d04 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/LeftoverScannerTests.swift
@@ -363,7 +363,38 @@ struct LeftoverScannerTests {
#expect(byPath["SomeVendor/Tunewell"]?.match.heldBack == .insideAnotherAppsFolder)
#expect(byPath["SomeVendor/Tunewell"]?.match.isRecommended == false)
#expect(byPath["SomeVendor/net.example.client"]?.match.isRecommended == true, "an identifier names the app wherever it sits")
- #expect(byPath["Nobody/Tunewell"]?.match.isRecommended == true, "a folder no installed app answers to is nobody's")
+ #expect(byPath["Nobody/Tunewell"]?.match.heldBack == .namedLikeTheApp, "a folder named for neither app nor maker")
+ }
+
+ /// A name found inside a folder counts only when every folder on the way is named for the app or its maker, by
+ /// the maker's part of its identifier or the developer its signature names. Inside any other folder, such as
+ /// macOS's own sync store or a command-line tool's settings, the name is all there is.
+ @Test func aNameInsideAFolderOfNeitherTheAppNorItsMakerIsNotEnough() async throws {
+ let app = InstalledApp(
+ url: URL(filePath: "/Applications/Hollow.app"),
+ bundleIdentifier: "org.night-owl.hollow",
+ name: "Hollow",
+ developer: "Lantern Works, Inc."
+ )
+ let makers = ["Night Owl/Hollow", "night-owl/Hollow", "Lantern Works/Hollow", "Lantern.localized/Hollow"]
+ let others = ["SyncServices/Hollow", "tool/telemetry/hollow"]
+ let directory = try TemporaryDirectory()
+ for folder in makers + others {
+ try directory.file("home/Library/Application Support/\(folder)/state.db")
+ }
+
+ let scan = await LeftoverScanner(environment: environment(in: directory)).scan(app, installedApps: [app])
+
+ let found = Dictionary(uniqueKeysWithValues: scan.leftovers.map {
+ ($0.url.path(percentEncoded: false).components(separatedBy: "Application Support/").last ?? "", $0)
+ })
+ for folder in makers {
+ #expect(found[folder]?.match.isRecommended == true, "\(folder)")
+ }
+ for folder in others {
+ #expect(found[folder]?.match.heldBack == .namedLikeTheApp, "\(folder)")
+ #expect(found[folder]?.match.isRecommended == false, "\(folder)")
+ }
}
/// Leaving a page cancels its scan, and a canceled scan starts no other measurement, so only those already under
@@ -1509,8 +1540,8 @@ struct LeftoverScannerTests {
})
#expect(found["home/Library/Caches/com.apple.python/Developer"]?.match.heldBack == .insideAnotherAppsFolder)
- // A plain vendor folder that no app claims is not affected.
- #expect(found["home/Library/Caches/CrashReporter/Developer"]?.match.isRecommended == true)
+ // A folder no app claims is somebody's too, and its name says nothing of this app or its maker.
+ #expect(found["home/Library/Caches/CrashReporter/Developer"]?.match.heldBack == .namedLikeTheApp)
}
/// Vendors keep a folder of their own in `/Users/Shared`. What is in there belongs to every account on the
diff --git a/SAFETY.md b/SAFETY.md
index 69b592af..65181f1e 100644
--- a/SAFETY.md
+++ b/SAFETY.md
@@ -236,9 +236,10 @@ Only matches Peel is `certain` or `likely` about, and only when nothing else ins
by where it is. An app of the same maker kept outside the Applications folders, such as a Nightly build on another
disk, counts like one inside them, and so does one Peel saw installed before: the files named for a Nightly you
removed are its, not this app's, and Orphaned Files lists them under it.
-- **A name alone is a guess.** Something matched by the app's name alone inside another app's folder, or one of
- Apple's, is shown and never selected, since it is most likely that app's data about this one; so is something
- matched by name alone at the top of your home folder or of a Library, or in a maker's folder there.
+- **A name alone is a guess.** Something matched by the app's name alone inside a folder named for neither the app
+ nor its maker, such as another app's, one of Apple's, or a command-line tool's, is shown and never selected, since a
+ folder of that name may be that program's own; so is something matched by name alone at the top of your home folder
+ or of a Library, or in a maker's folder there.
- **A plug-in goes by what it declares.** Its name says what it does rather than who made it, so a plug-in called
like the app is selected only when the identifier it declares is the app's or its maker's: another maker's plug-in
with the app's name is never selected.