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): don't use O_NOFOLLOW in open_dir() #3742

Merged
merged 3 commits into from
Mar 31, 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
4 changes: 2 additions & 2 deletions src/toolchain/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
currentprocess::{process, varsource::VarSource},
env_var, install,
notifications::Notification,
utils::{raw::open_dir, utils},
utils::{raw::open_dir_following_links, utils},
RustupError,
};

Expand Down Expand Up @@ -62,7 +62,7 @@ impl<'a> Toolchain<'a> {
let base_name = path
.file_name()
.ok_or_else(|| RustupError::InvalidToolchainName(name.to_string()))?;
let parent_dir = match open_dir(parent) {
let parent_dir = match open_dir_following_links(parent) {
Ok(d) => d,
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(false),
e => e?,
Expand Down
7 changes: 3 additions & 4 deletions src/utils/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn is_file<P: AsRef<Path>>(path: P) -> bool {
}

#[cfg(windows)]
pub fn open_dir(p: &Path) -> std::io::Result<File> {
pub fn open_dir_following_links(p: &Path) -> std::io::Result<File> {
use std::fs::OpenOptions;
use std::os::windows::fs::OpenOptionsExt;

Expand All @@ -42,14 +42,13 @@ pub fn open_dir(p: &Path) -> std::io::Result<File> {
options.custom_flags(FILE_FLAG_BACKUP_SEMANTICS);
options.open(p)
}

#[cfg(not(windows))]
pub fn open_dir(p: &Path) -> std::io::Result<File> {
pub fn open_dir_following_links(p: &Path) -> std::io::Result<File> {
use std::fs::OpenOptions;
use std::os::unix::fs::OpenOptionsExt;

let mut options = OpenOptions::new();
options.read(true);
options.custom_flags(libc::O_NOFOLLOW);
options.open(p)
}

Expand Down
26 changes: 26 additions & 0 deletions tests/suite/cli_misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,33 @@ fn rename_rls_remove() {
});
}

// issue #3737
/// `~/.rustup/toolchains` is permitted to be a symlink.
#[test]
#[cfg(any(unix, windows))]
fn toolchains_symlink() {
use rustup::utils::raw::symlink_dir;
use std::fs;

clitools::test(Scenario::SimpleV2, &|config| {
let cwd = config.current_dir();
let test_toolchains = cwd.join("toolchains-test");
fs::create_dir(&test_toolchains).unwrap();
symlink_dir(&test_toolchains, &config.rustupdir.join("toolchains")).unwrap();

config.expect_ok(&["rustup", "default", "nightly"]);
config.expect_ok_contains(&["rustup", "toolchain", "list"], "nightly", "");
config.expect_ok_contains(&["rustc", "--version"], "hash-nightly-2", "");
config.expect_ok(&["rustup", "toolchain", "uninstall", "nightly"]);
config.expect_stdout_ok(
&["rustup", "toolchain", "list"],
"no installed toolchains\n",
);
});
}

// issue #1169
/// A toolchain that is a stale symlink should be correctly uninstalled.
#[test]
#[cfg(any(unix, windows))]
fn toolchain_broken_symlink() {
Expand Down
Loading