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

fix(utils): resolve input path in delete_dir_contents() if it's a link #3754

Merged
merged 3 commits into from
Apr 9, 2024
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
2 changes: 1 addition & 1 deletion src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ fn update(cfg: &mut Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
} else {
common::update_all_channels(cfg, self_update, m.get_flag("force"))?;
info!("cleaning up downloads & tmp directories");
utils::delete_dir_contents(&cfg.download_dir);
utils::delete_dir_contents_following_links(&cfg.download_dir);
rami3l marked this conversation as resolved.
Show resolved Hide resolved
cfg.temp_cfg.clean();
}

Expand Down
2 changes: 1 addition & 1 deletion src/dist/temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl Cfg {
}

pub(crate) fn clean(&self) {
utils::delete_dir_contents(&self.root_directory);
utils::delete_dir_contents_following_links(&self.root_directory);
rami3l marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,10 @@ where
})
}

pub(crate) fn delete_dir_contents(dir_path: &Path) {
match remove_dir_all::remove_dir_contents(dir_path) {
pub(crate) fn delete_dir_contents_following_links(dir_path: &Path) {
use remove_dir_all::RemoveDir;

match raw::open_dir_following_links(dir_path).and_then(|mut p| p.remove_dir_contents(None)) {
Err(e) if e.kind() != io::ErrorKind::NotFound => {
panic!("Unable to clean up {}: {:?}", dir_path.display(), e);
}
Expand Down
30 changes: 30 additions & 0 deletions tests/suite/cli_misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,36 @@ fn toolchains_symlink() {
});
}

// issue #3344
rami3l marked this conversation as resolved.
Show resolved Hide resolved
/// `~/.rustup/tmp` and `~/.rustup/downloads` are permitted to be symlinks.
#[test]
#[cfg(any(unix, windows))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this equivalent to all platforms?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are indeed other platforms such as wasm, although I don't know if Rustup is meaningful there or not. Similar to other tests above/below it, this one relies on the fact that we compile for Unix or Windows. I just copied the code over as I mentioned above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that platforms like WASM exist, this seems okay.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a side effect of code evolution, it isn't the pattern in general, and rustup does depend on hardlinks to operate at all. Are there platforms that have hardlinks, but not symlinks?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rbtcollins Looks like symlinks are also available on WASM/WASI: https://doc.rust-lang.org/std/os/wasi/fs/fn.symlink.html, so I'm also okay with removing this line. Anyway Rustup in its current form will fail to build on WASM and not all dependencies of this test work on WASM anyway.

fn tmp_downloads_symlink() {
use rustup::utils::raw::symlink_dir;
use std::fs;

clitools::test(Scenario::ArchivesV2, &|config| {
let cwd = config.current_dir();

let test_tmp = cwd.join("tmp-test");
fs::create_dir(&test_tmp).unwrap();
symlink_dir(&test_tmp, &config.rustupdir.join("tmp")).unwrap();

let test_downloads = cwd.join("tmp-downloads");
fs::create_dir(&test_downloads).unwrap();
symlink_dir(&test_downloads, &config.rustupdir.join("downloads")).unwrap();

set_current_dist_date(config, "2015-01-01");
config.expect_ok(&["rustup", "default", "nightly"]);

set_current_dist_date(config, "2015-01-02");
config.expect_ok(&["rustup", "update"]);

assert!(config.rustupdir.join("tmp").exists());
assert!(config.rustupdir.join("downloads").exists());
});
}

// issue #1169
/// A toolchain that is a stale symlink should be correctly uninstalled.
#[test]
Expand Down
Loading