-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathinstall.rs
More file actions
163 lines (146 loc) · 5.19 KB
/
install.rs
File metadata and controls
163 lines (146 loc) · 5.19 KB
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
//! Installation and upgrade of both distribution-managed and local
//! toolchains
use std::path::{Path, PathBuf};
use anyhow::Result;
use tracing::debug;
use crate::{
config::Cfg,
dist::{DistOptions, manifest::Manifest, prefix::InstallPrefix},
errors::RustupError,
toolchain::{CustomToolchainName, LocalToolchainName, Toolchain},
utils,
};
#[derive(Clone, Debug)]
pub(crate) enum UpdateStatus {
Installed,
Updated(String), // Stores the version of rustc *before* the update
Unchanged,
}
pub(crate) enum InstallMethod<'cfg, 'a> {
Copy {
src: &'a Path,
dest: &'a CustomToolchainName,
cfg: &'cfg Cfg<'cfg>,
},
Link {
src: &'a Path,
dest: &'a CustomToolchainName,
cfg: &'cfg Cfg<'cfg>,
},
Dist(DistOptions<'cfg, 'a>),
}
impl InstallMethod<'_, '_> {
// Install a toolchain
#[tracing::instrument(level = "trace", err(level = "trace"), skip_all)]
pub(crate) async fn install(self) -> Result<UpdateStatus> {
self.install_with_manifest(None).await
}
pub(crate) async fn install_with_manifest(
self,
manifest: Option<(Manifest, String)>,
) -> Result<UpdateStatus> {
// Initialize rayon for use by the remove_dir_all crate limiting the number of threads.
// This will error if rayon is already initialized but it's fine to ignore that.
let _ = rayon::ThreadPoolBuilder::new()
.num_threads(self.cfg().process.io_thread_count()?.into())
.build_global();
match &self {
InstallMethod::Copy { .. }
| InstallMethod::Link { .. }
| InstallMethod::Dist(DistOptions {
old_date_version: None,
..
}) => debug!("installing toolchain {}", self.dest_basename()),
_ => debug!("updating existing install for '{}'", self.dest_basename()),
}
debug!("toolchain directory: {}", self.dest_path().display());
let updated = self.run(&self.dest_path(), manifest).await?;
let status = match updated {
false => {
debug!("toolchain is already up to date");
UpdateStatus::Unchanged
}
true => {
debug!("toolchain {} installed", self.dest_basename());
match &self {
InstallMethod::Dist(DistOptions {
old_date_version: Some((_, v)),
..
}) => UpdateStatus::Updated(v.clone()),
InstallMethod::Copy { .. }
| InstallMethod::Link { .. }
| InstallMethod::Dist { .. } => UpdateStatus::Installed,
}
}
};
// Final check, to ensure we're installed
match Toolchain::exists(self.cfg(), &self.local_name())? {
true => Ok(status),
false => Err(RustupError::ToolchainNotInstallable(self.dest_basename()).into()),
}
}
async fn run(&self, path: &Path, manifest: Option<(Manifest, String)>) -> Result<bool> {
if path.exists() {
// Don't uninstall first for Dist method
match self {
InstallMethod::Dist { .. } => {}
_ => {
uninstall(path)?;
}
}
}
match self {
InstallMethod::Copy { src, .. } => {
utils::copy_dir(src, path)?;
Ok(true)
}
InstallMethod::Link { src, .. } => {
utils::symlink_dir(src, path)?;
Ok(true)
}
InstallMethod::Dist(opts) => {
let prefix = &InstallPrefix::from(path.to_owned());
let maybe_new_hash = opts.install_into(prefix, manifest).await?;
if let Some(hash) = maybe_new_hash {
utils::write_file("update hash", &opts.update_hash, &hash)?;
Ok(true)
} else {
Ok(false)
}
}
}
}
fn cfg(&self) -> &Cfg<'_> {
match self {
InstallMethod::Copy { cfg, .. } => cfg,
InstallMethod::Link { cfg, .. } => cfg,
InstallMethod::Dist(DistOptions { cfg, .. }) => cfg,
}
}
fn local_name(&self) -> LocalToolchainName {
match self {
InstallMethod::Copy { dest, .. } => (*dest).into(),
InstallMethod::Link { dest, .. } => (*dest).into(),
InstallMethod::Dist(DistOptions {
toolchain: desc, ..
}) => (*desc).into(),
}
}
fn dest_basename(&self) -> String {
self.local_name().to_string()
}
fn dest_path(&self) -> PathBuf {
match self {
InstallMethod::Copy { cfg, dest, .. } => cfg.toolchain_path(&(*dest).into()),
InstallMethod::Link { cfg, dest, .. } => cfg.toolchain_path(&(*dest).into()),
InstallMethod::Dist(DistOptions {
cfg,
toolchain: desc,
..
}) => cfg.toolchain_path(&(*desc).into()),
}
}
}
pub(crate) fn uninstall(path: &Path) -> Result<()> {
utils::remove_dir("install", path)
}