From 35775afd84f4a68a5bf3464623f6408d077a3502 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Sun, 27 Nov 2022 21:52:18 -0500 Subject: [PATCH] Rewrite home to not rely on gamescope running --- Cargo.lock | 2 +- usdpl-back/Cargo.toml | 2 +- usdpl-back/src/api_common/dirs.rs | 43 +++++++++++++++++++------------ 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5217acd..8d56e42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1037,7 +1037,7 @@ dependencies = [ [[package]] name = "usdpl-back" -version = "0.7.1" +version = "0.7.2" dependencies = [ "async-recursion", "async-trait", diff --git a/usdpl-back/Cargo.toml b/usdpl-back/Cargo.toml index 2877093..debb2a5 100644 --- a/usdpl-back/Cargo.toml +++ b/usdpl-back/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "usdpl-back" -version = "0.7.1" +version = "0.7.2" edition = "2021" license = "GPL-3.0-only" repository = "https://github.com/NGnius/usdpl-rs" diff --git a/usdpl-back/src/api_common/dirs.rs b/usdpl-back/src/api_common/dirs.rs index ffaf715..69a9365 100644 --- a/usdpl-back/src/api_common/dirs.rs +++ b/usdpl-back/src/api_common/dirs.rs @@ -6,21 +6,32 @@ use std::process::Command; /// The home directory of the user currently running the Steam Deck UI (specifically: running gamescope). pub fn home() -> Option { - let pid_out = Command::new("pidof") - .args(["gamescope"]) + let who_out = Command::new("who") .output().ok()?; - let pid_out_str = String::from_utf8_lossy(pid_out.stdout.as_slice()); - //println!("pidof gamescope: {}", pid_out_str); - let pid_str = pid_out_str.split(" ").next()?.trim(); - let uid: String = super::files::read_single(format!("/proc/{}/loginuid", pid_str.trim())).ok()?; - //println!("uid: {}", uid); - //let pid: u32 = pid_str.parse().ok()?; - let user_info = Command::new("bash") - .args(["-c", &format!("id {}", uid)]) - .output().ok()?; - let user_out_str = String::from_utf8_lossy(user_info.stdout.as_slice()); - //println!("loginuid: {}", user_out_str); - let user_str = user_out_str.split(")").next()?; - let user = &user_str[user_str.find("(")?+1..]; - Some(Path::new("/home").join(user)) + let who_str = String::from_utf8_lossy(who_out.stdout.as_slice()); + for login in who_str.split("\n") { + let username = login + .split(" ") + .next()? + .trim(); + let path = Path::new("/home").join(username); + if path.is_dir() { + return Some(path); + } + } + None +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn home_test() { + let home_opt = home(); + assert!(home_opt.is_some(), "Expected valid home to be detected"); + let real_home = home_opt.unwrap(); + assert!(real_home.exists(), "Received invalid home dir"); + println!("Home dir detected as {}", real_home.display()); + } }