Skip to content

An open-source remote desktop application designed for self-hosting, as an alternative to TeamViewer.

Fix. Linux, run_cmds, trim new line (#7579)

fufesou2e11a8b4584d
diff --git a/libs/hbb_common/src/config.rs b/libs/hbb_common/src/config.rs
index 5446b7a01..acf304526 100644
--- a/libs/hbb_common/src/config.rs
+++ b/libs/hbb_common/src/config.rs
@@ -406,10 +406,12 @@ fn patch(path: PathBuf) -> PathBuf {
#[cfg(target_os = "linux")]
{
if _tmp == "/root" {
- if let Ok(user) = crate::platform::linux::run_cmds("whoami") {
+ if let Ok(user) = crate::platform::linux::run_cmds_trim_newline("whoami") {
if user != "root" {
let cmd = format!("getent passwd '{}' | awk -F':' '{{print $6}}'", user);
- if let Ok(output) = crate::platform::linux::run_cmds(&cmd) {
+ if let Ok(output) =
+ crate::platform::linux::run_cmds_trim_newline(&cmd)
+ {
return output.into();
}
return format!("/home/{user}").into();
diff --git a/libs/hbb_common/src/platform/linux.rs b/libs/hbb_common/src/platform/linux.rs
index 536ac3091..f7a6244dd 100644
--- a/libs/hbb_common/src/platform/linux.rs
+++ b/libs/hbb_common/src/platform/linux.rs
@@ -192,6 +192,8 @@ pub fn is_active_and_seat0(sid: &str) -> bool {
}
}
+// **Note** that the return value here, the last character is '\n'.
+// Use `run_cmds_trim_newline()` if you want to remove '\n' at the end.
pub fn run_cmds(cmds: &str) -> ResultType<String> {
let output = std::process::Command::new("sh")
.args(vec!["-c", cmds])
@@ -199,6 +201,18 @@ pub fn run_cmds(cmds: &str) -> ResultType<String> {
Ok(String::from_utf8_lossy(&output.stdout).to_string())
}
+pub fn run_cmds_trim_newline(cmds: &str) -> ResultType<String> {
+ let output = std::process::Command::new("sh")
+ .args(vec!["-c", cmds])
+ .output()?;
+ let out = String::from_utf8_lossy(&output.stdout);
+ Ok(if out.ends_with('\n') {
+ out[..out.len() - 1].to_string()
+ } else {
+ out.to_string()
+ })
+}
+
#[cfg(not(feature = "flatpak"))]
fn run_loginctl(args: Option<Vec<&str>>) -> std::io::Result<std::process::Output> {
let mut cmd = std::process::Command::new("loginctl");
@@ -257,3 +271,15 @@ pub fn system_message(title: &str, msg: &str, forever: bool) -> ResultType<()> {
}
crate::bail!("failed to post system message");
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_run_cmds_trim_newline() {
+ assert_eq!(run_cmds_trim_newline("echo -n 123").unwrap(), "123");
+ assert_eq!(run_cmds_trim_newline("echo 123").unwrap(), "123");
+ assert_eq!(run_cmds_trim_newline("whoami").unwrap() + "\n", run_cmds("whoami").unwrap());
+ }
+}
diff --git a/src/platform/linux.rs b/src/platform/linux.rs
index 7a1eac0ca..3c9b6c57f 100644
--- a/src/platform/linux.rs
+++ b/src/platform/linux.rs
@@ -1020,7 +1020,7 @@ mod desktop {
"getent passwd '{}' | awk -F':' '{{print $6}}'",
&self.username
);
- self.home = run_cmds(&cmd).unwrap_or(format!("/home/{}", &self.username));
+ self.home = run_cmds_trim_newline(&cmd).unwrap_or(format!("/home/{}", &self.username));
}
fn get_xauth_from_xorg(&mut self) {
@@ -1211,6 +1211,29 @@ mod desktop {
}
}
}
+
+ #[cfg(test)]
+ mod tests {
+ use super::*;
+
+ #[test]
+ fn test_desktop_env() {
+ let mut d = Desktop::default();
+ d.refresh();
+ if d.username == "root" {
+ assert_eq!(d.home, "/root");
+ } else {
+ if !d.username.is_empty() {
+ let home = super::super::get_env_var("HOME");
+ if !home.is_empty() {
+ assert_eq!(d.home, home);
+ } else {
+ //
+ }
+ }
+ }
+ }
+ }
}
pub struct WakeLock(Option<keepawake::AwakeHandle>);