From e69b66e248bfb431312f21b3476a74dbc065dd30 Mon Sep 17 00:00:00 2001 From: Miraculous Owonubi Date: Mon, 17 May 2021 03:38:12 +0100 Subject: [PATCH] select apk from binary path, not cwd --- relay-rust/src/execution_error.rs | 18 ++++++------------ relay-rust/src/main.rs | 24 ++++++++++++++---------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/relay-rust/src/execution_error.rs b/relay-rust/src/execution_error.rs index 39b5610b..93ac3ad2 100644 --- a/relay-rust/src/execution_error.rs +++ b/relay-rust/src/execution_error.rs @@ -15,6 +15,7 @@ */ use std::error; +use std::ffi::OsString; use std::fmt; use std::io; #[cfg(unix)] @@ -42,8 +43,8 @@ pub struct ProcessIoError { #[derive(Debug)] pub struct Cmd { - command: String, - args: Vec, + command: OsString, + args: Vec, } #[derive(Debug)] @@ -67,20 +68,13 @@ impl Termination { impl fmt::Display for Cmd { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{} {:?}", self.command, self.args) + write!(f, "{:?} {:?}", self.command, self.args) } } impl Cmd { - pub fn new(command: S1, args: Vec) -> Cmd - where - S1: Into, - S2: Into, - { - Self { - command: command.into(), - args: args.into_iter().map(Into::into).collect::>(), - } + pub fn new(command: OsString, args: Vec) -> Cmd { + Self { command, args } } } diff --git a/relay-rust/src/main.rs b/relay-rust/src/main.rs index cb80e9c8..a1af5509 100644 --- a/relay-rust/src/main.rs +++ b/relay-rust/src/main.rs @@ -29,6 +29,7 @@ use crate::adb_monitor::AdbMonitor; use crate::cli_args::CommandLineArguments; use crate::execution_error::{Cmd, CommandExecutionError, ProcessIoError, ProcessStatusError}; use std::env; +use std::ffi::OsString; use std::process::{self, exit}; use std::thread; use std::time::Duration; @@ -46,11 +47,14 @@ fn get_adb_path() -> String { } #[inline] -fn get_apk_path() -> String { +fn get_apk_path() -> Result { if let Some(env_adb) = std::env::var_os("GNIREHTET_APK") { - env_adb.into_string().expect("invalid GNIREHTET_APK value") + Ok(env_adb) } else { - "gnirehtet.apk".to_string() + let bin_path = env::current_exe().map_err(CommandExecutionError::Io)?; + let real_path = bin_path.read_link().unwrap_or(bin_path); + let apk_path = real_path.parent().unwrap().join("gnirehtet.apk"); + Ok(apk_path.into_os_string()) } } @@ -343,7 +347,7 @@ impl Command for RelayCommand { fn cmd_install(serial: Option<&str>) -> Result<(), CommandExecutionError> { info!(target: TAG, "Installing gnirehtet client..."); - exec_adb(serial, vec!["install".into(), "-r".into(), get_apk_path()]) + exec_adb(serial, vec!["install".into(), "-r".into(), get_apk_path()?]) } fn cmd_uninstall(serial: Option<&str>) -> Result<(), CommandExecutionError> { @@ -499,11 +503,11 @@ fn async_start(serial: Option<&str>, dns_servers: Option<&str>, routes: Option<& }); } -fn create_adb_args>(serial: Option<&str>, args: Vec) -> Vec { - let mut command = Vec::::new(); +fn create_adb_args>(serial: Option<&str>, args: Vec) -> Vec { + let mut command = Vec::new(); if let Some(serial) = serial { command.push("-s".into()); - command.push(serial.to_string()); + command.push(serial.into()); } for arg in args { command.push(arg.into()); @@ -511,12 +515,12 @@ fn create_adb_args>(serial: Option<&str>, args: Vec) -> Vec>( +fn exec_adb>( serial: Option<&str>, args: Vec, ) -> Result<(), CommandExecutionError> { let adb_args = create_adb_args(serial, args); - let adb = get_adb_path(); + let adb = get_adb_path().into(); debug!(target: TAG, "Execute: {:?} {:?}", adb, adb_args); match process::Command::new(&adb).args(&adb_args[..]).status() { Ok(exit_status) => { @@ -540,7 +544,7 @@ fn must_install_client(serial: Option<&str>) -> Result {