Skip to content

Commit 8f3e9df

Browse files
authored
refactor: improve pkexec handle local package path (#611)
1 parent 78a976d commit 8f3e9df

File tree

1 file changed

+3
-21
lines changed

1 file changed

+3
-21
lines changed

src/utils.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
env,
33
io::ErrorKind,
4-
path::{Path, PathBuf},
4+
path::PathBuf,
55
process::{Command, exit},
66
sync::atomic::Ordering,
77
};
@@ -45,23 +45,17 @@ pub fn root() -> Result<()> {
4545
return Ok(());
4646
}
4747

48-
let mut args = std::env::args().collect::<Vec<_>>();
49-
5048
// 检测是否有 DISPLAY,如果有,则在提权时使用 pkexec
5149
// 通常情况下 SSH 连接不会有 DISPLAY 环境变量,除非开启 X11 Forwarding
5250
if (env::var("DISPLAY").is_ok() || env::var("WAYLAND_DISPLAY").is_ok()) && !is_wsl() {
53-
// Workaround: 使用 pkexec 执行其它程序时,若你指定了相对路径
54-
// pkexec 并不会以当前路径作为起点寻求这个位置
55-
// 所以需要转换成绝对路径,再喂给 pkexec
56-
file_path_canonicalize(&mut args);
57-
5851
info!("{}", fl!("pkexec-tips-1"));
5952
info!("{}", fl!("pkexec-tips-2"));
6053

6154
NOT_ALLOW_CTRLC.store(true, Ordering::Relaxed);
6255

6356
let out = Command::new("pkexec")
64-
.args(args)
57+
.arg("--keep-cwd")
58+
.args(std::env::args())
6559
.spawn()
6660
.and_then(|x| x.wait_with_output())
6761
.map_err(|e| anyhow!(fl!("execute-pkexec-fail", e = e.to_string())))?;
@@ -80,18 +74,6 @@ pub fn is_root() -> bool {
8074
process::geteuid().is_root()
8175
}
8276

83-
fn file_path_canonicalize(args: &mut Vec<String>) {
84-
for arg in args {
85-
if !arg.ends_with(".deb") {
86-
continue;
87-
}
88-
89-
let path = Path::new(&arg);
90-
let path = path.canonicalize().unwrap_or(path.to_path_buf());
91-
*arg = path.display().to_string();
92-
}
93-
}
94-
9577
#[inline]
9678
pub fn get_lists_dir(config: &AptConfig) -> PathBuf {
9779
PathBuf::from(config.dir("Dir::State::lists", "lists/"))

0 commit comments

Comments
 (0)