Skip to content

Commit ed93e7f

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

File tree

3 files changed

+68
-70
lines changed

3 files changed

+68
-70
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: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@
3232
3333
use std::borrow::Cow;
3434
use std::env::{self, consts::EXE_SUFFIX};
35-
use std::fmt;
36-
use std::fs;
35+
#[cfg(not(windows))]
36+
use std::io;
3737
use std::io::Write;
3838
use std::path::{Component, MAIN_SEPARATOR, Path, PathBuf};
3939
use std::process::Command;
4040
use std::str::FromStr;
41+
use std::{fmt, fs};
4142

4243
use anyhow::{Context, Result, anyhow};
4344
use cfg_if::cfg_if;
@@ -1071,6 +1072,67 @@ pub(crate) fn uninstall(no_prompt: bool, process: &Process) -> Result<utils::Exi
10711072
Ok(utils::ExitCode(0))
10721073
}
10731074

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

1092-
use common::SelfUpdatePermission::*;
1154+
use SelfUpdatePermission::*;
10931155
let update_permitted = if cfg!(feature = "no-self-update") {
10941156
HardFail
10951157
} else {
1096-
common::self_update_permitted(true)?
1158+
self_update_permitted(true)?
10971159
};
10981160
match update_permitted {
10991161
HardFail => {

0 commit comments

Comments
 (0)