Skip to content

Commit 180008e

Browse files
refactor(downloads): remove some lifetime parameters to allow multi-thread installations
Co-authored-by: rami3l <rami3l@outlook.com>
1 parent ec791b7 commit 180008e

File tree

14 files changed

+305
-217
lines changed

14 files changed

+305
-217
lines changed

src/cli/self_update.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ pub(crate) fn self_update_permitted(explicit: bool) -> Result<SelfUpdatePermissi
11061106
}
11071107

11081108
/// Performs all of a self-update: check policy, download, apply and exit.
1109-
pub(crate) async fn self_update(dl_cfg: &DownloadCfg<'_>) -> Result<utils::ExitCode> {
1109+
pub(crate) async fn self_update(dl_cfg: &DownloadCfg) -> Result<utils::ExitCode> {
11101110
match self_update_permitted(false)? {
11111111
SelfUpdatePermission::HardFail => {
11121112
error!("Unable to self-update. STOP");
@@ -1123,7 +1123,7 @@ pub(crate) async fn self_update(dl_cfg: &DownloadCfg<'_>) -> Result<utils::ExitC
11231123
return run_update(setup_path);
11241124
} else {
11251125
// Try again in case we emitted "tool `{}` is already installed" last time.
1126-
install_proxies(dl_cfg.process)?;
1126+
install_proxies(&dl_cfg.process)?;
11271127
}
11281128

11291129
Ok(utils::ExitCode(0))
@@ -1221,7 +1221,7 @@ fn parse_new_rustup_version(version: String) -> String {
12211221
String::from(matched_version)
12221222
}
12231223

1224-
pub(crate) async fn prepare_update(dl_cfg: &DownloadCfg<'_>) -> Result<Option<PathBuf>> {
1224+
pub(crate) async fn prepare_update(dl_cfg: &DownloadCfg) -> Result<Option<PathBuf>> {
12251225
let cargo_home = dl_cfg.process.cargo_home()?;
12261226
let rustup_path = cargo_home.join(format!("bin{MAIN_SEPARATOR}rustup{EXE_SUFFIX}"));
12271227
let setup_path = cargo_home.join(format!("bin{MAIN_SEPARATOR}rustup-init{EXE_SUFFIX}"));
@@ -1244,10 +1244,10 @@ pub(crate) async fn prepare_update(dl_cfg: &DownloadCfg<'_>) -> Result<Option<Pa
12441244
// If someone really wants to use another version, they still can enforce
12451245
// that using the environment variable RUSTUP_OVERRIDE_HOST_TRIPLE.
12461246
#[cfg(windows)]
1247-
let triple = TargetTriple::from_host(dl_cfg.process).unwrap_or(triple);
1247+
let triple = TargetTriple::from_host(&dl_cfg.process).unwrap_or(triple);
12481248

12491249
// Get update root.
1250-
let update_root = update_root(dl_cfg.process);
1250+
let update_root = update_root(&dl_cfg.process);
12511251

12521252
// Get current version
12531253
let current_version = env!("CARGO_PKG_VERSION");
@@ -1275,16 +1275,16 @@ pub(crate) async fn prepare_update(dl_cfg: &DownloadCfg<'_>) -> Result<Option<Pa
12751275

12761276
// Download new version
12771277
info!("downloading self-update (new version: {available_version})");
1278-
download_file(&download_url, &setup_path, None, None, dl_cfg.process).await?;
1278+
download_file(&download_url, &setup_path, None, None, &dl_cfg.process).await?;
12791279

12801280
// Mark as executable
12811281
utils::make_executable(&setup_path)?;
12821282

12831283
Ok(Some(setup_path))
12841284
}
12851285

1286-
async fn get_available_rustup_version(dl_cfg: &DownloadCfg<'_>) -> Result<String> {
1287-
let update_root = update_root(dl_cfg.process);
1286+
async fn get_available_rustup_version(dl_cfg: &DownloadCfg) -> Result<String> {
1287+
let update_root = update_root(&dl_cfg.process);
12881288
let tempdir = tempfile::Builder::new()
12891289
.prefix("rustup-update")
12901290
.tempdir()
@@ -1294,7 +1294,14 @@ async fn get_available_rustup_version(dl_cfg: &DownloadCfg<'_>) -> Result<String
12941294
let release_file_url = format!("{update_root}/release-stable.toml");
12951295
let release_file_url = utils::parse_url(&release_file_url)?;
12961296
let release_file = tempdir.path().join("release-stable.toml");
1297-
download_file(&release_file_url, &release_file, None, None, dl_cfg.process).await?;
1297+
download_file(
1298+
&release_file_url,
1299+
&release_file,
1300+
None,
1301+
None,
1302+
&dl_cfg.process,
1303+
)
1304+
.await?;
12981305
let release_toml_str = utils::read_file("rustup release", &release_file)?;
12991306
let release_toml = toml::from_str::<RustupManifest>(&release_toml_str)
13001307
.context("unable to parse rustup release file")?;
@@ -1342,7 +1349,7 @@ impl fmt::Display for SchemaVersion {
13421349
}
13431350

13441351
/// Returns whether an update was available
1345-
pub(crate) async fn check_rustup_update(dl_cfg: &DownloadCfg<'_>) -> Result<bool> {
1352+
pub(crate) async fn check_rustup_update(dl_cfg: &DownloadCfg) -> Result<bool> {
13461353
let t = dl_cfg.process.stdout();
13471354
let mut t = t.lock();
13481355
// Get current rustup version

src/cli/self_update/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ pub(crate) async fn try_install_msvc(
278278
&visual_studio,
279279
None,
280280
None,
281-
dl_cfg.process,
281+
&dl_cfg.process,
282282
)
283283
.await?;
284284

src/config.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::fmt::{self, Debug, Display};
22
use std::io;
33
use std::path::{Path, PathBuf};
44
use std::str::FromStr;
5+
use std::sync::Arc;
56

67
use anyhow::{Context, Result, anyhow, bail};
78
use serde::Deserialize;
@@ -238,13 +239,13 @@ pub(crate) struct Cfg<'a> {
238239
pub toolchains_dir: PathBuf,
239240
pub update_hash_dir: PathBuf,
240241
pub download_dir: PathBuf,
241-
pub tmp_cx: temp::Context,
242+
pub tmp_cx: Arc<temp::Context>,
242243
pub toolchain_override: Option<ResolvableToolchainName>,
243244
pub env_override: Option<LocalToolchainName>,
244245
pub dist_root_url: String,
245246
pub quiet: bool,
246247
pub current_dir: PathBuf,
247-
pub process: &'a Process,
248+
pub process: &'a Process, // TODO: this may need to be inside an Arc.
248249
}
249250

250251
impl<'a> Cfg<'a> {
@@ -299,7 +300,10 @@ impl<'a> Cfg<'a> {
299300
};
300301

301302
let dist_root_server = dist_root_server(process)?;
302-
let tmp_cx = temp::Context::new(rustup_dir.join("tmp"), dist_root_server.as_str());
303+
let tmp_cx = Arc::new(temp::Context::new(
304+
rustup_dir.join("tmp"),
305+
dist_root_server.as_str(),
306+
));
303307
let dist_root = dist_root_server + "/dist";
304308

305309
let cfg = Self {

src/dist/component/components.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Components {
5555
Ok(None)
5656
}
5757
}
58-
fn write_version(&self, tx: &mut Transaction<'_>) -> Result<()> {
58+
fn write_version(&self, tx: &mut Transaction) -> Result<()> {
5959
tx.modify_file(self.prefix.rel_manifest_file(VERSION_FILE))?;
6060
utils::write_file(
6161
VERSION_FILE,
@@ -79,7 +79,7 @@ impl Components {
7979
})
8080
.collect())
8181
}
82-
pub(crate) fn add<'a>(&self, name: &str, tx: Transaction<'a>) -> ComponentBuilder<'a> {
82+
pub(crate) fn add(&self, name: &str, tx: Transaction) -> ComponentBuilder {
8383
ComponentBuilder {
8484
components: self.clone(),
8585
name: name.to_owned(),
@@ -96,14 +96,14 @@ impl Components {
9696
}
9797
}
9898

99-
pub(crate) struct ComponentBuilder<'a> {
99+
pub(crate) struct ComponentBuilder {
100100
components: Components,
101101
name: String,
102102
parts: Vec<ComponentPart>,
103-
tx: Transaction<'a>,
103+
tx: Transaction,
104104
}
105105

106-
impl<'a> ComponentBuilder<'a> {
106+
impl ComponentBuilder {
107107
pub(crate) fn copy_file(&mut self, path: PathBuf, src: &Path) -> Result<()> {
108108
self.parts.push(ComponentPart {
109109
kind: ComponentPartKind::File,
@@ -132,7 +132,7 @@ impl<'a> ComponentBuilder<'a> {
132132
});
133133
self.tx.move_dir(&self.name, path, src)
134134
}
135-
pub(crate) fn finish(mut self) -> Result<Transaction<'a>> {
135+
pub(crate) fn finish(mut self) -> Result<Transaction> {
136136
// Write component manifest
137137
let path = self.components.rel_component_manifest(&self.name);
138138
let abs_path = self.components.prefix.abs_path(&path);
@@ -255,11 +255,7 @@ impl Component {
255255
}
256256
Ok(result)
257257
}
258-
pub fn uninstall<'a>(
259-
&self,
260-
mut tx: Transaction<'a>,
261-
process: &Process,
262-
) -> Result<Transaction<'a>> {
258+
pub fn uninstall(&self, mut tx: Transaction, process: &Process) -> Result<Transaction> {
263259
// Update components file
264260
let path = self.components.rel_components_file();
265261
let abs_path = self.components.prefix.abs_path(&path);

src/dist/component/package.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ pub(crate) const VERSION_FILE: &str = "rust-installer-version";
2828

2929
pub trait Package: fmt::Debug {
3030
fn contains(&self, component: &str, short_name: Option<&str>) -> bool;
31-
fn install<'a>(
31+
fn install(
3232
&self,
3333
target: &Components,
3434
component: &str,
3535
short_name: Option<&str>,
36-
tx: Transaction<'a>,
37-
) -> Result<Transaction<'a>>;
36+
tx: Transaction,
37+
) -> Result<Transaction>;
3838
fn components(&self) -> Vec<String>;
3939
}
4040

@@ -78,13 +78,13 @@ impl Package for DirectoryPackage {
7878
false
7979
}
8080
}
81-
fn install<'a>(
81+
fn install(
8282
&self,
8383
target: &Components,
8484
name: &str,
8585
short_name: Option<&str>,
86-
tx: Transaction<'a>,
87-
) -> Result<Transaction<'a>> {
86+
tx: Transaction,
87+
) -> Result<Transaction> {
8888
let actual_name = if self.components.contains(name) {
8989
name
9090
} else if let Some(n) = short_name {
@@ -139,7 +139,7 @@ impl Package for DirectoryPackage {
139139
pub(crate) struct TarPackage(DirectoryPackage, temp::Dir);
140140

141141
impl TarPackage {
142-
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg<'_>) -> Result<Self> {
142+
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg) -> Result<Self> {
143143
let temp_dir = dl_cfg.tmp_cx.new_directory()?;
144144
let mut archive = tar::Archive::new(stream);
145145
// The rust-installer packages unpack to a directory called
@@ -159,7 +159,7 @@ impl TarPackage {
159159
fn unpack_ram(
160160
io_chunk_size: usize,
161161
effective_max_ram: Option<usize>,
162-
dl_cfg: &DownloadCfg<'_>,
162+
dl_cfg: &DownloadCfg,
163163
) -> usize {
164164
const RAM_ALLOWANCE_FOR_RUSTUP_AND_BUFFERS: usize = 200 * 1024 * 1024;
165165
let minimum_ram = io_chunk_size * 2;
@@ -285,7 +285,7 @@ enum DirStatus {
285285
fn unpack_without_first_dir<R: Read>(
286286
archive: &mut tar::Archive<R>,
287287
path: &Path,
288-
dl_cfg: &DownloadCfg<'_>,
288+
dl_cfg: &DownloadCfg,
289289
) -> Result<()> {
290290
let entries = archive.entries()?;
291291
let effective_max_ram = match effective_limits::memory_limit() {
@@ -296,7 +296,7 @@ fn unpack_without_first_dir<R: Read>(
296296
}
297297
};
298298
let unpack_ram = unpack_ram(IO_CHUNK_SIZE, effective_max_ram, dl_cfg);
299-
let mut io_executor: Box<dyn Executor> = get_executor(unpack_ram, dl_cfg.process)?;
299+
let mut io_executor: Box<dyn Executor> = get_executor(unpack_ram, &dl_cfg.process)?;
300300

301301
let mut directories: HashMap<PathBuf, DirStatus> = HashMap::new();
302302
// Path is presumed to exist. Call it a precondition.
@@ -530,13 +530,13 @@ impl Package for TarPackage {
530530
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
531531
self.0.contains(component, short_name)
532532
}
533-
fn install<'b>(
533+
fn install(
534534
&self,
535535
target: &Components,
536536
component: &str,
537537
short_name: Option<&str>,
538-
tx: Transaction<'b>,
539-
) -> Result<Transaction<'b>> {
538+
tx: Transaction,
539+
) -> Result<Transaction> {
540540
self.0.install(target, component, short_name, tx)
541541
}
542542
fn components(&self) -> Vec<String> {
@@ -548,7 +548,7 @@ impl Package for TarPackage {
548548
pub(crate) struct TarGzPackage(TarPackage);
549549

550550
impl TarGzPackage {
551-
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg<'_>) -> Result<Self> {
551+
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg) -> Result<Self> {
552552
let stream = flate2::read::GzDecoder::new(stream);
553553
Ok(TarGzPackage(TarPackage::new(stream, dl_cfg)?))
554554
}
@@ -558,13 +558,13 @@ impl Package for TarGzPackage {
558558
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
559559
self.0.contains(component, short_name)
560560
}
561-
fn install<'b>(
561+
fn install(
562562
&self,
563563
target: &Components,
564564
component: &str,
565565
short_name: Option<&str>,
566-
tx: Transaction<'b>,
567-
) -> Result<Transaction<'b>> {
566+
tx: Transaction,
567+
) -> Result<Transaction> {
568568
self.0.install(target, component, short_name, tx)
569569
}
570570
fn components(&self) -> Vec<String> {
@@ -576,7 +576,7 @@ impl Package for TarGzPackage {
576576
pub(crate) struct TarXzPackage(TarPackage);
577577

578578
impl TarXzPackage {
579-
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg<'_>) -> Result<Self> {
579+
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg) -> Result<Self> {
580580
let stream = xz2::read::XzDecoder::new(stream);
581581
Ok(TarXzPackage(TarPackage::new(stream, dl_cfg)?))
582582
}
@@ -586,13 +586,13 @@ impl Package for TarXzPackage {
586586
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
587587
self.0.contains(component, short_name)
588588
}
589-
fn install<'b>(
589+
fn install(
590590
&self,
591591
target: &Components,
592592
component: &str,
593593
short_name: Option<&str>,
594-
tx: Transaction<'b>,
595-
) -> Result<Transaction<'b>> {
594+
tx: Transaction,
595+
) -> Result<Transaction> {
596596
self.0.install(target, component, short_name, tx)
597597
}
598598
fn components(&self) -> Vec<String> {
@@ -604,7 +604,7 @@ impl Package for TarXzPackage {
604604
pub(crate) struct TarZStdPackage(TarPackage);
605605

606606
impl TarZStdPackage {
607-
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg<'_>) -> Result<Self> {
607+
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg) -> Result<Self> {
608608
let stream = zstd::stream::read::Decoder::new(stream)?;
609609
Ok(TarZStdPackage(TarPackage::new(stream, dl_cfg)?))
610610
}
@@ -614,13 +614,13 @@ impl Package for TarZStdPackage {
614614
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
615615
self.0.contains(component, short_name)
616616
}
617-
fn install<'b>(
617+
fn install(
618618
&self,
619619
target: &Components,
620620
component: &str,
621621
short_name: Option<&str>,
622-
tx: Transaction<'b>,
623-
) -> Result<Transaction<'b>> {
622+
tx: Transaction,
623+
) -> Result<Transaction> {
624624
self.0.install(target, component, short_name, tx)
625625
}
626626
fn components(&self) -> Vec<String> {

0 commit comments

Comments
 (0)