fix can't install when username contains &, @, ^

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2023-02-27 19:22:52 +08:00
parent 6f28643ad3
commit f94791793b
2 changed files with 26 additions and 11 deletions

View File

@ -1236,6 +1236,15 @@ pub fn uninstall_me() -> ResultType<()> {
fn write_cmds(cmds: String, ext: &str, tip: &str) -> ResultType<std::path::PathBuf> { fn write_cmds(cmds: String, ext: &str, tip: &str) -> ResultType<std::path::PathBuf> {
let mut tmp = std::env::temp_dir(); let mut tmp = std::env::temp_dir();
// When dir contains these characters, the bat file will not execute in elevated mode.
if vec!["&", "@", "^"]
.drain(..)
.any(|s| tmp.to_string_lossy().to_string().contains(s))
{
if let Ok(dir) = user_accessible_folder() {
tmp = dir;
}
}
tmp.push(format!("{}_{}.{}", crate::get_app_name(), tip, ext)); tmp.push(format!("{}_{}.{}", crate::get_app_name(), tip, ext));
let mut file = std::fs::File::create(&tmp)?; let mut file = std::fs::File::create(&tmp)?;
// in case cmds mixed with \r\n and \n, make sure all ending with \r\n // in case cmds mixed with \r\n and \n, make sure all ending with \r\n
@ -1872,3 +1881,19 @@ pub fn change_resolution(name: &str, width: usize, height: usize) -> ResultType<
Ok(()) Ok(())
} }
} }
pub fn user_accessible_folder() -> ResultType<PathBuf> {
let disk = std::env::var("SystemDrive").unwrap_or("C:".to_string());
let dir1 = PathBuf::from(format!("{}\\ProgramData", disk));
// NOTICE: "C:\Windows\Temp" requires permanent authorization.
let dir2 = PathBuf::from(format!("{}\\Windows\\Temp", disk));
let dir;
if dir1.exists() {
dir = dir1;
} else if dir2.exists() {
dir = dir2;
} else {
bail!("no vaild user accessible folder");
}
Ok(dir)
}

View File

@ -117,17 +117,7 @@ impl SharedMemory {
} }
fn flink(name: String) -> ResultType<String> { fn flink(name: String) -> ResultType<String> {
let disk = std::env::var("SystemDrive").unwrap_or("C:".to_string()); let mut dir = crate::platform::user_accessible_folder()?;
let dir1 = PathBuf::from(format!("{}\\ProgramData", disk));
let dir2 = PathBuf::from(format!("{}\\Windows\\Temp", disk));
let mut dir;
if dir1.exists() {
dir = dir1;
} else if dir2.exists() {
dir = dir2;
} else {
bail!("no vaild flink directory");
}
dir = dir.join(hbb_common::config::APP_NAME.read().unwrap().clone()); dir = dir.join(hbb_common::config::APP_NAME.read().unwrap().clone());
if !dir.exists() { if !dir.exists() {
std::fs::create_dir(&dir)?; std::fs::create_dir(&dir)?;