Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Leave a folder in place while a program runs from inside it
Țugui Dragoșe5a4327ba01a
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swiftindex 906ccfe5..83f8eccc 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift@@ -7,14 +7,14 @@ internal import PeelPrivilegedstruct OpenFiles {private let files: [(names: [String], process: String)]- /// Each regular file held open for reading or writing. A watch (`O_EVTONLY`, as Finder keeps on what it shows)- /// and a folder are no hold on what is inside.+ /// Each regular file held open for reading or writing, and the program each process runs, which it holds as long+ /// as it runs. A watch (`O_EVTONLY`, as Finder keeps on what it shows) and a folder are no hold on what is inside.init(excluding excluded: pid_t? = getpid()) {var pids = [pid_t](repeating: 0, count: 8_192)let count = Int(proc_listallpids(&pids, Int32(pids.count * MemoryLayout<pid_t>.size)))var files: [(names: [String], process: String)] = []for pid in pids.prefix(max(count, 0)) where pid > 0 && pid != excluded {- let paths = Self.paths(openBy: pid)+ let paths = Self.paths(openBy: pid) + [Self.program(of: pid)].compactMap(\.self)guard !paths.isEmpty else { continue }let process = Self.name(of: pid)files += paths.map { (PathComponents.of($0), process) }@@ -47,6 +47,15 @@ struct OpenFiles {}}+ /// The program a process runs, unless it is an app extension: macOS starts and ends those on their app's behalf,+ /// so the person could not quit one to let its app go.+ private static func program(of pid: pid_t) -> String? {+ var buffer = [CChar](repeating: 0, count: 4 * Int(MAXPATHLEN))+ guard proc_pidpath(pid, &buffer, UInt32(buffer.count)) > 0 else { return nil }+ let path = String(decoding: buffer.prefix { $0 != 0 }.map { UInt8(bitPattern: $0) }, as: UTF8.self)+ return PathComponents.of(path).contains { $0.hasSuffix(".appex") } ? nil : path+ }+private static func name(of pid: pid_t) -> String {var buffer = [CChar](repeating: 0, count: 2 * Int(MAXCOMLEN) + 1)guard proc_name(pid, &buffer, UInt32(buffer.count)) > 0 else { return "process \(pid)" }diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swiftindex ecde1b18..2764892f 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift@@ -36,4 +36,34 @@ struct OpenFilesTests {#expect(OpenFiles().holders(of: directory.url.appending(path: "Cache")).isEmpty)}++ /// A program running from inside a folder holds it, as an agent an app keeps in its support folder does.+ @Test func seesAProgramRunningFromInsideAFolder() throws {+ let directory = try TemporaryDirectory()+ let tool = try directory.directory("Support/Agent.app/Contents/MacOS").appending(path: "agent")+ try FileManager.default.copyItem(atPath: "/bin/sleep", toPath: tool.path(percentEncoded: false))+ let process = Process()+ process.executableURL = tool+ process.arguments = ["30"]+ try process.run()+ defer { process.terminate() }++ let folder = directory.url.appending(path: "Support", directoryHint: .isDirectory)+ #expect(OpenFiles(excluding: nil).holders(of: folder) == ["agent"])+ }++ /// An app extension runs on its app's behalf and macOS ends it once the app goes, so it holds nothing.+ @Test func anAppExtensionRunningFromInsideAnAppHoldsNothing() throws {+ let directory = try TemporaryDirectory()+ let tool = try directory.directory("Example.app/Contents/PlugIns/Share.appex/Contents/MacOS")+ .appending(path: "share")+ try FileManager.default.copyItem(atPath: "/bin/sleep", toPath: tool.path(percentEncoded: false))+ let process = Process()+ process.executableURL = tool+ process.arguments = ["30"]+ try process.run()+ defer { process.terminate() }++ #expect(OpenFiles(excluding: nil).holders(of: directory.url.appending(path: "Example.app")).isEmpty)+ }}