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.

Don't stop a move on macOS's question about a folder it doesn't touch

Țugui Dragoșb948943057ab
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
index 00f92c54..02581446 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
@@ -27,7 +27,7 @@ struct OpenFiles {
init(excluding excluded: pid_t? = getpid(), attachedImages: [String] = []) {
excludedProgram = excluded.flatMap(RunningProgram.path(of:)).map(PathComponents.of)
self.attachedImages = attachedImages.map {
- PathComponents.of(PathPattern.canonical(URL(filePath: $0)).path(percentEncoded: false))
+ PathComponents.of(PathPattern.canonicalWithoutOpening(URL(filePath: $0)).path(percentEncoded: false))
}
var pids = [pid_t](repeating: 0, count: 8_192)
let count = Int(proc_listallpids(&pids, Int32(pids.count * MemoryLayout<pid_t>.size)))
@@ -152,7 +152,7 @@ struct OpenFiles {
["node", "deno", "bun", "ruby", "perl"].contains(name) || name.hasPrefix("python")
else { return [] }
return (arguments(of: pid) ?? []).dropFirst().filter { $0.hasPrefix("/") }.map {
- PathPattern.canonical(URL(filePath: $0)).path(percentEncoded: false)
+ PathPattern.canonicalWithoutOpening(URL(filePath: $0)).path(percentEncoded: false)
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift b/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift
index eafda67c..76995ef4 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift
@@ -19,6 +19,21 @@ public enum PathPattern {
name(of: url.path(percentEncoded: false)).map { URL(filePath: $0) } ?? url
}
+ /// The name `canonical` finds, without opening anything, for a path Peel only compares and nobody asked it to
+ /// touch: opening one in Downloads, Documents, or Desktop makes macOS ask the person, and waits for the answer.
+ static func canonicalWithoutOpening(_ url: URL) -> URL {
+ guard let real = realName(of: url.path(percentEncoded: false)) else { return url }
+ // `realpath` keeps the data volume's name for what a firmlink shows elsewhere, and the kernel's name doesn't.
+ let dataVolume = "/System/Volumes/Data"
+ guard real.hasPrefix(dataVolume + "/") else { return URL(filePath: real) }
+ let shown = String(real.dropFirst(dataVolume.count))
+ var there = stat()
+ var here = stat()
+ let isOneItem = lstat(real, &there) == 0 && lstat(shown, &here) == 0
+ && there.st_dev == here.st_dev && there.st_ino == here.st_ino
+ return URL(filePath: isOneItem ? shown : real)
+ }
+
/// The name the kernel gives what `path` leads to, read back from an open descriptor. macOS also accepts
/// paths that name a file indirectly: `/.nofollow/` or `/.resolve/<flags>/` before any path, and
/// `/.vol/<device>/<inode>` for a file by its numbers. On macOS 26, `realpath` resolves none of these,
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/PathPatternTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/PathPatternTests.swift
index e75bfed7..acb34de5 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/PathPatternTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/PathPatternTests.swift
@@ -77,6 +77,26 @@ struct PathPatternTests {
}
/// A bracket in a real name is not a character class: `App [Beta]` has to find itself.
+ @Test func aNameFoundWithoutOpeningIsTheKernelsName() throws {
+ let directory = try TemporaryDirectory()
+ let file = try directory.file("Images/Disk.dmg", bytes: 16)
+ let link = directory.url.appending(path: "Images/link")
+ try FileManager.default.createSymbolicLink(at: link, withDestinationURL: file)
+ let places = [
+ file, link, URL(filePath: file.path(percentEncoded: false).uppercased()), directory.url,
+ URL(filePath: "/tmp"), URL(filePath: "/System/Volumes/Data/Users"), URL(filePath: "/Users"),
+ ]
+
+ for place in places {
+ #expect(
+ PathPattern.canonicalWithoutOpening(place).path(percentEncoded: false)
+ == PathPattern.canonical(place).path(percentEncoded: false)
+ )
+ }
+ let absent = directory.url.appending(path: "Images/absent.dmg")
+ #expect(PathPattern.canonicalWithoutOpening(absent) == absent)
+ }
+
@Test func findsAFolderWhoseNameHoldsABracket() throws {
let directory = try TemporaryDirectory()
try directory.directory("Library/Caches/App [Beta]")