Skip to content

Commit

Permalink
Fix install loader cwd to always be exe location
Browse files Browse the repository at this point in the history
- See #110 - installer started from Start menu (by searching), will use cwd c:\Windows\system32, which is not writeable
- This ensures installer always has its cwd at the location of the .exe
  • Loading branch information
drojf committed Sep 5, 2020
1 parent 5efd459 commit ca120e9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions install_loader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use clap::{App, Arg, ArgMatches};
use std::error::Error;
use std::path::PathBuf;

mod archive_extractor;
mod config;
Expand Down Expand Up @@ -37,9 +38,30 @@ fn handle_open_command(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
}
}

fn fix_cwd() -> Result<PathBuf, Box<dyn Error>> {
let exe_path = std::env::current_exe()?;
let containing_path = exe_path.parent().ok_or("Invalid Path")?;
std::env::set_current_dir(containing_path)?;
Ok(containing_path.to_path_buf())
}

fn main() -> Result<(), Box<dyn Error>> {
panic_handler::set_hook(String::from("07th-mod_crash.log"));

// Change current directory to .exe path, if current .exe path is known
let old_cwd = std::env::current_dir();
match fix_cwd() {
Ok(new_cwd) => println!(
"Successfully changed path from {:?} to {:?}",
old_cwd, new_cwd
),
Err(e) => println!(
"Couldn't fix exe path - cwd remains as [{:?}]. Error: [{}]",
std::env::current_dir(),
e
),
}

let open_about_msg = r#"Shows an open dialog and:
- if user selected a path, writes the chosen path to stdout, returns 0
- if user cancelled, writes nothing to stdout, returns 0
Expand Down

0 comments on commit ca120e9

Please sign in to comment.