aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonas Maier <>2026-03-10 15:24:19 +0100
committerJonas Maier <>2026-03-10 15:24:19 +0100
commitc0d6f09ce24d05b9d6cc82bb3394dab0f74a5d57 (patch)
tree73ec2e767377eb875fb4f7889b7c658d14c7bf30
parent83e5044fd6e5c2a8bccd29f5a8462e7016823aed (diff)
downloadpish-c0d6f09ce24d05b9d6cc82bb3394dab0f74a5d57.tar.gz
export fun concentrated in its own module
-rw-r--r--src/export_fun.rs38
-rw-r--r--src/run/mod.rs10
2 files changed, 24 insertions, 24 deletions
diff --git a/src/export_fun.rs b/src/export_fun.rs
index a98699c..fb7911c 100644
--- a/src/export_fun.rs
+++ b/src/export_fun.rs
@@ -26,8 +26,8 @@ use std::os::unix::net::AncillaryData;
use std::os::unix::net::SocketAncillary;
use std::os::unix::net::UnixListener;
use std::os::unix::net::UnixStream;
-use std::path::Path;
use std::path::PathBuf;
+use std::process::Command;
use std::process::exit;
use std::sync::Arc;
use std::sync::Mutex;
@@ -134,6 +134,23 @@ fn handle_client(mut stream: UnixStream) -> io::Result<()> {
exit(exit_code)
}
+pub fn prepare_command(session: Arc<Mutex<Session>>, cmd: &mut Command) {
+ let Ok(session) = session.lock() else {
+ return;
+ };
+
+ let Some(sr) = session.socket_running.as_ref() else {
+ return;
+ };
+
+ let my_path = std::env::var_os("PATH").expect("no PATH - seriously?");
+ let mut new_path = sr.bin_path.as_os_str().as_bytes().to_vec();
+ new_path.push(b':');
+ new_path.extend_from_slice(my_path.as_bytes());
+ cmd.env("PATH", OsStr::from_bytes(&new_path));
+ cmd.env("PISH_SOCKET", sr.socket_path.as_os_str());
+}
+
pub fn maybe_run_defined_function() {
if let Some(program_name) = std::env::args_os().next() {
let program_name = program_name.as_bytes();
@@ -171,20 +188,11 @@ fn unique_string() -> String {
}
pub struct SocketRunning {
- bin_dir: PathBuf,
- path: PathBuf,
+ bin_path: PathBuf,
+ socket_path: PathBuf,
recv: Receiver<()>,
}
-impl SocketRunning {
- pub fn socket_path(&self) -> &Path {
- &self.path
- }
- pub fn path(&self) -> &Path {
- &self.bin_dir
- }
-}
-
#[must_use]
struct SocketDropper {
session: Arc<Mutex<Session>>,
@@ -225,8 +233,8 @@ pub fn listen(session: Arc<Mutex<Session>>) -> impl Drop {
let mut se = session.lock().unwrap();
assert!(se.socket_running.is_none());
se.socket_running = Some(SocketRunning {
- bin_dir,
- path: socket_path.clone(),
+ bin_path: bin_dir,
+ socket_path: socket_path.clone(),
recv,
});
}
@@ -277,7 +285,7 @@ fn create_function_hook_res(
let session = session.lock().map_err(|e| format!("{e:?}"))?;
let sock_run = session.socket_running.as_ref().ok_or("no socket running")?;
let exe_path = current_exe()?;
- let symlink_path = sock_run.bin_dir.join(OsStr::from_bytes(fun_name));
+ let symlink_path = sock_run.bin_path.join(OsStr::from_bytes(fun_name));
symlink(exe_path, symlink_path)?;
Ok(())
}
diff --git a/src/run/mod.rs b/src/run/mod.rs
index 5a8370f..c7f4408 100644
--- a/src/run/mod.rs
+++ b/src/run/mod.rs
@@ -218,15 +218,7 @@ impl Executor {
command.stdin(stdin);
command.stdout(stdout);
- // TODO: move to export_fun.rs?
- if let Some(sr) = self.se.lock().unwrap().socket_running.as_ref() {
- let my_path = std::env::var_os("PATH").expect("no PATH - seriously?");
- let mut new_path = sr.path().as_os_str().as_bytes().to_vec();
- new_path.push(b':');
- new_path.extend_from_slice(my_path.as_bytes());
- command.env("PATH", OsStr::from_bytes(&new_path));
- command.env("PISH_SOCKET", sr.socket_path().as_os_str());
- }
+ crate::export_fun::prepare_command(self.se.clone(), &mut command);
match command.spawn() {
Ok(c) => SpawnedCmd::Child(c),