From 5fc32971df7eb968ab230fad71a059dd277bc570 Mon Sep 17 00:00:00 2001 From: fufesou Date: Fri, 27 May 2022 11:07:24 +0800 Subject: [PATCH] better_linux_run_cmds: run cmds without creating tmp files Signed-off-by: fufesou --- src/lang/cs.rs | 2 +- src/lang/eo.rs | 1 + src/lang/template.rs | 2 +- src/lang/tr.rs | 2 +- src/lang/tw.rs | 2 +- src/platform/linux.rs | 31 +++++++++---------------------- 6 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/lang/cs.rs b/src/lang/cs.rs index 42593d8ec..7018e909d 100644 --- a/src/lang/cs.rs +++ b/src/lang/cs.rs @@ -42,7 +42,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Invalid IP", "Neplatná IP adresa"), ("id_change_tip", "Použít je mozné pouze znaky a-z, A-Z, 0-9 a _ (podtržítko). Dále je třeba aby začínalo na písmeno a-z, A-Z. Délka mezi 6 a 16 znaky."), ("Invalid format", "Neplatný formát"), - ("This function is turned off by the server", "Tato funkce je vypnuta serverem"), + ("server_not_support", "Server zatím nepodporuje"), ("Not available", "Není k dispozici"), ("Too frequent", "Příliš časté"), ("Cancel", "Storno"), diff --git a/src/lang/eo.rs b/src/lang/eo.rs index e7be14e51..0edd74e25 100644 --- a/src/lang/eo.rs +++ b/src/lang/eo.rs @@ -42,6 +42,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Invalid IP", "IP nevalida"), ("id_change_tip", "Nur la signoj a-z, A-Z, 0-9, _ (substreko) povas esti uzataj. La unua litero povas esti inter a-z, A-Z. La longeco devas esti inter 6 kaj 16."), ("Invalid format", "Formato nevalida"), + ("server_not_support", "Ankoraŭ ne subtenata de la servilo"), ("Not available", "Nedisponebla"), ("Too frequent", "Tro ofte ŝanĝita, bonvolu reprovi poste"), ("Cancel", "Nuligi"), diff --git a/src/lang/template.rs b/src/lang/template.rs index 84be6ed8d..cc56af085 100644 --- a/src/lang/template.rs +++ b/src/lang/template.rs @@ -42,7 +42,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Invalid IP", ""), ("id_change_tip", ""), ("Invalid format", ""), - ("This function is turned off by the server", ""), + ("server_not_support", ""), ("Not available", ""), ("Too frequent", ""), ("Cancel", ""), diff --git a/src/lang/tr.rs b/src/lang/tr.rs index 02da22e2b..d29fe9d50 100644 --- a/src/lang/tr.rs +++ b/src/lang/tr.rs @@ -42,7 +42,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Invalid IP", "Geçersiz IP adresi"), ("id_change_tip", "Yalnızca a-z, A-Z, 0-9 ve _ (alt çizgi) karakterlerini kullanabilirsiniz. İlk karakter a-z veya A-Z olmalıdır. Uzunluk 6 ile 16 karakter arasında olmalıdır."), ("Invalid format", "Hatalı Format"), - ("This function is turned off by the server", "Bu özellik sunucu tarafından kapatıldı"), + ("server_not_support", "Henüz sunucu tarafından desteklenmiyor"), ("Not available", "Erişilebilir değil"), ("Too frequent", "Çok sık"), ("Cancel", "İptal"), diff --git a/src/lang/tw.rs b/src/lang/tw.rs index f72140af6..3316e89c7 100644 --- a/src/lang/tw.rs +++ b/src/lang/tw.rs @@ -184,7 +184,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("x11 expected", "預期 x11"), ("Port", "連接埠"), ("Settings", "設定"), - ("Username", " 使用者名稱"), + ("Username", "使用者名稱"), ("Invalid port", "連接埠無效"), ("Closed manually by the peer", "由對方手動關閉"), ("Enable remote configuration modification", "啟用遠端更改設定"), diff --git a/src/platform/linux.rs b/src/platform/linux.rs index 79d87492a..efd6476b6 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -1,7 +1,6 @@ use super::{CursorData, ResultType}; use hbb_common::{allow_err, bail, log}; use libc::{c_char, c_int, c_void}; -use std::io::prelude::*; use std::{ cell::RefCell, sync::{ @@ -371,8 +370,7 @@ fn get_display_server_of_session(session: &str) -> String { .replace("TTY=", "") .trim_end() .into(); - if let Ok(Some(xorg_results)) = - run_cmds(format!("ps -e | grep \"{}.\\\\+Xorg\"", tty)) + if let Ok(xorg_results) = run_cmds(format!("ps -e | grep \"{}.\\\\+Xorg\"", tty)) // And check if Xorg is running on that tty { if xorg_results.trim_end().to_string() != "" { @@ -441,9 +439,7 @@ pub fn current_is_wayland() -> bool { pub fn modify_default_login() -> String { let dsession = std::env::var("DESKTOP_SESSION").unwrap(); let user_name = std::env::var("USERNAME").unwrap(); - if let Ok(Some(x)) = - run_cmds("ls /usr/share/* | grep ${DESKTOP_SESSION}-xorg.desktop".to_owned()) - { + if let Ok(x) = run_cmds("ls /usr/share/* | grep ${DESKTOP_SESSION}-xorg.desktop".to_owned()) { if x.trim_end().to_string() != "" { match std::process::Command::new("pkexec") .args(vec![ @@ -471,7 +467,7 @@ pub fn modify_default_login() -> String { return "Fix failed! Please re-login with X server manually".to_owned(); } } - } else if let Ok(Some(z)) = + } else if let Ok(z) = run_cmds("ls /usr/share/* | grep ${DESKTOP_SESSION:0:-8}.desktop".to_owned()) { if z.trim_end().to_string() != "" { @@ -605,20 +601,11 @@ pub fn is_installed() -> bool { true } -fn run_cmds(cmds: String) -> ResultType> { - let mut tmp = std::env::temp_dir(); - tmp.push(format!("{}_{}", crate::get_app_name(), crate::get_time())); - let mut file = std::fs::File::create(&tmp)?; - file.write_all(cmds.as_bytes())?; - file.sync_all()?; - if let Ok(output) = std::process::Command::new("bash") - .arg(tmp.to_str().unwrap_or("")) - .output() - { - Ok(Some(String::from_utf8_lossy(&output.stdout).to_string())) - } else { - Ok(None) - } +pub fn run_cmds(cmds: String) -> ResultType { + let output = std::process::Command::new("sh") + .args(vec!["-c", &cmds]) + .output()?; + Ok(String::from_utf8_lossy(&output.stdout).to_string()) } fn get_env_tries(name: &str, uid: &str, n: usize) -> String { @@ -635,7 +622,7 @@ fn get_env_tries(name: &str, uid: &str, n: usize) -> String { fn get_env(name: &str, uid: &str) -> String { let cmd = format!("ps -u {} -o pid= | xargs -I__ cat /proc/__/environ 2>/dev/null | tr '\\0' '\\n' | grep '^{}=' | tail -1 | sed 's/{}=//g'", uid, name, name); log::debug!("Run: {}", &cmd); - if let Ok(Some(x)) = run_cmds(cmd) { + if let Ok(x) = run_cmds(cmd) { x.trim_end().to_string() } else { "".to_owned()