add arg to avoid possible recursive run

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2022-09-29 09:00:04 +08:00
parent 02aedd234c
commit 9d8032e5e7
2 changed files with 47 additions and 12 deletions

View File

@ -8,12 +8,20 @@ pub fn core_main() -> Option<Vec<String>> {
let mut args = Vec::new();
let mut i = 0;
let mut is_setup = false;
let mut _is_elevate = false;
let mut _is_run_as_system = false;
for arg in std::env::args() {
// to-do: how to pass to flutter?
if i == 0 && crate::common::is_setup(&arg) {
is_setup = true;
} else if i > 0 {
args.push(arg);
if arg == "--elevate" {
_is_elevate = true;
} else if arg == "--run-as-system" {
_is_run_as_system = true;
} else {
args.push(arg);
}
}
i += 1;
}
@ -60,8 +68,7 @@ pub fn core_main() -> Option<Vec<String>> {
#[cfg(windows)]
#[cfg(not(debug_assertions))]
if !crate::platform::is_installed() && args.is_empty() {
let arg = if is_setup { "--noinstall" } else { "" };
crate::platform::run_check_elevation(arg);
crate::platform::elevate_or_run_as_system(is_setup, _is_elevate, _is_run_as_system);
}
if args.is_empty() {
std::thread::spawn(move || crate::start_server(false));

View File

@ -1472,22 +1472,50 @@ pub fn run_as_system(arg: &str) -> ResultType<()> {
Ok(())
}
pub fn run_check_elevation(arg: &str) {
if let Ok(false) = is_elevated(None) {
if let Ok(true) = elevate(arg) {
std::process::exit(0);
} else {
// do nothing but prompt
}
pub fn elevate_or_run_as_system(is_setup: bool, is_elevate: bool, is_run_as_system: bool) {
// avoid possible run recursively due to failed run, which hasn't happened yet.
let arg_elevate = if is_setup {
"--noinstall --elevate"
} else {
"--elevate"
};
let arg_run_as_system = if is_setup {
"--noinstall --run-as-system"
} else {
"--run-as-system"
};
let rerun_as_system = || {
if !is_root() {
if run_as_system(arg).is_ok() {
if run_as_system(arg_run_as_system).is_ok() {
std::process::exit(0);
} else {
// to-do: should not happen
log::error!("Failed to run as system");
}
}
};
if is_elevate {
if !is_elevated(None).map_or(true, |b| b) {
log::error!("Failed to elevate");
return;
}
rerun_as_system();
} else if is_run_as_system {
if !is_root() {
log::error!("Failed to be system");
}
} else {
if let Ok(true) = is_elevated(None) {
// right click
rerun_as_system();
} else {
// left click || run without install
if let Ok(true) = elevate(arg_elevate) {
std::process::exit(0);
} else {
// do nothing but prompt
}
}
}
}