From 0db473b1be2d5c4355c03da68b25952fa3038958 Mon Sep 17 00:00:00 2001 From: Benjamin Chen Date: Wed, 9 Oct 2019 01:00:26 -0400 Subject: [PATCH] Delete content of downloads & tmp dirs after full rustup update --- src/cli/rustup_mode.rs | 3 +++ src/dist/temp.rs | 5 +++++ src/utils/utils.rs | 16 ++++++++++++++++ tests/cli-rustup.rs | 6 ++++++ 4 files changed, 30 insertions(+) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 7dfa4fd7f1..77042252d5 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -812,6 +812,9 @@ fn update(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> { } } else { common::update_all_channels(cfg, self_update, m.is_present("force"))?; + info!("cleaning up downloads & tmp directories"); + utils::delete_dir_contents(&cfg.download_dir); + cfg.temp_cfg.clean(); } Ok(()) diff --git a/src/dist/temp.rs b/src/dist/temp.rs index 511fcecde7..8318c3e6e6 100644 --- a/src/dist/temp.rs +++ b/src/dist/temp.rs @@ -1,4 +1,5 @@ use crate::utils::raw; +use crate::utils::utils; use std::error; use std::fmt::{self, Display}; use std::fs; @@ -196,6 +197,10 @@ impl Cfg { } } } + + pub fn clean(&self) { + utils::delete_dir_contents(&self.root_directory); + } } impl fmt::Debug for Cfg { diff --git a/src/utils/utils.rs b/src/utils/utils.rs index 3622eb6956..d5288765ed 100644 --- a/src/utils/utils.rs +++ b/src/utils/utils.rs @@ -587,6 +587,22 @@ where }) } +pub fn delete_dir_contents(dir_path: &Path) { + let dir_contents = fs::read_dir(dir_path); + if let Ok(contents) = dir_contents { + for entry in contents { + if let Ok(entry) = entry { + let path = entry.path(); + if path.is_dir() { + remove_dir_all::remove_dir_all(path).expect("Failed to remove a dir"); + } else { + fs::remove_file(path).expect("Failed to remove a file"); + } + }; + } + }; +} + pub struct FileReaderWithProgress<'a> { fh: std::io::BufReader, notify_handler: &'a dyn Fn(Notification<'_>), diff --git a/tests/cli-rustup.rs b/tests/cli-rustup.rs index be3eb7183d..51d4e8f721 100644 --- a/tests/cli-rustup.rs +++ b/tests/cli-rustup.rs @@ -59,6 +59,7 @@ info: installing component 'cargo' info: installing component 'rust-docs' info: installing component 'rust-std' info: installing component 'rustc' +info: cleaning up downloads & tmp directories " ), ); @@ -98,6 +99,7 @@ info: installing component 'cargo' info: installing component 'rust-docs' info: installing component 'rust-std' info: installing component 'rustc' +info: cleaning up downloads & tmp directories " ), ); @@ -120,6 +122,7 @@ fn rustup_stable_no_change() { ), for_host!( r"info: syncing channel updates for 'stable-{0}' +info: cleaning up downloads & tmp directories " ), ); @@ -188,6 +191,7 @@ info: installing component 'cargo' info: installing component 'rust-docs' info: installing component 'rust-std' info: installing component 'rustc' +info: cleaning up downloads & tmp directories " ), ); @@ -244,6 +248,7 @@ info: installing component 'cargo' info: installing component 'rust-docs' info: installing component 'rust-std' info: installing component 'rustc' +info: cleaning up downloads & tmp directories " ), ); @@ -260,6 +265,7 @@ fn rustup_no_channels() { &["rustup", "update", "--no-self-update"], r"", r"info: no updatable toolchains installed +info: cleaning up downloads & tmp directories ", ); })