Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete content of downloads dir of user home in update subcommand #2046

Merged
merged 1 commit into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
5 changes: 5 additions & 0 deletions src/dist/temp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::utils::raw;
use crate::utils::utils;
use std::error;
use std::fmt::{self, Display};
use std::fs;
Expand Down Expand Up @@ -196,6 +197,10 @@ impl Cfg {
}
}
}

pub fn clean(&self) {
utils::delete_dir_contents(&self.root_directory);
}
}

impl fmt::Debug for Cfg {
Expand Down
16 changes: 16 additions & 0 deletions src/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::fs::File>,
notify_handler: &'a dyn Fn(Notification<'_>),
Expand Down
6 changes: 6 additions & 0 deletions tests/cli-rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"
),
);
Expand Down Expand Up @@ -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
"
),
);
Expand All @@ -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
"
),
);
Expand Down Expand Up @@ -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
"
),
);
Expand Down Expand Up @@ -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
"
),
);
Expand All @@ -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
",
);
})
Expand Down