Skip to content

Commit 0ea9162

Browse files
rami3lFranciscoTGouveia
authored andcommitted
feat(downloads): refactor for allowing installations to happen on a separate thread
Co-authored-by: FranciscoTGouveia <[email protected]>
1 parent 5127c16 commit 0ea9162

File tree

16 files changed

+606
-360
lines changed

16 files changed

+606
-360
lines changed

src/cli/common.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
//! Just a dumping ground for cli stuff
22
3-
use std::cell::RefCell;
43
use std::fmt::Display;
54
use std::fs;
65
#[cfg(not(windows))]
76
use std::io::ErrorKind;
87
use std::io::{BufRead, Write};
98
use std::path::{Path, PathBuf};
10-
use std::sync::{Arc, LazyLock, Mutex};
9+
use std::sync::{Arc, LazyLock, Mutex, RwLock};
1110
use std::{cmp, env};
1211

1312
use anyhow::{Context, Result, anyhow};
@@ -126,14 +125,14 @@ pub(crate) fn read_line(process: &Process) -> Result<String> {
126125

127126
pub(super) struct Notifier {
128127
tracker: Mutex<DownloadTracker>,
129-
ram_notice_shown: RefCell<bool>,
128+
ram_notice_shown: RwLock<bool>,
130129
}
131130

132131
impl Notifier {
133132
pub(super) fn new(quiet: bool, process: &Process) -> Self {
134133
Self {
135134
tracker: Mutex::new(DownloadTracker::new_with_display_progress(!quiet, process)),
136-
ram_notice_shown: RefCell::new(false),
135+
ram_notice_shown: RwLock::new(false),
137136
}
138137
}
139138

@@ -146,10 +145,10 @@ impl Notifier {
146145
util_notifications::Notification::SetDefaultBufferSize(_),
147146
)) = &n
148147
{
149-
if *self.ram_notice_shown.borrow() {
148+
if *self.ram_notice_shown.read().unwrap() {
150149
return;
151150
} else {
152-
*self.ram_notice_shown.borrow_mut() = true;
151+
*self.ram_notice_shown.write().unwrap() = true;
153152
}
154153
};
155154
let level = n.level();

src/config.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,15 @@ pub(crate) struct Cfg<'a> {
233233
pub toolchain_override: Option<ResolvableToolchainName>,
234234
pub env_override: Option<LocalToolchainName>,
235235
pub dist_root_url: String,
236-
pub notify_handler: Arc<dyn Fn(Notification<'_>)>,
236+
pub notify_handler: Arc<NotifyHandler>,
237237
pub current_dir: PathBuf,
238238
pub process: &'a Process,
239239
}
240240

241241
impl<'a> Cfg<'a> {
242242
pub(crate) fn from_env(
243243
current_dir: PathBuf,
244-
notify_handler: Arc<dyn Fn(Notification<'_>)>,
244+
notify_handler: Arc<NotifyHandler>,
245245
process: &'a Process,
246246
) -> Result<Self> {
247247
// Set up the rustup home directory
@@ -295,7 +295,7 @@ impl<'a> Cfg<'a> {
295295
let tmp_cx = temp::Context::new(
296296
rustup_dir.join("tmp"),
297297
dist_root_server.as_str(),
298-
Box::new(move |n| (notify_clone)(n.into())),
298+
Arc::new(move |n| (notify_clone)(n.into())),
299299
);
300300
let dist_root = dist_root_server + "/dist";
301301

@@ -330,13 +330,13 @@ impl<'a> Cfg<'a> {
330330
/// construct a download configuration
331331
pub(crate) fn download_cfg(
332332
&'a self,
333-
notify_handler: &'a dyn Fn(crate::dist::Notification<'_>),
333+
notify_handler: Arc<dist::notifications::NotifyHandler>,
334334
) -> DownloadCfg<'a> {
335335
DownloadCfg {
336336
dist_root: &self.dist_root_url,
337-
tmp_cx: &self.tmp_cx,
337+
tmp_cx: Arc::new(self.tmp_cx.clone()),
338338
download_dir: &self.download_dir,
339-
notify_handler,
339+
notify_handler: Arc::clone(&notify_handler),
340340
process: self.process,
341341
}
342342
}

src/dist/component/components.rs

Lines changed: 14 additions & 12 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,18 +255,20 @@ 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);
266262
let temp = tx.temp().new_file()?;
267263
utils::filter_file("components", &abs_path, &temp, |l| l != self.name)?;
268264
tx.modify_file(path)?;
269-
utils::rename("components", &temp, &abs_path, tx.notify_handler(), process)?;
265+
utils::rename(
266+
"components",
267+
&temp,
268+
&abs_path,
269+
&*tx.notify_handler(),
270+
process,
271+
)?;
270272

271273
// TODO: If this is the last component remove the components file
272274
// and the version file.

src/dist/component/package.rs

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::fmt;
77
use std::io::{self, ErrorKind as IOErrorKind, Read};
88
use std::mem;
99
use std::path::{Path, PathBuf};
10+
use std::sync::Arc;
1011

1112
use anyhow::{Context, Result, anyhow, bail};
1213
use tar::EntryType;
@@ -26,13 +27,13 @@ pub(crate) const VERSION_FILE: &str = "rust-installer-version";
2627

2728
pub trait Package: fmt::Debug {
2829
fn contains(&self, component: &str, short_name: Option<&str>) -> bool;
29-
fn install<'a>(
30+
fn install(
3031
&self,
3132
target: &Components,
3233
component: &str,
3334
short_name: Option<&str>,
34-
tx: Transaction<'a>,
35-
) -> Result<Transaction<'a>>;
35+
tx: Transaction,
36+
) -> Result<Transaction>;
3637
fn components(&self) -> Vec<String>;
3738
}
3839

@@ -79,13 +80,13 @@ impl Package for DirectoryPackage {
7980
false
8081
}
8182
}
82-
fn install<'a>(
83+
fn install(
8384
&self,
8485
target: &Components,
8586
name: &str,
8687
short_name: Option<&str>,
87-
tx: Transaction<'a>,
88-
) -> Result<Transaction<'a>> {
88+
tx: Transaction,
89+
) -> Result<Transaction> {
8990
let actual_name = if self.components.contains(name) {
9091
name
9192
} else if let Some(n) = short_name {
@@ -137,13 +138,13 @@ impl Package for DirectoryPackage {
137138

138139
#[derive(Debug)]
139140
#[allow(dead_code)] // temp::Dir is held for drop.
140-
pub(crate) struct TarPackage<'a>(DirectoryPackage, temp::Dir<'a>);
141+
pub(crate) struct TarPackage(DirectoryPackage, temp::Dir);
141142

142-
impl<'a> TarPackage<'a> {
143+
impl TarPackage {
143144
pub(crate) fn new<R: Read>(
144145
stream: R,
145-
tmp_cx: &'a temp::Context,
146-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
146+
tmp_cx: Arc<temp::Context>,
147+
notify_handler: Option<&dyn Fn(Notification<'_>)>,
147148
process: &Process,
148149
) -> Result<Self> {
149150
let temp_dir = tmp_cx.new_directory()?;
@@ -532,17 +533,17 @@ fn unpack_without_first_dir<R: Read>(
532533
Ok(())
533534
}
534535

535-
impl Package for TarPackage<'_> {
536+
impl Package for TarPackage {
536537
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
537538
self.0.contains(component, short_name)
538539
}
539-
fn install<'b>(
540+
fn install(
540541
&self,
541542
target: &Components,
542543
component: &str,
543544
short_name: Option<&str>,
544-
tx: Transaction<'b>,
545-
) -> Result<Transaction<'b>> {
545+
tx: Transaction,
546+
) -> Result<Transaction> {
546547
self.0.install(target, component, short_name, tx)
547548
}
548549
fn components(&self) -> Vec<String> {
@@ -551,36 +552,36 @@ impl Package for TarPackage<'_> {
551552
}
552553

553554
#[derive(Debug)]
554-
pub(crate) struct TarGzPackage<'a>(TarPackage<'a>);
555+
pub(crate) struct TarGzPackage(TarPackage);
555556

556-
impl<'a> TarGzPackage<'a> {
557+
impl TarGzPackage {
557558
pub(crate) fn new<R: Read>(
558559
stream: R,
559-
tmp_cx: &'a temp::Context,
560-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
560+
tmp_cx: Arc<temp::Context>,
561+
notify_handler: Option<&dyn Fn(Notification<'_>)>,
561562
process: &Process,
562563
) -> Result<Self> {
563564
let stream = flate2::read::GzDecoder::new(stream);
564565
Ok(TarGzPackage(TarPackage::new(
565566
stream,
566-
tmp_cx,
567+
Arc::clone(&tmp_cx),
567568
notify_handler,
568569
process,
569570
)?))
570571
}
571572
}
572573

573-
impl Package for TarGzPackage<'_> {
574+
impl Package for TarGzPackage {
574575
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
575576
self.0.contains(component, short_name)
576577
}
577-
fn install<'b>(
578+
fn install(
578579
&self,
579580
target: &Components,
580581
component: &str,
581582
short_name: Option<&str>,
582-
tx: Transaction<'b>,
583-
) -> Result<Transaction<'b>> {
583+
tx: Transaction,
584+
) -> Result<Transaction> {
584585
self.0.install(target, component, short_name, tx)
585586
}
586587
fn components(&self) -> Vec<String> {
@@ -589,36 +590,36 @@ impl Package for TarGzPackage<'_> {
589590
}
590591

591592
#[derive(Debug)]
592-
pub(crate) struct TarXzPackage<'a>(TarPackage<'a>);
593+
pub(crate) struct TarXzPackage(TarPackage);
593594

594-
impl<'a> TarXzPackage<'a> {
595+
impl TarXzPackage {
595596
pub(crate) fn new<R: Read>(
596597
stream: R,
597-
tmp_cx: &'a temp::Context,
598-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
598+
tmp_cx: Arc<temp::Context>,
599+
notify_handler: Option<&dyn Fn(Notification<'_>)>,
599600
process: &Process,
600601
) -> Result<Self> {
601602
let stream = xz2::read::XzDecoder::new(stream);
602603
Ok(TarXzPackage(TarPackage::new(
603604
stream,
604-
tmp_cx,
605+
Arc::clone(&tmp_cx),
605606
notify_handler,
606607
process,
607608
)?))
608609
}
609610
}
610611

611-
impl Package for TarXzPackage<'_> {
612+
impl Package for TarXzPackage {
612613
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
613614
self.0.contains(component, short_name)
614615
}
615-
fn install<'b>(
616+
fn install(
616617
&self,
617618
target: &Components,
618619
component: &str,
619620
short_name: Option<&str>,
620-
tx: Transaction<'b>,
621-
) -> Result<Transaction<'b>> {
621+
tx: Transaction,
622+
) -> Result<Transaction> {
622623
self.0.install(target, component, short_name, tx)
623624
}
624625
fn components(&self) -> Vec<String> {
@@ -627,36 +628,36 @@ impl Package for TarXzPackage<'_> {
627628
}
628629

629630
#[derive(Debug)]
630-
pub(crate) struct TarZStdPackage<'a>(TarPackage<'a>);
631+
pub(crate) struct TarZStdPackage(TarPackage);
631632

632-
impl<'a> TarZStdPackage<'a> {
633+
impl TarZStdPackage {
633634
pub(crate) fn new<R: Read>(
634635
stream: R,
635-
tmp_cx: &'a temp::Context,
636-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
636+
tmp_cx: Arc<temp::Context>,
637+
notify_handler: Option<&dyn Fn(Notification<'_>)>,
637638
process: &Process,
638639
) -> Result<Self> {
639640
let stream = zstd::stream::read::Decoder::new(stream)?;
640641
Ok(TarZStdPackage(TarPackage::new(
641642
stream,
642-
tmp_cx,
643+
Arc::clone(&tmp_cx),
643644
notify_handler,
644645
process,
645646
)?))
646647
}
647648
}
648649

649-
impl Package for TarZStdPackage<'_> {
650+
impl Package for TarZStdPackage {
650651
fn contains(&self, component: &str, short_name: Option<&str>) -> bool {
651652
self.0.contains(component, short_name)
652653
}
653-
fn install<'b>(
654+
fn install(
654655
&self,
655656
target: &Components,
656657
component: &str,
657658
short_name: Option<&str>,
658-
tx: Transaction<'b>,
659-
) -> Result<Transaction<'b>> {
659+
tx: Transaction,
660+
) -> Result<Transaction> {
660661
self.0.install(target, component, short_name, tx)
661662
}
662663
fn components(&self) -> Vec<String> {

0 commit comments

Comments
 (0)