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
Let an app go while another program only reads its code
Țugui Dragoș48baa3e371ba
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swiftindex b94a5f3b..2bdc3689 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift@@ -6,29 +6,35 @@ internal import PeelPrivileged/// app's agent, a tool's daemon, a server. Of another account's processes, root's included, only the program each/// runs is seen, never the files it holds open.struct OpenFiles {- private let files: [(names: [String], process: String)]+ /// `holdsAnApp` is false for a file a process only reads: inside an app, that holds nothing.+ private let files: [(names: [String], process: String, holdsAnApp: Bool)]/// 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)] = []+ var files: [(names: [String], process: String, holdsAnApp: Bool)] = []for pid in pids.prefix(max(count, 0)) where pid > 0 && pid != excluded {- let paths = Self.paths(openBy: pid) + [Self.program(of: pid)].compactMap(\.self)+ let program = Self.program(of: pid).map { [(path: $0, holdsAnApp: true)] } ?? []+ let paths = Self.paths(openBy: pid) + programguard !paths.isEmpty else { continue }let process = Self.name(of: pid)- files += paths.map { (PathComponents.of($0), process) }+ files += paths.map { (PathComponents.of($0.path), process, $0.holdsAnApp) }}self.files = files}+ /// The programs that hold `url`. An app is code, which loses nothing when it moves, so a program that only reads+ /// inside it, as Safari reads an app's Safari extension, holds nothing: what runs from it or writes in it does.func holders(of url: URL) -> [String] {let names = PathComponents.of(PathPattern.canonical(url).path(percentEncoded: false))- return Set(files.filter { $0.names.starts(with: names) }.map(\.process)).sorted()+ let isAnApp = url.pathExtension.caseInsensitiveCompare("app") == .orderedSame+ return Set(files.filter { $0.names.starts(with: names) && ($0.holdsAnApp || !isAnApp) }.map(\.process)).sorted()}- private static func paths(openBy pid: pid_t) -> [String] {+ /// The regular files `pid` holds open, each with whether it may write to it.+ private static func paths(openBy pid: pid_t) -> [(path: String, holdsAnApp: Bool)] {let size = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, nil, 0)guard size > 0 else { return [] }var descriptors = [proc_fdinfo](repeating: proc_fdinfo(), count: Int(size) / MemoryLayout<proc_fdinfo>.size)@@ -42,9 +48,10 @@ struct OpenFiles {info.pfi.fi_openflags & UInt32(O_EVTONLY) == 0,mode_t(info.pvip.vip_vi.vi_stat.vst_mode) & S_IFMT == S_IFREGelse { return nil }- return withUnsafeBytes(of: info.pvip.vip_path) { bytes in+ let path = withUnsafeBytes(of: info.pvip.vip_path) { bytes inString(decoding: bytes.prefix { $0 != 0 }, as: UTF8.self)}+ return (path, info.pfi.fi_openflags & UInt32(FWRITE) != 0)}}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swiftindex 21c7772e..c796b09d 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift@@ -52,6 +52,23 @@ struct OpenFilesTests {#expect(OpenFiles(excluding: nil).holders(of: folder) == ["agent"])}+ /// Inside an app, a file another program only reads holds nothing, as Safari reads the code of an app's Safari+ /// extension: code loses nothing when it moves. A file open for writing still holds the app.+ @Test func insideAnAppOnlyAFileOpenForWritingHoldsIt() throws {+ let directory = try TemporaryDirectory()+ let code = try directory.file("Example.app/Contents/PlugIns/Open.appex/Contents/MacOS/Open")+ let state = try directory.file("Example.app/Contents/Resources/state.db")+ let app = directory.url.appending(path: "Example.app", directoryHint: .isDirectory)+ let reading = try FileHandle(forReadingFrom: code)+ defer { try? reading.close() }++ #expect(OpenFiles(excluding: nil).holders(of: app).isEmpty)++ let writing = try FileHandle(forWritingTo: state)+ defer { try? writing.close() }+ #expect(!OpenFiles(excluding: nil).holders(of: app).isEmpty)+ }+/// A process of another account, root's included, can't be asked its name, but the program it runs says it.@Test func namesAProcessOfAnotherAccountByItsProgram() {#expect(OpenFiles.name(of: 1) == "launchd")diff --git a/SAFETY.md b/SAFETY.mdindex 4e42389e..1ffc90e4 100644--- a/SAFETY.md+++ b/SAFETY.md@@ -117,11 +117,14 @@ and each item is written down the moment it moves, so one cut short by a crash oHistory, as an interrupted removal, the next time Peel or `peel` opens it. The space is freed when *you* emptythe Trash: Peel never does that for you.-Right before anything moves, Peel looks at the files other programs of yours hold open, and leaves where it is any-file or folder with one of them inside, naming the program: moving a database or a cache from under an agent, a-daemon, or a server that is using it would break it. Programs that run as another account or as root, macOS's own-among them, cannot be seen this way, which is why Space also leaves macOS's own caches unselected, and in the-Library at the top of the disk, where those programs keep theirs, does not list them at all.+Right before anything moves, Peel looks at the files other programs of yours hold open and at the program each+process runs, and leaves where it is any file or folder with one of them inside, naming the program: moving a+database or a cache from under an agent, a daemon, or a server that is using it would break it. An app is code,+which loses nothing when it moves, so a program that only reads inside it, as Safari reads an app's Safari+extension, does not keep it in place; one that runs from it or writes in it does. Of programs that run as another+account or as root, macOS's own among them, only the program can be seen, never the files it holds open, which is+why Space also leaves macOS's own caches unselected, and in the Library at the top of the disk, where those+programs keep theirs, does not list them at all.A move to the Trash, or back from it, never replaces what is already at the new name. Most disks refuse that bythemselves; on one that cannot, such as exFAT, Peel first takes the name with an empty placeholder, which only a