Skip to content

Commit

Permalink
Merge pull request #408 from brson/defense
Browse files Browse the repository at this point in the history
Refuse to install if it looks like other Rust installations are present
  • Loading branch information
alexcrichton committed May 7, 2016
2 parents 9e3832f + 0080271 commit be2d312
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/rustup-cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ fn canonical_cargo_home() -> Result<String> {
pub fn install(no_prompt: bool, verbose: bool,
opts: InstallOpts) -> Result<()> {

try!(do_pre_install_sanity_checks());

let selected_opts;

if !no_prompt {
Expand Down Expand Up @@ -290,6 +292,49 @@ pub fn install(no_prompt: bool, verbose: bool,
Ok(())
}

fn do_pre_install_sanity_checks() -> Result<()> {

let multirust_manifest_path
= PathBuf::from("/usr/local/lib/rustlib/manifest-multirust");
let rustc_manifest_path
= PathBuf::from("/usr/local/lib/rustlib/manifest-rustc");
let uninstaller_path
= PathBuf::from("/usr/local/lib/rustlib/uninstall.sh");
let rustup_sh_path
= env::home_dir().map(|d| d.join(".rustup"));
let rustup_sh_version_path = rustup_sh_path.as_ref().map(|p| p.join("rustup-version"));

let multirust_exists =
multirust_manifest_path.exists() && uninstaller_path.exists();
let rustc_exists =
rustc_manifest_path.exists() && uninstaller_path.exists();
let rustup_sh_exists =
rustup_sh_version_path.map(|p| p.exists()) == Some(true);

if multirust_exists {
warn!("it looks like you have an existing installation of multirust");
warn!("rustup cannot be installed alongside multirust. Please uninstall first");
warn!("run `{}` as root to uninstall multirust", uninstaller_path.display());
return Err("cannot install while multirust is installed".into());
}

if rustc_exists {
warn!("it looks like you have an existing installation of Rust");
warn!("rustup cannot be installed alongside Rust. Please uninstall first");
warn!("run `{}` as root to uninstall Rust", uninstaller_path.display());
return Err("cannot install while Rust is installed".into());
}

if rustup_sh_exists {
warn!("it looks like you have existing rustup.sh metadata");
warn!("rustup cannot be installed while rustup.sh metadata exists");
warn!("delete `{}` to remove rustup.sh", rustup_sh_path.expect("").display());
return Err("cannot install while rustup.sh is installed".into());
}

Ok(())
}

fn pre_install_msg(no_modify_path: bool) -> Result<String> {
let cargo_home = try!(utils::cargo_home());
let cargo_home_bin = cargo_home.join("bin");
Expand Down
22 changes: 22 additions & 0 deletions tests/cli-self-update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,3 +1046,25 @@ fn uninstall_doesnt_mess_with_a_non_unicode_path() {
assert!(path.bytes == reg_value.bytes);
});
}

#[test]
#[ignore] // untestable
fn install_but_multirust_is_installed() {
}

#[test]
#[ignore] // untestable
fn install_but_rustc_is_installed() {
}

#[test]
fn install_but_rustup_sh_is_installed() {
setup(&|config| {
let rustup_dir = config.homedir.join(".rustup");
fs::create_dir_all(&rustup_dir).unwrap();
let version_file = rustup_dir.join("rustup-version");
raw::write_file(&version_file, "").unwrap();
expect_err(config, &["rustup-init", "-y"],
"cannot install while rustup.sh is installed");
});
}

0 comments on commit be2d312

Please sign in to comment.