From eb2654fa93b9584573566e9c35258ed7605e21ce Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Mon, 1 Aug 2022 10:21:22 +0300 Subject: [PATCH] command parser file resolution fix --- ableos/src/scratchpad.rs | 78 ++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 52 deletions(-) diff --git a/ableos/src/scratchpad.rs b/ableos/src/scratchpad.rs index d7d57249..6d8b9c5b 100644 --- a/ableos/src/scratchpad.rs +++ b/ableos/src/scratchpad.rs @@ -160,63 +160,37 @@ pub fn command_parser(user: String, command: String) { "quit" => shutdown(), _ => { - let home_exec_path = format!("/home/{}/bins/{}.wasm", user, bin_name); - let shared_exec_path = format!("/shared/bins/{}.wasm", bin_name); - let system_exec_path = format!("/system/bins/{}.wasm", bin_name); - - let home_exec_file = fs.open(&home_exec_path.as_bytes(), OpenOptions::new().read(true)); - let shared_exec_file = - fs.open(&shared_exec_path.as_bytes(), OpenOptions::new().read(true)); - let system_exec_file = - fs.open(&system_exec_path.as_bytes(), OpenOptions::new().read(true)); - - let mut in_home = false; - let mut in_shared = false; - let mut in_system = false; - - let mut binary_prog: Vec = vec![]; - - match home_exec_file { - Ok(file) => { - let ret = file.read_to_end(&mut binary_prog).unwrap(); - in_home = true; + let mut options = OpenOptions::new(); + options.read(true); + let file = { + let path = format!("/home/{user}/bins/{bin_name}.wasm"); + if let Ok(file ) = fs.open(&path.as_bytes(), &options) { + file + } else { + let path = format!("/shared/bins/{bin_name}.wasm"); + if let Ok(file) = fs.open(&path.as_bytes(), &options) { + file + } else { + let path = format!("/system/bins/{bin_name}.wasm"); + match fs.open(&path.as_bytes(), &options) { + Ok(file) => file, + Err(error) => { + trace!("{:?}", error); + println!("No such binary: {}", bin_name); + error!("No such binary: {}", bin_name); + return; + } + } + } } + }; - Err(error) => { - trace!("{:?}", error); - in_home = false; - } - } - match shared_exec_file { - Ok(file) => { - let ret = file.read_to_end(&mut binary_prog).unwrap(); - in_shared = true; - } - Err(error) => { - trace!("{:?}", error); - in_shared = false; - } - } - match system_exec_file { - Ok(file) => { - let ret = file.read_to_end(&mut binary_prog).unwrap(); - in_system = true; - } - - Err(error) => { - trace!("{:?}", error); - in_system = false; - } - } + let mut binary = vec![]; + file.read_to_end(&mut binary).unwrap(); let args = iter.collect::>(); println!("{:?}", args); - if in_home || in_shared || in_system { - run_program(&binary_prog); - } else { - println!("No such binary: {}", bin_name); - error!("No such binary: {}", bin_name); - } + run_program(&binary); } } }