From b3531aca5fe6a61355a683b396963f431e7581f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=B4=E5=A4=A9?= Date: Fri, 13 Mar 2026 01:14:44 +0800 Subject: [PATCH] fix: replace unsafe libc::geteuid with std::env for CI clippy compatibility --- src-tauri/Cargo.lock | 1 - src-tauri/Cargo.toml | 1 - src-tauri/src/commands/config.rs | 11 ++++++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index b522fbd..da49381 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -334,7 +334,6 @@ dependencies = [ "chrono", "dirs", "ed25519-dalek", - "libc", "rand 0.8.5", "regex", "reqwest 0.12.28", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index ae9cb1c..a7be803 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -31,4 +31,3 @@ base64 = "0.22" urlencoding = "2" regex = "1" tokio = { version = "1", features = ["process", "time"] } -libc = "0.2" diff --git a/src-tauri/src/commands/config.rs b/src-tauri/src/commands/config.rs index 6182168..dc78124 100644 --- a/src-tauri/src/commands/config.rs +++ b/src-tauri/src/commands/config.rs @@ -30,6 +30,15 @@ impl Drop for GuardianPause { /// 预设 npm 源列表 const DEFAULT_REGISTRY: &str = "https://registry.npmmirror.com"; +/// Linux: 检测是否以 root 身份运行(避免 unsafe libc 调用) +#[cfg(target_os = "linux")] +fn nix_is_root() -> bool { + std::env::var("USER") + .or_else(|_| std::env::var("EUID")) + .map(|v| v == "root" || v == "0") + .unwrap_or(false) +} + /// 读取用户配置的 npm registry,fallback 到淘宝镜像 fn get_configured_registry() -> String { let path = super::openclaw_dir().join("npm-registry.txt"); @@ -64,7 +73,7 @@ fn npm_command() -> Command { #[cfg(target_os = "linux")] { // Linux 非 root 用户全局 npm install 需要 sudo - let need_sudo = unsafe { libc::geteuid() } != 0; + let need_sudo = !nix_is_root(); let mut cmd = if need_sudo { let mut c = Command::new("sudo"); c.args(["npm", "--registry", ®istry]);