Skip to content

Commit a1fe74c

Browse files
authored
Merge pull request #2046 from BeniCheni/clean-downloads-after-update
Delete content of downloads dir of user home in update subcommand
2 parents 9c1492e + 0db473b commit a1fe74c

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

src/cli/rustup_mode.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,9 @@ fn update(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
812812
}
813813
} else {
814814
common::update_all_channels(cfg, self_update, m.is_present("force"))?;
815+
info!("cleaning up downloads & tmp directories");
816+
utils::delete_dir_contents(&cfg.download_dir);
817+
cfg.temp_cfg.clean();
815818
}
816819

817820
Ok(())

src/dist/temp.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::utils::raw;
2+
use crate::utils::utils;
23
use std::error;
34
use std::fmt::{self, Display};
45
use std::fs;
@@ -196,6 +197,10 @@ impl Cfg {
196197
}
197198
}
198199
}
200+
201+
pub fn clean(&self) {
202+
utils::delete_dir_contents(&self.root_directory);
203+
}
199204
}
200205

201206
impl fmt::Debug for Cfg {

src/utils/utils.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,22 @@ where
587587
})
588588
}
589589

590+
pub fn delete_dir_contents(dir_path: &Path) {
591+
let dir_contents = fs::read_dir(dir_path);
592+
if let Ok(contents) = dir_contents {
593+
for entry in contents {
594+
if let Ok(entry) = entry {
595+
let path = entry.path();
596+
if path.is_dir() {
597+
remove_dir_all::remove_dir_all(path).expect("Failed to remove a dir");
598+
} else {
599+
fs::remove_file(path).expect("Failed to remove a file");
600+
}
601+
};
602+
}
603+
};
604+
}
605+
590606
pub struct FileReaderWithProgress<'a> {
591607
fh: std::io::BufReader<std::fs::File>,
592608
notify_handler: &'a dyn Fn(Notification<'_>),

tests/cli-rustup.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ info: installing component 'cargo'
5959
info: installing component 'rust-docs'
6060
info: installing component 'rust-std'
6161
info: installing component 'rustc'
62+
info: cleaning up downloads & tmp directories
6263
"
6364
),
6465
);
@@ -98,6 +99,7 @@ info: installing component 'cargo'
9899
info: installing component 'rust-docs'
99100
info: installing component 'rust-std'
100101
info: installing component 'rustc'
102+
info: cleaning up downloads & tmp directories
101103
"
102104
),
103105
);
@@ -120,6 +122,7 @@ fn rustup_stable_no_change() {
120122
),
121123
for_host!(
122124
r"info: syncing channel updates for 'stable-{0}'
125+
info: cleaning up downloads & tmp directories
123126
"
124127
),
125128
);
@@ -188,6 +191,7 @@ info: installing component 'cargo'
188191
info: installing component 'rust-docs'
189192
info: installing component 'rust-std'
190193
info: installing component 'rustc'
194+
info: cleaning up downloads & tmp directories
191195
"
192196
),
193197
);
@@ -244,6 +248,7 @@ info: installing component 'cargo'
244248
info: installing component 'rust-docs'
245249
info: installing component 'rust-std'
246250
info: installing component 'rustc'
251+
info: cleaning up downloads & tmp directories
247252
"
248253
),
249254
);
@@ -260,6 +265,7 @@ fn rustup_no_channels() {
260265
&["rustup", "update", "--no-self-update"],
261266
r"",
262267
r"info: no updatable toolchains installed
268+
info: cleaning up downloads & tmp directories
263269
",
264270
);
265271
})

0 commit comments

Comments
 (0)