Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rbtcollins committed Aug 15, 2023
1 parent 970557d commit 6c2f960
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ pub(crate) async fn prepare_update() -> Result<Option<PathBuf>> {

// Download new version
info!("downloading self-update");
utils::download_file(&download_url, &setup_path, None, &|_| ()).await?;
utils::download_file(&download_url, &setup_path, None, None).await?;

// Mark as executable
utils::make_executable(&setup_path)?;
Expand All @@ -1207,7 +1207,7 @@ async fn get_available_rustup_version() -> Result<String> {
let release_file_url = format!("{update_root}/release-stable.toml");
let release_file_url = utils::parse_url(&release_file_url)?;
let release_file = tempdir.path().join("release-stable.toml");
utils::download_file(&release_file_url, &release_file, None, &|_| ()).await?;
utils::download_file(&release_file_url, &release_file, None, None).await?;
let release_toml_str = utils::read_file("rustup release", &release_file)?;
let release_toml: toml::Value =
toml::from_str(&release_toml_str).context("unable to parse rustup release file")?;
Expand Down
7 changes: 4 additions & 3 deletions src/cli/self_update/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::io::Write;
use std::os::windows::ffi::{OsStrExt, OsStringExt};
use std::path::Path;
use std::process::Command;
use std::sync::Arc;

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

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

info!("downloading Visual Studio installer");
utils::download_file(&visual_studio_url, &visual_studio, None, &move |n| {
let notify = Some(Arc::new(&move |n| {
download_tracker
.lock()
.unwrap()
.handle_notification(&crate::Notification::Install(
crate::dist::Notification::Utils(n),
));
})
.await?;
}));
utils::download_file(&visual_studio_url, &visual_studio, None, notify).await?;

// Run the installer. Arguments are documented at:
// https://docs.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual-studio
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl Cfg {
/// construct a download configuration
pub(crate) fn download_cfg<'a>(
&'a self,
notify_handler: &'a dyn Fn(crate::dist::Notification<'_>),
notify_handler: Option<Arc<dyn Fn(crate::dist::Notification<'_>)>>,
) -> DownloadCfg<'a> {
DownloadCfg {
dist_root: &self.dist_root_url,
Expand Down
8 changes: 5 additions & 3 deletions src/dist/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,9 +699,11 @@ pub(crate) async fn update_from_dist(
.await;

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

res
Expand Down
11 changes: 6 additions & 5 deletions src/dist/download.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fs;
use std::ops;
use std::path::{Path, PathBuf};
use std::sync::Arc;

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

const UPDATE_HASH_LEN: usize = 20;

#[derive(Copy, Clone)]
#[derive(Clone)]
pub struct DownloadCfg<'a> {
pub dist_root: &'a str,
pub temp_cfg: &'a temp::Cfg,
pub download_dir: &'a PathBuf,
pub notify_handler: &'a dyn Fn(Notification<'_>),
pub notify_handler: Option<Arc<dyn Fn(Notification<'_>)>>,
}

pub(crate) struct File {
Expand All @@ -42,7 +43,7 @@ impl<'a> DownloadCfg<'a> {
utils::ensure_dir_exists(
"Download Directory",
self.download_dir,
&self.notify_handler,
self.notify_handler.as_ref(),
)?;
let target_file = self.download_dir.join(Path::new(hash));

Expand Down Expand Up @@ -76,7 +77,7 @@ impl<'a> DownloadCfg<'a> {
&partial_file_path,
Some(&mut hasher),
true,
&|n| (self.notify_handler)(n.into()),
Some(Arc::new(|n| (self.notify_handler)(n.into()))),
)
.await
{
Expand Down Expand Up @@ -171,7 +172,7 @@ impl<'a> DownloadCfg<'a> {
let file = self.temp_cfg.new_file_with_ext("", ext)?;

let mut hasher = Sha256::new();
utils::download_file(&url, &file, Some(&mut hasher), &|n| {
utils::download_file(&url, &file, Some(&mut hasher), |n| {
(self.notify_handler)(n.into())
})
.await?;
Expand Down
2 changes: 1 addition & 1 deletion src/dist/manifestation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ fn update_from_dist(
&manifest_url,
&manifest_file,
None,
&|_| {},
None,
))?;
let manifest_str = utils::read_file("manifest", &manifest_file)?;
let manifest = Manifest::parse(&manifest_str)?;
Expand Down
8 changes: 5 additions & 3 deletions src/toolchain/distributable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
convert::Infallible, env::consts::EXE_SUFFIX, ffi::OsStr, fs, path::Path, process::Command,
sync::Arc,
};

use anyhow::{anyhow, Context};
Expand Down Expand Up @@ -495,9 +496,10 @@ impl<'a> DistributableToolchain<'a> {
remove_components: vec![component],
};

let notify_handler =
&|n: crate::dist::Notification<'_>| (self.cfg.notify_handler)(n.into());
let download_cfg = self.cfg.download_cfg(&notify_handler);
let notify_handler = Some(Arc::new(&|n: crate::dist::Notification<'_>| {
(self.cfg.notify_handler)(n.into())
}));
let download_cfg = self.cfg.download_cfg(notify_handler);

utils::run_future(manifestation.update(
&manifest,
Expand Down
23 changes: 14 additions & 9 deletions src/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fs::{self, File};
use std::future::Future;
use std::io::{self, BufReader, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use anyhow::{anyhow, bail, Context, Result};
use home::env as home;
Expand Down Expand Up @@ -138,17 +139,17 @@ pub async fn download_file(
url: &Url,
path: &Path,
hasher: Option<&mut Sha256>,
notify_handler: &dyn Fn(Notification<'_>),
notify_handler: Option<Arc<&dyn Fn(Notification<'_>)>>,
) -> Result<()> {
download_file_with_resume(url, path, hasher, false, &notify_handler).await
download_file_with_resume(url, path, hasher, false, notify_handler).await
}

pub(crate) async fn download_file_with_resume(
url: &Url,
path: &Path,
hasher: Option<&mut Sha256>,
resume_from_partial: bool,
notify_handler: &dyn Fn(Notification<'_>),
notify_handler: Option<Arc<&dyn Fn(Notification<'_>)>>,
) -> Result<()> {
use download::DownloadError as DEK;
match download_file_(url, path, hasher, resume_from_partial, notify_handler).await {
Expand Down Expand Up @@ -183,14 +184,16 @@ async fn download_file_(
path: &Path,
hasher: Option<&mut Sha256>,
resume_from_partial: bool,
notify_handler: &dyn Fn(Notification<'_>),
notify_handler: Option<Arc<&dyn Fn(Notification<'_>)>>,
) -> Result<()> {
use download::download_to_path_with_backend;
use download::{Backend, Event, TlsBackend};
use sha2::Digest;
use std::cell::RefCell;

notify_handler(Notification::DownloadingFile(url, path));
notify_handler
.as_ref()
.map(|notify_handler| notify_handler(Notification::DownloadingFile(url, path)));

let hasher = RefCell::new(hasher);

Expand All @@ -203,7 +206,7 @@ async fn download_file_(
}
}

match msg {
notify_handler.as_ref().map(|notify_handler| match msg {
Event::DownloadContentLengthReceived(len) => {
notify_handler(Notification::DownloadContentLengthReceived(len));
}
Expand All @@ -213,7 +216,7 @@ async fn download_file_(
Event::ResumingPartialDownload => {
notify_handler(Notification::ResumingPartialDownload);
}
}
});

Ok(())
};
Expand All @@ -240,12 +243,14 @@ async fn download_file_(
};
(Backend::Reqwest(tls_backend), Notification::UsingReqwest)
};
notify_handler(notification);
notify_handler
.as_ref()
.map(|notify_handler| notify_handler(notification));
let res =
download_to_path_with_backend(backend, url, path, resume_from_partial, Some(callback))
.await;

notify_handler(Notification::DownloadFinished);
notify_handler.map(|notify_handler| notify_handler(Notification::DownloadFinished));

res
}
Expand Down

0 comments on commit 6c2f960

Please sign in to comment.