cargo_update/ops/mod.rs
1//! Main functions doing actual work.
2//!
3//! Use `installed_registry_packages()` to list the installed packages,
4//! then use `intersect_packages()` to confirm which ones should be updated,
5//! poll the packages' latest versions by calling `RegistryPackage::pull_version()` on them,
6//! continue with doing whatever you wish.
7
8
9use git2::{self, ErrorCode as GitErrorCode, Config as GitConfig, Error as GitError, Cred as GitCred, RemoteCallbacks, CredentialType, FetchOptions,
10 ProxyOptions, Repository, Blob, Tree, Oid};
11use curl::easy::{WriteError as CurlWriteError, Handler as CurlHandler, SslOpt as CurlSslOpt, Easy2 as CurlEasy, List as CurlList};
12use semver::{VersionReq as SemverReq, Version as Semver};
13#[cfg(target_vendor = "apple")]
14use security_framework::os::macos::keychain::SecKeychain;
15#[cfg(target_os = "windows")]
16use windows::Win32::Security::Credentials as WinCred;
17use std::io::{self, ErrorKind as IoErrorKind, BufWriter, BufReader, BufRead, Write};
18use std::collections::{BTreeMap, BTreeSet};
19use std::{slice, cmp, env, mem, str, fs};
20use chrono::{FixedOffset, DateTime, Utc};
21use curl::multi::{Multi as CurlMulti, Easy2Handle as CurlEasyHandle};
22use std::process::{Command, Stdio};
23use std::ffi::{OsString, OsStr};
24use std::path::{PathBuf, Path};
25use std::hash::{Hasher, Hash};
26use std::iter::FromIterator;
27#[cfg(target_os = "windows")]
28use windows::core::PCSTR;
29use std::time::Duration;
30#[cfg(all(unix, not(target_vendor = "apple")))]
31use std::sync::LazyLock;
32use serde_json as json;
33use std::borrow::Cow;
34use std::sync::Mutex;
35#[cfg(any(target_os = "windows", all(unix, not(target_vendor = "apple"))))]
36use std::ptr;
37use url::Url;
38use toml;
39use hex;
40
41mod config;
42
43pub use self::config::*;
44
45
46// cargo-audit 0.17.5 (registry+https://github.com/rust-lang/crates.io-index)
47// cargo-audit 0.17.5 (sparse+https://index.crates.io/)
48// -> (name, version, registry)
49// ("cargo-audit", "0.17.5", "https://github.com/rust-lang/crates.io-index")
50// ("cargo-audit", "0.17.5", "https://index.crates.io/")
51fn parse_registry_package_ident(ident: &str) -> Option<(&str, &str, &str)> {
52 let mut idx = ident.splitn(3, ' ');
53 let (name, version, mut reg) = (idx.next()?, idx.next()?, idx.next()?);
54 reg = reg.strip_prefix('(')?.strip_suffix(')')?;
55 Some((name, version, reg.strip_prefix("registry+").or_else(|| reg.strip_prefix("sparse+"))?))
56}
57// alacritty 0.1.0 (git+https://github.com/jwilm/alacritty#eb231b3e70b87875df4bdd1974d5e94704024d70)
58// chattium-oxide-client 0.1.0
59// (git+https://github.com/nabijaczleweli/chattium-oxide-client?branch=master#108a7b94f0e0dcb2a875f70fc0459d5a682df14c)
60// -> (name, url, sha)
61// ("alacritty", "https://github.com/jwilm/alacritty", "eb231b3e70b87875df4bdd1974d5e94704024d70")
62// ("chattium-oxide-client", "https://github.com/nabijaczleweli/chattium-oxide-client?branch=master",
63// "108a7b94f0e0dcb2a875f70fc0459d5a682df14c")
64fn parse_git_package_ident(ident: &str) -> Option<(&str, &str, &str)> {
65 let mut idx = ident.splitn(3, ' ');
66 let (name, _, blob) = (idx.next()?, idx.next()?, idx.next()?);
67 let (url, sha) = blob.strip_prefix("(git+")?.strip_suffix(')')?.split_once('#')?;
68 if sha.len() != 40 {
69 return None;
70 }
71 Some((name, url, sha))
72}
73
74
75/// A representation of a package from the main [`crates.io`](https://crates.io) repository.
76///
77/// The newest version of a package is pulled from [`crates.io`](https://crates.io) via `pull_version()`.
78///
79/// The `parse()` function parses the format used in `$HOME/.cargo/.crates.toml`.
80///
81/// # Examples
82///
83/// ```
84/// # extern crate cargo_update;
85/// # extern crate semver;
86/// # use cargo_update::ops::RegistryPackage;
87/// # use semver::Version as Semver;
88/// # fn main() {
89/// let package_s = "racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)";
90/// let mut package = RegistryPackage::parse(package_s, vec!["racer.exe".to_string()]).unwrap();
91/// assert_eq!(package,
92/// RegistryPackage {
93/// name: "racer".to_string(),
94/// registry: "https://github.com/rust-lang/crates.io-index".into(),
95/// version: Some(Semver::parse("1.2.10").unwrap()),
96/// newest_version: None,
97/// alternative_version: None,
98/// max_version: None,
99/// executables: vec!["racer.exe".to_string()],
100/// });
101///
102/// # /*
103/// package.pull_version(®istry_tree, ®istry);
104/// # */
105/// # package.newest_version = Some(Semver::parse("1.2.11").unwrap());
106/// assert!(package.newest_version.is_some());
107/// # }
108/// ```
109#[derive(Debug, Clone, Hash, PartialEq, Eq)]
110pub struct RegistryPackage {
111 /// The package's name.
112 ///
113 /// Go to `https://crates.io/crates/{name}` to get the crate info, if available on the main repository.
114 pub name: String,
115 /// The registry the package is available from.
116 ///
117 /// Can be a name from ~/.cargo/config.
118 ///
119 /// The main repository is `https://github.com/rust-lang/crates.io-index`, or `sparse+https://index.crates.io/`.
120 pub registry: Cow<'static, str>,
121 /// The package's locally installed version.
122 pub version: Option<Semver>,
123 /// The latest version of the package, available at [`crates.io`](https://crates.io), if in main repository.
124 ///
125 /// `None` by default, acquire via `RegistryPackage::pull_version()`.
126 pub newest_version: Option<Semver>,
127 /// If present, the alternative newest version not chosen because of unfulfilled requirements like (not) being a prerelease.
128 pub alternative_version: Option<Semver>,
129 /// User-bounded maximum version to update up to.
130 pub max_version: Option<Semver>,
131 /// Executables currently installed for this package.
132 pub executables: Vec<String>,
133}
134
135/// A representation of a package a remote git repository.
136///
137/// The newest commit is pulled from that repo via `pull_version()`.
138///
139/// The `parse()` function parses the format used in `$HOME/.cargo/.crates.toml`.
140///
141/// # Examples
142///
143/// ```
144/// # extern crate cargo_update;
145/// # extern crate git2;
146/// # use cargo_update::ops::GitRepoPackage;
147/// # fn main() {
148/// let package_s = "alacritty 0.1.0 (git+https://github.com/jwilm/alacritty#eb231b3e70b87875df4bdd1974d5e94704024d70)";
149/// let mut package = GitRepoPackage::parse(package_s, vec!["alacritty".to_string()]).unwrap();
150/// assert_eq!(package,
151/// GitRepoPackage {
152/// name: "alacritty".to_string(),
153/// url: "https://github.com/jwilm/alacritty".to_string(),
154/// branch: None,
155/// id: git2::Oid::from_str("eb231b3e70b87875df4bdd1974d5e94704024d70").unwrap(),
156/// newest_id: Err(git2::Error::from_str("")),
157/// executables: vec!["alacritty".to_string()],
158/// });
159///
160/// # /*
161/// package.pull_version(®istry_tree, ®istry);
162/// # */
163/// # package.newest_id = git2::Oid::from_str("5f7885749c4d7e48869b1fc0be4d430601cdbbfa");
164/// assert!(package.newest_id.is_ok());
165/// # }
166/// ```
167#[derive(Debug, PartialEq)]
168pub struct GitRepoPackage {
169 /// The package's name.
170 pub name: String,
171 /// The remote git repo URL.
172 pub url: String,
173 /// The installed branch, or `None` for default.
174 pub branch: Option<String>,
175 /// The package's locally installed version's object hash.
176 pub id: Oid,
177 /// The latest version of the package available at the main [`crates.io`](https://crates.io) repository.
178 ///
179 /// `None` by default, acquire via `GitRepoPackage::pull_version()`.
180 pub newest_id: Result<Oid, GitError>,
181 /// Executables currently installed for this package.
182 pub executables: Vec<String>,
183}
184impl Hash for GitRepoPackage {
185 fn hash<H: Hasher>(&self, state: &mut H) {
186 self.name.hash(state);
187 self.url.hash(state);
188 self.branch.hash(state);
189 self.id.hash(state);
190 match &self.newest_id {
191 Ok(nid) => nid.hash(state),
192 Err(err) => {
193 err.raw_code().hash(state);
194 err.raw_class().hash(state);
195 err.message().hash(state);
196 }
197 }
198 self.executables.hash(state);
199 }
200}
201
202
203impl RegistryPackage {
204 /// Try to decypher a package descriptor into a `RegistryPackage`.
205 ///
206 /// Will return `None` if the given package descriptor is invalid.
207 ///
208 /// In the returned instance, `newest_version` is always `None`, get it via `RegistryPackage::pull_version()`.
209 ///
210 /// The executable list is used as-is.
211 ///
212 /// # Examples
213 ///
214 /// Main repository packages:
215 ///
216 /// ```
217 /// # extern crate cargo_update;
218 /// # extern crate semver;
219 /// # use cargo_update::ops::RegistryPackage;
220 /// # use semver::Version as Semver;
221 /// # fn main() {
222 /// let package_s = "racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)";
223 /// assert_eq!(RegistryPackage::parse(package_s, vec!["racer.exe".to_string()]).unwrap(),
224 /// RegistryPackage {
225 /// name: "racer".to_string(),
226 /// registry: "https://github.com/rust-lang/crates.io-index".into(),
227 /// version: Some(Semver::parse("1.2.10").unwrap()),
228 /// newest_version: None,
229 /// alternative_version: None,
230 /// max_version: None,
231 /// executables: vec!["racer.exe".to_string()],
232 /// });
233 ///
234 /// let package_s = "cargo-outdated 0.2.0 (registry+file:///usr/local/share/cargo)";
235 /// assert_eq!(RegistryPackage::parse(package_s, vec!["cargo-outdated".to_string()]).unwrap(),
236 /// RegistryPackage {
237 /// name: "cargo-outdated".to_string(),
238 /// registry: "file:///usr/local/share/cargo".into(),
239 /// version: Some(Semver::parse("0.2.0").unwrap()),
240 /// newest_version: None,
241 /// alternative_version: None,
242 /// max_version: None,
243 /// executables: vec!["cargo-outdated".to_string()],
244 /// });
245 /// # }
246 /// ```
247 ///
248 /// Git repository:
249 ///
250 /// ```
251 /// # use cargo_update::ops::RegistryPackage;
252 /// let package_s = "treesize 0.2.1 (git+https://github.com/melak47/treesize-rs#v0.2.1)";
253 /// assert!(RegistryPackage::parse(package_s, vec!["treesize".to_string()]).is_none());
254 /// ```
255 pub fn parse(what: &str, executables: Vec<String>) -> Option<RegistryPackage> {
256 parse_registry_package_ident(what).map(|(name, version, registry)| {
257 RegistryPackage {
258 name: name.to_string(),
259 registry: registry.to_string().into(),
260 version: Some(Semver::parse(version).unwrap()),
261 newest_version: None,
262 alternative_version: None,
263 max_version: None,
264 executables: executables,
265 }
266 })
267 }
268
269 fn want_to_install_prerelease(&self, version_to_install: &Semver, install_prereleases: Option<bool>) -> bool {
270 if install_prereleases.unwrap_or(false) {
271 return true;
272 }
273
274 // otherwise only want to install prerelease if the current version is a prerelease with the same maj.min.patch
275 self.version
276 .as_ref()
277 .map(|cur| {
278 cur.is_prerelease() && cur.major == version_to_install.major && cur.minor == version_to_install.minor && cur.patch == version_to_install.patch
279 })
280 .unwrap_or(false)
281 }
282
283 /// Read the version list for this crate off the specified repository tree and set the latest and alternative versions.
284 pub fn pull_version(&mut self, registry: &RegistryTree, registry_parent: &Registry, install_prereleases: Option<bool>,
285 released_after: Option<DateTime<Utc>>) {
286 let mut vers_git;
287 let vers = match (registry, registry_parent) {
288 (RegistryTree::Git(registry), Registry::Git(registry_parent)) => {
289 vers_git = find_package_data(&self.name, registry, registry_parent)
290 .ok_or_else(|| format!("package {} not found", self.name))
291 .and_then(|pd| crate_versions(pd.content()).map_err(|e| format!("package {}: {}", self.name, e)))
292 .unwrap();
293 vers_git.sort();
294 &vers_git
295 }
296 (RegistryTree::Sparse, Registry::Sparse(registry_parent)) => ®istry_parent[&self.name],
297 _ => unreachable!(),
298 };
299
300 self.newest_version = None;
301 self.alternative_version = None;
302
303 let mut vers = vers.iter()
304 .rev()
305 .filter(|(_, dt)| match (dt, released_after) {
306 (_, None) => true,
307 (None, Some(_)) => false,
308 (Some(dt), Some(ra)) => ra > *dt,
309 })
310 .map(|(v, _)| v);
311 if let Some(newest) = vers.next() {
312 self.newest_version = Some(newest.clone());
313
314 if self.newest_version.as_ref().unwrap().is_prerelease() &&
315 !self.want_to_install_prerelease(self.newest_version.as_ref().unwrap(), install_prereleases) {
316 if let Some(newest_nonpre) = vers.find(|v| !v.is_prerelease()) {
317 mem::swap(&mut self.alternative_version, &mut self.newest_version);
318 self.newest_version = Some(newest_nonpre.clone());
319 }
320 }
321 }
322 }
323
324 /// Check whether this package needs to be installed
325 ///
326 /// # Examples
327 ///
328 /// ```
329 /// # extern crate cargo_update;
330 /// # extern crate semver;
331 /// # use semver::{VersionReq as SemverReq, Version as Semver};
332 /// # use cargo_update::ops::RegistryPackage;
333 /// # use std::str::FromStr;
334 /// # fn main() {
335 /// assert!(RegistryPackage {
336 /// name: "racer".to_string(),
337 /// registry: "https://github.com/rust-lang/crates.io-index".into(),
338 /// version: Some(Semver::parse("1.7.2").unwrap()),
339 /// newest_version: Some(Semver::parse("2.0.6").unwrap()),
340 /// alternative_version: None,
341 /// max_version: None,
342 /// executables: vec!["racer".to_string()],
343 /// }.needs_update(None, None, false));
344 /// assert!(RegistryPackage {
345 /// name: "racer".to_string(),
346 /// registry: "https://github.com/rust-lang/crates.io-index".into(),
347 /// version: None,
348 /// newest_version: Some(Semver::parse("2.0.6").unwrap()),
349 /// alternative_version: None,
350 /// max_version: None,
351 /// executables: vec!["racer".to_string()],
352 /// }.needs_update(None, None, false));
353 /// assert!(RegistryPackage {
354 /// name: "racer".to_string(),
355 /// registry: "https://github.com/rust-lang/crates.io-index".into(),
356 /// version: Some(Semver::parse("2.0.7").unwrap()),
357 /// newest_version: Some(Semver::parse("2.0.6").unwrap()),
358 /// alternative_version: None,
359 /// max_version: None,
360 /// executables: vec!["racer".to_string()],
361 /// }.needs_update(None, None, true));
362 /// assert!(!RegistryPackage {
363 /// name: "racer".to_string(),
364 /// registry: "https://github.com/rust-lang/crates.io-index".into(),
365 /// version: Some(Semver::parse("2.0.6").unwrap()),
366 /// newest_version: Some(Semver::parse("2.0.6").unwrap()),
367 /// alternative_version: None,
368 /// max_version: None,
369 /// executables: vec!["racer".to_string()],
370 /// }.needs_update(None, None, false));
371 /// assert!(!RegistryPackage {
372 /// name: "racer".to_string(),
373 /// registry: "https://github.com/rust-lang/crates.io-index".into(),
374 /// version: Some(Semver::parse("2.0.6").unwrap()),
375 /// newest_version: None,
376 /// alternative_version: None,
377 /// max_version: None,
378 /// executables: vec!["racer".to_string()],
379 /// }.needs_update(None, None, false));
380 ///
381 /// let req = SemverReq::from_str("^1.7").unwrap();
382 /// assert!(RegistryPackage {
383 /// name: "racer".to_string(),
384 /// registry: "https://github.com/rust-lang/crates.io-index".into(),
385 /// version: Some(Semver::parse("1.7.2").unwrap()),
386 /// newest_version: Some(Semver::parse("1.7.3").unwrap()),
387 /// alternative_version: None,
388 /// max_version: None,
389 /// executables: vec!["racer".to_string()],
390 /// }.needs_update(Some(&req), None, false));
391 /// assert!(RegistryPackage {
392 /// name: "racer".to_string(),
393 /// registry: "https://github.com/rust-lang/crates.io-index".into(),
394 /// version: None,
395 /// newest_version: Some(Semver::parse("2.0.6").unwrap()),
396 /// alternative_version: None,
397 /// max_version: None,
398 /// executables: vec!["racer".to_string()],
399 /// }.needs_update(Some(&req), None, false));
400 /// assert!(!RegistryPackage {
401 /// name: "racer".to_string(),
402 /// registry: "https://github.com/rust-lang/crates.io-index".into(),
403 /// version: Some(Semver::parse("1.7.2").unwrap()),
404 /// newest_version: Some(Semver::parse("2.0.6").unwrap()),
405 /// alternative_version: None,
406 /// max_version: None,
407 /// executables: vec!["racer".to_string()],
408 /// }.needs_update(Some(&req), None, false));
409 ///
410 /// assert!(!RegistryPackage {
411 /// name: "cargo-audit".to_string(),
412 /// registry: "https://github.com/rust-lang/crates.io-index".into(),
413 /// version: None,
414 /// newest_version: Some(Semver::parse("0.9.0-beta2").unwrap()),
415 /// alternative_version: None,
416 /// max_version: None,
417 /// executables: vec!["racer".to_string()],
418 /// }.needs_update(Some(&req), None, false));
419 /// assert!(RegistryPackage {
420 /// name: "cargo-audit".to_string(),
421 /// registry: "https://github.com/rust-lang/crates.io-index".into(),
422 /// version: None,
423 /// newest_version: Some(Semver::parse("0.9.0-beta2").unwrap()),
424 /// alternative_version: None,
425 /// max_version: None,
426 /// executables: vec!["racer".to_string()],
427 /// }.needs_update(Some(&req), Some(true), false));
428 /// # }
429 /// ```
430 pub fn needs_update(&self, req: Option<&SemverReq>, install_prereleases: Option<bool>, downdate: bool) -> bool {
431 fn criterion(fromver: &Semver, tover: &Semver, downdate: bool) -> bool {
432 if downdate {
433 fromver != tover
434 } else {
435 fromver < tover
436 }
437 }
438
439 let update_to_version = self.update_to_version();
440
441 (req.into_iter().zip(self.version.as_ref()).map(|(sr, cv)| !sr.matches(cv)).next().unwrap_or(true) ||
442 req.into_iter().zip(update_to_version).map(|(sr, uv)| sr.matches(uv)).next().unwrap_or(true)) &&
443 update_to_version.map(|upd_v| {
444 (!upd_v.is_prerelease() || self.want_to_install_prerelease(upd_v, install_prereleases)) &&
445 (self.version.is_none() || criterion(self.version.as_ref().unwrap(), upd_v, downdate))
446 })
447 .unwrap_or(false)
448 }
449
450 /// Get package version to update to, or `None` if the crate has no newest version (was yanked)
451 ///
452 /// # Examples
453 ///
454 /// ```
455 /// # extern crate cargo_update;
456 /// # extern crate semver;
457 /// # use cargo_update::ops::RegistryPackage;
458 /// # use semver::Version as Semver;
459 /// # fn main() {
460 /// assert_eq!(RegistryPackage {
461 /// name: "racer".to_string(),
462 /// registry: "https://github.com/rust-lang/crates.io-index".into(),
463 /// version: Some(Semver::parse("1.7.2").unwrap()),
464 /// newest_version: Some(Semver::parse("2.0.6").unwrap()),
465 /// alternative_version: None,
466 /// max_version: Some(Semver::parse("2.0.5").unwrap()),
467 /// executables: vec!["racer".to_string()],
468 /// }.update_to_version(),
469 /// Some(&Semver::parse("2.0.5").unwrap()));
470 /// assert_eq!(RegistryPackage {
471 /// name: "gutenberg".to_string(),
472 /// registry: "https://github.com/rust-lang/crates.io-index".into(),
473 /// version: Some(Semver::parse("0.0.7").unwrap()),
474 /// newest_version: None,
475 /// alternative_version: None,
476 /// max_version: None,
477 /// executables: vec!["gutenberg".to_string()],
478 /// }.update_to_version(),
479 /// None);
480 /// # }
481 /// ```
482 pub fn update_to_version(&self) -> Option<&Semver> {
483 self.newest_version.as_ref().map(|new_v| cmp::min(new_v, self.max_version.as_ref().unwrap_or(new_v)))
484 }
485}
486
487impl GitRepoPackage {
488 /// Try to decypher a package descriptor into a `GitRepoPackage`.
489 ///
490 /// Will return `None` if:
491 ///
492 /// * the given package descriptor is invalid, or
493 /// * the package descriptor is not from a git repository.
494 ///
495 /// In the returned instance, `newest_version` is always `None`, get it via `GitRepoPackage::pull_version()`.
496 ///
497 /// The executable list is used as-is.
498 ///
499 /// # Examples
500 ///
501 /// Remote git repo packages:
502 ///
503 /// ```
504 /// # extern crate cargo_update;
505 /// # extern crate git2;
506 /// # use cargo_update::ops::GitRepoPackage;
507 /// # fn main() {
508 /// let package_s = "alacritty 0.1.0 (git+https://github.com/jwilm/alacritty#eb231b3e70b87875df4bdd1974d5e94704024d70)";
509 /// assert_eq!(GitRepoPackage::parse(package_s, vec!["alacritty".to_string()]).unwrap(),
510 /// GitRepoPackage {
511 /// name: "alacritty".to_string(),
512 /// url: "https://github.com/jwilm/alacritty".to_string(),
513 /// branch: None,
514 /// id: git2::Oid::from_str("eb231b3e70b87875df4bdd1974d5e94704024d70").unwrap(),
515 /// newest_id: Err(git2::Error::from_str("")),
516 /// executables: vec!["alacritty".to_string()],
517 /// });
518 ///
519 /// let package_s = "chattium-oxide-client 0.1.0 \
520 /// (git+https://github.com/nabijaczleweli/chattium-oxide-client\
521 /// ?branch=master#108a7b94f0e0dcb2a875f70fc0459d5a682df14c)";
522 /// assert_eq!(GitRepoPackage::parse(package_s, vec!["chattium-oxide-client.exe".to_string()]).unwrap(),
523 /// GitRepoPackage {
524 /// name: "chattium-oxide-client".to_string(),
525 /// url: "https://github.com/nabijaczleweli/chattium-oxide-client".to_string(),
526 /// branch: Some("master".to_string()),
527 /// id: git2::Oid::from_str("108a7b94f0e0dcb2a875f70fc0459d5a682df14c").unwrap(),
528 /// newest_id: Err(git2::Error::from_str("")),
529 /// executables: vec!["chattium-oxide-client.exe".to_string()],
530 /// });
531 /// # }
532 /// ```
533 ///
534 /// Main repository package:
535 ///
536 /// ```
537 /// # use cargo_update::ops::GitRepoPackage;
538 /// let package_s = "racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)";
539 /// assert!(GitRepoPackage::parse(package_s, vec!["racer".to_string()]).is_none());
540 /// ```
541 pub fn parse(what: &str, executables: Vec<String>) -> Option<GitRepoPackage> {
542 parse_git_package_ident(what).map(|(name, url, sha)| {
543 let mut url = Url::parse(url).unwrap();
544 let branch = url.query_pairs().find(|&(ref name, _)| name == "branch").map(|(_, value)| value.to_string());
545 url.set_query(None);
546 GitRepoPackage {
547 name: name.to_string(),
548 url: url.into(),
549 branch: branch,
550 id: Oid::from_str(sha).unwrap(),
551 newest_id: Err(GitError::from_str("")),
552 executables: executables,
553 }
554 })
555 }
556
557 /// Clone the repo and check what the latest commit's hash is.
558 pub fn pull_version<Pt: AsRef<Path>, Pg: AsRef<Path>>(&mut self, temp_dir: Pt, git_db_dir: Pg, http_proxy: Option<&str>, fork_git: bool) {
559 self.pull_version_impl(temp_dir.as_ref(), git_db_dir.as_ref(), http_proxy, fork_git)
560 }
561
562 fn pull_version_impl(&mut self, temp_dir: &Path, git_db_dir: &Path, http_proxy: Option<&str>, fork_git: bool) {
563 let clone_dir = find_git_db_repo(git_db_dir, &self.url).unwrap_or_else(|| temp_dir.join(&self.name));
564 if !clone_dir.exists() {
565 self.newest_id = if fork_git {
566 Command::new(env::var_os("GIT").as_ref().map(OsString::as_os_str).unwrap_or(OsStr::new("git")))
567 .args(&["ls-remote", "--", &self.url, self.branch.as_ref().map(String::as_str).unwrap_or("HEAD")])
568 .arg(&clone_dir)
569 .stderr(Stdio::inherit())
570 .output()
571 .ok()
572 .filter(|s| s.status.success())
573 .map(|s| s.stdout)
574 .and_then(|o| String::from_utf8(o).ok())
575 .and_then(|o| o.split('\t').next().and_then(|o| Oid::from_str(o).ok()))
576 .ok_or(GitError::from_str(""))
577 } else {
578 with_authentication(&self.url, |creds| {
579 git2::Remote::create_detached(self.url.clone()).and_then(|mut r| {
580 let mut cb = RemoteCallbacks::new();
581 cb.credentials(|a, b, c| creds(a, b, c));
582 r.connect_auth(git2::Direction::Fetch,
583 Some(cb),
584 http_proxy.map(|http_proxy| proxy_options_from_proxy_url(&self.url, http_proxy)))
585 .and_then(|rc| {
586 rc.list()?
587 .into_iter()
588 .find(|rh| match self.branch.as_ref() {
589 Some(b) => {
590 if rh.name().starts_with("refs/heads/") {
591 rh.name()["refs/heads/".len()..] == b[..]
592 } else if rh.name().starts_with("refs/tags/") {
593 rh.name()["refs/tags/".len()..] == b[..]
594 } else {
595 false
596 }
597 }
598 None => rh.name() == "HEAD",
599 })
600 .map(|rh| rh.oid())
601 .ok_or(git2::Error::from_str(""))
602 })
603 })
604 })
605 };
606 if self.newest_id.is_ok() {
607 return;
608 }
609 }
610
611 let repo = self.pull_version_repo(&clone_dir, http_proxy, fork_git);
612
613 self.newest_id = repo.and_then(|r| r.head().and_then(|h| h.target().ok_or_else(|| GitError::from_str("HEAD not a direct reference"))));
614 }
615
616 fn pull_version_fresh_clone(&self, clone_dir: &Path, http_proxy: Option<&str>, fork_git: bool) -> Result<Repository, GitError> {
617 if fork_git {
618 Command::new(env::var_os("GIT").as_ref().map(OsString::as_os_str).unwrap_or(OsStr::new("git")))
619 .arg("clone")
620 .args(self.branch.as_ref().map(|_| "-b"))
621 .args(self.branch.as_ref())
622 .args(&["--bare", "--", &self.url])
623 .arg(clone_dir)
624 .status()
625 .map_err(|e| GitError::from_str(&e.to_string()))
626 .and_then(|e| if e.success() {
627 Repository::open(clone_dir)
628 } else {
629 Err(GitError::from_str(&e.to_string()))
630 })
631 } else {
632 with_authentication(&self.url, |creds| {
633 let mut bldr = git2::build::RepoBuilder::new();
634
635 let mut cb = RemoteCallbacks::new();
636 cb.credentials(|a, b, c| creds(a, b, c));
637 bldr.fetch_options(fetch_options_from_proxy_url_and_callbacks(&self.url, http_proxy, cb));
638 if let Some(ref b) = self.branch.as_ref() {
639 bldr.branch(b);
640 }
641
642 bldr.bare(true);
643 bldr.clone(&self.url, &clone_dir)
644 })
645 }
646 }
647
648 fn pull_version_repo(&self, clone_dir: &Path, http_proxy: Option<&str>, fork_git: bool) -> Result<Repository, GitError> {
649 if let Ok(r) = Repository::open(clone_dir) {
650 // If `Repository::open` is successful, both `clone_dir` exists *and* points to a valid repository.
651 //
652 // Fetch the specified or default branch, reset it to the remote HEAD.
653
654 let (branch, tofetch) = match self.branch.as_ref() {
655 Some(b) => {
656 // Cargo doesn't point the HEAD at the chosen (via "--branch") branch when installing
657 // https://github.com/nabijaczleweli/cargo-update/issues/143
658 r.set_head(&format!("refs/heads/{}", b)).map_err(|e| panic!("Couldn't set HEAD to chosen branch {}: {}", b, e)).unwrap();
659 (Cow::from(b), Cow::from(b))
660 }
661
662 None => {
663 match r.find_reference("HEAD")
664 .map_err(|e| panic!("No HEAD in {}: {}", clone_dir.display(), e))
665 .unwrap()
666 .symbolic_target() {
667 Some(ht) => (ht["refs/heads/".len()..].to_string().into(), "+HEAD:refs/remotes/origin/HEAD".into()),
668 None => {
669 // Versions up to v4.0.0 (well, 59be1c0de283dabce320a860a3d533d00910a6a9, but who's counting)
670 // called r.set_head("FETCH_HEAD"), which made HEAD a direct SHA reference.
671 // This is obviously problematic when trying to read the default branch, and these checkouts can persist
672 // (https://github.com/nabijaczleweli/cargo-update/issues/139#issuecomment-665847290);
673 // yeeting them shouldn't be a problem, since that's what we *would* do anyway,
674 // and we set up for the non-pessimised path in later runs.
675 fs::remove_dir_all(clone_dir).unwrap();
676 return self.pull_version_fresh_clone(clone_dir, http_proxy, fork_git);
677 }
678 }
679
680 }
681 };
682
683 let mut remote = "origin";
684 r.find_remote("origin")
685 .or_else(|_| {
686 remote = &self.url;
687 r.remote_anonymous(&self.url)
688 })
689 .and_then(|mut rm| if fork_git {
690 Command::new(env::var_os("GIT").as_ref().map(OsString::as_os_str).unwrap_or(OsStr::new("git")))
691 .arg("-C")
692 .arg(r.path())
693 .args(&["fetch", remote, &tofetch])
694 .status()
695 .map_err(|e| GitError::from_str(&e.to_string()))
696 .and_then(|e| if e.success() {
697 Ok(())
698 } else {
699 Err(GitError::from_str(&e.to_string()))
700 })
701 } else {
702 with_authentication(&self.url, |creds| {
703 let mut cb = RemoteCallbacks::new();
704 cb.credentials(|a, b, c| creds(a, b, c));
705
706 rm.fetch(&[&tofetch[..]],
707 Some(&mut fetch_options_from_proxy_url_and_callbacks(&self.url, http_proxy, cb)),
708 None)
709 })
710 })
711 .map_err(|e| panic!("Fetching {} from {}: {}", clone_dir.display(), self.url, e))
712 .unwrap();
713 r.branch(&branch,
714 &r.find_reference("FETCH_HEAD")
715 .map_err(|e| panic!("No FETCH_HEAD in {}: {}", clone_dir.display(), e))
716 .unwrap()
717 .peel_to_commit()
718 .map_err(|e| panic!("FETCH_HEAD not a commit in {}: {}", clone_dir.display(), e))
719 .unwrap(),
720 true)
721 .map_err(|e| panic!("Setting local branch {} in {}: {}", branch, clone_dir.display(), e))
722 .unwrap();
723 Ok(r)
724 } else {
725 // If we could not open the repository either it does not exist, or exists but is invalid,
726 // in which case remove it to trigger a fresh clone.
727 let _ = fs::remove_dir_all(&clone_dir).or_else(|_| fs::remove_file(&clone_dir));
728
729 self.pull_version_fresh_clone(clone_dir, http_proxy, fork_git)
730 }
731 }
732
733 /// Check whether this package needs to be installed
734 ///
735 /// # Examples
736 ///
737 /// ```
738 /// # extern crate cargo_update;
739 /// # extern crate git2;
740 /// # use cargo_update::ops::GitRepoPackage;
741 /// # fn main() {
742 /// assert!(GitRepoPackage {
743 /// name: "alacritty".to_string(),
744 /// url: "https://github.com/jwilm/alacritty".to_string(),
745 /// branch: None,
746 /// id: git2::Oid::from_str("eb231b3e70b87875df4bdd1974d5e94704024d70").unwrap(),
747 /// newest_id: git2::Oid::from_str("5f7885749c4d7e48869b1fc0be4d430601cdbbfa"),
748 /// executables: vec!["alacritty".to_string()],
749 /// }.needs_update());
750 /// assert!(!GitRepoPackage {
751 /// name: "alacritty".to_string(),
752 /// url: "https://github.com/jwilm/alacritty".to_string(),
753 /// branch: None,
754 /// id: git2::Oid::from_str("5f7885749c4d7e48869b1fc0be4d430601cdbbfa").unwrap(),
755 /// newest_id: git2::Oid::from_str("5f7885749c4d7e48869b1fc0be4d430601cdbbfa"),
756 /// executables: vec!["alacritty".to_string()],
757 /// }.needs_update());
758 /// # }
759 /// ```
760 pub fn needs_update(&self) -> bool {
761 self.newest_id.is_ok() && self.id != *self.newest_id.as_ref().unwrap()
762 }
763}
764
765
766/// One of elements with which to filter required packages.
767#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
768pub struct PackageFilterElement(pub bool, pub PackageFilterElementValue);
769#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
770pub enum PackageFilterElementValue {
771 /// Requires toolchain to be specified to the specified toolchain.
772 ///
773 /// Parsed name: `"toolchain"`.
774 Toolchain(String),
775 /// Matches package with name matching this glob.
776 ///
777 /// Parsed name: `"name"`. Glob parsed with `str::split('*')`.
778 Name(Vec<String>),
779}
780
781impl PackageFilterElement {
782 /// Parse one filter specifier into up to one package filter
783 ///
784 /// # Examples
785 ///
786 /// ```
787 /// # use cargo_update::ops::{PackageFilterElement, PackageFilterElementValue};
788 /// assert_eq!(PackageFilterElement::parse("toolchain=nightly"),
789 /// Ok(PackageFilterElement(false, PackageFilterElementValue::Toolchain("nightly".to_string()))));
790 /// assert_eq!(PackageFilterElement::parse("!name=cargo*"),
791 /// Ok(PackageFilterElement(true, PackageFilterElementValue::Name(vec!["cargo".to_string(), "".to_string()]))));
792 ///
793 /// assert!(PackageFilterElement::parse("capitalism").is_err());
794 /// assert!(PackageFilterElement::parse("communism=good").is_err());
795 /// ```
796 pub fn parse(from: &str) -> Result<PackageFilterElement, String> {
797 let (negate, from) = match from.strip_prefix("!") {
798 Some(from) => (true, from),
799 None => (false, from),
800 };
801 let (key, value) = from.split_at(from.find('=').ok_or_else(|| format!(r#"Filter string "{}" does not contain the key/value separator "=""#, from))?);
802 let value = &value[1..];
803
804 Ok(PackageFilterElement(negate,
805 match key {
806 "toolchain" => PackageFilterElementValue::Toolchain(value.to_string()),
807 "name" => PackageFilterElementValue::Name(value.split('*').map(str::to_string).collect()),
808 _ => return Err(format!(r#"Unrecognised filter key "{}""#, key)),
809 }))
810 }
811
812 /// Check if the specified package config matches this filter element.
813 ///
814 /// # Examples
815 ///
816 /// ```
817 /// # use cargo_update::ops::{PackageFilterElement, PackageFilterElementValue, ConfigOperation, PackageConfig};
818 /// assert!(PackageFilterElement(false, PackageFilterElementValue::Toolchain("nightly".to_string()))
819 /// .matches("treesize", &PackageConfig::from(&[ConfigOperation::SetToolchain("nightly".to_string())])));
820 ///
821 /// assert!(!PackageFilterElement(false, PackageFilterElementValue::Toolchain("nightly".to_string()))
822 /// .matches("treesize", &PackageConfig::from(&[])));
823 ///
824 /// let notcargo = PackageFilterElement::parse("!name=cargo*").unwrap();
825 /// assert!(notcargo.matches("treesize", &PackageConfig::from(&[])));
826 /// assert!(!notcargo.matches("cargo-update", &PackageConfig::from(&[])));
827 /// ```
828 pub fn matches(&self, name: &str, cfg: &PackageConfig) -> bool {
829 self.0 ^
830 match self.1 {
831 PackageFilterElementValue::Toolchain(ref chain) => Some(chain) == cfg.toolchain.as_ref(),
832 PackageFilterElementValue::Name(ref glob) => {
833 match &glob[..] {
834 [] => true /* unreachable */,
835 [exact] => exact == name,
836 [front, back] => name.starts_with(front) && name.ends_with(back),
837 [front, chunks @ .., back] => matchglob(name, front, chunks, back),
838 }
839 }
840 }
841 }
842}
843
844fn matchglob(name: &str, front: &str, glob: &[String], back: &str) -> bool {
845 let Some(name) = name.strip_prefix(front) else { return false };
846 let Some(mut name) = name.strip_suffix(back) else { return false };
847 for chunk in glob {
848 let Some(idx) = name.find(chunk) else { return false };
849 name = &name[idx + chunk.len()..];
850 }
851 true
852}
853
854/// `cargo` configuration, as obtained from `.cargo/config[.toml]`
855#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
856pub struct CargoConfig {
857 pub net_git_fetch_with_cli: bool,
858 /// https://blog.rust-lang.org/2023/03/09/Rust-1.68.0.html#cargos-sparse-protocol
859 /// https://doc.rust-lang.org/stable/cargo/reference/registry-index.html#sparse-protocol
860 pub registries_crates_io_protocol_sparse: bool,
861 pub http: HttpCargoConfig,
862 pub sparse_registries: SparseRegistryConfig,
863}
864
865#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Default)]
866pub struct HttpCargoConfig {
867 pub cainfo: Option<PathBuf>,
868 pub check_revoke: bool,
869}
870
871/// https://github.com/nabijaczleweli/cargo-update/issues/300
872#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
873pub struct SparseRegistryConfig {
874 pub global_credential_providers: Vec<SparseRegistryAuthProvider>,
875 pub crates_io_credential_provider: Option<SparseRegistryAuthProvider>,
876 pub crates_io_token_env: Option<String>,
877 pub crates_io_token: Option<String>,
878 pub registry_tokens_env: BTreeMap<CargoConfigEnvironmentNormalisedString, String>,
879 pub registry_tokens: BTreeMap<String, String>,
880 pub credential_aliases: BTreeMap<CargoConfigEnvironmentNormalisedString, Vec<String>>,
881}
882
883impl SparseRegistryConfig {
884 pub fn credential_provider(&self, v: toml::Value) -> Option<SparseRegistryAuthProvider> {
885 SparseRegistryConfig::credential_provider_impl(&self.credential_aliases, v)
886 }
887
888 fn credential_provider_impl(credential_aliases: &BTreeMap<CargoConfigEnvironmentNormalisedString, Vec<String>>, v: toml::Value)
889 -> Option<SparseRegistryAuthProvider> {
890 match v {
891 toml::Value::String(s) => Some(CargoConfig::string_provider(s, &credential_aliases)),
892 toml::Value::Array(a) => Some(SparseRegistryAuthProvider::from_config(CargoConfig::string_array(a))),
893 _ => None,
894 }
895 }
896}
897
898/// https://doc.rust-lang.org/cargo/reference/registry-authentication.html
899#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
900pub enum SparseRegistryAuthProvider {
901 /// The default; does not read `CARGO_REGISTRY_TOKEN` or `CARGO_REGISTRIES_{}_TOKEN` environment variables.
902 TokenNoEnvironment,
903 /// `cargo:token`
904 Token,
905 /// `cargo:wincred` (Win32)
906 Wincred,
907 /// `cargo:macos-keychain` (Apple)
908 MacosKeychain,
909 /// `cargo:libsecret`; this `dlopen()`s `libsecret-1.so.0` on non-Apple UNIX.
910 Libsecret,
911 /// `cargo:token-from-stdout prog arg arg`
912 TokenFromStdout(Vec<String>),
913 /// Not `cargo:`-prefixed
914 ///
915 /// https://doc.rust-lang.org/cargo/reference/credential-provider-protocol.html
916 ///
917 /// We do *not* care about `"cache"`, `"expiration"`, or `"operation_independent"`,
918 /// always behaving as-if `"never"`/`_`/`true`.
919 ///
920 /// We don't provide the optional `{"registry": {"headers": ...}}` field.
921 Provider(Vec<String>),
922}
923
924impl SparseRegistryAuthProvider {
925 /// Parses a `["cargo:token-from-stdout", "whatever"]`-style entry
926 pub fn from_config(mut toks: Vec<String>) -> SparseRegistryAuthProvider {
927 match toks.get(0).map(String::as_str).unwrap_or("") {
928 "cargo:token" => SparseRegistryAuthProvider::Token,
929 "cargo:wincred" => SparseRegistryAuthProvider::Wincred,
930 "cargo:macos-keychain" => SparseRegistryAuthProvider::MacosKeychain,
931 "cargo:libsecret" => SparseRegistryAuthProvider::Libsecret,
932 "cargo:token-from-stdout" => {
933 toks.remove(0);
934 SparseRegistryAuthProvider::TokenFromStdout(toks)
935 }
936 _ => SparseRegistryAuthProvider::Provider(toks),
937 }
938 }
939}
940
941/// https://doc.rust-lang.org/cargo/reference/config.html#environment-variables
942#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
943pub struct CargoConfigEnvironmentNormalisedString(pub String);
944impl CargoConfigEnvironmentNormalisedString {
945 /// `tr a-z.- A-Z__`
946 pub fn normalise(mut s: String) -> CargoConfigEnvironmentNormalisedString {
947 s.make_ascii_uppercase();
948 while let Some(i) = s.find(['.', '-']) {
949 s.replace_range(i..i + 1, "_");
950 }
951 CargoConfigEnvironmentNormalisedString(s)
952 }
953}
954
955impl CargoConfig {
956 pub fn load(crates_file: &Path) -> CargoConfig {
957 let mut cfg = fs::read_to_string(crates_file.with_file_name("config"))
958 .or_else(|_| fs::read_to_string(crates_file.with_file_name("config.toml")))
959 .ok()
960 .and_then(|s| s.parse::<toml::Value>().ok());
961 let mut creds = fs::read_to_string(crates_file.with_file_name("credentials"))
962 .or_else(|_| fs::read_to_string(crates_file.with_file_name("credentials.toml")))
963 .ok()
964 .and_then(|s| s.parse::<toml::Value>().ok());
965
966 let credential_aliases = None.or_else(|| match cfg.as_mut()?.as_table_mut()?.remove("credential-alias")? {
967 toml::Value::Table(t) => Some(t),
968 _ => None,
969 })
970 .unwrap_or_default()
971 .into_iter()
972 .flat_map(|(k, v)| {
973 match v {
974 toml::Value::String(s) => Some(s.split(' ').map(String::from).collect()),
975 toml::Value::Array(a) => Some(CargoConfig::string_array(a)),
976 _ => None,
977 }
978 .map(|v| (CargoConfigEnvironmentNormalisedString::normalise(k), v))
979 })
980 .chain(env::vars_os()
981 .map(|(k, v)| (k.into_encoded_bytes(), v))
982 .filter(|(k, _)| k.starts_with(b"CARGO_CREDENTIAL_ALIAS_"))
983 .filter(|(k, _)| k["CARGO_CREDENTIAL_ALIAS_".len()..].iter().all(|&b| !(b.is_ascii_lowercase() || b == b'.' || b == b'-')))
984 .flat_map(|(mut k, v)| {
985 let k = String::from_utf8(k.drain("CARGO_CREDENTIAL_ALIAS_".len()..).collect()).ok()?;
986 let v = v.into_string().ok()?;
987 Some((CargoConfigEnvironmentNormalisedString(k), v.split(' ').map(String::from).collect()))
988 }))
989 .collect();
990
991 CargoConfig {
992 net_git_fetch_with_cli: env::var("CARGO_NET_GIT_FETCH_WITH_CLI")
993 .ok()
994 .and_then(|e| if e.is_empty() {
995 Some(toml::Value::String(String::new()))
996 } else {
997 e.parse::<toml::Value>().ok()
998 })
999 .or_else(|| {
1000 cfg.as_mut()?
1001 .as_table_mut()?
1002 .get_mut("net")?
1003 .as_table_mut()?
1004 .remove("git-fetch-with-cli")
1005 })
1006 .map(CargoConfig::truthy)
1007 .unwrap_or(false),
1008 registries_crates_io_protocol_sparse: env::var("CARGO_REGISTRIES_CRATES_IO_PROTOCOL")
1009 .map(|s| s == "sparse")
1010 .ok()
1011 .or_else(|| {
1012 Some(cfg.as_mut()?
1013 .as_table_mut()?
1014 .get_mut("registries")?
1015 .as_table_mut()?
1016 .get_mut("crates-io")?
1017 .as_table_mut()?
1018 .remove("protocol")?
1019 .as_str()? == "sparse")
1020 })
1021 // // Horrifically expensive (82-93ms end-to-end) and largely unnecessary
1022 // .or_else(|| {
1023 // let mut l = String::new();
1024 // // let before = std::time::Instant::now();
1025 // BufReader::new(Command::new(cargo).arg("version").stdout(Stdio::piped()).spawn().ok()?.stdout?).read_line(&mut l).ok()?;
1026 // // let after = std::time::Instant::now();
1027 //
1028 // // cargo 1.63.0 (fd9c4297c 2022-07-01)
1029 // Some(Semver::parse(l.split_whitespace().nth(1)?).ok()? >= Semver::new(1, 70, 0))
1030 // })
1031 // .unwrap_or(false),
1032 .unwrap_or(true),
1033 http: HttpCargoConfig {
1034 cainfo: env::var_os("CARGO_HTTP_CAINFO")
1035 .map(PathBuf::from)
1036 .or_else(|| {
1037 CargoConfig::string(cfg.as_mut()?
1038 .as_table_mut()?
1039 .get_mut("http")?
1040 .as_table_mut()?
1041 .remove("cainfo")?)
1042 .map(PathBuf::from)
1043 }),
1044 check_revoke: env::var("CARGO_HTTP_CHECK_REVOKE")
1045 .ok()
1046 .map(toml::Value::String)
1047 .or_else(|| {
1048 cfg.as_mut()?
1049 .as_table_mut()?
1050 .get_mut("http")?
1051 .as_table_mut()?
1052 .remove("check-revoke")
1053 })
1054 .map(CargoConfig::truthy)
1055 .unwrap_or(cfg!(target_os = "windows")),
1056 },
1057 sparse_registries: SparseRegistryConfig {
1058 // Supposedly this is CARGO_REGISTRY_GLOBAL_CREDENTIAL_PROVIDERS but they don't specify how they serialise arrays so
1059 global_credential_providers: None.or_else(|| {
1060 CargoConfig::string_array_v(cfg.as_mut()?
1061 .as_table_mut()?
1062 .get_mut("registry")?
1063 .as_table_mut()?
1064 .remove("global-credential-providers")?)
1065 })
1066 .map(|a| a.into_iter().map(|s| CargoConfig::string_provider(s, &credential_aliases)).collect())
1067 .unwrap_or_else(|| vec![SparseRegistryAuthProvider::TokenNoEnvironment]),
1068 crates_io_credential_provider: env::var("CARGO_REGISTRY_CREDENTIAL_PROVIDER")
1069 .ok()
1070 .map(toml::Value::String)
1071 .or_else(|| {
1072 cfg.as_mut()?
1073 .as_table_mut()?
1074 .get_mut("registry")?
1075 .as_table_mut()?
1076 .remove("credential-provider")
1077 })
1078 .and_then(|v| SparseRegistryConfig::credential_provider_impl(&credential_aliases, v)),
1079 crates_io_token_env: env::var("CARGO_REGISTRY_TOKEN").ok(),
1080 crates_io_token: None.or_else(|| {
1081 CargoConfig::string(creds.as_mut()?
1082 .as_table_mut()?
1083 .get_mut("registry")?
1084 .as_table_mut()?
1085 .remove("token")?)
1086 })
1087 .or_else(|| {
1088 CargoConfig::string(cfg.as_mut()?
1089 .as_table_mut()?
1090 .get_mut("registry")?
1091 .as_table_mut()?
1092 .remove("token")?)
1093 }),
1094 registry_tokens_env: env::vars_os()
1095 .map(|(k, v)| (k.into_encoded_bytes(), v))
1096 .filter(|(k, _)| k.starts_with(b"CARGO_REGISTRIES_") && k.ends_with(b"_TOKEN"))
1097 .filter(|(k, _)| {
1098 k["CARGO_REGISTRIES_".len()..k.len() - b"_TOKEN".len()].iter().all(|&b| !(b.is_ascii_lowercase() || b == b'.' || b == b'-'))
1099 })
1100 .flat_map(|(mut k, v)| {
1101 let k = String::from_utf8(k.drain("CARGO_REGISTRIES_".len()..k.len() - b"_TOKEN".len()).collect()).ok()?;
1102 Some((CargoConfigEnvironmentNormalisedString(k), v.into_string().ok()?))
1103 })
1104 .collect(),
1105 registry_tokens: cfg.as_mut()
1106 .into_iter()
1107 .chain(creds.as_mut())
1108 .flat_map(|c| {
1109 c.as_table_mut()?
1110 .get_mut("registries")?
1111 .as_table_mut()
1112 })
1113 .flat_map(|r| r.into_iter().flat_map(|(name, v)| Some((name.clone(), CargoConfig::string(v.as_table_mut()?.remove("token")?)?))))
1114 .collect(),
1115 credential_aliases: credential_aliases,
1116 },
1117 }
1118 }
1119
1120 fn truthy(v: toml::Value) -> bool {
1121 match v {
1122 toml::Value::String(ref s) if s == "" => false,
1123 toml::Value::Float(0.) => false,
1124 toml::Value::Integer(0) |
1125 toml::Value::Boolean(false) => false,
1126 _ => true,
1127 }
1128 }
1129
1130 fn string(v: toml::Value) -> Option<String> {
1131 match v {
1132 toml::Value::String(s) => Some(s),
1133 _ => None,
1134 }
1135 }
1136
1137 fn string_array(a: Vec<toml::Value>) -> Vec<String> {
1138 a.into_iter().flat_map(CargoConfig::string).collect()
1139 }
1140
1141 fn string_array_v(v: toml::Value) -> Option<Vec<String>> {
1142 match v {
1143 toml::Value::Array(s) => Some(CargoConfig::string_array(s)),
1144 _ => None,
1145 }
1146 }
1147
1148 fn string_provider(s: String, credential_aliases: &BTreeMap<CargoConfigEnvironmentNormalisedString, Vec<String>>) -> SparseRegistryAuthProvider {
1149 match credential_aliases.get(&CargoConfigEnvironmentNormalisedString::normalise(s.clone())) {
1150 Some(av) => SparseRegistryAuthProvider::Provider(av.clone()),
1151 None => {
1152 SparseRegistryAuthProvider::from_config(if s.contains(' ') {
1153 s.split(' ').map(String::from).collect()
1154 } else {
1155 vec![s]
1156 })
1157 }
1158 }
1159 }
1160}
1161
1162
1163/// [Follow `install.root`](https://github.com/nabijaczleweli/cargo-update/issues/23) in the `config` or `config.toml` file
1164/// in the cargo directory specified.
1165///
1166/// # Examples
1167///
1168/// ```
1169/// # use cargo_update::ops::crates_file_in;
1170/// # use std::env::temp_dir;
1171/// # let cargo_dir = temp_dir();
1172/// let cargo_dir = crates_file_in(&cargo_dir);
1173/// # let _ = cargo_dir;
1174/// ```
1175pub fn crates_file_in(cargo_dir: &Path) -> PathBuf {
1176 crates_file_in_impl(cargo_dir, BTreeSet::new())
1177}
1178fn crates_file_in_impl<'cd>(cargo_dir: &'cd Path, mut seen: BTreeSet<&'cd Path>) -> PathBuf {
1179 if !seen.insert(cargo_dir) {
1180 panic!("Cargo config install.root loop at {:?} (saw {:?})", cargo_dir.display(), seen);
1181 }
1182
1183 let mut config_file = cargo_dir.join("config");
1184 let mut config_data = fs::read_to_string(&config_file);
1185 if config_data.is_err() {
1186 config_file.set_file_name("config.toml");
1187 config_data = fs::read_to_string(&config_file);
1188 }
1189 if let Ok(config_data) = config_data {
1190 if let Some(idir) = toml::from_str::<toml::Value>(&config_data)
1191 .unwrap()
1192 .get("install")
1193 .and_then(|t| t.as_table())
1194 .and_then(|t| t.get("root"))
1195 .and_then(|t| t.as_str()) {
1196 return crates_file_in_impl(Path::new(idir), seen);
1197 }
1198 }
1199
1200 config_file.set_file_name(".crates.toml");
1201 config_file
1202}
1203
1204fn installed_packages_table(crates_file: &Path) -> Option<toml::Table> {
1205 let crates_data = fs::read_to_string(crates_file).ok()?;
1206 Some(toml::from_str::<toml::Value>(&crates_data).unwrap().get_mut("v1")?.as_table_mut().map(mem::take).unwrap())
1207}
1208
1209/// List the installed packages at the specified location that originate
1210/// from the a cargo registry.
1211///
1212/// If the `.crates.toml` file doesn't exist an empty vector is returned.
1213///
1214/// This also deduplicates packages and assumes the latest version as the correct one to work around
1215/// [#44](https://github.com/nabijaczleweli/cargo-update/issues/44) a.k.a.
1216/// [rust-lang/cargo#4321](https://github.com/rust-lang/cargo/issues/4321).
1217///
1218/// # Examples
1219///
1220/// ```
1221/// # use cargo_update::ops::installed_registry_packages;
1222/// # use std::env::temp_dir;
1223/// # let cargo_dir = temp_dir().join(".crates.toml");
1224/// let packages = installed_registry_packages(&cargo_dir);
1225/// for package in &packages {
1226/// println!("{} v{}", package.name, package.version.as_ref().unwrap());
1227/// }
1228/// ```
1229pub fn installed_registry_packages(crates_file: &Path) -> Vec<RegistryPackage> {
1230 let mut res = Vec::<RegistryPackage>::new();
1231 for pkg in installed_packages_table(crates_file)
1232 .into_iter()
1233 .flatten()
1234 .flat_map(|(s, x)| CargoConfig::string_array_v(x).and_then(|x| RegistryPackage::parse(&s, x))) {
1235 if let Some(saved) = res.iter_mut().find(|p| p.name == pkg.name) {
1236 if saved.version.is_none() || saved.version.as_ref().unwrap() < pkg.version.as_ref().unwrap() {
1237 saved.version = pkg.version;
1238 }
1239 continue;
1240 }
1241
1242 res.push(pkg);
1243 }
1244 res
1245}
1246
1247/// List the installed packages at the specified location that originate
1248/// from a remote git repository.
1249///
1250/// If the `.crates.toml` file doesn't exist an empty vector is returned.
1251///
1252/// This also deduplicates packages and assumes the latest-mentioned version as the most correct.
1253///
1254/// # Examples
1255///
1256/// ```
1257/// # use cargo_update::ops::installed_git_repo_packages;
1258/// # use std::env::temp_dir;
1259/// # let cargo_dir = temp_dir().join(".crates.toml");
1260/// let packages = installed_git_repo_packages(&cargo_dir);
1261/// for package in &packages {
1262/// println!("{} v{}", package.name, package.id);
1263/// }
1264/// ```
1265pub fn installed_git_repo_packages(crates_file: &Path) -> Vec<GitRepoPackage> {
1266 let mut res = Vec::<GitRepoPackage>::new();
1267 for pkg in installed_packages_table(crates_file)
1268 .into_iter()
1269 .flatten()
1270 .flat_map(|(s, x)| CargoConfig::string_array_v(x).and_then(|x| GitRepoPackage::parse(&s, x))) {
1271 if let Some(saved) = res.iter_mut().find(|p| p.name == pkg.name) {
1272 saved.id = pkg.id;
1273 continue;
1274 }
1275
1276 res.push(pkg);
1277 }
1278 res
1279}
1280
1281/// Filter out the installed packages not specified to be updated and add the packages you specify to install,
1282/// if they aren't already installed via git.
1283///
1284/// List installed packages with `installed_registry_packages()`.
1285///
1286/// # Examples
1287///
1288/// ```
1289/// # use cargo_update::ops::{RegistryPackage, intersect_packages};
1290/// # fn installed_registry_packages(_: &()) {}
1291/// # let cargo_dir = ();
1292/// # let packages_to_update = [("racer".to_string(), None,
1293/// # "registry+https://github.com/rust-lang/crates.io-index".into()),
1294/// # ("cargo-outdated".to_string(), None,
1295/// # "registry+https://github.com/rust-lang/crates.io-index".into())];
1296/// let mut installed_packages = installed_registry_packages(&cargo_dir);
1297/// # let mut installed_packages =
1298/// # vec![RegistryPackage::parse("cargo-outdated 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
1299/// # vec!["cargo-outdated".to_string()]).unwrap(),
1300/// # RegistryPackage::parse("racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
1301/// # vec!["racer.exe".to_string()]).unwrap(),
1302/// # RegistryPackage::parse("rustfmt 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
1303/// # vec!["rustfmt".to_string(), "cargo-format".to_string()]).unwrap()];
1304/// installed_packages = intersect_packages(&installed_packages, &packages_to_update, false, &[]);
1305/// # assert_eq!(&installed_packages,
1306/// # &[RegistryPackage::parse("cargo-outdated 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
1307/// # vec!["cargo-outdated".to_string()]).unwrap(),
1308/// # RegistryPackage::parse("racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
1309/// # vec!["racer.exe".to_string()]).unwrap()]);
1310/// ```
1311pub fn intersect_packages(installed: &[RegistryPackage], to_update: &[(String, Option<Semver>, Cow<'static, str>)], allow_installs: bool,
1312 installed_git: &[GitRepoPackage])
1313 -> Vec<RegistryPackage> {
1314 installed.iter()
1315 .filter(|p| to_update.iter().any(|u| p.name == u.0))
1316 .cloned()
1317 .map(|p| RegistryPackage { max_version: to_update.iter().find(|u| p.name == u.0).and_then(|u| u.1.clone()), ..p })
1318 .chain(to_update.iter()
1319 .filter(|p| allow_installs && !installed.iter().any(|i| i.name == p.0) && !installed_git.iter().any(|i| i.name == p.0))
1320 .map(|p| {
1321 RegistryPackage {
1322 name: p.0.clone(),
1323 registry: p.2.clone(),
1324 version: None,
1325 newest_version: None,
1326 alternative_version: None,
1327 max_version: p.1.clone(),
1328 executables: vec![],
1329 }
1330 }))
1331 .collect()
1332}
1333
1334/// Parse the raw crate descriptor from the repository into a collection of `Semver`s and publication times (if present).
1335///
1336/// # Examples
1337///
1338/// ```
1339/// # use cargo_update::ops::crate_versions;
1340/// # use std::fs;
1341/// # let desc_path = "test-data/checksums-versions.json";
1342/// # let package = "checksums";
1343/// let versions = crate_versions(&fs::read(desc_path).unwrap()).expect(package);
1344///
1345/// println!("Released versions of checksums:");
1346/// for (ver, pubtime) in &versions {
1347/// match pubtime {
1348/// None => println!(" {}", ver),
1349/// Some(pubtime) => println!(" {} ({})", ver, pubtime),
1350/// }
1351/// }
1352/// ```
1353pub fn crate_versions(buf: &[u8]) -> Result<Vec<(Semver, Option<DateTime<FixedOffset>>)>, Cow<'static, str>> {
1354 buf.split_inclusive(|&b| b == b'\n').map(crate_version_line).flat_map(Result::transpose).collect()
1355}
1356fn crate_version_line(line: &[u8]) -> Result<Option<(Semver, Option<DateTime<FixedOffset>>)>, Cow<'static, str>> {
1357 if line == b"\n" {
1358 return Ok(None);
1359 }
1360 match json::from_slice(line).map_err(|e| e.to_string())? {
1361 json::Value::Object(o) => {
1362 if matches!(o.get("yanked"), Some(&json::Value::Bool(true))) {
1363 return Ok(None);
1364 }
1365
1366 let v = match o.get("vers").ok_or("no \"vers\" key")? {
1367 json::Value::String(ref v) => Semver::parse(&v).map_err(|e| e.to_string())?,
1368 _ => return Err("\"vers\" not string".into()),
1369 };
1370
1371 let pt = match o.get("pubtime") {
1372 None => None,
1373 Some(json::Value::String(ref pt)) => Some(DateTime::parse_from_rfc3339(pt).map_err(|e| e.to_string())?),
1374 Some(_) => return Err("\"pubtime\" not string".into()),
1375 };
1376
1377 Ok(Some((v, pt)))
1378 }
1379 _ => Err("line not object".into()),
1380 }
1381}
1382
1383/// Get the location of the registry index corresponding ot the given URL; if not present – make it and its parents.
1384///
1385/// As odd as it may be, this [can happen (if rarely) and is a supported
1386/// configuration](https://github.com/nabijaczleweli/cargo-update/issues/150).
1387///
1388/// Sparse registries do nothing and return a meaningless value.
1389///
1390/// # Examples
1391///
1392/// ```
1393/// # #[cfg(all(target_pointer_width="64", target_endian="little"))] // github.com/nabijaczleweli/cargo-update/issues/235
1394/// # {
1395/// # use cargo_update::ops::assert_index_path;
1396/// # use std::env::temp_dir;
1397/// # use std::path::Path;
1398/// # let cargo_dir = temp_dir().join("cargo_update-doctest").join("assert_index_path-0");
1399/// # let idx_dir = cargo_dir.join("registry").join("index").join("github.com-1ecc6299db9ec823");
1400/// let index = assert_index_path(&cargo_dir, "https://github.com/rust-lang/crates.io-index", false).unwrap();
1401///
1402/// // Use find_package_data() to look for packages
1403/// # assert_eq!(index, idx_dir);
1404/// # assert_eq!(assert_index_path(&cargo_dir, "https://index.crates.io/", true).unwrap(), Path::new("/ENOENT"));
1405/// # }
1406/// ```
1407pub fn assert_index_path(cargo_dir: &Path, registry_url: &str, sparse: bool) -> Result<PathBuf, Cow<'static, str>> {
1408 if sparse {
1409 return Ok(PathBuf::from("/ENOENT"));
1410 }
1411
1412 let path = cargo_dir.join("registry").join("index").join(registry_shortname(registry_url));
1413 match path.metadata() {
1414 Ok(meta) => {
1415 if meta.is_dir() {
1416 Ok(path)
1417 } else {
1418 Err(format!("{} (index directory for {}) not a directory", path.display(), registry_url).into())
1419 }
1420 }
1421 Err(ref e) if e.kind() == IoErrorKind::NotFound => {
1422 fs::create_dir_all(&path).map_err(|e| format!("Couldn't create {} (index directory for {}): {}", path.display(), registry_url, e))?;
1423 Ok(path)
1424 }
1425 Err(e) => Err(format!("Couldn't read {} (index directory for {}): {}", path.display(), registry_url, e).into()),
1426 }
1427}
1428
1429/// Opens or initialises a git repository at `registry`, or returns a blank sparse registry.
1430///
1431/// Error type distinguishes init error from open error.
1432pub fn open_index_repository(registry: &Path, sparse: bool) -> Result<Registry, (bool, GitError)> {
1433 match sparse {
1434 false => {
1435 Repository::open(®istry).map(Registry::Git).or_else(|e| if e.code() == GitErrorCode::NotFound {
1436 Repository::init(®istry).map(Registry::Git).map_err(|e| (true, e))
1437 } else {
1438 Err((false, e))
1439 })
1440 }
1441 true => Ok(Registry::Sparse(BTreeMap::new())),
1442 }
1443}
1444
1445#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
1446pub struct SparseRegistryAuthProviderBundle<'sr>(pub Cow<'sr, [SparseRegistryAuthProvider]>,
1447 pub &'sr OsStr,
1448 pub &'sr str,
1449 pub Cow<'sr, str>,
1450 pub Option<&'sr str>,
1451 pub Option<&'sr str>);
1452impl<'sr> SparseRegistryAuthProviderBundle<'sr> {
1453 pub fn try(&self) -> Option<Cow<'sr, str>> {
1454 let SparseRegistryAuthProviderBundle(providers, install_cargo, repo_name, repo_url, token_env, token) = self;
1455 providers.iter()
1456 .rev()
1457 .find_map(|p| match p {
1458 SparseRegistryAuthProvider::TokenNoEnvironment => token.map(Cow::from),
1459 SparseRegistryAuthProvider::Token => token_env.or(*token).map(Cow::from),
1460 SparseRegistryAuthProvider::Wincred => {
1461 #[allow(unused_mut)]
1462 let mut ret = None;
1463 #[cfg(target_os="windows")]
1464 unsafe {
1465 let mut cred = ptr::null_mut();
1466 if WinCred::CredReadA(PCSTR(format!("cargo-registry:{}\0", repo_url).as_ptr()),
1467 WinCred::CRED_TYPE_GENERIC,
1468 None,
1469 &mut cred)
1470 .is_ok() {
1471 ret = str::from_utf8(slice::from_raw_parts((*cred).CredentialBlob, (*cred).CredentialBlobSize as usize))
1472 .map(str::to_string)
1473 .map(Cow::from)
1474 .ok();
1475 WinCred::CredFree(cred as _);
1476 }
1477 }
1478 ret
1479 }
1480 SparseRegistryAuthProvider::MacosKeychain => {
1481 #[allow(unused_mut, unused_assignments)]
1482 let mut ret = None;
1483 #[cfg(target_vendor = "apple")]
1484 {
1485 ret = SecKeychain::default()
1486 .and_then(|k| k.find_generic_password(&format!("cargo-registry:{}", repo_url), ""))
1487 .ok()
1488 .and_then(|(p, _)| str::from_utf8(&*p).map(str::to_string).map(Cow::from).ok());
1489 }
1490 ret
1491 }
1492 SparseRegistryAuthProvider::Libsecret => {
1493 #[allow(unused_mut)]
1494 let mut ret = None;
1495 #[cfg(all(unix, not(target_vendor = "apple")))]
1496 #[allow(non_camel_case_types)]
1497 unsafe {
1498 #[repr(C)]
1499 struct SecretSchemaAttribute {
1500 name: *const u8,
1501 flags: libc::c_int, // SECRET_SCHEMA_ATTRIBUTE_STRING = 0
1502 }
1503 #[repr(C)]
1504 struct SecretSchema {
1505 name: *const u8,
1506 flags: libc::c_int,
1507 attributes: [SecretSchemaAttribute; 32],
1508 reserved: libc::c_int,
1509 reserved1: *const (),
1510 reserved2: *const (),
1511 reserved3: *const (),
1512 reserved4: *const (),
1513 reserved5: *const (),
1514 reserved6: *const (),
1515 reserved7: *const (),
1516 }
1517 unsafe impl Sync for SecretSchema {}
1518 type secret_password_lookup_sync_t = extern "C" fn(*const SecretSchema, *mut (), *mut (), ...) -> *mut u8;
1519 type secret_password_free_t = extern "C" fn(*mut u8);
1520
1521 static LIBSECRET: LazyLock<Option<(secret_password_lookup_sync_t, secret_password_free_t)>> = LazyLock::new(|| unsafe {
1522 let libsecret = libc::dlopen(b"libsecret-1.so.0\0".as_ptr() as _, libc::RTLD_LAZY);
1523 if libsecret.is_null() {
1524 return None;
1525 }
1526 let lookup = libc::dlsym(libsecret, b"secret_password_lookup_sync\0".as_ptr() as _);
1527 let free = libc::dlsym(libsecret, b"secret_password_free\0".as_ptr() as _);
1528 if lookup.is_null() || free.is_null() {
1529 libc::dlclose(libsecret);
1530 return None;
1531 }
1532 Some((mem::transmute(lookup), mem::transmute(free)))
1533 });
1534 static SCHEMA: SecretSchema = unsafe {
1535 let mut schema: SecretSchema = mem::zeroed();
1536 schema.name = b"org.rust-lang.cargo.registry\0".as_ptr() as _;
1537 schema.attributes[0].name = b"url\0".as_ptr() as _;
1538 schema
1539 };
1540
1541 if let Some((lookup, free)) = *LIBSECRET {
1542 let pass = lookup(&SCHEMA,
1543 ptr::null_mut(),
1544 ptr::null_mut(),
1545 b"url\0".as_ptr(),
1546 format!("{}\0", repo_url).as_ptr(),
1547 ptr::null() as *const u8);
1548 if !pass.is_null() {
1549 ret = str::from_utf8(slice::from_raw_parts(pass, libc::strlen(pass as _))).map(str::to_string).map(Cow::from).ok();
1550 free(pass);
1551 }
1552 }
1553 }
1554 ret
1555 }
1556 SparseRegistryAuthProvider::TokenFromStdout(args) => {
1557 Command::new(&args[0])
1558 .args(&args[1..])
1559 .env("CARGO", install_cargo)
1560 .env("CARGO_REGISTRY_INDEX_URL", &repo_url[..])
1561 .env("CARGO_REGISTRY_NAME_OPT", repo_name)
1562 .stdin(Stdio::inherit())
1563 .stderr(Stdio::inherit())
1564 .output()
1565 .ok()
1566 .filter(|o| o.status.success())
1567 .map(|o| o.stdout)
1568 .and_then(|o| String::from_utf8(o).ok())
1569 .map(|mut o| {
1570 o.replace_range(o.rfind(|c| c != '\n').unwrap_or(o.len()) + 1..o.len(), "");
1571 o.replace_range(0..o.find(|c| c != '\n').unwrap_or(0), "");
1572 o.into()
1573 })
1574 }
1575 SparseRegistryAuthProvider::Provider(args) => {
1576 Command::new(&args[0])
1577 .arg("--cargo-plugin")
1578 .stdin(Stdio::piped())
1579 .stdout(Stdio::piped())
1580 .spawn()
1581 .ok()
1582 .and_then(|mut child| {
1583 let mut stdin = BufWriter::new(child.stdin.take().unwrap());
1584 let mut stdout = BufReader::new(child.stdout.take().unwrap());
1585
1586 let mut l = String::new();
1587 stdout.read_line(&mut l).map_err(|_| child.kill()).ok()?;
1588 {
1589 let mut hello: json::Value = json::from_str(&l).map_err(|_| child.kill()).ok()?;
1590 hello.as_object_mut()
1591 .and_then(|h| h.remove("v"))
1592 .and_then(|mut v| v.as_array_mut().filter(|vs| vs.contains(&json::Value::Number(1.into()))).map(drop))
1593 .ok_or_else(|| child.kill())
1594 .ok()?;
1595 }
1596
1597 let req = json::Value::Object({
1598 let mut kv = json::Map::new();
1599 kv.insert("v".to_string(), json::Value::Number(1.into()));
1600 kv.insert("registry".to_string(),
1601 json::Value::Object({
1602 let mut kv = json::Map::new();
1603 kv.insert("index-url".to_string(), json::Value::String(repo_url.to_string()));
1604 kv.insert("name".to_string(), json::Value::String(repo_name.to_string()));
1605 kv
1606 }));
1607 kv.insert("kind".to_string(), json::Value::String("get".to_string()));
1608 kv.insert("operation".to_string(), json::Value::String("read".to_string()));
1609 kv.insert("args".to_string(),
1610 json::Value::Array(args.into_iter().skip(1).cloned().map(json::Value::String).collect()));
1611 kv
1612 });
1613 json::to_writer(&mut stdin, &req).map_err(|_| child.kill()).ok()?;
1614 stdin.write_all(b"\n").map_err(|_| child.kill()).ok()?;
1615 stdin.flush().map_err(|_| child.kill()).ok()?;
1616
1617 l.clear();
1618 stdout.read_line(&mut l).map_err(|_| child.kill()).ok()?;
1619 let mut res: json::Value = json::from_str(&l).map_err(|_| child.kill()).ok()?;
1620 match res.as_object_mut()
1621 .and_then(|h| h.remove("Ok"))
1622 .and_then(|mut ok| ok.as_object_mut().and_then(|ok| ok.remove("token"))) {
1623 Some(json::Value::String(tok)) => Some(tok.into()),
1624 Some(_) => {
1625 let _ = child.kill();
1626 None
1627 }
1628 None => {
1629 let _ = io::stderr()
1630 .write_all(b"\n")
1631 .ok()
1632 .and_then(|_| json::to_writer(&mut io::stderr(), &res).ok().and_then(|_| io::stderr().write_all(b"\n").ok()));
1633 None
1634 }
1635 }
1636 })
1637 }
1638 })
1639 }
1640}
1641
1642/// Collect everything needed to get an authentication token for the given registry.
1643pub fn auth_providers<'sr>(crates_file: &Path, cargo: &'sr OsStr, sparse_registries: &'sr SparseRegistryConfig, sparse: bool, repo_name: &'sr str,
1644 repo_url: &'sr str)
1645 -> SparseRegistryAuthProviderBundle<'sr> {
1646 if !sparse {
1647 return SparseRegistryAuthProviderBundle(vec![].into(), cargo, "!sparse", "!sparse".into(), None, None);
1648 }
1649
1650 if repo_name == "crates-io" {
1651 let ret = match sparse_registries.crates_io_credential_provider.as_ref() {
1652 Some(prov) => slice::from_ref(prov).into(),
1653 None => sparse_registries.global_credential_providers[..].into(),
1654 };
1655 return SparseRegistryAuthProviderBundle(ret,
1656 cargo,
1657 repo_name,
1658 format!("sparse+{}", repo_url).into(),
1659 sparse_registries.crates_io_token_env.as_deref(),
1660 sparse_registries.crates_io_token.as_deref());
1661 }
1662
1663 // Supposedly this is
1664 // format!("CARGO_REGISTRIES_{}_CREDENTIAL_PROVIDER",
1665 // CargoConfigEnvironmentNormalisedString::normalise(repo_name.to_string()).0)
1666 // but they don't specify how they serialise arrays so
1667 let ret: Cow<'sr, [SparseRegistryAuthProvider]> = match fs::read_to_string(crates_file.with_file_name("config"))
1668 .or_else(|_| fs::read_to_string(crates_file.with_file_name("config.toml")))
1669 .ok()
1670 .and_then(|s| s.parse::<toml::Value>().ok())
1671 .and_then(|mut c| {
1672 sparse_registries.credential_provider(c.as_table_mut()?
1673 .remove("registries")?
1674 .as_table_mut()?
1675 .remove(repo_name)?
1676 .as_table_mut()?
1677 .remove("credential-provider")?)
1678 }) {
1679 Some(prov) => vec![prov].into(),
1680 None => sparse_registries.global_credential_providers[..].into(),
1681 };
1682 let token_env = if ret.contains(&SparseRegistryAuthProvider::Token) {
1683 sparse_registries.registry_tokens_env.get(&CargoConfigEnvironmentNormalisedString::normalise(repo_name.to_string())).map(String::as_str)
1684 } else {
1685 None
1686 };
1687 SparseRegistryAuthProviderBundle(ret,
1688 cargo,
1689 repo_name,
1690 format!("sparse+{}", repo_url).into(),
1691 token_env,
1692 sparse_registries.registry_tokens.get(repo_name).map(String::as_str))
1693}
1694
1695/// Update the specified index repository from the specified URL.
1696///
1697/// Historically, `cargo search` was used, first of an
1698/// [empty string](https://github.com/nabijaczleweli/cargo-update/commit/aa090b4a38a486654cd73b173c3f49f6a56aa059#diff-639fbc4ef05b315af92b4d836c31b023R24),
1699/// then a [ZWNJ](https://github.com/nabijaczleweli/cargo-update/commit/aeccbd6252a2ddc90dc796117cefe327fbd7fb58#diff-639fbc4ef05b315af92b4d836c31b023R48)
1700/// ([why?](https://github.com/nabijaczleweli/cargo-update/commit/08a7111831c6397b7d67a51f9b77bee0a3bbbed4#diff-639fbc4ef05b315af92b4d836c31b023R47)).
1701///
1702/// The need for this in-house has first emerged with [#93](https://github.com/nabijaczleweli/cargo-update/issues/93): since
1703/// [`cargo` v1.29.0-nightly](https://github.com/rust-lang/cargo/pull/5621/commits/5e680f2849e44ce9dfe44416c3284a3b30747e74),
1704/// the registry was no longer updated.
1705/// So a [two-year-old `cargo` issue](https://github.com/rust-lang/cargo/issues/3377#issuecomment-417950125) was dug up,
1706/// asking for a `cargo update-registry` command, followed by a [PR](https://github.com/rust-lang/cargo/pull/5961) implementing
1707/// this.
1708/// Up to this point, there was no good substitute: `cargo install lazy_static`, the poster-child of replacements errored out
1709/// and left garbage in the console, making it unsuitable.
1710///
1711/// But then, a [man of steel eyes and hawk will](https://github.com/Eh2406) has emerged, seemingly from nowhere, remarking:
1712///
1713/// > [21:09] Eh2406:
1714/// https://github.com/rust-lang/cargo/blob/1ee1ef0ea7ab47d657ca675e3b1bd2fcd68b5aab/src/cargo/sources/registry/remote.rs#L204
1715/// <br />
1716/// > [21:10] Eh2406: looks like it is a git fetch of "refs/heads/master:refs/remotes/origin/master"<br />
1717/// > [21:11] Eh2406: You are already poking about in cargos internal representation of the index, is this so much more?
1718///
1719/// It, well, isn't. And with some `cargo` maintainers being firmly against blind-merging that `cargo update-registry` PR,
1720/// here I go recycling <del>the same old song</del> that implementation (but simpler, and badlier).
1721///
1722/// Honourable mentions:
1723/// * [**@joshtriplett**](https://github.com/joshtriplett), for being a bastion for the people and standing with me in
1724/// advocacy for `cargo update-registry`
1725/// (NB: it was *his* issue from 2016 requesting it, funny how things turn around)
1726/// * [**@alexcrichton**](https://github.com/alexcrichton), for not getting overly too fed up with me while managing that PR
1727/// and producing a brilliant
1728/// argument list for doing it in-house (as well as suggesting I write another crate for this)
1729/// * And lastly, because mostly, [**@Eh2406**](https://github.com/Eh2406), for swooping in and saving me in my hour of
1730/// <del>need</del> not having a good replacement.
1731///
1732/// Most of this would have been impossible, of course, without the [`rust-lang` Discord server](https://discord.gg/rust-lang),
1733/// so shoutout to whoever convinced people that Discord is actually good.
1734///
1735/// Sometimes, however, even this isn't enough (see https://github.com/nabijaczleweli/cargo-update/issues/163),
1736/// hence `fork_git`, which actually runs `$GIT` (default: `git`).
1737///
1738/// # Sparse indices
1739///
1740/// Have a `.cache` under the obvious path, then the usual `ca/rg/cargo-update`, but *the file is different than the standard
1741/// format*: it starts with a ^A or ^C (I'm assuming these are versions, and if I looked at more files I would also've seen
1742/// ^C), then Some Binary Data, then the ETag(?), then {NUL, version, NUL, usual JSON blob line} repeats.
1743///
1744/// I do not wanna be touching that shit. Just suck off all the files.<br />
1745/// Shoulda stored the blobs verbatim and used `If-Modified-Since`. Too me.
1746///
1747/// Only in this mode is the package list used.
1748pub fn update_index<W: Write, A: AsRef<str>, I: Iterator<Item = A>>(index_repo: &mut Registry, repo_url: &str, packages: I, http_proxy: Option<&str>,
1749 fork_git: bool, http: &HttpCargoConfig, auth_token: Option<&str>, out: &mut W)
1750 -> Result<(), String> {
1751 write!(out,
1752 " {} registry '{}'{}",
1753 ["Updating", "Polling"][matches!(index_repo, Registry::Sparse(_)) as usize],
1754 repo_url,
1755 ["\n", ""][matches!(index_repo, Registry::Sparse(_)) as usize]).and_then(|_| out.flush())
1756 .map_err(|e| format!("failed to write updating message: {}", e))?;
1757 match index_repo {
1758 Registry::Git(index_repo) => {
1759 if fork_git {
1760 Command::new(env::var_os("GIT").as_ref().map(OsString::as_os_str).unwrap_or(OsStr::new("git"))).arg("-C")
1761 .arg(index_repo.path())
1762 .args(&["fetch", "-f", repo_url, "HEAD:refs/remotes/origin/HEAD"])
1763 .status()
1764 .map_err(|e| e.to_string())
1765 .and_then(|e| if e.success() {
1766 Ok(())
1767 } else {
1768 Err(e.to_string())
1769 })?;
1770 } else {
1771 index_repo.remote_anonymous(repo_url)
1772 .and_then(|mut r| {
1773 with_authentication(repo_url, |creds| {
1774 let mut cb = RemoteCallbacks::new();
1775 cb.credentials(|a, b, c| creds(a, b, c));
1776
1777 r.fetch(&["HEAD:refs/remotes/origin/HEAD"],
1778 Some(&mut fetch_options_from_proxy_url_and_callbacks(repo_url, http_proxy, cb)),
1779 None)
1780 })
1781 })
1782 .map_err(|e| e.message().to_string())?;
1783 }
1784 }
1785 Registry::Sparse(registry) => {
1786 let mut sucker = CurlMulti::new();
1787 sucker.pipelining(true, true).map_err(|e| format!("pipelining: {}", e))?;
1788
1789 let writussy = Mutex::new(&mut *out);
1790 let mut conns: Vec<_> = Result::from_iter(packages.map(|pkg| {
1791 sucker.add2(CurlEasy::new(SparseHandler(pkg.as_ref().to_string(), Err("init".into()), &writussy, Some(b'.'))))
1792 .map(|h| (Some(h), Ok(())))
1793 .map_err(|e| format!("add2: {}", e))
1794 }))?;
1795 const ATTEMPTS: u8 = 4;
1796 for attempt in 0..ATTEMPTS {
1797 if conns.is_empty() {
1798 break;
1799 }
1800 std::thread::sleep(Duration::from_secs((1 << attempt) - 1));
1801
1802 for c in &mut conns {
1803 let mut conn = sucker.remove2(c.0.take().unwrap()).map_err(|e| format!("remove2: {}", e))?;
1804 conn.get_mut().1 = Ok((vec![], vec![]));
1805 conn.get_mut().3.get_or_insert(b'0' + attempt);
1806 conn.reset();
1807
1808 conn.url(&split_package_path(&conn.get_ref().0).into_iter().fold(repo_url.to_string(), |mut u, s| {
1809 if !u.ends_with('/') {
1810 u.push('/');
1811 }
1812 u.push_str(&s);
1813 u
1814 }))
1815 .map_err(|e| format!("url: {}", e))?;
1816 if let Some(auth_token) = auth_token.as_ref() {
1817 let mut headers = CurlList::new();
1818 headers.append(&format!("Authorization: {}", auth_token)).map_err(|e| format!("append: {}", e))?;
1819 conn.http_headers(headers).map_err(|e| format!("http_headers: {}", e))?;
1820 }
1821 if let Some(http_proxy) = http_proxy {
1822 conn.proxy(http_proxy).map_err(|e| format!("proxy: {}", e))?;
1823 }
1824 conn.pipewait(true).map_err(|e| format!("pipewait: {}", e))?;
1825 conn.progress(true).map_err(|e| format!("progress: {}", e))?;
1826 if let Some(cainfo) = http.cainfo.as_ref() {
1827 conn.cainfo(cainfo).map_err(|e| format!("cainfo: {}", e))?;
1828 }
1829 conn.ssl_options(CurlSslOpt::new().no_revoke(!http.check_revoke)).map_err(|e| format!("ssl_options: {}", e))?;
1830 c.0 = Some(sucker.add2(conn).map_err(|e| format!("add2: {}", e))?);
1831 }
1832 while sucker.perform().map_err(|e| format!("perform: {}", e))? > 0 {
1833 sucker.wait(&mut [], Duration::from_millis(200)).map_err(|e| format!("wait: {}", e))?;
1834 }
1835
1836 sucker.messages(|m| {
1837 for c in &mut conns {
1838 // Yes, a linear search; this is much faster than adding 2+n sets of CURLINFO_PRIVATE calls
1839 if let Some(err) = m.result_for2(&c.0.as_ref().unwrap()) {
1840 c.1 = err;
1841 }
1842 }
1843 });
1844
1845 let mut retainer = |c: &mut (Option<CurlEasyHandle<SparseHandler<'_, '_, _>>>, Result<(), _>)| {
1846 let pkg = mem::take(&mut c.0.as_mut().unwrap().get_mut().0);
1847 match c.0.as_mut().unwrap().response_code().map_err(|e| format!("response_code: {}", e))? {
1848 0 | 200 => {
1849 let (mut resp, buf) =
1850 mem::replace(&mut c.0.as_mut().unwrap().get_mut().1, Err("taken".into())).map_err(|e| format!("package {}: {}", pkg, e))?;
1851 mem::replace(&mut c.1, Ok(())).map_err(|e| format!("package {}: {}", pkg, e))?;
1852 if !buf.is_empty() {
1853 if c.0.as_ref().unwrap().get_ref().3.is_some() {
1854 return Err(format!("package {}: {} bytes of trailing garbage", pkg, buf.len()))?;
1855 } else {
1856 // Some broken registries (kellnr <https://github.com/nabijaczleweli/cargo-update/pull/341>)
1857 // return a complete response that's 1 byte too short;
1858 // if curl told us we managed to read the whole response, try pretending to append it back
1859 resp.extend(crate_version_line(&buf).map_err(|e| format!("package {}: trailing garbage as crate version: {}", pkg, e))?);
1860 }
1861 }
1862 resp.sort();
1863 sucker.remove2(c.0.take().unwrap()).map_err(|e| format!("remove2: {}", e))?;
1864 registry.insert(pkg, resp);
1865 Ok(false)
1866 }
1867 rc @ 404 | rc @ 410 | rc @ 451 => Err(format!("package {} doesn't exist: HTTP {}", pkg, rc)),
1868 rc @ 408 | rc @ 429 | rc @ 503 | rc @ 504 => {
1869 if attempt == ATTEMPTS - 1 {
1870 Err(format!("package {}: HTTP {} after {} attempts", pkg, rc, ATTEMPTS))
1871 } else {
1872 c.0.as_mut().unwrap().get_mut().0 = pkg;
1873 Ok(true)
1874 }
1875 }
1876 rc => Err(format!("package {}: HTTP {}", pkg, rc)),
1877 }
1878 };
1879 let mut err = Ok(());
1880 conns.retain_mut(|c| {
1881 if err.is_err() {
1882 return false;
1883 }
1884 match retainer(c) {
1885 Ok(r) => r,
1886 Err(e) => {
1887 if let Ok(mut out) = writussy.lock() {
1888 let _ = writeln!(out);
1889 }
1890 err = Err(e);
1891 false
1892 }
1893 }
1894 });
1895 err?;
1896 }
1897 if let Ok(mut out) = writussy.lock() {
1898 let _ = writeln!(out);
1899 };
1900 }
1901 }
1902 writeln!(out).map_err(|e| format!("failed to write post-update newline: {}", e))?;
1903
1904 Ok(())
1905}
1906// TODO: Mutex wants to be nonpoison
1907struct SparseHandler<'m, 'w: 'm, W: Write>(String,
1908 Result<(Vec<(Semver, Option<DateTime<FixedOffset>>)>, Vec<u8>), Cow<'static, str>>,
1909 &'m Mutex<&'w mut W>,
1910 Option<u8>);
1911
1912impl<'m, 'w: 'm, W: Write> CurlHandler for SparseHandler<'m, 'w, W> {
1913 fn write(&mut self, data: &[u8]) -> Result<usize, CurlWriteError> {
1914 let mut consumed = 0;
1915 self.1 = mem::replace(&mut self.1, Err("write".into())).and_then(|(mut vers, mut buf)| {
1916 for l in data.split_inclusive(|&b| b == b'\n') {
1917 if !l.ends_with(b"\n") {
1918 buf.extend(l);
1919 consumed += l.len();
1920 continue;
1921 }
1922
1923 let line = if buf.is_empty() {
1924 l
1925 } else {
1926 buf.extend(l);
1927 &buf[..]
1928 };
1929 vers.extend(crate_version_line(line)?);
1930 buf.clear();
1931 consumed += l.len();
1932 }
1933 Ok((vers, buf))
1934 });
1935 Ok(consumed)
1936 }
1937
1938 fn progress(&mut self, dltotal: f64, dlnow: f64, _: f64, _: f64) -> bool {
1939 if dltotal != 0.0 && dltotal == dlnow {
1940 if let Some(status) = self.3.take() {
1941 if let Ok(mut out) = self.2.lock() {
1942 let _ = out.write_all(&[status]).and_then(|_| out.flush());
1943 }
1944 }
1945 }
1946 true
1947 }
1948}
1949
1950
1951/// Either an open git repository with a git registry, or a map of (package, sorted versions), populated by
1952/// [`update_index()`](fn.update_index.html)
1953pub enum Registry {
1954 Git(Repository),
1955 Sparse(BTreeMap<String, Vec<(Semver, Option<DateTime<FixedOffset>>)>>),
1956}
1957
1958/// A git tree corresponding to the latest revision of a git registry.
1959pub enum RegistryTree<'a> {
1960 Git(Tree<'a>),
1961 Sparse,
1962}
1963
1964/// Get `FETCH_HEAD` or `origin/HEAD`, then unwrap it to the tree it points to.
1965pub fn parse_registry_head(registry_repo: &Registry) -> Result<RegistryTree<'_>, GitError> {
1966 match registry_repo {
1967 Registry::Git(registry_repo) => {
1968 registry_repo.revparse_single("FETCH_HEAD")
1969 .or_else(|_| registry_repo.revparse_single("origin/HEAD"))
1970 .map(|h| h.as_commit().unwrap().tree().unwrap())
1971 .map(RegistryTree::Git)
1972 }
1973 Registry::Sparse(_) => Ok(RegistryTree::Sparse),
1974 }
1975}
1976
1977
1978fn proxy_options_from_proxy_url<'a>(repo_url: &str, proxy_url: &str) -> ProxyOptions<'a> {
1979 let mut prx = ProxyOptions::new();
1980 let mut url = Cow::from(proxy_url);
1981
1982 // Cargo allows [protocol://]host[:port], but git needs the protocol, try to crudely add it here if missing;
1983 // confer https://github.com/nabijaczleweli/cargo-update/issues/144.
1984 if Url::parse(proxy_url).is_err() {
1985 if let Ok(rurl) = Url::parse(repo_url) {
1986 let replacement_proxy_url = format!("{}://{}", rurl.scheme(), proxy_url);
1987 if Url::parse(&replacement_proxy_url).is_ok() {
1988 url = Cow::from(replacement_proxy_url);
1989 }
1990 }
1991 }
1992
1993 prx.url(&url);
1994 prx
1995}
1996
1997fn fetch_options_from_proxy_url_and_callbacks<'a>(repo_url: &str, proxy_url: Option<&str>, callbacks: RemoteCallbacks<'a>) -> FetchOptions<'a> {
1998 let mut ret = FetchOptions::new();
1999 if let Some(proxy_url) = proxy_url {
2000 ret.proxy_options(proxy_options_from_proxy_url(repo_url, proxy_url));
2001 }
2002 ret.remote_callbacks(callbacks);
2003 ret
2004}
2005
2006/// Get the URL to update index from, whether it's "sparse", and the cargo name for it from the config file parallel to the
2007/// specified crates file
2008///
2009/// First gets the source name corresponding to the given URL, if appropriate,
2010/// then chases the `source.$SRCNAME.replace-with` chain,
2011/// then retrieves the URL from `source.$SRCNAME.registry` of the final source.
2012///
2013/// Prepopulates with `source.crates-io.registry = "https://github.com/rust-lang/crates.io-index"`,
2014/// as specified in the book
2015///
2016/// If `registries_crates_io_protocol_sparse`, `https://github.com/rust-lang/crates.io-index` is replaced with
2017/// `sparse+https://index.crates.io/`.
2018///
2019/// Consult [#107](https://github.com/nabijaczleweli/cargo-update/issues/107) and
2020/// the Cargo Book for details: https://doc.rust-lang.org/cargo/reference/source-replacement.html,
2021/// https://doc.rust-lang.org/cargo/reference/registries.html.
2022pub fn get_index_url(crates_file: &Path, registry: &str, registries_crates_io_protocol_sparse: bool)
2023 -> Result<(Cow<'static, str>, bool, Cow<'static, str>), Cow<'static, str>> {
2024 let mut config_file = crates_file.with_file_name("config");
2025 let config = if let Ok(cfg) = fs::read_to_string(&config_file).or_else(|_| {
2026 config_file.set_file_name("config.toml");
2027 fs::read_to_string(&config_file)
2028 }) {
2029 toml::from_str::<toml::Value>(&cfg).map_err(|e| format!("{} not TOML: {}", config_file.display(), e))?
2030 } else {
2031 if registry == "https://github.com/rust-lang/crates.io-index" {
2032 if registries_crates_io_protocol_sparse {
2033 return Ok(("https://index.crates.io/".into(), true, "crates-io".into()));
2034 } else {
2035 return Ok((registry.to_string().into(), false, "crates-io".into()));
2036 }
2037 } else {
2038 Err(format!("Non-crates.io registry specified and no config file found at {} or {}. \
2039 Due to a Cargo limitation we will not be able to install from there \
2040 until it's given a [source.NAME] in that file!",
2041 config_file.with_file_name("config").display(),
2042 config_file.display()))?
2043 }
2044 };
2045
2046 let mut replacements = BTreeMap::new();
2047 let mut registries = BTreeMap::new();
2048 let mut cur_source = Cow::from(registry);
2049
2050 // Special case, always present
2051 registries.insert("crates-io",
2052 Cow::from(if registries_crates_io_protocol_sparse {
2053 "sparse+https://index.crates.io/"
2054 } else {
2055 "https://github.com/rust-lang/crates.io-index"
2056 }));
2057 if cur_source == "https://github.com/rust-lang/crates.io-index" || cur_source == "sparse+https://index.crates.io/" {
2058 cur_source = "crates-io".into();
2059 }
2060
2061 if let Some(source) = config.get("source") {
2062 for (name, v) in source.as_table().ok_or("source not table")? {
2063 if let Some(replacement) = v.get("replace-with") {
2064 replacements.insert(&name[..],
2065 replacement.as_str().ok_or_else(|| format!("source.{}.replacement not string", name))?);
2066 }
2067
2068 if let Some(url) = v.get("registry") {
2069 let url = url.as_str().ok_or_else(|| format!("source.{}.registry not string", name))?.to_string().into();
2070 if cur_source == url {
2071 cur_source = name.into();
2072 }
2073
2074 registries.insert(&name[..], url);
2075 }
2076 }
2077 }
2078
2079 if let Some(registries_tabls) = config.get("registries") {
2080 let table = registries_tabls.as_table().ok_or("registries is not a table")?;
2081 for (name, url) in table.iter().flat_map(|(name, val)| val.as_table()?.get("index")?.as_str().map(|v| (name, v))) {
2082 if cur_source == url.strip_prefix("sparse+").unwrap_or(url) {
2083 cur_source = name.into()
2084 }
2085 registries.insert(name, url.into());
2086 }
2087 }
2088
2089 if Url::parse(&cur_source).is_ok() {
2090 Err(format!("Non-crates.io registry specified and {} couldn't be found in the config file at {}. \
2091 Due to a Cargo limitation we will not be able to install from there \
2092 until it's given a [source.NAME] in that file!",
2093 cur_source,
2094 config_file.display()))?
2095 }
2096
2097 while let Some(repl) = replacements.get(&cur_source[..]) {
2098 cur_source = Cow::from(&repl[..]);
2099 }
2100
2101 registries.get(&cur_source[..])
2102 .map(|reg| (reg.strip_prefix("sparse+").unwrap_or(reg).to_string().into(), reg.starts_with("sparse+"), cur_source.to_string().into()))
2103 .ok_or_else(|| {
2104 format!("Couldn't find appropriate source URL for {} in {} (resolved to {:?})",
2105 registry,
2106 config_file.display(),
2107 cur_source)
2108 .into()
2109 })
2110}
2111
2112/// Based on
2113/// https://github.com/rust-lang/cargo/blob/bb28e71202260180ecff658cd0fa0c7ba86d0296/src/cargo/sources/git/utils.rs#L344
2114/// and
2115/// https://github.com/rust-lang/cargo/blob/5102de2b7de997b03181063417f20874a06a67c0/src/cargo/sources/git/utils.rs#L644,
2116/// then
2117/// https://github.com/rust-lang/cargo/blob/5102de2b7de997b03181063417f20874a06a67c0/src/cargo/sources/git/utils.rs#L437
2118/// (see that link for full comments)
2119fn with_authentication<T, F>(url: &str, mut f: F) -> Result<T, GitError>
2120 where F: FnMut(&mut git2::Credentials) -> Result<T, GitError>
2121{
2122 let cfg = GitConfig::open_default().unwrap();
2123
2124 let mut cred_helper = git2::CredentialHelper::new(url);
2125 cred_helper.config(&cfg);
2126
2127 let mut ssh_username_requested = false;
2128 let mut cred_helper_bad = None;
2129 let mut ssh_agent_attempts = Vec::new();
2130 let mut any_attempts = false;
2131 let mut tried_ssh_key = false;
2132
2133 let mut res = f(&mut |url, username, allowed| {
2134 any_attempts = true;
2135
2136 if allowed.contains(CredentialType::USERNAME) {
2137 ssh_username_requested = true;
2138
2139 Err(GitError::from_str("username to be tried later"))
2140 } else if allowed.contains(CredentialType::SSH_KEY) && !tried_ssh_key {
2141 tried_ssh_key = true;
2142
2143 let username = username.unwrap();
2144 ssh_agent_attempts.push(username.to_string());
2145
2146 GitCred::ssh_key_from_agent(username)
2147 } else if allowed.contains(CredentialType::USER_PASS_PLAINTEXT) && cred_helper_bad.is_none() {
2148 let ret = GitCred::credential_helper(&cfg, url, username);
2149 cred_helper_bad = Some(ret.is_err());
2150 ret
2151 } else if allowed.contains(CredentialType::DEFAULT) {
2152 GitCred::default()
2153 } else {
2154 Err(GitError::from_str("no authentication available"))
2155 }
2156 });
2157
2158 if ssh_username_requested {
2159 // NOTE: this is the only divergence from the original cargo code: we also try cfg["user.name"]
2160 // see https://github.com/nabijaczleweli/cargo-update/issues/110#issuecomment-533091965 for explanation
2161 for uname in cred_helper.username
2162 .into_iter()
2163 .chain(cfg.get_string("user.name"))
2164 .chain(["USERNAME", "USER"].iter().flat_map(env::var))
2165 .chain(Some("git".to_string())) {
2166 let mut ssh_attempts = 0;
2167
2168 res = f(&mut |_, _, allowed| {
2169 if allowed.contains(CredentialType::USERNAME) {
2170 return GitCred::username(&uname);
2171 } else if allowed.contains(CredentialType::SSH_KEY) {
2172 ssh_attempts += 1;
2173 if ssh_attempts == 1 {
2174 ssh_agent_attempts.push(uname.to_string());
2175 return GitCred::ssh_key_from_agent(&uname);
2176 }
2177 }
2178
2179 Err(GitError::from_str("no authentication available"))
2180 });
2181
2182 if ssh_attempts != 2 {
2183 break;
2184 }
2185 }
2186 }
2187
2188 if res.is_ok() || !any_attempts {
2189 res
2190 } else {
2191 let err = res.err().map(|e| format!("{}: ", e)).unwrap_or_default();
2192
2193 let mut msg = format!("{}failed to authenticate when downloading repository {}", err, url);
2194 if !ssh_agent_attempts.is_empty() {
2195 msg.push_str(" (tried ssh-agent, but none of the following usernames worked: ");
2196 for (i, uname) in ssh_agent_attempts.into_iter().enumerate() {
2197 if i != 0 {
2198 msg.push_str(", ");
2199 }
2200 msg.push('\"');
2201 msg.push_str(&uname);
2202 msg.push('\"');
2203 }
2204 msg.push(')');
2205 }
2206
2207 if let Some(failed_cred_helper) = cred_helper_bad {
2208 msg.push_str(" (tried to find username+password via ");
2209 if failed_cred_helper {
2210 msg.push_str("git's credential.helper support, but failed)");
2211 } else {
2212 msg.push_str("credential.helper, but found credentials were incorrect)");
2213 }
2214 }
2215
2216 Err(GitError::from_str(&msg))
2217 }
2218}
2219
2220
2221/// Split and lower-case `cargo-update` into `[ca, rg, cargo-update]`, `jot` into `[3, j, jot]`, &c.
2222pub fn split_package_path(cratename: &str) -> Vec<Cow<'_, str>> {
2223 let mut elems = Vec::new();
2224 if cratename.is_empty() {
2225 panic!("0-length cratename");
2226 }
2227 if cratename.len() <= 3 {
2228 elems.push(["1", "2", "3"][cratename.len() - 1].into())
2229 }
2230 match cratename.len() {
2231 1 | 2 => {}
2232 3 => elems.push(lcase(&cratename[0..1])),
2233 _ => {
2234 elems.push(lcase(&cratename[0..2]));
2235 elems.push(lcase(&cratename[2..4]));
2236 }
2237 }
2238 elems.push(lcase(cratename));
2239 elems
2240}
2241
2242fn lcase(s: &str) -> Cow<'_, str> {
2243 if s.bytes().any(|b| b.is_ascii_uppercase()) {
2244 s.to_ascii_lowercase().into()
2245 } else {
2246 s.into()
2247 }
2248}
2249
2250/// Find package data in the specified cargo git index tree.
2251pub fn find_package_data<'t>(cratename: &str, registry: &Tree<'t>, registry_parent: &'t Repository) -> Option<Blob<'t>> {
2252 let elems = split_package_path(cratename);
2253
2254 let ent = registry.get_name(&elems[0])?;
2255 let obj = ent.to_object(registry_parent).ok()?;
2256 let ent = obj.as_tree()?.get_name(&elems[1])?;
2257 let obj = ent.to_object(registry_parent).ok()?;
2258 let obj = if elems.len() == 3 {
2259 let ent = obj.as_tree()?.get_name(&elems[2])?;
2260 ent.to_object(registry_parent).ok()?
2261 } else {
2262 obj
2263 };
2264 obj.into_blob().ok()
2265}
2266
2267/// Check if there's a proxy specified to be used.
2268///
2269/// Look for `http.proxy` key in the `config` file parallel to the specified crates file.
2270///
2271/// Then look for `git`'s `http.proxy`.
2272///
2273/// Then for the `http_proxy`, `HTTP_PROXY`, `https_proxy`, and `HTTPS_PROXY` environment variables, in that order.
2274///
2275/// Based on Cargo's [`http_proxy_exists()` and
2276/// `http_proxy()`](https://github.com/rust-lang/cargo/blob/eebd1da3a89e9c7788d109b3e615e1e25dc2cfcd/src/cargo/ops/registry.rs)
2277///
2278/// If a proxy is specified, but an empty string, treat it as unspecified.
2279///
2280/// # Examples
2281///
2282/// ```
2283/// # use cargo_update::ops::find_proxy;
2284/// # use std::env::temp_dir;
2285/// # let crates_file = temp_dir().join(".crates.toml");
2286/// match find_proxy(&crates_file) {
2287/// Some(proxy) => println!("Proxy found at {}", proxy),
2288/// None => println!("No proxy detected"),
2289/// }
2290/// ```
2291pub fn find_proxy(crates_file: &Path) -> Option<String> {
2292 if let Ok(crates_file) = fs::read_to_string(crates_file) {
2293 if let Some(toml::Value::String(proxy)) =
2294 toml::from_str::<toml::Value>(&crates_file)
2295 .unwrap()
2296 .get_mut("http")
2297 .and_then(|t| t.as_table_mut())
2298 .and_then(|t| t.remove("proxy")) {
2299 if !proxy.is_empty() {
2300 return Some(proxy);
2301 }
2302 }
2303 }
2304
2305 if let Ok(cfg) = GitConfig::open_default() {
2306 if let Ok(proxy) = cfg.get_string("http.proxy") {
2307 if !proxy.is_empty() {
2308 return Some(proxy);
2309 }
2310 }
2311 }
2312
2313 ["http_proxy", "HTTP_PROXY", "https_proxy", "HTTPS_PROXY"].iter().flat_map(env::var).filter(|proxy| !proxy.is_empty()).next()
2314}
2315
2316/// Find the bare git repository in the specified directory for the specified crate
2317///
2318/// The db directory is usually `$HOME/.cargo/git/db/`
2319///
2320/// The resulting paths are children of this directory in the format
2321/// [`{last_url_segment || "_empty"}-{hash(url)}`]
2322/// (https://github.com/rust-lang/cargo/blob/74f2b400d2be43da798f99f94957d359bc223988/src/cargo/sources/git/source.rs#L62-L73)
2323pub fn find_git_db_repo(git_db_dir: &Path, url: &str) -> Option<PathBuf> {
2324 let path = git_db_dir.join(format!("{}-{}",
2325 match Url::parse(url)
2326 .ok()?
2327 .path_segments()
2328 .and_then(|mut segs| segs.next_back())
2329 .unwrap_or("") {
2330 "" => "_empty",
2331 url => url,
2332 },
2333 cargo_hash(url)));
2334
2335 if path.is_dir() { Some(path) } else { None }
2336}
2337
2338
2339/// The short filesystem name for the repository, as used by `cargo`
2340///
2341/// Must be equivalent to
2342/// https://github.com/rust-lang/cargo/blob/74f2b400d2be43da798f99f94957d359bc223988/src/cargo/sources/registry/mod.rs#L387-L402
2343/// and
2344/// https://github.com/rust-lang/cargo/blob/74f2b400d2be43da798f99f94957d359bc223988/src/cargo/util/hex.rs
2345///
2346/// For main repository it's `github.com-1ecc6299db9ec823`
2347pub fn registry_shortname(url: &str) -> String {
2348 struct RegistryHash<'u>(&'u str);
2349 impl<'u> Hash for RegistryHash<'u> {
2350 fn hash<S: Hasher>(&self, hasher: &mut S) {
2351 SourceKind::Registry.hash(hasher);
2352 self.0.hash(hasher);
2353 }
2354 }
2355
2356 format!("{}-{}",
2357 Url::parse(url).map_err(|e| format!("{} not an URL: {}", url, e)).unwrap().host_str().unwrap_or(""),
2358 cargo_hash(RegistryHash(url)))
2359}
2360
2361/// Stolen from and equivalent to `short_hash()` from
2362/// https://github.com/rust-lang/cargo/blob/74f2b400d2be43da798f99f94957d359bc223988/src/cargo/util/hex.rs
2363#[allow(deprecated)]
2364pub fn cargo_hash<T: Hash>(whom: T) -> String {
2365 use std::hash::SipHasher;
2366
2367 let mut hasher = SipHasher::new_with_keys(0, 0);
2368 whom.hash(&mut hasher);
2369 let hash = hasher.finish();
2370 hex::encode(&[(hash >> 0) as u8,
2371 (hash >> 8) as u8,
2372 (hash >> 16) as u8,
2373 (hash >> 24) as u8,
2374 (hash >> 32) as u8,
2375 (hash >> 40) as u8,
2376 (hash >> 48) as u8,
2377 (hash >> 56) as u8])
2378}
2379
2380/// These two are stolen verbatim from
2381/// https://github.com/rust-lang/cargo/blob/74f2b400d2be43da798f99f94957d359bc223988/src/cargo/core/source/source_id.rs#L48-L73
2382/// in order to match our hash with
2383/// https://github.com/rust-lang/cargo/blob/74f2b400d2be43da798f99f94957d359bc223988/src/cargo/core/source/source_id.rs#L510
2384#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2385#[allow(unused)]
2386enum SourceKind {
2387 Git(GitReference),
2388 Path,
2389 Registry,
2390 LocalRegistry,
2391 Directory,
2392}
2393
2394#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2395#[allow(unused)]
2396enum GitReference {
2397 Tag(String),
2398 Branch(String),
2399 Rev(String),
2400}
2401
2402
2403trait SemverExt {
2404 fn is_prerelease(&self) -> bool;
2405}
2406impl SemverExt for Semver {
2407 fn is_prerelease(&self) -> bool {
2408 !self.pre.is_empty()
2409 }
2410}