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.

Find the moved cache of a Homebrew that reads its taps

Țugui Dragoșa6c45cde2a82
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/Relocation.swift b/Packages/PeelCore/Sources/PeelCore/Developer/Relocation.swift
index 7e62dbce..46f521d7 100644
--- a/Packages/PeelCore/Sources/PeelCore/Developer/Relocation.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Developer/Relocation.swift
@@ -94,7 +94,7 @@ extension DeveloperCaches {
case .goBuildCache:
settings.goBuildCache.flatMap { Self.holdsGosReadme($0) ? [($0, $0)] : nil } ?? []
case .homebrewCache(let inside):
- settings.homebrewCaches.filter { $0.appending(path: "api", directoryHint: .isDirectory).isRealFolder }
+ settings.homebrewCaches.filter(Self.holdsWhatOnlyHomebrewWrites)
.flatMap { cache in Self.entries(of: cache, named: inside).map { ($0, cache) } }
case .janDataFolder(let inside):
settings.janDataFolder.map { [($0.appending(path: inside, directoryHint: .isDirectory), $0)] } ?? []
@@ -102,6 +102,15 @@ extension DeveloperCaches {
return found.map { (Self.entry($0.place), Self.entry($0.named)) }.filter(\.place.isRealFolder)
}
+ /// Whether a folder a setting names is Homebrew's cache: it holds Homebrew's `api` folder, or, for a Homebrew
+ /// that reads its taps and has none, a download under the name Homebrew gives one, `<SHA-256>--<name>`.
+ private static func holdsWhatOnlyHomebrewWrites(_ cache: URL) -> Bool {
+ guard !cache.appending(path: "api", directoryHint: .isDirectory).isRealFolder else { return true }
+ let downloads = cache.appending(path: "downloads").path(percentEncoded: false)
+ let names = (try? FileManager.default.contentsOfDirectory(atPath: downloads)) ?? []
+ return names.contains { $0.prefixMatch(of: /[0-9a-f]{64}--/) != nil }
+ }
+
/// The entry a place names, without a final `/`: with one, the kernel follows a link to what it leads to.
private static func entry(_ url: URL) -> URL {
let path = url.path(percentEncoded: false)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
index 20f6cc0a..96b6a03a 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
@@ -578,6 +578,20 @@ struct DeveloperCachesTests {
#expect(listed == Set(["Cask", "downloads", "go_cache"].map { "\(moved)/\($0)" }))
}
+ @Test func findsTheMovedCacheOfAHomebrewThatReadsItsTaps() async throws {
+ let directory = try TemporaryDirectory()
+ let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
+ let moved = directory.url.appending(path: "Fast/Homebrew").path(percentEncoded: false)
+ try directory.file("home/.homebrew/brew.env", contents: Data("HOMEBREW_CACHE=\(moved)\n".utf8))
+ let digest = String(repeating: "0a", count: 32)
+ try directory.file("Fast/Homebrew/downloads/\(digest)--tool--1.0.tar.gz", bytes: 4_096)
+ let homebrew = DeveloperCaches.definitions.filter { $0.id == "homebrew" }
+
+ let found = await DeveloperCaches.scan(homebrew, homeDirectory: home, rootDirectory: directory.url)
+
+ #expect(found.flatMap(\.locations).map { $0.url.path(percentEncoded: false) } == ["\(moved)/downloads"])
+ }
+
/// A large `DerivedData`, or a Gradle cache on a cold disk, may not be measured in time. Such a folder is not
/// read as empty: it is listed first, with no size, and never selected for the user.
@Test func aFolderThatDidNotAnswerInTimeIsNotReadAsEmpty() async throws {