Skip to content

Commit 3a86fbb

Browse files
committed
WIP
1 parent 5e6a3ea commit 3a86fbb

File tree

8 files changed

+38
-32
lines changed

8 files changed

+38
-32
lines changed

src/cli/self_update.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1186,12 +1186,7 @@ pub(crate) fn prepare_update() -> Result<Option<PathBuf>> {
11861186

11871187
// Download new version
11881188
info!("downloading self-update");
1189-
utils::run_future(utils::download_file(
1190-
&download_url,
1191-
&setup_path,
1192-
None,
1193-
&|_| (),
1194-
))?;
1189+
utils::run_future(utils::download_file(&download_url, &setup_path, None, None))?;
11951190

11961191
// Mark as executable
11971192
utils::make_executable(&setup_path)?;
@@ -1216,7 +1211,7 @@ pub(crate) fn get_available_rustup_version() -> Result<String> {
12161211
&release_file_url,
12171212
&release_file,
12181213
None,
1219-
&|_| (),
1214+
None,
12201215
))?;
12211216
let release_toml_str = utils::read_file("rustup release", &release_file)?;
12221217
let release_toml: toml::Value =

src/cli/self_update/windows.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::io::Write;
55
use std::os::windows::ffi::{OsStrExt, OsStringExt};
66
use std::path::Path;
77
use std::process::Command;
8+
use std::sync::Arc;
89

910
use anyhow::{anyhow, Context, Result};
1011

