-
Notifications
You must be signed in to change notification settings - Fork 904
/
Copy pathunix.rs
200 lines (171 loc) · 6.93 KB
/
unix.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use std::path::{Path, PathBuf};
use std::process::Command;
use super::super::errors::*;
use super::path_update::PathUpdateMethod;
use super::{canonical_cargo_home, install_bins};
use crate::process;
use crate::utils::utils;
use crate::utils::Notification;
// If the user is trying to install with sudo, on some systems this will
// result in writing root-owned files to the user's home directory, because
// sudo is configured not to change $HOME. Don't let that bogosity happen.
pub fn do_anti_sudo_check(no_prompt: bool) -> Result<utils::ExitCode> {
pub fn home_mismatch() -> (bool, PathBuf, PathBuf) {
let fallback = || (false, PathBuf::new(), PathBuf::new());
// test runner should set this, nothing else
if process()
.var_os("RUSTUP_INIT_SKIP_SUDO_CHECK")
.map_or(false, |s| s == "yes")
{
return fallback();
}
match (utils::home_dir_from_passwd(), process().var_os("HOME")) {
(Some(pw), Some(eh)) if eh != pw => return (true, PathBuf::from(eh), pw),
(None, _) => warn!("getpwuid_r: couldn't get user data"),
_ => {}
}
fallback()
}
match home_mismatch() {
(false, _, _) => {}
(true, env_home, euid_home) => {
err!("$HOME differs from euid-obtained home directory: you may be using sudo");
err!("$HOME directory: {}", env_home.display());
err!("euid-obtained home directory: {}", euid_home.display());
if !no_prompt {
err!("if this is what you want, restart the installation with `-y'");
return Ok(utils::ExitCode(1));
}
}
}
Ok(utils::ExitCode(0))
}
pub fn delete_rustup_and_cargo_home() -> Result<()> {
let cargo_home = utils::cargo_home()?;
utils::remove_dir("cargo_home", &cargo_home, &|_: Notification<'_>| ())?;
Ok(())
}
pub fn complete_windows_uninstall() -> Result<utils::ExitCode> {
panic!("stop doing that")
}
pub fn do_remove_from_path(methods: &[PathUpdateMethod]) -> Result<()> {
for method in methods {
if let PathUpdateMethod::RcFile(ref rcpath) = *method {
let file = utils::read_file("rcfile", rcpath)?;
let addition = format!("\n{}\n", shell_export_string()?);
let file_bytes = file.into_bytes();
let addition_bytes = addition.into_bytes();
let idx = file_bytes
.windows(addition_bytes.len())
.position(|w| w == &*addition_bytes);
if let Some(i) = idx {
let mut new_file_bytes = file_bytes[..i].to_vec();
new_file_bytes.extend(&file_bytes[i + addition_bytes.len()..]);
let new_file = String::from_utf8(new_file_bytes).unwrap();
utils::write_file("rcfile", rcpath, &new_file)?;
} else {
// Weird case. rcfile no longer needs to be modified?
}
} else {
unreachable!()
}
}
Ok(())
}
pub fn write_env() -> Result<()> {
let env_file = utils::cargo_home()?.join("env");
let env_str = format!("{}\n", shell_export_string()?);
utils::write_file("env", &env_file, &env_str)?;
Ok(())
}
pub fn shell_export_string() -> Result<String> {
let path = format!("{}/bin", canonical_cargo_home()?);
// The path is *prepended* in case there are system-installed
// rustc's that need to be overridden.
Ok(format!(r#"export PATH="{}:$PATH""#, path))
}
pub fn do_add_to_path(methods: &[PathUpdateMethod]) -> Result<()> {
for method in methods {
if let PathUpdateMethod::RcFile(ref rcpath) = *method {
let file = if rcpath.exists() {
utils::read_file("rcfile", rcpath)?
} else {
String::new()
};
let addition = format!("\n{}", shell_export_string()?);
if !file.contains(&addition) {
utils::append_file("rcfile", rcpath, &addition).chain_err(|| {
ErrorKind::WritingShellProfile {
path: rcpath.to_path_buf(),
}
})?;
}
} else {
unreachable!()
}
}
Ok(())
}
/// Tell the upgrader to replace the rustup bins, then delete
/// itself. Like with uninstallation, on Windows we're going to
/// have to jump through hoops to make everything work right.
///
/// On windows we're not going to wait for it to finish before exiting
/// successfully, so it should not do much, and it should try
/// really hard to succeed, because at this point the upgrade is
/// considered successful.
pub fn run_update(setup_path: &Path) -> Result<utils::ExitCode> {
let status = Command::new(setup_path)
.arg("--self-replace")
.status()
.chain_err(|| "unable to run updater")?;
if !status.success() {
return Err("self-updated failed to replace rustup executable".into());
}
Ok(utils::ExitCode(0))
}
/// This function is as the final step of a self-upgrade. It replaces
/// `CARGO_HOME`/bin/rustup with the running exe, and updates the the
/// links to it. On windows this will run *after* the original
/// rustup process exits.
pub fn self_replace() -> Result<utils::ExitCode> {
install_bins()?;
Ok(utils::ExitCode(0))
}
/// Decide which rcfiles we're going to update, so we
/// can tell the user before they confirm.
pub fn get_add_path_methods() -> Vec<PathUpdateMethod> {
let home_dir = utils::home_dir().unwrap();
let profile = home_dir.join(".profile");
let mut profiles = vec![profile];
if let Ok(shell) = process().var("SHELL") {
if shell.contains("zsh") {
let var = process().var_os("ZDOTDIR");
let zdotdir = var.as_deref().map_or_else(|| home_dir.as_path(), Path::new);
let zprofile = zdotdir.join(".zprofile");
profiles.push(zprofile);
}
}
let bash_profile = home_dir.join(".bash_profile");
// Only update .bash_profile if it exists because creating .bash_profile
// will cause .profile to not be read
if bash_profile.exists() {
profiles.push(bash_profile);
}
profiles.into_iter().map(PathUpdateMethod::RcFile).collect()
}
/// Decide which rcfiles we're going to update, so we
/// can tell the user before they confirm.
pub fn get_remove_path_methods() -> Result<Vec<PathUpdateMethod>> {
let profile = utils::home_dir().map(|p| p.join(".profile"));
let bash_profile = utils::home_dir().map(|p| p.join(".bash_profile"));
let rcfiles = vec![profile, bash_profile];
let existing_rcfiles = rcfiles.into_iter().filter_map(|f| f).filter(|f| f.exists());
let export_str = shell_export_string()?;
let matching_rcfiles = existing_rcfiles.filter(|f| {
let file = utils::read_file("rcfile", f).unwrap_or_default();
let addition = format!("\n{}", export_str);
file.contains(&addition)
});
Ok(matching_rcfiles.map(PathUpdateMethod::RcFile).collect())
}