Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an entry to the installed programs on windows #2670

Merged
merged 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ pub fn install(
do_write_env_files()?;

if !opts.no_modify_path {
do_add_to_programs()?;
rbtcollins marked this conversation as resolved.
Show resolved Hide resolved
do_add_to_path()?;
}
utils::create_rustup_home()?;
Expand Down Expand Up @@ -855,6 +856,7 @@ pub fn uninstall(no_prompt: bool) -> Result<utils::ExitCode> {

// Remove CARGO_HOME/bin from PATH
do_remove_from_path()?;
do_remove_from_programs()?;

// Delete everything in CARGO_HOME *except* the rustup bin

Expand Down
8 changes: 8 additions & 0 deletions src/cli/self_update/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ pub fn do_write_env_files() -> Result<()> {
Ok(())
}

pub fn do_add_to_programs() -> Result<()> {
Ok(())
}

pub fn do_remove_from_programs() -> Result<()> {
Ok(())
}

/// Tell the upgrader to replace the rustup bins, then delete
/// itself. Like with uninstallation, on Windows we're going to
/// have to jump through hoops to make everything work right.
Expand Down
64 changes: 54 additions & 10 deletions src/cli/self_update/windows.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::env::consts::EXE_SUFFIX;
use std::ffi::{OsStr, OsString};
use std::os::windows::ffi::{OsStrExt, OsStringExt};
use std::path::Path;
use std::process::Command;

Expand All @@ -10,6 +12,9 @@ use crate::process;
use crate::utils::utils;
use crate::utils::Notification;

use winreg::enums::{RegType, HKEY_CURRENT_USER, KEY_READ, KEY_WRITE};
use winreg::{RegKey, RegValue};

pub fn ensure_prompt() -> Result<()> {
writeln!(process().stdout(),)?;
writeln!(process().stdout(), "Press the Enter key to continue.")?;
Expand Down Expand Up @@ -42,7 +47,6 @@ pub fn do_msvc_check(opts: &InstallOpts<'_>) -> Result<bool> {

/// Run by rustup-gc-$num.exe to delete CARGO_HOME
pub fn complete_windows_uninstall() -> Result<utils::ExitCode> {
use std::ffi::OsStr;
use std::process::Stdio;

wait_for_parent()?;
Expand Down Expand Up @@ -150,8 +154,6 @@ fn _apply_new_path(new_path: Option<Vec<u16>>) -> Result<()> {
use winapi::um::winuser::{
SendMessageTimeoutA, HWND_BROADCAST, SMTO_ABORTIFHUNG, WM_SETTINGCHANGE,
};
use winreg::enums::{RegType, HKEY_CURRENT_USER, KEY_READ, KEY_WRITE};
use winreg::{RegKey, RegValue};

let new_path = match new_path {
Some(new_path) => new_path,
Expand Down Expand Up @@ -198,8 +200,6 @@ fn _apply_new_path(new_path: Option<Vec<u16>>) -> Result<()> {
// should not mess with it.
fn get_windows_path_var() -> Result<Option<Vec<u16>>> {
use std::io;
use winreg::enums::{HKEY_CURRENT_USER, KEY_READ, KEY_WRITE};
use winreg::RegKey;

let root = RegKey::predef(HKEY_CURRENT_USER);
let environment = root
Expand Down Expand Up @@ -270,9 +270,6 @@ fn _with_path_cargo_home_bin<F>(f: F) -> Result<Option<Vec<u16>>>
where
F: FnOnce(Vec<u16>, Vec<u16>) -> Option<Vec<u16>>,
{
use std::ffi::OsString;
use std::os::windows::ffi::OsStrExt;

let windows_path = get_windows_path_var()?;
let mut path_str = utils::cargo_home()?;
path_str.push("bin");
Expand All @@ -285,6 +282,55 @@ pub fn do_remove_from_path() -> Result<()> {
_apply_new_path(new_path)
}

const RUSTUP_UNINSTALL_ENTRY: &str = r"Software\Microsoft\Windows\CurrentVersion\Uninstall\Rustup";

pub fn do_add_to_programs() -> Result<()> {
use std::path::PathBuf;

let key = RegKey::predef(HKEY_CURRENT_USER)
.create_subkey(RUSTUP_UNINSTALL_ENTRY)
.chain_err(|| ErrorKind::PermissionDenied)?
rbtcollins marked this conversation as resolved.
Show resolved Hide resolved
.0;

// Don't overwrite registry if Rustup is already installed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not certain this is right, but for now lets leave it. I think it might be more that we want to determine default RUSTUP_HOME being overridden etc, and otherwise we should be writing the current correct value into the registry each time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the "current correct value"? As someone who overrides RUSTUP_HOME and use temporary installs of rustup in parallel of the standard one, I wouldn't want those temporary installs to replace the standard one in the registry.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A later install wont override a previous one, as long as the previous install is still installed.
However when uninstalling, the registry key will be unconditionally removed, and then recreated on the next install. See #2670 (comment)

By the way, any rustup self uninstall will unistall rustup in the current RUSTUP_HOME, regardless of where the executable you're running is.

let prev = key
.get_raw_value("UninstallString")
.map(|val| from_winreg_value(&val));
if let Ok(Some(s)) = prev {
let mut path = PathBuf::from(OsString::from_wide(&s));
path.pop();
if path.exists() {
return Ok(());
}
}

let mut path = utils::cargo_home()?;
path.push("bin\rustup.exe");
let mut uninstall_cmd = OsString::from("\"");
uninstall_cmd.push(path);
uninstall_cmd.push("\" self uninstall");

let reg_value = RegValue {
bytes: to_winreg_bytes(uninstall_cmd.encode_wide().collect()),
vtype: RegType::REG_SZ,
};

key.set_raw_value("UninstallString", &reg_value)
.chain_err(|| ErrorKind::PermissionDenied)?;
rbtcollins marked this conversation as resolved.
Show resolved Hide resolved
key.set_value("DisplayName", &"Rustup: the Rust toolchain installer")
.chain_err(|| ErrorKind::PermissionDenied)?;
rbtcollins marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}

pub fn do_remove_from_programs() -> Result<()> {
match RegKey::predef(HKEY_CURRENT_USER).delete_subkey_all(RUSTUP_UNINSTALL_ENTRY) {
Ok(()) => Ok(()),
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e).chain_err(|| ErrorKind::PermissionDenied),
}
}

/// Convert a vector UCS-2 chars to a null-terminated UCS-2 string in bytes
pub fn to_winreg_bytes(mut v: Vec<u16>) -> Vec<u8> {
v.push(0);
Expand All @@ -296,7 +342,6 @@ pub fn to_winreg_bytes(mut v: Vec<u16>) -> Vec<u8> {
/// does a lossy unicode conversion.
pub fn from_winreg_value(val: &winreg::RegValue) -> Option<Vec<u16>> {
use std::slice;
use winreg::enums::RegType;

match val.vtype {
RegType::REG_SZ | RegType::REG_EXPAND_SZ => {
Expand Down Expand Up @@ -364,7 +409,6 @@ pub fn self_replace() -> Result<utils::ExitCode> {
pub fn delete_rustup_and_cargo_home() -> Result<()> {
use std::io;
use std::mem;
use std::os::windows::ffi::OsStrExt;
use std::ptr;
use std::thread;
use std::time::Duration;
Expand Down