From 83634341aa6683e1b96717757557c7d83a89b3fd Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sun, 31 Jan 2021 15:47:49 +0100 Subject: feat(users/Profpatsch/execline): add args_for_exec `exec_into_args` would just read argv and exec into it, but we want to be able to write commands which take some positional arguments first. Thus we split the invocation into `args_for_exec`, which returns the positional arguments and prog, and then pass prog to `exec_into_args` when we want to exec eventually (prog is still an iterator at this point). Change-Id: I0b180c1a100b96363fe33ba2c42034ed41716b7a Reviewed-on: https://cl.tvl.fyi/c/depot/+/2474 Tested-by: BuildkiteCI Reviewed-by: Profpatsch --- users/Profpatsch/execline/default.nix | 17 +------------ users/Profpatsch/execline/exec_helpers.rs | 41 +++++++++++++++++++++++++++++++ users/Profpatsch/netencode/default.nix | 17 ++++++------- 3 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 users/Profpatsch/execline/exec_helpers.rs (limited to 'users') diff --git a/users/Profpatsch/execline/default.nix b/users/Profpatsch/execline/default.nix index 51f4923e9628..852fcfcfa005 100644 --- a/users/Profpatsch/execline/default.nix +++ b/users/Profpatsch/execline/default.nix @@ -3,22 +3,7 @@ let exec-helpers = depot.users.Profpatsch.writers.rustSimpleLib { name = "exec-helpers"; - } '' - use std::os::unix::process::CommandExt; - use std::ffi::OsStr; - use std::os::unix::ffi::OsStrExt; - pub fn exec_into_args<'a, I>(prog_name: &str, env_additions: I) -> ! - where - I: IntoIterator, - { - let mut argv = std::env::args_os(); - let prog = argv.nth(1).expect(&format!("{}: first argument must be an executable", prog_name)); - let args = argv; - let env = env_additions.into_iter().map(|(k,v)| (OsStr::from_bytes(k), OsStr::from_bytes(v))); - let err = std::process::Command::new(prog).args(args).envs(env).exec(); - panic!("{}: exec failed: {:?}", prog_name, err); - } - ''; + } (builtins.readFile ./exec_helpers.rs); in { inherit diff --git a/users/Profpatsch/execline/exec_helpers.rs b/users/Profpatsch/execline/exec_helpers.rs new file mode 100644 index 000000000000..4e4149882b40 --- /dev/null +++ b/users/Profpatsch/execline/exec_helpers.rs @@ -0,0 +1,41 @@ +use std::os::unix::process::CommandExt; +use std::ffi::OsStr; +use std::os::unix::ffi::{OsStringExt, OsStrExt}; + +pub fn args_for_exec(current_prog_name: &str, no_of_positional_args: usize) -> (Vec>, Vec>) { + let mut args = std::env::args_os(); + // remove argv[0] + let _ = args.nth(0); + let mut args = args.map(|arg| arg.into_vec()); + let mut pos_args = vec![]; + // get positional args + for i in 1..no_of_positional_args { + pos_args.push( + args.nth(0).expect( + &format!("{}: expects {} positional args, only got {}", current_prog_name, no_of_positional_args, i)) + ); + } + // prog... is the rest of the iterator + let prog : Vec> = args.collect(); + (pos_args, prog) +} + +pub fn exec_into_args<'a, 'b, Args, Arg, Env, Key, Val>(current_prog_name: &str, args: Args, env_additions: Env) -> ! + where + Args: IntoIterator, + Arg: AsRef<[u8]>, + Env: IntoIterator, + Key: AsRef<[u8]>, + Val: AsRef<[u8]>, +{ + // TODO: is this possible without collecting into a Vec first, just leaving it an IntoIterator? + let args = args.into_iter().collect::>(); + let mut args = args.iter().map(|v| OsStr::from_bytes(v.as_ref())); + let prog = args.nth(1).expect(&format!("{}: first argument must be an executable", current_prog_name)); + // TODO: same here + let env = env_additions.into_iter().collect::>(); + let env = env.iter().map(|(k,v)| (OsStr::from_bytes(k.as_ref()), OsStr::from_bytes(v.as_ref()))); + let err = std::process::Command::new(prog).args(args).envs(env).exec(); + panic!("{}: exec failed: {:?}", current_prog_name, err); +} + diff --git a/users/Profpatsch/netencode/default.nix b/users/Profpatsch/netencode/default.nix index 99cffc75b618..fb1d2c2ef831 100644 --- a/users/Profpatsch/netencode/default.nix +++ b/users/Profpatsch/netencode/default.nix @@ -99,17 +99,16 @@ let extern crate netencode; extern crate exec_helpers; use netencode::dec::{Record, ScalarAsBytes, Decoder, DecodeError}; + fn main() { let t = netencode::t_from_stdin_or_panic("record-splice-env"); - match Record::::dec(t) { - Ok(map) => { - exec_helpers::exec_into_args( - "record-splice-env", - map.iter().map(|(k,v)| (k.as_bytes(), &v[..]) - ); - }, - Err(DecodeError(err)) => panic!("{}", err), - } + let (_, prog) = exec_helpers::args_for_exec("record-splice-env", 0); + match Record::::dec(t) { + Ok(map) => { + exec_helpers::exec_into_args("record-splice-env", prog, map); + }, + Err(DecodeError(err)) => panic!("{}", err), + } } ''; -- cgit 1.4.1