-
Notifications
You must be signed in to change notification settings - Fork 893
/
config.rs
1238 lines (1098 loc) · 42.1 KB
/
config.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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::borrow::Cow;
use std::fmt::{self, Display};
use std::io;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::FromStr;
use std::sync::Arc;
use anyhow::{anyhow, bail, Context, Result};
use sequoia_openpgp::{parse::Parse, policy, Cert};
use serde::Deserialize;
use thiserror::Error as ThisError;
use crate::cli::self_update::SelfUpdateMode;
use crate::dist::download::DownloadCfg;
use crate::dist::{
dist::{self, Profile},
temp,
};
use crate::errors::RustupError;
use crate::fallback_settings::FallbackSettings;
use crate::notifications::*;
use crate::process;
use crate::settings::{Settings, SettingsFile, DEFAULT_METADATA_VERSION};
use crate::toolchain::{DistributableToolchain, Toolchain, UpdateStatus};
use crate::utils::utils;
#[derive(Debug, ThisError)]
enum OverrideFileConfigError {
#[error("empty toolchain override file detected. Please remove it, or else specify the desired toolchain properties in the file")]
Empty,
#[error("missing toolchain properties in toolchain override file")]
Invalid,
#[error("error parsing override file")]
Parsing,
}
#[derive(Debug, Default, Deserialize, PartialEq, Eq)]
struct OverrideFile {
toolchain: ToolchainSection,
}
impl OverrideFile {
fn is_empty(&self) -> bool {
self.toolchain.is_empty()
}
}
#[derive(Debug, Default, Deserialize, PartialEq, Eq)]
struct ToolchainSection {
channel: Option<String>,
path: Option<PathBuf>,
components: Option<Vec<String>>,
targets: Option<Vec<String>>,
profile: Option<String>,
}
impl ToolchainSection {
fn is_empty(&self) -> bool {
self.channel.is_none()
&& self.components.is_none()
&& self.targets.is_none()
&& self.path.is_none()
}
}
impl<T: Into<String>> From<T> for OverrideFile {
fn from(channel: T) -> Self {
let override_ = channel.into();
if Path::new(&override_).is_absolute() {
Self {
toolchain: ToolchainSection {
path: Some(PathBuf::from(override_)),
..Default::default()
},
}
} else {
Self {
toolchain: ToolchainSection {
channel: Some(override_),
..Default::default()
},
}
}
}
}
#[derive(Debug)]
pub(crate) enum OverrideReason {
Environment,
CommandLine,
OverrideDB(PathBuf),
ToolchainFile(PathBuf),
}
impl Display for OverrideReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
match self {
Self::Environment => write!(f, "environment override by RUSTUP_TOOLCHAIN"),
Self::CommandLine => write!(f, "overridden by +toolchain on the command line"),
Self::OverrideDB(path) => write!(f, "directory override for '{}'", path.display()),
Self::ToolchainFile(path) => write!(f, "overridden by '{}'", path.display()),
}
}
}
#[derive(Default, Debug)]
struct OverrideCfg<'a> {
toolchain: Option<Toolchain<'a>>,
components: Vec<String>,
targets: Vec<String>,
profile: Option<dist::Profile>,
}
impl<'a> OverrideCfg<'a> {
fn from_file(
cfg: &'a Cfg,
cfg_path: Option<impl AsRef<Path>>,
file: OverrideFile,
) -> Result<Self> {
Ok(Self {
toolchain: match (file.toolchain.channel, file.toolchain.path) {
(Some(name), None) => Some(Toolchain::from(cfg, &name)?),
(None, Some(path)) => {
if file.toolchain.targets.is_some()
|| file.toolchain.components.is_some()
|| file.toolchain.profile.is_some()
{
bail!(
"toolchain options are ignored for path toolchain ({})",
path.display()
)
}
Some(Toolchain::from_path(cfg, cfg_path, &path)?)
}
(Some(channel), Some(path)) => {
bail!(
"cannot specify both channel ({}) and path ({}) simultaneously",
channel,
path.display()
)
}
(None, None) => None,
},
components: file.toolchain.components.unwrap_or_default(),
targets: file.toolchain.targets.unwrap_or_default(),
profile: file
.toolchain
.profile
.as_deref()
.map(dist::Profile::from_str)
.transpose()?,
})
}
}
lazy_static::lazy_static! {
static ref BUILTIN_PGP_KEY: Cert =
Cert::from_bytes(&include_bytes!("rust-key.pgp.ascii")[..]).unwrap();
}
#[allow(clippy::large_enum_variant)] // Builtin is tiny, the rest are sane
#[derive(Debug)]
pub enum PgpPublicKey {
Builtin,
FromEnvironment(PathBuf, Cert),
FromConfiguration(PathBuf, Cert),
}
impl PgpPublicKey {
/// Retrieve the key.
pub(crate) fn cert(&self) -> &Cert {
match self {
Self::Builtin => &BUILTIN_PGP_KEY,
Self::FromEnvironment(_, k) => k,
Self::FromConfiguration(_, k) => k,
}
}
/// Display the key in detail for the user
pub(crate) fn show_key(&self) -> Result<Vec<String>> {
fn format_hex(bytes: &[u8], separator: &str, every: usize) -> Result<String> {
use std::fmt::Write;
let mut ret = String::new();
let mut wait = every;
for b in bytes.iter() {
if wait == 0 {
ret.push_str(separator);
wait = every;
}
wait -= 1;
write!(ret, "{:02X}", b)?;
}
Ok(ret)
}
let mut ret = vec![format!("from {}", self)];
let cert = self.cert();
let keyid = format_hex(cert.keyid().as_bytes(), "-", 4)?;
let algo = cert.primary_key().pk_algo();
let fpr = format_hex(cert.fingerprint().as_bytes(), " ", 2)?;
let p = policy::StandardPolicy::new();
let uid0 = cert
.with_policy(&p, None)?
.primary_userid()
.map(|u| u.userid().to_string())
.unwrap_or_else(|_| "<No User ID>".into());
ret.push(format!(" {:?}/{} - {}", algo, keyid, uid0));
ret.push(format!(" Fingerprint: {}", fpr));
Ok(ret)
}
}
impl Display for PgpPublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Builtin => write!(f, "builtin Rust release key"),
Self::FromEnvironment(p, _) => {
write!(f, "key specified in RUSTUP_PGP_KEY ({})", p.display())
}
Self::FromConfiguration(p, _) => {
write!(f, "key specified in configuration file ({})", p.display())
}
}
}
}
pub(crate) const UNIX_FALLBACK_SETTINGS: &str = "/etc/rustup/settings.toml";
pub struct Cfg {
profile_override: Option<dist::Profile>,
pub rustup_dir: PathBuf,
pub settings_file: SettingsFile,
pub fallback_settings: Option<FallbackSettings>,
pub toolchains_dir: PathBuf,
pub update_hash_dir: PathBuf,
pub download_dir: PathBuf,
pub temp_cfg: temp::Cfg,
pgp_keys: Vec<PgpPublicKey>,
pub toolchain_override: Option<String>,
pub env_override: Option<String>,
pub dist_root_url: String,
pub notify_handler: Arc<dyn Fn(Notification<'_>)>,
}
impl Cfg {
pub(crate) fn from_env(notify_handler: Arc<dyn Fn(Notification<'_>)>) -> Result<Self> {
// Set up the rustup home directory
let rustup_dir = utils::rustup_home()?;
utils::ensure_dir_exists("home", &rustup_dir, notify_handler.as_ref())?;
let settings_file = SettingsFile::new(rustup_dir.join("settings.toml"));
// Centralised file for multi-user systems to provide admin/distributor set initial values.
let fallback_settings = if cfg!(not(windows)) {
// If present, use the RUSTUP_OVERRIDE_UNIX_FALLBACK_SETTINGS environment
// variable as settings path, or UNIX_FALLBACK_SETTINGS otherwise
FallbackSettings::new(
match process().var("RUSTUP_OVERRIDE_UNIX_FALLBACK_SETTINGS") {
Ok(s) => PathBuf::from(s),
Err(_) => PathBuf::from(UNIX_FALLBACK_SETTINGS),
},
)?
} else {
None
};
let toolchains_dir = rustup_dir.join("toolchains");
let update_hash_dir = rustup_dir.join("update-hashes");
let download_dir = rustup_dir.join("downloads");
// PGP keys
let mut pgp_keys: Vec<PgpPublicKey> = vec![PgpPublicKey::Builtin];
if let Some(ref s_path) = process().var_os("RUSTUP_PGP_KEY") {
let path = PathBuf::from(s_path);
let file = utils::open_file("RUSTUP_PGP_KEY", &path)?;
let key = Cert::from_reader(file).map_err(|error| RustupError::InvalidPgpKey {
path: s_path.into(),
source: error,
})?;
pgp_keys.push(PgpPublicKey::FromEnvironment(path, key));
}
settings_file.with(|s| {
if let Some(s) = &s.pgp_keys {
let path = PathBuf::from(s);
let file = utils::open_file("PGP Key from config", &path)?;
let key = Cert::from_reader(file).map_err(|error| {
anyhow!(RustupError::InvalidPgpKey {
path: s.into(),
source: error,
})
})?;
pgp_keys.push(PgpPublicKey::FromConfiguration(path, key));
}
Ok(())
})?;
// Environment override
let env_override = process()
.var("RUSTUP_TOOLCHAIN")
.ok()
.and_then(utils::if_not_empty);
let dist_root_server = match process().var("RUSTUP_DIST_SERVER") {
Ok(ref s) if !s.is_empty() => s.clone(),
_ => {
// For backward compatibility
process()
.var("RUSTUP_DIST_ROOT")
.ok()
.and_then(utils::if_not_empty)
.map_or(Cow::Borrowed(dist::DEFAULT_DIST_ROOT), Cow::Owned)
.as_ref()
.trim_end_matches("/dist")
.to_owned()
}
};
let notify_clone = notify_handler.clone();
let temp_cfg = temp::Cfg::new(
rustup_dir.join("tmp"),
dist_root_server.as_str(),
Box::new(move |n| (notify_clone)(n.into())),
);
let dist_root = dist_root_server + "/dist";
let cfg = Self {
profile_override: None,
rustup_dir,
settings_file,
fallback_settings,
toolchains_dir,
update_hash_dir,
download_dir,
temp_cfg,
pgp_keys,
notify_handler,
toolchain_override: None,
env_override,
dist_root_url: dist_root,
};
// Run some basic checks against the constructed configuration
// For now, that means simply checking that 'stable' can resolve
// for the current configuration.
cfg.resolve_toolchain("stable")
.context("Unable parse configuration")?;
Ok(cfg)
}
/// construct a download configuration
pub(crate) fn download_cfg<'a>(
&'a self,
notify_handler: &'a dyn Fn(crate::dist::Notification<'_>),
) -> DownloadCfg<'a> {
DownloadCfg {
dist_root: &self.dist_root_url,
temp_cfg: &self.temp_cfg,
download_dir: &self.download_dir,
notify_handler,
pgp_keys: self.get_pgp_keys(),
}
}
pub(crate) fn get_pgp_keys(&self) -> &[PgpPublicKey] {
&self.pgp_keys
}
pub(crate) fn set_profile_override(&mut self, profile: dist::Profile) {
self.profile_override = Some(profile);
}
pub(crate) fn set_default(&self, toolchain: &str) -> Result<()> {
self.settings_file.with_mut(|s| {
s.default_toolchain = if toolchain == "none" {
None
} else {
Some(toolchain.to_owned())
};
Ok(())
})?;
(self.notify_handler)(Notification::SetDefaultToolchain(toolchain));
Ok(())
}
pub(crate) fn set_profile(&mut self, profile: &str) -> Result<()> {
match Profile::from_str(profile) {
Ok(p) => {
self.profile_override = None;
self.settings_file.with_mut(|s| {
s.profile = Some(p);
Ok(())
})?;
(self.notify_handler)(Notification::SetProfile(profile));
Ok(())
}
Err(err) => Err(err),
}
}
pub(crate) fn set_auto_self_update(&mut self, mode: &str) -> Result<()> {
match SelfUpdateMode::from_str(mode) {
Ok(update_mode) => {
self.settings_file.with_mut(|s| {
s.auto_self_update = Some(update_mode);
Ok(())
})?;
(self.notify_handler)(Notification::SetSelfUpdate(mode));
Ok(())
}
Err(err) => Err(err),
}
}
pub(crate) fn set_toolchain_override(&mut self, toolchain_override: &str) {
self.toolchain_override = Some(toolchain_override.to_owned());
}
// Returns a profile, if one exists in the settings file.
//
// Returns `Err` if the settings file could not be read or the profile is
// invalid. Returns `Ok(...)` if there is a valid profile, and `Ok(Profile::Default)`
// if there is no profile in the settings file. The last variant happens when
// a user upgrades from a version of Rustup without profiles to a version of
// Rustup with profiles.
pub(crate) fn get_profile(&self) -> Result<dist::Profile> {
if let Some(p) = self.profile_override {
return Ok(p);
}
self.settings_file.with(|s| {
let p = match s.profile {
Some(p) => p,
None => Profile::Default,
};
Ok(p)
})
}
pub(crate) fn get_self_update_mode(&self) -> Result<SelfUpdateMode> {
self.settings_file.with(|s| {
let mode = match &s.auto_self_update {
Some(mode) => mode.clone(),
None => SelfUpdateMode::Enable,
};
Ok(mode)
})
}
pub(crate) fn get_toolchain(&self, name: &str, create_parent: bool) -> Result<Toolchain<'_>> {
if create_parent {
utils::ensure_dir_exists("toolchains", &self.toolchains_dir, &|n| {
(self.notify_handler)(n)
})?;
}
if name.is_empty() {
anyhow::bail!("toolchain names must not be empty");
}
Toolchain::from(self, name)
}
pub(crate) fn get_hash_file(&self, toolchain: &str, create_parent: bool) -> Result<PathBuf> {
if create_parent {
utils::ensure_dir_exists(
"update-hash",
&self.update_hash_dir,
self.notify_handler.as_ref(),
)?;
}
Ok(self.update_hash_dir.join(toolchain))
}
pub(crate) fn which_binary_by_toolchain(
&self,
toolchain_name: &str,
binary: &str,
) -> Result<PathBuf> {
let toolchain = self.get_toolchain(toolchain_name, false)?;
if toolchain.exists() {
Ok(toolchain.binary_file(binary))
} else {
Err(RustupError::ToolchainNotInstalled(toolchain_name.to_string()).into())
}
}
pub(crate) fn which_binary(&self, path: &Path, binary: &str) -> Result<PathBuf> {
let (toolchain, _) = self.find_or_install_override_toolchain_or_default(path)?;
Ok(toolchain.binary_file(binary))
}
pub(crate) fn upgrade_data(&self) -> Result<()> {
let current_version = self.settings_file.with(|s| Ok(s.version.clone()))?;
if current_version == DEFAULT_METADATA_VERSION {
(self.notify_handler)(Notification::MetadataUpgradeNotNeeded(¤t_version));
return Ok(());
}
(self.notify_handler)(Notification::UpgradingMetadata(
¤t_version,
DEFAULT_METADATA_VERSION,
));
match &*current_version {
"2" => {
// The toolchain installation format changed. Just delete them all.
(self.notify_handler)(Notification::UpgradeRemovesToolchains);
let dirs = utils::read_dir("toolchains", &self.toolchains_dir)?;
for dir in dirs {
let dir = dir.context("IO Error reading toolchains")?;
utils::remove_dir("toolchain", &dir.path(), self.notify_handler.as_ref())?;
}
// Also delete the update hashes
let files = utils::read_dir("update hashes", &self.update_hash_dir)?;
for file in files {
let file = file.context("IO Error reading update hashes")?;
utils::remove_file("update hash", &file.path())?;
}
self.settings_file.with_mut(|s| {
s.version = DEFAULT_METADATA_VERSION.to_owned();
Ok(())
})
}
_ => Err(RustupError::UnknownMetadataVersion(current_version).into()),
}
}
pub(crate) fn find_default(&self) -> Result<Option<Toolchain<'_>>> {
let opt_name = self.get_default()?;
if let Some(name) = opt_name {
let toolchain = Toolchain::from(self, &name)?;
Ok(Some(toolchain))
} else {
Ok(None)
}
}
pub(crate) fn find_override(
&self,
path: &Path,
) -> Result<Option<(Toolchain<'_>, OverrideReason)>> {
self.find_override_config(path).map(|opt| {
opt.and_then(|(override_cfg, reason)| {
override_cfg.toolchain.map(|toolchain| (toolchain, reason))
})
})
}
fn find_override_config(
&self,
path: &Path,
) -> Result<Option<(OverrideCfg<'_>, OverrideReason)>> {
let mut override_ = None;
// First check toolchain override from command
if let Some(ref name) = self.toolchain_override {
override_ = Some((name.into(), OverrideReason::CommandLine));
}
// Check RUSTUP_TOOLCHAIN
if let Some(ref name) = self.env_override {
override_ = Some((name.into(), OverrideReason::Environment));
}
// Then walk up the directory tree from 'path' looking for either the
// directory in override database, or a `rust-toolchain` file.
if override_.is_none() {
self.settings_file.with(|s| {
override_ = self.find_override_from_dir_walk(path, s)?;
Ok(())
})?;
}
if let Some((file, reason)) = override_ {
// This is hackishly using the error chain to provide a bit of
// extra context about what went wrong. The CLI will display it
// on a line after the proximate error.
let reason_err = match reason {
OverrideReason::Environment => {
"the RUSTUP_TOOLCHAIN environment variable specifies an uninstalled toolchain"
.to_string()
}
OverrideReason::CommandLine => {
"the +toolchain on the command line specifies an uninstalled toolchain"
.to_string()
}
OverrideReason::OverrideDB(ref path) => format!(
"the directory override for '{}' specifies an uninstalled toolchain",
utils::canonicalize_path(path, self.notify_handler.as_ref()).display(),
),
OverrideReason::ToolchainFile(ref path) => format!(
"the toolchain file at '{}' specifies an uninstalled toolchain",
utils::canonicalize_path(path, self.notify_handler.as_ref()).display(),
),
};
let cfg_file = if let OverrideReason::ToolchainFile(ref path) = reason {
Some(path)
} else {
None
};
let override_cfg = OverrideCfg::from_file(self, cfg_file, file)?;
if let Some(toolchain) = &override_cfg.toolchain {
// Overridden toolchains can be literally any string, but only
// distributable toolchains will be auto-installed by the wrapping
// code; provide a nice error for this common case. (default could
// be set badly too, but that is much less common).
if !toolchain.exists() && toolchain.is_custom() {
// Strip the confusing NotADirectory error and only mention that the
// override toolchain is not installed.
return Err(anyhow!(reason_err)).with_context(|| {
format!("override toolchain '{}' is not installed", toolchain.name())
});
}
}
Ok(Some((override_cfg, reason)))
} else {
Ok(None)
}
}
fn find_override_from_dir_walk(
&self,
dir: &Path,
settings: &Settings,
) -> Result<Option<(OverrideFile, OverrideReason)>> {
let notify = self.notify_handler.as_ref();
let mut dir = Some(dir);
while let Some(d) = dir {
// First check the override database
if let Some(name) = settings.dir_override(d, notify) {
let reason = OverrideReason::OverrideDB(d.to_owned());
return Ok(Some((name.into(), reason)));
}
// Then look for 'rust-toolchain' or 'rust-toolchain.toml'
let path_rust_toolchain = d.join("rust-toolchain");
let path_rust_toolchain_toml = d.join("rust-toolchain.toml");
let (toolchain_file, contents, parse_mode) = match (
utils::read_file("toolchain file", &path_rust_toolchain),
utils::read_file("toolchain file", &path_rust_toolchain_toml),
) {
(contents, Err(_)) => {
// no `rust-toolchain.toml` exists
(path_rust_toolchain, contents, ParseMode::Both)
}
(Err(_), Ok(contents)) => {
// only `rust-toolchain.toml` exists
(path_rust_toolchain_toml, Ok(contents), ParseMode::OnlyToml)
}
(Ok(contents), Ok(_)) => {
// both `rust-toolchain` and `rust-toolchain.toml` exist
notify(Notification::DuplicateToolchainFile {
rust_toolchain: &path_rust_toolchain,
rust_toolchain_toml: &path_rust_toolchain_toml,
});
(path_rust_toolchain, Ok(contents), ParseMode::Both)
}
};
if let Ok(contents) = contents {
let add_file_context = || format!("in {}", toolchain_file.to_string_lossy());
let override_file = Cfg::parse_override_file(contents, parse_mode)
.with_context(add_file_context)?;
if let Some(toolchain_name) = &override_file.toolchain.channel {
let all_toolchains = self.list_toolchains().with_context(add_file_context)?;
if !all_toolchains.iter().any(|s| s == toolchain_name) {
// The given name is not resolvable as a toolchain, so
// instead check it's plausible for installation later
dist::validate_channel_name(toolchain_name)
.with_context(add_file_context)?;
}
}
let reason = OverrideReason::ToolchainFile(toolchain_file);
return Ok(Some((override_file, reason)));
}
dir = d.parent();
}
Ok(None)
}
fn parse_override_file<S: AsRef<str>>(
contents: S,
parse_mode: ParseMode,
) -> Result<OverrideFile> {
let contents = contents.as_ref();
match (contents.lines().count(), parse_mode) {
(0, _) => Err(anyhow!(OverrideFileConfigError::Empty)),
(1, ParseMode::Both) => {
let channel = contents.trim();
if channel.is_empty() {
Err(anyhow!(OverrideFileConfigError::Empty))
} else {
Ok(channel.into())
}
}
_ => {
let override_file = toml::from_str::<OverrideFile>(contents)
.context(OverrideFileConfigError::Parsing)?;
if override_file.is_empty() {
Err(anyhow!(OverrideFileConfigError::Invalid))
} else {
Ok(override_file)
}
}
}
}
pub(crate) fn find_or_install_override_toolchain_or_default(
&self,
path: &Path,
) -> Result<(Toolchain<'_>, Option<OverrideReason>)> {
fn components_exist(
distributable: &DistributableToolchain<'_>,
components: &[&str],
targets: &[&str],
) -> Result<bool> {
let components_requested = !components.is_empty() || !targets.is_empty();
// If we're here, the toolchain exists on disk and is a dist toolchain
// so we should attempt to load its manifest
let desc = if let Some(desc) = distributable.get_toolchain_desc_with_manifest()? {
desc
} else {
// We can't read the manifest. If this is a v1 install that's understandable
// and we assume the components are all good, otherwise we need to have a go
// at re-fetching the manifest to try again.
return Ok(distributable.guess_v1_manifest());
};
match (desc.list_components(), components_requested) {
// If the toolchain does not support components but there were components requested, bubble up the error
(Err(e), true) => Err(e),
// Otherwise check if all the components we want are installed
(Ok(installed_components), _) => Ok(components.iter().all(|name| {
installed_components.iter().any(|status| {
let cname = status.component.short_name(&desc.manifest);
let cname = cname.as_str();
let cnameim = status.component.short_name_in_manifest();
let cnameim = cnameim.as_str();
(cname == *name || cnameim == *name) && status.installed
})
})
// And that all the targets we want are installed
&& targets.iter().all(|name| {
installed_components
.iter()
.filter(|c| c.component.short_name_in_manifest() == "rust-std")
.any(|status| {
let ctarg = status.component.target();
(ctarg == *name) && status.installed
})
})),
_ => Ok(true),
}
}
if let Some((toolchain, components, targets, reason, profile)) =
match self.find_override_config(path)? {
Some((
OverrideCfg {
toolchain,
components,
targets,
profile,
},
reason,
)) => {
let default = if toolchain.is_none() {
self.find_default()?
} else {
None
};
toolchain
.or(default)
.map(|toolchain| (toolchain, components, targets, Some(reason), profile))
}
None => self
.find_default()?
.map(|toolchain| (toolchain, vec![], vec![], None, None)),
}
{
if toolchain.is_custom() {
if !toolchain.exists() {
return Err(
RustupError::ToolchainNotInstalled(toolchain.name().to_string()).into(),
);
}
} else {
let components: Vec<_> = components.iter().map(AsRef::as_ref).collect();
let targets: Vec<_> = targets.iter().map(AsRef::as_ref).collect();
let distributable = DistributableToolchain::new(&toolchain)?;
if !toolchain.exists() || !components_exist(&distributable, &components, &targets)?
{
distributable.install_from_dist(true, false, &components, &targets, profile)?;
}
}
Ok((toolchain, reason))
} else {
// No override and no default set
Err(RustupError::ToolchainNotSelected.into())
}
}
pub(crate) fn get_default(&self) -> Result<Option<String>> {
let user_opt = self.settings_file.with(|s| Ok(s.default_toolchain.clone()));
if let Some(fallback_settings) = &self.fallback_settings {
match user_opt {
Err(_) | Ok(None) => return Ok(fallback_settings.default_toolchain.clone()),
_ => {}
};
};
user_opt
}
pub(crate) fn list_toolchains(&self) -> Result<Vec<String>> {
if utils::is_directory(&self.toolchains_dir) {
let mut toolchains: Vec<_> = utils::read_dir("toolchains", &self.toolchains_dir)?
.filter_map(io::Result::ok)
.filter(|e| e.file_type().map(|f| !f.is_file()).unwrap_or(false))
.filter_map(|e| e.file_name().into_string().ok())
.collect();
utils::toolchain_sort(&mut toolchains);
Ok(toolchains)
} else {
Ok(Vec::new())
}
}
pub(crate) fn list_channels(&self) -> Result<Vec<(String, Result<Toolchain<'_>>)>> {
let toolchains = self.list_toolchains()?;
// Convert the toolchain strings to Toolchain values
let toolchains = toolchains.into_iter();
let toolchains = toolchains.map(|n| (n.clone(), self.get_toolchain(&n, true)));
// Filter out toolchains that don't track a release channel
Ok(toolchains
.filter(|&(_, ref t)| t.as_ref().map(Toolchain::is_tracking).unwrap_or(false))
.collect())
}
pub(crate) fn update_all_channels(
&self,
force_update: bool,
) -> Result<Vec<(String, Result<UpdateStatus>)>> {
let channels = self.list_channels()?;
let channels = channels.into_iter();
// Update toolchains and collect the results
let channels = channels.map(|(n, t)| {
let st = t.and_then(|t| {
let distributable = DistributableToolchain::new(&t)?;
let st = distributable.install_from_dist(force_update, false, &[], &[], None);
if let Err(ref e) = st {
(self.notify_handler)(Notification::NonFatalError(e));
}
st
});
(n, st)
});
Ok(channels.collect())
}
pub(crate) fn check_metadata_version(&self) -> Result<()> {
utils::assert_is_directory(&self.rustup_dir)?;
self.settings_file.with(|s| {
(self.notify_handler)(Notification::ReadMetadataVersion(&s.version));
if s.version == DEFAULT_METADATA_VERSION {
Ok(())
} else {
Err(anyhow!(
"rustup's metadata is out of date. run `rustup self upgrade-data`"
))
}
})
}
pub(crate) fn toolchain_for_dir(
&self,
path: &Path,
) -> Result<(Toolchain<'_>, Option<OverrideReason>)> {
self.find_or_install_override_toolchain_or_default(path)
}
pub(crate) fn create_command_for_dir(&self, path: &Path, binary: &str) -> Result<Command> {
let (ref toolchain, _) = self.toolchain_for_dir(path)?;
if let Some(cmd) = self.maybe_do_cargo_fallback(toolchain, binary)? {
Ok(cmd)
} else {
// NB this can only fail in race conditions since we used toolchain
// for dir.
let installed = toolchain.as_installed_common()?;
installed.create_command(binary)
}
}
pub(crate) fn create_command_for_toolchain(
&self,
toolchain: &str,
install_if_missing: bool,
binary: &str,
) -> Result<Command> {
let toolchain = self.get_toolchain(toolchain, false)?;
if install_if_missing && !toolchain.exists() {
let distributable = DistributableToolchain::new(&toolchain)?;
distributable.install_from_dist(true, false, &[], &[], None)?;
}
if let Some(cmd) = self.maybe_do_cargo_fallback(&toolchain, binary)? {
Ok(cmd)
} else {
// NB note this really can't fail due to to having installed the toolchain if needed
let installed = toolchain.as_installed_common()?;
installed.create_command(binary)
}
}
// Custom toolchains don't have cargo, so here we detect that situation and
// try to find a different cargo.
fn maybe_do_cargo_fallback(
&self,
toolchain: &Toolchain<'_>,
binary: &str,
) -> Result<Option<Command>> {
if !toolchain.is_custom() {
return Ok(None);
}
if binary != "cargo" && binary != "cargo.exe" {
return Ok(None);
}
let cargo_path = toolchain.path().join("bin/cargo");
let cargo_exe_path = toolchain.path().join("bin/cargo.exe");
if cargo_path.exists() || cargo_exe_path.exists() {
return Ok(None);
}
// XXX: This could actually consider all distributable toolchains in principle.
for fallback in &["nightly", "beta", "stable"] {
let fallback = self.get_toolchain(fallback, false)?;
if fallback.exists() {
let distributable = DistributableToolchain::new(&fallback)?;
let cmd = distributable.create_fallback_command("cargo", toolchain)?;
return Ok(Some(cmd));
}
}
Ok(None)
}
pub(crate) fn set_default_host_triple(&self, host_triple: &str) -> Result<()> {
// Ensure that the provided host_triple is capable of resolving
// against the 'stable' toolchain. This provides early errors
// if the supplied triple is insufficient / bad.
dist::PartialToolchainDesc::from_str("stable")?
.resolve(&dist::TargetTriple::new(host_triple))?;
self.settings_file.with_mut(|s| {
s.default_host_triple = Some(host_triple.to_owned());
Ok(())
})
}
pub(crate) fn get_default_host_triple(&self) -> Result<dist::TargetTriple> {
Ok(self
.settings_file
.with(|s| {