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

* Fix. Linux, run_cmds, trim new line

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* add tests

Signed-off-by: fufesou <shuanglongchen@yeah.net>

---------

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2024-04-02 14:47:13 +08:00 committed by GitHub
parent 58fe95d6fd
commit 2e11a8b458
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 3 deletions

View File

@ -406,10 +406,12 @@ fn patch(path: PathBuf) -> PathBuf {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
if _tmp == "/root" { 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" { if user != "root" {
let cmd = format!("getent passwd '{}' | awk -F':' '{{print $6}}'", user); 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 output.into();
} }
return format!("/home/{user}").into(); return format!("/home/{user}").into();

View File

@ -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> { pub fn run_cmds(cmds: &str) -> ResultType<String> {
let output = std::process::Command::new("sh") let output = std::process::Command::new("sh")
.args(vec!["-c", cmds]) .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()) 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"))] #[cfg(not(feature = "flatpak"))]
fn run_loginctl(args: Option<Vec<&str>>) -> std::io::Result<std::process::Output> { fn run_loginctl(args: Option<Vec<&str>>) -> std::io::Result<std::process::Output> {
let mut cmd = std::process::Command::new("loginctl"); 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"); 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());
}
}

View File

@ -1020,7 +1020,7 @@ mod desktop {
"getent passwd '{}' | awk -F':' '{{print $6}}'", "getent passwd '{}' | awk -F':' '{{print $6}}'",
&self.username &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) { 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>); pub struct WakeLock(Option<keepawake::AwakeHandle>);