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.

Keep a folder in place while a script run from it is running

Țugui Dragoș8b8cc5fe3ff7
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md
index 24db275c..88769eaf 100644
--- a/ARCHITECTURE.md
+++ b/ARCHITECTURE.md
@@ -120,8 +120,9 @@ the Homebrew page's uninstall and cleanups are Homebrew's own.
shell completions left in `/usr/local` go last, once the app they lead into has moved. A browser's manifest in
`/Library` goes through the helper too, which takes it only once the program it names is gone.
- An item stays where it is while a program holds it (`OpenFiles`): a file one of the user's programs holds
- open, or the program any process runs from it. When only an app's own background programs hold it, the move
- stops their launch jobs, moves the app, and starts each job again unless its file went to the Trash too.
+ open, the program any process runs from it, or a script in it a program such as Node.js was given to run.
+ When only an app's own background programs hold it, the move stops their launch jobs, moves the app, and
+ starts each job again unless its file went to the Trash too.
Inside an app, which is code and loses nothing when it moves, a file open only for reading holds nothing, and
neither does the program running from an app that uninstalls itself once it has moved (`UninstallsItself`:
Mullvad VPN's daemon, and Citrix Workspace's agent and TeamViewer's daemon, each known by the `WatchPaths` of
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
index f77cf742..95668dbe 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
@@ -31,12 +31,13 @@ struct OpenFiles {
var programs: [pid_t: String] = [:]
for pid in pids.prefix(max(count, 0)) where pid > 0 && pid != excluded && !Self.isACopyOfThisProcess(pid) {
let path = RunningProgram.path(of: pid)
- let program = path.flatMap(Self.program(at:)).map { [(path: $0, hold: Hold.runs)] } ?? []
- let paths = Self.paths(openBy: pid) + program
+ let owner = kill(pid, 0) != 0 && errno == EPERM ? Self.owner(ofAccount: Self.uid(of: pid)) : .thisAccount
+ let runs = (path.flatMap(Self.program(at:)).map { [$0] } ?? [])
+ + (owner == .thisAccount ? Self.scripts(runBy: pid, program: path) : [])
+ let paths = Self.paths(openBy: pid) + runs.map { (path: $0, hold: Hold.runs) }
guard !paths.isEmpty else { continue }
programs[pid] = path
let process = Self.name(of: pid, program: path)
- let owner = kill(pid, 0) != 0 && errno == EPERM ? Self.owner(ofAccount: Self.uid(of: pid)) : .thisAccount
let isJava = path.map { URL(filePath: $0).lastPathComponent == "java" } == true
let daemon = owner == .thisAccount && isJava
? Self.arguments(of: pid).flatMap(BuildDaemon.running(with:)) : nil
@@ -106,6 +107,17 @@ struct OpenFiles {
return .heldOpen(by: Set(named.map(\.process)).sorted())
}
+ /// The scripts a process runs when its program is one that runs a script named as an argument (Node.js, Deno,
+ /// Bun, Python, Ruby or Perl): it loads from beside each as long as it runs, as a program runs from its own file.
+ private static func scripts(runBy pid: pid_t, program: String?) -> [String] {
+ guard let name = program.map({ URL(filePath: $0).lastPathComponent }),
+ ["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)
+ }
+ }
+
/// The arguments a process of this account was started with, as `sysctl` gives them (`KERN_PROCARGS2`): their
/// count, the program's path and the zero bytes after it, then each argument, ended by a zero byte.
static func arguments(of pid: pid_t) -> [String]? {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
index 6c67def1..e47f9bbf 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
@@ -177,6 +177,21 @@ struct OpenFilesTests {
#expect(refusal == .heldByABuildTool(by: ["Gradle"]))
}
+ @Test func aScriptAProgramRunsHoldsTheFolderItRunsFrom() throws {
+ let directory = try TemporaryDirectory()
+ let script = try directory.file("home/.npm/_npx/0123abcd/node_modules/.bin/server")
+ let errors = try directory.file("errors.log")
+ let arguments = ["-", script.path(percentEncoded: false)]
+ let node = try Self.program("bin/node", in: directory, arguments: arguments, writingTo: errors)
+ defer { node.terminate() }
+ let viewer = try Self.program("bin/viewer", in: directory, arguments: arguments, writingTo: errors)
+ defer { viewer.terminate() }
+
+ let refusal = OpenFiles().refusal(of: directory.url.appending(path: "home/.npm/_npx"), lettingItsProgramsRun: false)
+
+ #expect(refusal == .heldOpen(by: ["node"]))
+ }
+
@Test func knowsEachBuildToolsDaemonByHowTheToolStartsIt() {
let gradle = ["/jdk/bin/java", "-Xmx512m", "-cp", "/x/gradle-launcher-9.2.jar",
"org.gradle.launcher.daemon.bootstrap.GradleDaemon", "9.2"]
diff --git a/SAFETY.md b/SAFETY.md
index 32818dd0..2d818c34 100644
--- a/SAFETY.md
+++ b/SAFETY.md
@@ -160,9 +160,10 @@ short by a crash or Force Quit still reaches History, as an interrupted removal,
it.
Right before anything moves, and again once a second while a removal of many items goes on, 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 and how to free the item: quit it, or stop it in Background Items when it
-has no window, since launchd starts such a program again. A build tool's daemon, which stays after a build and keeps
+other programs of yours hold open, at the program each process runs, and at the script a program such as Node.js or
+Python was given to run, and leaves where it is any file or folder with one of them inside, naming the program and
+how to free the item: quit it, or stop it in Background Items when it has no window, since launchd starts such a
+program again. A build tool's daemon, which stays after a build and keeps
the tool's caches open, is named by its tool with the command that tool documents for stopping it: Gradle's and
Kotlin's stop with `gradle --stop`, Bazel's with `bazel shutdown`, and mvnd's with `mvnd --stop`.
Moving a database or a cache from under an agent, a daemon, or a server that is using it would break it. A program