-
Notifications
You must be signed in to change notification settings - Fork 911
/
Copy pathmanifestation.rs
641 lines (558 loc) · 23.3 KB
/
manifestation.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//! Maintains a Rust installation by installing individual Rust
//! platform components from a distribution server.
use crate::dist::component::{Components, Package, TarGzPackage, TarXzPackage, Transaction};
use crate::dist::config::Config;
use crate::dist::dist::{Profile, TargetTriple, DEFAULT_DIST_SERVER};
use crate::dist::download::{DownloadCfg, File};
use crate::dist::manifest::{Component, Manifest, TargetedPackage};
use crate::dist::notifications::*;
use crate::dist::prefix::InstallPrefix;
use crate::dist::temp;
use crate::errors::*;
use crate::utils::utils;
use std::path::Path;
pub const DIST_MANIFEST: &str = "multirust-channel-manifest.toml";
pub const CONFIG_FILE: &str = "multirust-config.toml";
enum Format {
Gz,
Xz,
}
#[derive(Debug)]
pub struct Manifestation {
installation: Components,
target_triple: TargetTriple,
}
#[derive(Debug)]
pub struct Changes {
pub explicit_add_components: Vec<Component>,
pub remove_components: Vec<Component>,
}
impl Changes {
pub fn none() -> Self {
Self {
explicit_add_components: Vec::new(),
remove_components: Vec::new(),
}
}
fn iter_add_components(&self) -> impl Iterator<Item = &Component> {
self.explicit_add_components.iter()
}
fn check_invariants(&self, config: &Option<Config>) {
for component_to_add in self.iter_add_components() {
assert!(
!self.remove_components.contains(component_to_add),
"can't both add and remove components"
);
}
for component_to_remove in &self.remove_components {
let config = config
.as_ref()
.expect("removing component on fresh install?");
assert!(
config.components.contains(component_to_remove),
"removing package that isn't installed"
);
}
}
}
#[derive(PartialEq, Debug)]
pub enum UpdateStatus {
Changed,
Unchanged,
}
impl Manifestation {
/// Open the install prefix for updates from a distribution
/// channel. The install prefix directory does not need to exist;
/// it will be created as needed. If there's an existing install
/// then the rust-install installation format will be verified. A
/// bad installer version is the only reason this will fail.
pub fn open(prefix: InstallPrefix, triple: TargetTriple) -> Result<Self> {
// TODO: validate the triple with the existing install as well
// as the metadata format of the existing install
Ok(Self {
installation: Components::open(prefix)?,
target_triple: triple,
})
}
/// Install or update from a given channel manifest, while
/// selecting extension components to add or remove.
///
/// `update` takes a manifest describing a release of Rust (which
/// may be either a freshly-downloaded one, or the same one used
/// for the previous install), as well as lists of extension
/// components to add and remove.
/// From that it schedules a list of components to install and
/// to uninstall to bring the installation up to date. It
/// downloads the components' packages. Then in a Transaction
/// uninstalls old packages and installs new packages, writes the
/// distribution manifest to "rustlib/rustup-dist.toml" and a
/// configuration containing the component name-target pairs to
/// "rustlib/rustup-config.toml".
pub fn update(
&self,
new_manifest: &Manifest,
changes: Changes,
force_update: bool,
download_cfg: &DownloadCfg<'_>,
notify_handler: &dyn Fn(Notification<'_>),
toolchain_str: &str,
) -> Result<UpdateStatus> {
// Some vars we're going to need a few times
let temp_cfg = download_cfg.temp_cfg;
let prefix = self.installation.prefix();
let rel_installed_manifest_path = prefix.rel_manifest_file(DIST_MANIFEST);
let installed_manifest_path = prefix.path().join(&rel_installed_manifest_path);
// Create the lists of components needed for installation
let config = self.read_config()?;
let update = Update::build_update(self, new_manifest, &changes, &config, notify_handler)?;
if update.nothing_changes() {
return Ok(UpdateStatus::Unchanged);
}
// Validate that the requested components are available
match update.unavailable_components(new_manifest, toolchain_str) {
Ok(_) => {}
_ if force_update => {}
Err(e) => return Err(e),
}
let altered = temp_cfg.dist_server != DEFAULT_DIST_SERVER;
// Download component packages and validate hashes
let mut things_to_install: Vec<(Component, Format, File)> = Vec::new();
let mut things_downloaded: Vec<String> = Vec::new();
let components = update.components_urls_and_hashes(new_manifest)?;
for (component, format, url, hash) in components {
notify_handler(Notification::DownloadingComponent(
&component.short_name(new_manifest),
&self.target_triple,
component.target.as_ref(),
));
let url = if altered {
url.replace(DEFAULT_DIST_SERVER, temp_cfg.dist_server.as_str())
} else {
url
};
let url_url = utils::parse_url(&url)?;
let downloaded_file = download_cfg
.download(&url_url, &hash)
.chain_err(|| ErrorKind::ComponentDownloadFailed(component.name(new_manifest)))?;
things_downloaded.push(hash);
things_to_install.push((component, format, downloaded_file));
}
// Begin transaction
let mut tx = Transaction::new(prefix.clone(), temp_cfg, notify_handler);
// If the previous installation was from a v1 manifest we need
// to uninstall it first.
tx = self.maybe_handle_v2_upgrade(&config, tx)?;
// Uninstall components
for component in &update.components_to_uninstall {
let notification = if altered {
Notification::RemovingOldComponent
} else {
Notification::RemovingComponent
};
notify_handler(notification(
&component.short_name(new_manifest),
&self.target_triple,
component.target.as_ref(),
));
tx = self.uninstall_component(&component, new_manifest, tx, ¬ify_handler)?;
}
// Install components
for (component, format, installer_file) in things_to_install {
// For historical reasons, the rust-installer component
// names are not the same as the dist manifest component
// names. Some are just the component name some are the
// component name plus the target triple.
let pkg_name = component.name_in_manifest();
let short_pkg_name = component.short_name_in_manifest();
let short_name = component.short_name(new_manifest);
notify_handler(Notification::InstallingComponent(
&short_name,
&self.target_triple,
component.target.as_ref(),
));
let notification_converter = |notification: crate::utils::Notification<'_>| {
notify_handler(notification.into());
};
let gz;
let xz;
let reader =
utils::FileReaderWithProgress::new_file(&installer_file, ¬ification_converter)?;
let package: &dyn Package = match format {
Format::Gz => {
gz = TarGzPackage::new(reader, temp_cfg, Some(¬ification_converter))?;
&gz
}
Format::Xz => {
xz = TarXzPackage::new(reader, temp_cfg, Some(¬ification_converter))?;
&xz
}
};
// If the package doesn't contain the component that the
// manifest says it does then somebody must be playing a joke on us.
if !package.contains(&pkg_name, Some(&short_pkg_name)) {
return Err(ErrorKind::CorruptComponent(short_name).into());
}
tx = package.install(&self.installation, &pkg_name, Some(&short_pkg_name), tx)?;
}
// Install new distribution manifest
let new_manifest_str = new_manifest.clone().stringify();
tx.modify_file(rel_installed_manifest_path.to_owned())?;
utils::write_file("manifest", &installed_manifest_path, &new_manifest_str)?;
// Write configuration.
//
// NB: This configuration is mostly for keeping track of the name/target pairs
// that identify installed components. The rust-installer metadata maintained by
// `Components` *also* tracks what is installed, but it only tracks names, not
// name/target. Needs to be fixed in rust-installer.
let mut new_config = Config::new();
new_config.components = update.final_component_list;
let config_str = new_config.stringify();
let rel_config_path = prefix.rel_manifest_file(CONFIG_FILE);
let config_path = prefix.path().join(&rel_config_path);
tx.modify_file(rel_config_path.to_owned())?;
utils::write_file("dist config", &config_path, &config_str)?;
// End transaction
tx.commit();
download_cfg.clean(&things_downloaded)?;
Ok(UpdateStatus::Changed)
}
pub fn uninstall(
&self,
manifest: &Manifest,
temp_cfg: &temp::Cfg,
notify_handler: &dyn Fn(Notification<'_>),
) -> Result<()> {
let prefix = self.installation.prefix();
let mut tx = Transaction::new(prefix.clone(), temp_cfg, notify_handler);
// Read configuration and delete it
let rel_config_path = prefix.rel_manifest_file(CONFIG_FILE);
let config_str = utils::read_file("dist config", &prefix.path().join(&rel_config_path))?;
let config = Config::parse(&config_str)?;
tx.remove_file("dist config", rel_config_path)?;
for component in config.components {
tx = self.uninstall_component(&component, manifest, tx, notify_handler)?;
}
tx.commit();
Ok(())
}
fn uninstall_component<'a>(
&self,
component: &Component,
manifest: &Manifest,
mut tx: Transaction<'a>,
notify_handler: &dyn Fn(Notification<'_>),
) -> Result<Transaction<'a>> {
// For historical reasons, the rust-installer component
// names are not the same as the dist manifest component
// names. Some are just the component name some are the
// component name plus the target triple.
let name = component.name_in_manifest();
let short_name = component.short_name_in_manifest();
if let Some(c) = self.installation.find(&name)? {
tx = c.uninstall(tx)?;
} else if let Some(c) = self.installation.find(&short_name)? {
tx = c.uninstall(tx)?;
} else {
notify_handler(Notification::MissingInstalledComponent(
&component.short_name(manifest),
));
}
Ok(tx)
}
// Read the config file. Config files are presently only created
// for v2 installations.
pub fn read_config(&self) -> Result<Option<Config>> {
let prefix = self.installation.prefix();
let rel_config_path = prefix.rel_manifest_file(CONFIG_FILE);
let config_path = prefix.path().join(rel_config_path);
if utils::path_exists(&config_path) {
let config_str = utils::read_file("dist config", &config_path)?;
Ok(Some(Config::parse(&config_str)?))
} else {
Ok(None)
}
}
pub fn load_manifest(&self) -> Result<Option<Manifest>> {
let prefix = self.installation.prefix();
let old_manifest_path = prefix.manifest_file(DIST_MANIFEST);
if utils::path_exists(&old_manifest_path) {
let manifest_str = utils::read_file("installed manifest", &old_manifest_path)?;
Ok(Some(Manifest::parse(&manifest_str)?))
} else {
Ok(None)
}
}
/// Installation using the legacy v1 manifest format
pub fn update_v1(
&self,
new_manifest: &[String],
update_hash: Option<&Path>,
temp_cfg: &temp::Cfg,
notify_handler: &dyn Fn(Notification<'_>),
) -> Result<Option<String>> {
// If there's already a v2 installation then something has gone wrong
if self.read_config()?.is_some() {
return Err(
"the server unexpectedly provided an obsolete version of the distribution manifest"
.into(),
);
}
let url = new_manifest
.iter()
.find(|u| u.contains(&format!("{}{}", self.target_triple, ".tar.gz")));
if url.is_none() {
return Err(format!(
"binary package was not provided for '{}'",
self.target_triple.to_string()
)
.into());
}
// Only replace once. The cost is inexpensive.
let url = url
.unwrap()
.replace(DEFAULT_DIST_SERVER, temp_cfg.dist_server.as_str());
notify_handler(Notification::DownloadingComponent(
"rust",
&self.target_triple,
Some(&self.target_triple),
));
use std::path::PathBuf;
let dld_dir = PathBuf::from("bogus");
let dlcfg = DownloadCfg {
dist_root: "bogus",
download_dir: &dld_dir,
temp_cfg,
notify_handler,
};
let dl = dlcfg.download_and_check(&url, update_hash, ".tar.gz")?;
if dl.is_none() {
return Ok(None);
};
let (installer_file, installer_hash) = dl.unwrap();
let prefix = self.installation.prefix();
notify_handler(Notification::InstallingComponent(
"rust",
&self.target_triple,
Some(&self.target_triple),
));
// Begin transaction
let mut tx = Transaction::new(prefix.clone(), temp_cfg, notify_handler);
// Uninstall components
let components = self.installation.list()?;
for component in components {
tx = component.uninstall(tx)?;
}
// Install all the components in the installer
let notification_converter = |notification: crate::utils::Notification<'_>| {
notify_handler(notification.into());
};
let reader =
utils::FileReaderWithProgress::new_file(&installer_file, ¬ification_converter)?;
let package: &dyn Package =
&TarGzPackage::new(reader, temp_cfg, Some(¬ification_converter))?;
for component in package.components() {
tx = package.install(&self.installation, &component, None, tx)?;
}
// End transaction
tx.commit();
Ok(Some(installer_hash))
}
// If the previous installation was from a v1 manifest, then it
// doesn't have a configuration or manifest-derived list of
// component/target pairs. Uninstall it using the intaller's
// component list before upgrading.
fn maybe_handle_v2_upgrade<'a>(
&self,
config: &Option<Config>,
mut tx: Transaction<'a>,
) -> Result<Transaction<'a>> {
let installed_components = self.installation.list()?;
let looks_like_v1 = config.is_none() && !installed_components.is_empty();
if !looks_like_v1 {
return Ok(tx);
}
for component in installed_components {
tx = component.uninstall(tx)?;
}
Ok(tx)
}
}
#[derive(Debug)]
struct Update {
components_to_uninstall: Vec<Component>,
components_to_install: Vec<Component>,
final_component_list: Vec<Component>,
missing_components: Vec<Component>,
}
impl Update {
/// Returns components to uninstall, install, and the list of all
/// components that will be up to date after the update.
fn build_update(
manifestation: &Manifestation,
new_manifest: &Manifest,
changes: &Changes,
config: &Option<Config>,
notify_handler: &dyn Fn(Notification<'_>),
) -> Result<Self> {
// The package to install.
let rust_package = new_manifest.get_package("rust")?;
let rust_target_package = rust_package.get_target(Some(&manifestation.target_triple))?;
changes.check_invariants(&config);
// The list of components already installed, empty if a new install
let mut starting_list = config
.as_ref()
.map(|c| c.components.clone())
.unwrap_or_default();
let installed_components = manifestation.installation.list()?;
let looks_like_v1 = config.is_none() && !installed_components.is_empty();
if looks_like_v1 {
let mut profile_components = new_manifest
.get_profile_components(Profile::Default, &manifestation.target_triple)?;
starting_list.append(&mut profile_components);
}
let mut result = Self {
components_to_uninstall: vec![],
components_to_install: vec![],
final_component_list: vec![],
missing_components: vec![],
};
// Find the final list of components we want to be left with when
// we're done: required components, added components, and existing
// installed components.
result.build_final_component_list(
&starting_list,
rust_target_package,
new_manifest,
&changes,
);
// If this is a full upgrade then the list of components to
// uninstall is all that are currently installed, and those
// to install the final list. It's a complete reinstall.
//
// If it's a modification then the components to uninstall are
// those that are currently installed but not in the final list.
// To install are those on the final list but not already
// installed.
let old_manifest = manifestation.load_manifest()?;
let just_modifying_existing_install = old_manifest.as_ref() == Some(new_manifest);
if just_modifying_existing_install {
for existing_component in &starting_list {
if !result.final_component_list.contains(existing_component) {
result
.components_to_uninstall
.push(existing_component.clone())
}
}
for component in &result.final_component_list {
if !starting_list.contains(component) {
result.components_to_install.push(component.clone());
} else if changes.explicit_add_components.contains(&component) {
notify_handler(Notification::ComponentAlreadyInstalled(
&component.description(new_manifest),
));
}
}
} else {
result.components_to_uninstall = starting_list.clone();
result.components_to_install = result.final_component_list.clone();
}
Ok(result)
}
/// Build the list of components we'll have installed at the end
fn build_final_component_list(
&mut self,
starting_list: &[Component],
rust_target_package: &TargetedPackage,
new_manifest: &Manifest,
changes: &Changes,
) {
// Add requested components
for component in &changes.explicit_add_components {
self.final_component_list.push(component.clone());
}
// Add components that are already installed
for existing_component in starting_list {
let removed = changes.remove_components.contains(existing_component);
if !removed {
// If there is a rename in the (new) manifest, then we uninstall the component with the
// old name and install a component with the new name
if let Some(renamed_component) = new_manifest.rename_component(&existing_component)
{
let is_already_included =
self.final_component_list.contains(&renamed_component);
if !is_already_included {
self.final_component_list.push(renamed_component);
}
} else {
let is_already_included =
self.final_component_list.contains(existing_component);
if !is_already_included {
let component_is_present =
rust_target_package.components.contains(existing_component);
if component_is_present {
self.final_component_list.push(existing_component.clone());
} else {
self.missing_components.push(existing_component.clone());
}
}
}
}
}
}
fn nothing_changes(&self) -> bool {
self.components_to_uninstall.is_empty() && self.components_to_install.is_empty()
}
fn unavailable_components(&self, new_manifest: &Manifest, toolchain_str: &str) -> Result<()> {
let mut unavailable_components: Vec<Component> = self
.components_to_install
.iter()
.filter(|c| {
use crate::dist::manifest::*;
let pkg: Option<&Package> =
new_manifest.get_package(&c.short_name_in_manifest()).ok();
let target_pkg: Option<&TargetedPackage> =
pkg.and_then(|p| p.get_target(c.target.as_ref()).ok());
target_pkg.map(TargetedPackage::available) != Some(true)
})
.cloned()
.collect();
unavailable_components.extend_from_slice(&self.missing_components);
if !unavailable_components.is_empty() {
return Err(ErrorKind::RequestedComponentsUnavailable(
unavailable_components,
new_manifest.clone(),
toolchain_str.to_owned(),
)
.into());
}
Ok(())
}
/// Map components to urls and hashes
fn components_urls_and_hashes(
&self,
new_manifest: &Manifest,
) -> Result<Vec<(Component, Format, String, String)>> {
let mut components_urls_and_hashes = Vec::new();
for component in &self.components_to_install {
let package = new_manifest.get_package(&component.short_name_in_manifest())?;
let target_package = package.get_target(component.target.as_ref())?;
let bins = match target_package.bins {
None => continue,
Some(ref bins) => bins,
};
let c_u_h = if let (Some(url), Some(hash)) = (bins.xz_url.clone(), bins.xz_hash.clone())
{
(component.clone(), Format::Xz, url, hash)
} else {
(
component.clone(),
Format::Gz,
bins.url.clone(),
bins.hash.clone(),
)
};
components_urls_and_hashes.push(c_u_h);
}
Ok(components_urls_and_hashes)
}
}