Skip to content

Commit 6775681

Browse files
committed
cli: move more self update logic into self_update module
1 parent 4d5351d commit 6775681

File tree

3 files changed

+66
-69
lines changed

3 files changed

+66
-69
lines changed

src/cli/common.rs

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
use std::cell::RefCell;
44
use std::fmt::Display;
55
use std::fs;
6-
#[cfg(not(windows))]
7-
use std::io::ErrorKind;
86
use std::io::{BufRead, Write};
97
use std::path::{Path, PathBuf};
108
use std::sync::{Arc, LazyLock, Mutex};
@@ -15,7 +13,6 @@ use git_testament::{git_testament, render_testament};
1513
use tracing::{debug, error, info, trace, warn};
1614
use tracing_subscriber::{EnvFilter, Registry, reload::Handle};
1715

18-
use super::self_update;
1916
use crate::{
2017
cli::download_tracker::DownloadTracker,
2118
config::Cfg,
@@ -303,67 +300,6 @@ pub(crate) async fn update_all_channels(
303300
Ok(exit_code)
304301
}
305302

306-
#[derive(Clone, Copy, Debug)]
307-
pub(crate) enum SelfUpdatePermission {
308-
HardFail,
309-
#[cfg(not(windows))]
310-
Skip,
311-
Permit,
312-
}
313-
314-
#[cfg(windows)]
315-
pub(crate) fn self_update_permitted(_explicit: bool) -> Result<SelfUpdatePermission> {
316-
Ok(SelfUpdatePermission::Permit)
317-
}
318-
319-
#[cfg(not(windows))]
320-
pub(crate) fn self_update_permitted(explicit: bool) -> Result<SelfUpdatePermission> {
321-
// Detect if rustup is not meant to self-update
322-
let current_exe = env::current_exe()?;
323-
let current_exe_dir = current_exe.parent().expect("Rustup isn't in a directory‽");
324-
if let Err(e) = tempfile::Builder::new()
325-
.prefix("updtest")
326-
.tempdir_in(current_exe_dir)
327-
{
328-
match e.kind() {
329-
ErrorKind::PermissionDenied => {
330-
trace!("Skipping self-update because we cannot write to the rustup dir");
331-
if explicit {
332-
return Ok(SelfUpdatePermission::HardFail);
333-
} else {
334-
return Ok(SelfUpdatePermission::Skip);
335-
}
336-
}
337-
_ => return Err(e.into()),
338-
}
339-
}
340-
Ok(SelfUpdatePermission::Permit)
341-
}
342-
343-
/// Performs all of a self-update: check policy, download, apply and exit.
344-
pub(crate) async fn self_update(process: &Process) -> Result<utils::ExitCode> {
345-
match self_update_permitted(false)? {
346-
SelfUpdatePermission::HardFail => {
347-
error!("Unable to self-update. STOP");
348-
return Ok(utils::ExitCode(1));
349-
}
350-
#[cfg(not(windows))]
351-
SelfUpdatePermission::Skip => return Ok(utils::ExitCode(0)),
352-
SelfUpdatePermission::Permit => {}
353-
}
354-
355-
let setup_path = self_update::prepare_update(process).await?;
356-
357-
if let Some(setup_path) = &setup_path {
358-
return self_update::run_update(setup_path);
359-
} else {
360-
// Try again in case we emitted "tool `{}` is already installed" last time.
361-
self_update::install_proxies(process)?;
362-
}
363-
364-
Ok(utils::ExitCode(0))
365-
}
366-
367303
/// Print a list of items (targets or components) to stdout.
368304
///
369305
/// `items` represents the list of items, with the name and a boolean

src/cli/rustup_mode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ async fn update(
996996
}
997997
}
998998
if self_update {
999-
exit_code &= common::self_update(cfg.process).await?;
999+
exit_code &= self_update::self_update(cfg.process).await?;
10001000
}
10011001
} else if ensure_active_toolchain {
10021002
let (toolchain, reason) = cfg.ensure_active_toolchain(force_non_host, true).await?;
@@ -1005,7 +1005,7 @@ async fn update(
10051005
} else {
10061006
exit_code &= common::update_all_channels(cfg, opts.force).await?;
10071007
if self_update {
1008-
exit_code &= common::self_update(cfg.process).await?;
1008+
exit_code &= self_update::self_update(cfg.process).await?;
10091009
}
10101010

10111011
info!("cleaning up downloads & tmp directories");

src/cli/self_update.rs

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use std::borrow::Cow;
3434
use std::env::{self, consts::EXE_SUFFIX};
3535
use std::fmt;
3636
use std::fs;
37-
use std::io::Write;
37+
use std::io::{self, Write};
3838
use std::path::{Component, MAIN_SEPARATOR, Path, PathBuf};
3939
use std::process::Command;
4040
use std::str::FromStr;
@@ -1071,6 +1071,67 @@ pub(crate) fn uninstall(no_prompt: bool, process: &Process) -> Result<utils::Exi
10711071
Ok(utils::ExitCode(0))
10721072
}
10731073

1074+
#[derive(Clone, Copy, Debug)]
1075+
pub(crate) enum SelfUpdatePermission {
1076+
HardFail,
1077+
#[cfg(not(windows))]
1078+
Skip,
1079+
Permit,
1080+
}
1081+
1082+
#[cfg(windows)]
1083+
pub(crate) fn self_update_permitted(_explicit: bool) -> Result<SelfUpdatePermission> {
1084+
Ok(SelfUpdatePermission::Permit)
1085+
}
1086+
1087+
#[cfg(not(windows))]
1088+
pub(crate) fn self_update_permitted(explicit: bool) -> Result<SelfUpdatePermission> {
1089+
// Detect if rustup is not meant to self-update
1090+
let current_exe = env::current_exe()?;
1091+
let current_exe_dir = current_exe.parent().expect("Rustup isn't in a directory‽");
1092+
if let Err(e) = tempfile::Builder::new()
1093+
.prefix("updtest")
1094+
.tempdir_in(current_exe_dir)
1095+
{
1096+
match e.kind() {
1097+
io::ErrorKind::PermissionDenied => {
1098+
trace!("Skipping self-update because we cannot write to the rustup dir");
1099+
if explicit {
1100+
return Ok(SelfUpdatePermission::HardFail);
1101+
} else {
1102+
return Ok(SelfUpdatePermission::Skip);
1103+
}
1104+
}
1105+
_ => return Err(e.into()),
1106+
}
1107+
}
1108+
Ok(SelfUpdatePermission::Permit)
1109+
}
1110+
1111+
/// Performs all of a self-update: check policy, download, apply and exit.
1112+
pub(crate) async fn self_update(process: &Process) -> Result<utils::ExitCode> {
1113+
match self_update_permitted(false)? {
1114+
SelfUpdatePermission::HardFail => {
1115+
error!("Unable to self-update. STOP");
1116+
return Ok(utils::ExitCode(1));
1117+
}
1118+
#[cfg(not(windows))]
1119+
SelfUpdatePermission::Skip => return Ok(utils::ExitCode(0)),
1120+
SelfUpdatePermission::Permit => {}
1121+
}
1122+
1123+
let setup_path = prepare_update(process).await?;
1124+
1125+
if let Some(setup_path) = &setup_path {
1126+
return run_update(setup_path);
1127+
} else {
1128+
// Try again in case we emitted "tool `{}` is already installed" last time.
1129+
install_proxies(process)?;
1130+
}
1131+
1132+
Ok(utils::ExitCode(0))
1133+
}
1134+
10741135
/// Self update downloads rustup-init to `CARGO_HOME`/bin/rustup-init
10751136
/// and runs it.
10761137
///
@@ -1089,11 +1150,11 @@ pub(crate) fn uninstall(no_prompt: bool, process: &Process) -> Result<utils::Exi
10891150
pub(crate) async fn update(cfg: &Cfg<'_>) -> Result<utils::ExitCode> {
10901151
common::warn_if_host_is_emulated(cfg.process);
10911152

1092-
use common::SelfUpdatePermission::*;
1153+
use SelfUpdatePermission::*;
10931154
let update_permitted = if cfg!(feature = "no-self-update") {
10941155
HardFail
10951156
} else {
1096-
common::self_update_permitted(true)?
1157+
self_update_permitted(true)?
10971158
};
10981159
match update_permitted {
10991160
HardFail => {

0 commit comments

Comments
 (0)