@@ -184,15 +185,15 @@ pub(crate) async fn try_install_msvc(opts: &InstallOpts<'_>) -> Result<ContinueI
184185
download_tracker.lock().unwrap().download_finished();
185186

186187
info!("downloading Visual Studio installer");
187-
utils::download_file(&visual_studio_url, &visual_studio, None, &move |n| {
188+
let notify = Some(Arc::new(&move |n| {
188189
download_tracker
189190
.lock()
190191
.unwrap()
191192
.handle_notification(&crate::Notification::Install(
192193
crate::dist::Notification::Utils(n),
193194
));
194-
})
195-
.await?;
195+
}));
196+
utils::download_file(&visual_studio_url, &visual_studio, None, notify).await?;
196197

197198
// Run the installer. Arguments are documented at:
198199
// https://docs.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual-studio

src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ impl Cfg {
281281
/// construct a download configuration
282282
pub(crate) fn download_cfg<'a>(
283283
&'a self,
284-
notify_handler: &'a dyn Fn(crate::dist::Notification<'_>),
284+
notify_handler: Option<Arc<dyn Fn(crate::dist::Notification<'_>)>>,
285285
) -> DownloadCfg<'a> {
286286
DownloadCfg {
287287
dist_root: &self.dist_root_url,

src/dist/dist.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -698,9 +698,11 @@ pub(crate) fn update_from_dist(
698698
);
699699

700700
// Don't leave behind an empty / broken installation directory
701-
if res.is_err() && fresh_install {
702-
// FIXME Ignoring cascading errors
703-
let _ = utils::remove_dir("toolchain", prefix.path(), download.notify_handler);
701+
if let Some(notify_handler) = download.notify_handler {
702+
if res.is_err() && fresh_install {
703+
// FIXME Ignoring cascading errors
704+
let _ = utils::remove_dir("toolchain", prefix.path(), notify_handler.as_ref());
705+
}
704706
}
705707

706708
res

src/dist/download.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fs;
22
use std::ops;
33
use std::path::{Path, PathBuf};
4+
use std::sync::Arc;
45

56
use anyhow::{anyhow, Context, Result};
67
use sha2::{Digest, Sha256};
@@ -13,12 +14,12 @@ use crate::utils::utils;
1314

1415
const UPDATE_HASH_LEN: usize = 20;
1516

16-
#[derive(Copy, Clone)]
17+
#[derive(Clone)]
1718
pub struct DownloadCfg<'a> {
1819
pub dist_root: &'a str,
1920
pub temp_cfg: &'a temp::Cfg,
2021
pub download_dir: &'a PathBuf,
21-
pub notify_handler: &'a dyn Fn(Notification<'_>),
22+
pub notify_handler: Option<Arc<dyn Fn(Notification<'_>)>>,
2223
}
2324

2425
pub(crate) struct File {
@@ -42,7 +43,7 @@ impl<'a> DownloadCfg<'a> {
4243
utils::ensure_dir_exists(
4344
"Download Directory",
4445
self.download_dir,
45-
&self.notify_handler,
46+
self.notify_handler.as_ref(),
4647
)?;
4748
let target_file = self.download_dir.join(Path::new(hash));
4849

@@ -76,7 +77,7 @@ impl<'a> DownloadCfg<'a> {
7677
&partial_file_path,
7778
Some(&mut hasher),
7879
true,
79-
&|n| (self.notify_handler)(n.into()),
80+
Some(Arc::new(|n| (self.notify_handler)(n.into()))),
8081
)
8182
.await
8283
{
@@ -170,7 +171,7 @@ impl<'a> DownloadCfg<'a> {
170171
let file = self.temp_cfg.new_file_with_ext("", ext)?;
171172

172173
let mut hasher = Sha256::new();
173-
utils::run_future(utils::download_file(&url, &file, Some(&mut hasher), &|n| {
174+
utils::run_future(utils::download_file(&url, &file, Some(&mut hasher), |n| {
174175
(self.notify_handler)(n.into())
175176
}))?;
176177
let actual_hash = format!("{:x}", hasher.finalize());

src/dist/manifestation/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ fn update_from_dist(
447447
&manifest_url,
448448
&manifest_file,
449449
None,
450-
&|_| {},
450+
None,
451451
))?;
452452
let manifest_str = utils::read_file("manifest", &manifest_file)?;
453453
let manifest = Manifest::parse(&manifest_str)?;

src/toolchain/distributable.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{
22
convert::Infallible, env::consts::EXE_SUFFIX, ffi::OsStr, fs, path::Path, process::Command,
3+
sync::Arc,
34
};
45

56
use anyhow::{anyhow, Context};
@@ -490,9 +491,10 @@ impl<'a> DistributableToolchain<'a> {
490491
remove_components: vec![component],
491492
};
492493

493-
let notify_handler =
494-
&|n: crate::dist::Notification<'_>| (self.cfg.notify_handler)(n.into());
495-
let download_cfg = self.cfg.download_cfg(&notify_handler);
494+
let notify_handler = Some(Arc::new(&|n: crate::dist::Notification<'_>| {
495+
(self.cfg.notify_handler)(n.into())
496+
}));
497+
let download_cfg = self.cfg.download_cfg(notify_handler);
496498

497499
manifestation.update(
498500
&manifest,

src/utils/utils.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fs::{self, File};
33
use std::future::Future;
44
use std::io::{self, BufReader, Write};
55
use std::path::{Path, PathBuf};
6+
use std::sync::Arc;
67

78
use anyhow::{anyhow, bail, Context, Result};
89
use home::env as home;
@@ -138,17 +139,17 @@ pub async fn download_file(
138139
url: &Url,
139140
path: &Path,
140141
hasher: Option<&mut Sha256>,
141-
notify_handler: &dyn Fn(Notification<'_>),
142+
notify_handler: Option<Arc<&dyn Fn(Notification<'_>)>>,
142143
) -> Result<()> {
143-
download_file_with_resume(url, path, hasher, false, &notify_handler).await
144+
download_file_with_resume(url, path, hasher, false, notify_handler).await
144145
}
145146

146147
pub(crate) async fn download_file_with_resume(
147148
url: &Url,
148149
path: &Path,
149150
hasher: Option<&mut Sha256>,
150151
resume_from_partial: bool,
151-
notify_handler: &dyn Fn(Notification<'_>),
152+
notify_handler: Option<Arc<&dyn Fn(Notification<'_>)>>,
152153
) -> Result<()> {
153154
use download::DownloadError as DEK;
154155
match download_file_(url, path, hasher, resume_from_partial, notify_handler).await {
@@ -183,14 +184,16 @@ async fn download_file_(
183184
path: &Path,
184185
hasher: Option<&mut Sha256>,
185186
resume_from_partial: bool,
186-
notify_handler: &dyn Fn(Notification<'_>),
187+
notify_handler: Option<Arc<&dyn Fn(Notification<'_>)>>,
187188
) -> Result<()> {
188189
use download::download_to_path_with_backend;
189190
use download::{Backend, Event, TlsBackend};
190191
use sha2::Digest;
191192
use std::cell::RefCell;
192193

193-
notify_handler(Notification::DownloadingFile(url, path));
194+
notify_handler
195+
.as_ref()
196+
.map(|notify_handler| notify_handler(Notification::DownloadingFile(url, path)));
194197

195198
let hasher = RefCell::new(hasher);
196199

@@ -203,7 +206,7 @@ async fn download_file_(
203206
}
204207
}
205208

206-
match msg {
209+
notify_handler.as_ref().map(|notify_handler| match msg {
207210
Event::DownloadContentLengthReceived(len) => {
208211
notify_handler(Notification::DownloadContentLengthReceived(len));
209212
}
@@ -213,7 +216,7 @@ async fn download_file_(
213216
Event::ResumingPartialDownload => {
214217
notify_handler(Notification::ResumingPartialDownload);
215218
}
216-
}
219+
});
217220

218221
Ok(())
219222
};
@@ -240,12 +243,14 @@ async fn download_file_(
240243
};
241244
(Backend::Reqwest(tls_backend), Notification::UsingReqwest)
242245
};
243-
notify_handler(notification);
246+
notify_handler
247+
.as_ref()
248+
.map(|notify_handler| notify_handler(notification));
244249
let res =
245250
download_to_path_with_backend(backend, url, path, resume_from_partial, Some(callback))
246251
.await;
247252

248-
notify_handler(Notification::DownloadFinished);
253+
notify_handler.map(|notify_handler| notify_handler(Notification::DownloadFinished));
249254

250255
res
251256
}

0 commit comments

Comments
 (0)