Skip to main content

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(&registry_tree, &registry);
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(&registry_tree, &registry);
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)) => &registry_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) => match &glob[..] {
833                [] => true /* unreachable */,
834                [exact] => exact == name,
835                [front, back] => name.starts_with(front) && name.ends_with(back),
836                [front, chunks @ .., back] => matchglob(name, front, chunks, back),
837            },
838        }
839    }
840}
841
842fn matchglob(name: &str, front: &str, glob: &[String], back: &str) -> bool {
843    let Some(name) = name.strip_prefix(front) else { return false };
844    let Some(mut name) = name.strip_suffix(back) else { return false };
845    for chunk in glob {
846        let Some(idx) = name.find(chunk) else { return false };
847        name = &name[idx + chunk.len()..];
848    }
849    true
850}
851
852/// `cargo` configuration, as obtained from `.cargo/config[.toml]`
853#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
854pub struct CargoConfig {
855    pub net_git_fetch_with_cli: bool,
856    /// https://blog.rust-lang.org/2023/03/09/Rust-1.68.0.html#cargos-sparse-protocol
857    /// https://doc.rust-lang.org/stable/cargo/reference/registry-index.html#sparse-protocol
858    pub registries_crates_io_protocol_sparse: bool,
859    pub http: HttpCargoConfig,
860    pub sparse_registries: SparseRegistryConfig,
861}
862
863#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
864pub struct HttpCargoConfig {
865    pub cainfo: Option<PathBuf>,
866    pub check_revoke: bool,
867}
868
869/// https://github.com/nabijaczleweli/cargo-update/issues/300
870#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
871pub struct SparseRegistryConfig {
872    pub global_credential_providers: Vec<SparseRegistryAuthProvider>,
873    pub crates_io_credential_provider: Option<SparseRegistryAuthProvider>,
874    pub crates_io_token_env: Option<String>,
875    pub crates_io_token: Option<String>,
876    pub registry_tokens_env: BTreeMap<CargoConfigEnvironmentNormalisedString, String>,
877    pub registry_tokens: BTreeMap<String, String>,
878    pub credential_aliases: BTreeMap<CargoConfigEnvironmentNormalisedString, Vec<String>>,
879}
880
881impl SparseRegistryConfig {
882    pub fn credential_provider(&self, v: toml::Value) -> Option<SparseRegistryAuthProvider> {
883        SparseRegistryConfig::credential_provider_impl(&self.credential_aliases, v)
884    }
885
886    fn credential_provider_impl(credential_aliases: &BTreeMap<CargoConfigEnvironmentNormalisedString, Vec<String>>, v: toml::Value)
887                                -> Option<SparseRegistryAuthProvider> {
888        match v {
889            toml::Value::String(s) => Some(CargoConfig::string_provider(s, &credential_aliases)),
890            toml::Value::Array(a) => Some(SparseRegistryAuthProvider::from_config(CargoConfig::string_array(a))),
891            _ => None,
892        }
893    }
894}
895
896/// https://doc.rust-lang.org/cargo/reference/registry-authentication.html
897#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
898pub enum SparseRegistryAuthProvider {
899    /// The default; does not read `CARGO_REGISTRY_TOKEN` or `CARGO_REGISTRIES_{}_TOKEN` environment variables.
900    TokenNoEnvironment,
901    /// `cargo:token`
902    Token,
903    /// `cargo:wincred` (Win32)
904    Wincred,
905    /// `cargo:macos-keychain` (Apple)
906    MacosKeychain,
907    /// `cargo:libsecret`; this `dlopen()`s `libsecret-1.so.0` on non-Apple UNIX.
908    Libsecret,
909    /// `cargo:token-from-stdout prog arg arg`
910    TokenFromStdout(Vec<String>),
911    /// Not `cargo:`-prefixed
912    ///
913    /// https://doc.rust-lang.org/cargo/reference/credential-provider-protocol.html
914    ///
915    /// We do *not* care about `"cache"`, `"expiration"`, or `"operation_independent"`,
916    /// always behaving as-if `"never"`/`_`/`true`.
917    ///
918    /// We don't provide the optional `{"registry": {"headers": ...}}` field.
919    Provider(Vec<String>),
920}
921
922impl SparseRegistryAuthProvider {
923    /// Parses a `["cargo:token-from-stdout", "whatever"]`-style entry
924    pub fn from_config(mut toks: Vec<String>) -> SparseRegistryAuthProvider {
925        match toks.get(0).map(String::as_str).unwrap_or("") {
926            "cargo:token" => SparseRegistryAuthProvider::Token,
927            "cargo:wincred" => SparseRegistryAuthProvider::Wincred,
928            "cargo:macos-keychain" => SparseRegistryAuthProvider::MacosKeychain,
929            "cargo:libsecret" => SparseRegistryAuthProvider::Libsecret,
930            "cargo:token-from-stdout" => {
931                toks.remove(0);
932                SparseRegistryAuthProvider::TokenFromStdout(toks)
933            }
934            _ => SparseRegistryAuthProvider::Provider(toks),
935        }
936    }
937}
938
939/// https://doc.rust-lang.org/cargo/reference/config.html#environment-variables
940#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
941pub struct CargoConfigEnvironmentNormalisedString(pub String);
942impl CargoConfigEnvironmentNormalisedString {
943    /// `tr a-z.- A-Z__`
944    pub fn normalise(mut s: String) -> CargoConfigEnvironmentNormalisedString {
945        s.make_ascii_uppercase();
946        while let Some(i) = s.find(['.', '-']) {
947            s.replace_range(i..i + 1, "_");
948        }
949        CargoConfigEnvironmentNormalisedString(s)
950    }
951}
952
953impl CargoConfig {
954    pub fn load(crates_file: &Path) -> CargoConfig {
955        let mut cfg = fs::read_to_string(crates_file.with_file_name("config"))
956            .or_else(|_| fs::read_to_string(crates_file.with_file_name("config.toml")))
957            .ok()
958            .and_then(|s| s.parse::<toml::Value>().ok());
959        let mut creds = fs::read_to_string(crates_file.with_file_name("credentials"))
960            .or_else(|_| fs::read_to_string(crates_file.with_file_name("credentials.toml")))
961            .ok()
962            .and_then(|s| s.parse::<toml::Value>().ok());
963
964        let credential_aliases = None.or_else(|| match cfg.as_mut()?.as_table_mut()?.remove("credential-alias")? {
965                toml::Value::Table(t) => Some(t),
966                _ => None,
967            })
968            .unwrap_or_default()
969            .into_iter()
970            .flat_map(|(k, v)| {
971                match v {
972                        toml::Value::String(s) => Some(s.split(' ').map(String::from).collect()),
973                        toml::Value::Array(a) => Some(CargoConfig::string_array(a)),
974                        _ => None,
975                    }
976                    .map(|v| (CargoConfigEnvironmentNormalisedString::normalise(k), v))
977            })
978            .chain(env::vars_os()
979                .map(|(k, v)| (k.into_encoded_bytes(), v))
980                .filter(|(k, _)| k.starts_with(b"CARGO_CREDENTIAL_ALIAS_"))
981                .filter(|(k, _)| k["CARGO_CREDENTIAL_ALIAS_".len()..].iter().all(|&b| !(b.is_ascii_lowercase() || b == b'.' || b == b'-')))
982                .flat_map(|(mut k, v)| {
983                    let k = String::from_utf8(k.drain("CARGO_CREDENTIAL_ALIAS_".len()..).collect()).ok()?;
984                    let v = v.into_string().ok()?;
985                    Some((CargoConfigEnvironmentNormalisedString(k), v.split(' ').map(String::from).collect()))
986                }))
987            .collect();
988
989        CargoConfig {
990            net_git_fetch_with_cli: env::var("CARGO_NET_GIT_FETCH_WITH_CLI")
991                .ok()
992                .and_then(|e| if e.is_empty() {
993                    Some(toml::Value::String(String::new()))
994                } else {
995                    e.parse::<toml::Value>().ok()
996                })
997                .or_else(|| {
998                    cfg.as_mut()?
999                        .as_table_mut()?
1000                        .get_mut("net")?
1001                        .as_table_mut()?
1002                        .remove("git-fetch-with-cli")
1003                })
1004                .map(CargoConfig::truthy)
1005                .unwrap_or(false),
1006            registries_crates_io_protocol_sparse: env::var("CARGO_REGISTRIES_CRATES_IO_PROTOCOL")
1007                .map(|s| s == "sparse")
1008                .ok()
1009                .or_else(|| {
1010                    Some(cfg.as_mut()?
1011                        .as_table_mut()?
1012                        .get_mut("registries")?
1013                        .as_table_mut()?
1014                        .get_mut("crates-io")?
1015                        .as_table_mut()?
1016                        .remove("protocol")?
1017                        .as_str()? == "sparse")
1018                })
1019                // // Horrifically expensive (82-93ms end-to-end) and largely unnecessary
1020                // .or_else(|| {
1021                //     let mut l = String::new();
1022                //     // let before = std::time::Instant::now();
1023                //     BufReader::new(Command::new(cargo).arg("version").stdout(Stdio::piped()).spawn().ok()?.stdout?).read_line(&mut l).ok()?;
1024                //     // let after = std::time::Instant::now();
1025                //
1026                //     // cargo 1.63.0 (fd9c4297c 2022-07-01)
1027                //     Some(Semver::parse(l.split_whitespace().nth(1)?).ok()? >= Semver::new(1, 70, 0))
1028                // })
1029                // .unwrap_or(false),
1030                .unwrap_or(true),
1031            http: HttpCargoConfig {
1032                cainfo: env::var_os("CARGO_HTTP_CAINFO")
1033                    .map(PathBuf::from)
1034                    .or_else(|| {
1035                        CargoConfig::string(cfg.as_mut()?
1036                                .as_table_mut()?
1037                                .get_mut("http")?
1038                                .as_table_mut()?
1039                                .remove("cainfo")?)
1040                            .map(PathBuf::from)
1041                    }),
1042                check_revoke: env::var("CARGO_HTTP_CHECK_REVOKE")
1043                    .ok()
1044                    .map(toml::Value::String)
1045                    .or_else(|| {
1046                        cfg.as_mut()?
1047                            .as_table_mut()?
1048                            .get_mut("http")?
1049                            .as_table_mut()?
1050                            .remove("check-revoke")
1051                    })
1052                    .map(CargoConfig::truthy)
1053                    .unwrap_or(cfg!(target_os = "windows")),
1054            },
1055            sparse_registries: SparseRegistryConfig {
1056                // Supposedly this is CARGO_REGISTRY_GLOBAL_CREDENTIAL_PROVIDERS but they don't specify how they serialise arrays so
1057                global_credential_providers: None.or_else(|| {
1058                        CargoConfig::string_array_v(cfg.as_mut()?
1059                            .as_table_mut()?
1060                            .get_mut("registry")?
1061                            .as_table_mut()?
1062                            .remove("global-credential-providers")?)
1063                    })
1064                    .map(|a| a.into_iter().map(|s| CargoConfig::string_provider(s, &credential_aliases)).collect())
1065                    .unwrap_or_else(|| vec![SparseRegistryAuthProvider::TokenNoEnvironment]),
1066                crates_io_credential_provider: env::var("CARGO_REGISTRY_CREDENTIAL_PROVIDER")
1067                    .ok()
1068                    .map(toml::Value::String)
1069                    .or_else(|| {
1070                        cfg.as_mut()?
1071                            .as_table_mut()?
1072                            .get_mut("registry")?
1073                            .as_table_mut()?
1074                            .remove("credential-provider")
1075                    })
1076                    .and_then(|v| SparseRegistryConfig::credential_provider_impl(&credential_aliases, v)),
1077                crates_io_token_env: env::var("CARGO_REGISTRY_TOKEN").ok(),
1078                crates_io_token: None.or_else(|| {
1079                        CargoConfig::string(creds.as_mut()?
1080                            .as_table_mut()?
1081                            .get_mut("registry")?
1082                            .as_table_mut()?
1083                            .remove("token")?)
1084                    })
1085                    .or_else(|| {
1086                        CargoConfig::string(cfg.as_mut()?
1087                            .as_table_mut()?
1088                            .get_mut("registry")?
1089                            .as_table_mut()?
1090                            .remove("token")?)
1091                    }),
1092                registry_tokens_env: env::vars_os()
1093                    .map(|(k, v)| (k.into_encoded_bytes(), v))
1094                    .filter(|(k, _)| k.starts_with(b"CARGO_REGISTRIES_") && k.ends_with(b"_TOKEN"))
1095                    .filter(|(k, _)| {
1096                        k["CARGO_REGISTRIES_".len()..k.len() - b"_TOKEN".len()].iter().all(|&b| !(b.is_ascii_lowercase() || b == b'.' || b == b'-'))
1097                    })
1098                    .flat_map(|(mut k, v)| {
1099                        let k = String::from_utf8(k.drain("CARGO_REGISTRIES_".len()..k.len() - b"_TOKEN".len()).collect()).ok()?;
1100                        Some((CargoConfigEnvironmentNormalisedString(k), v.into_string().ok()?))
1101                    })
1102                    .collect(),
1103                registry_tokens: cfg.as_mut()
1104                    .into_iter()
1105                    .chain(creds.as_mut())
1106                    .flat_map(|c| {
1107                        c.as_table_mut()?
1108                            .get_mut("registries")?
1109                            .as_table_mut()
1110                    })
1111                    .flat_map(|r| r.into_iter().flat_map(|(name, v)| Some((name.clone(), CargoConfig::string(v.as_table_mut()?.remove("token")?)?))))
1112                    .collect(),
1113                credential_aliases: credential_aliases,
1114            },
1115        }
1116    }
1117
1118    fn truthy(v: toml::Value) -> bool {
1119        match v {
1120            toml::Value::String(ref s) if s == "" => false,
1121            toml::Value::Float(0.) => false,
1122            toml::Value::Integer(0) |
1123            toml::Value::Boolean(false) => false,
1124            _ => true,
1125        }
1126    }
1127
1128    fn string(v: toml::Value) -> Option<String> {
1129        match v {
1130            toml::Value::String(s) => Some(s),
1131            _ => None,
1132        }
1133    }
1134
1135    fn string_array(a: Vec<toml::Value>) -> Vec<String> {
1136        a.into_iter().flat_map(CargoConfig::string).collect()
1137    }
1138
1139    fn string_array_v(v: toml::Value) -> Option<Vec<String>> {
1140        match v {
1141            toml::Value::Array(s) => Some(CargoConfig::string_array(s)),
1142            _ => None,
1143        }
1144    }
1145
1146    fn string_provider(s: String, credential_aliases: &BTreeMap<CargoConfigEnvironmentNormalisedString, Vec<String>>) -> SparseRegistryAuthProvider {
1147        match credential_aliases.get(&CargoConfigEnvironmentNormalisedString::normalise(s.clone())) {
1148            Some(av) => SparseRegistryAuthProvider::Provider(av.clone()),
1149            None => {
1150                SparseRegistryAuthProvider::from_config(if s.contains(' ') {
1151                    s.split(' ').map(String::from).collect()
1152                } else {
1153                    vec![s]
1154                })
1155            }
1156        }
1157    }
1158}
1159
1160
1161/// [Follow `install.root`](https://github.com/nabijaczleweli/cargo-update/issues/23) in the `config` or `config.toml` file
1162/// in the cargo directory specified.
1163///
1164/// # Examples
1165///
1166/// ```
1167/// # use cargo_update::ops::crates_file_in;
1168/// # use std::env::temp_dir;
1169/// # let cargo_dir = temp_dir();
1170/// let cargo_dir = crates_file_in(&cargo_dir);
1171/// # let _ = cargo_dir;
1172/// ```
1173pub fn crates_file_in(cargo_dir: &Path) -> PathBuf {
1174    crates_file_in_impl(cargo_dir, BTreeSet::new())
1175}
1176fn crates_file_in_impl<'cd>(cargo_dir: &'cd Path, mut seen: BTreeSet<&'cd Path>) -> PathBuf {
1177    if !seen.insert(cargo_dir) {
1178        panic!("Cargo config install.root loop at {:?} (saw {:?})", cargo_dir.display(), seen);
1179    }
1180
1181    let mut config_file = cargo_dir.join("config");
1182    let mut config_data = fs::read_to_string(&config_file);
1183    if config_data.is_err() {
1184        config_file.set_file_name("config.toml");
1185        config_data = fs::read_to_string(&config_file);
1186    }
1187    if let Ok(config_data) = config_data {
1188        if let Some(idir) = toml::from_str::<toml::Value>(&config_data)
1189            .unwrap()
1190            .get("install")
1191            .and_then(|t| t.as_table())
1192            .and_then(|t| t.get("root"))
1193            .and_then(|t| t.as_str()) {
1194            return crates_file_in_impl(Path::new(idir), seen);
1195        }
1196    }
1197
1198    config_file.set_file_name(".crates.toml");
1199    config_file
1200}
1201
1202fn installed_packages_table(crates_file: &Path) -> Option<toml::Table> {
1203    let crates_data = fs::read_to_string(crates_file).ok()?;
1204    Some(toml::from_str::<toml::Value>(&crates_data).unwrap().get_mut("v1")?.as_table_mut().map(mem::take).unwrap())
1205}
1206
1207/// List the installed packages at the specified location that originate
1208/// from the a cargo registry.
1209///
1210/// If the `.crates.toml` file doesn't exist an empty vector is returned.
1211///
1212/// This also deduplicates packages and assumes the latest version as the correct one to work around
1213/// [#44](https://github.com/nabijaczleweli/cargo-update/issues/44) a.k.a.
1214/// [rust-lang/cargo#4321](https://github.com/rust-lang/cargo/issues/4321).
1215///
1216/// # Examples
1217///
1218/// ```
1219/// # use cargo_update::ops::installed_registry_packages;
1220/// # use std::env::temp_dir;
1221/// # let cargo_dir = temp_dir().join(".crates.toml");
1222/// let packages = installed_registry_packages(&cargo_dir);
1223/// for package in &packages {
1224///     println!("{} v{}", package.name, package.version.as_ref().unwrap());
1225/// }
1226/// ```
1227pub fn installed_registry_packages(crates_file: &Path) -> Vec<RegistryPackage> {
1228    let mut res = Vec::<RegistryPackage>::new();
1229    for pkg in installed_packages_table(crates_file)
1230        .into_iter()
1231        .flatten()
1232        .flat_map(|(s, x)| CargoConfig::string_array_v(x).and_then(|x| RegistryPackage::parse(&s, x))) {
1233        if let Some(saved) = res.iter_mut().find(|p| p.name == pkg.name) {
1234            if saved.version.is_none() || saved.version.as_ref().unwrap() < pkg.version.as_ref().unwrap() {
1235                saved.version = pkg.version;
1236            }
1237            continue;
1238        }
1239
1240        res.push(pkg);
1241    }
1242    res
1243}
1244
1245/// List the installed packages at the specified location that originate
1246/// from a  remote git repository.
1247///
1248/// If the `.crates.toml` file doesn't exist an empty vector is returned.
1249///
1250/// This also deduplicates packages and assumes the latest-mentioned version as the most correct.
1251///
1252/// # Examples
1253///
1254/// ```
1255/// # use cargo_update::ops::installed_git_repo_packages;
1256/// # use std::env::temp_dir;
1257/// # let cargo_dir = temp_dir().join(".crates.toml");
1258/// let packages = installed_git_repo_packages(&cargo_dir);
1259/// for package in &packages {
1260///     println!("{} v{}", package.name, package.id);
1261/// }
1262/// ```
1263pub fn installed_git_repo_packages(crates_file: &Path) -> Vec<GitRepoPackage> {
1264    let mut res = Vec::<GitRepoPackage>::new();
1265    for pkg in installed_packages_table(crates_file)
1266        .into_iter()
1267        .flatten()
1268        .flat_map(|(s, x)| CargoConfig::string_array_v(x).and_then(|x| GitRepoPackage::parse(&s, x))) {
1269        if let Some(saved) = res.iter_mut().find(|p| p.name == pkg.name) {
1270            saved.id = pkg.id;
1271            continue;
1272        }
1273
1274        res.push(pkg);
1275    }
1276    res
1277}
1278
1279/// Filter out the installed packages not specified to be updated and add the packages you specify to install,
1280/// if they aren't already installed via git.
1281///
1282/// List installed packages with `installed_registry_packages()`.
1283///
1284/// # Examples
1285///
1286/// ```
1287/// # use cargo_update::ops::{RegistryPackage, intersect_packages};
1288/// # fn installed_registry_packages(_: &()) {}
1289/// # let cargo_dir = ();
1290/// # let packages_to_update = [("racer".to_string(), None,
1291/// #                            "registry+https://github.com/rust-lang/crates.io-index".into()),
1292/// #                           ("cargo-outdated".to_string(), None,
1293/// #                            "registry+https://github.com/rust-lang/crates.io-index".into())];
1294/// let mut installed_packages = installed_registry_packages(&cargo_dir);
1295/// # let mut installed_packages =
1296/// #     vec![RegistryPackage::parse("cargo-outdated 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
1297/// #     vec!["cargo-outdated".to_string()]).unwrap(),
1298/// #          RegistryPackage::parse("racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
1299/// #     vec!["racer.exe".to_string()]).unwrap(),
1300/// #          RegistryPackage::parse("rustfmt 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
1301/// #     vec!["rustfmt".to_string(), "cargo-format".to_string()]).unwrap()];
1302/// installed_packages = intersect_packages(&installed_packages, &packages_to_update, false, &[]);
1303/// # assert_eq!(&installed_packages,
1304/// #   &[RegistryPackage::parse("cargo-outdated 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
1305/// #                            vec!["cargo-outdated".to_string()]).unwrap(),
1306/// #     RegistryPackage::parse("racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
1307/// #                            vec!["racer.exe".to_string()]).unwrap()]);
1308/// ```
1309pub fn intersect_packages(installed: &[RegistryPackage], to_update: &[(String, Option<Semver>, Cow<'static, str>)], allow_installs: bool,
1310                          installed_git: &[GitRepoPackage])
1311                          -> Vec<RegistryPackage> {
1312    installed.iter()
1313        .filter(|p| to_update.iter().any(|u| p.name == u.0))
1314        .cloned()
1315        .map(|p| RegistryPackage { max_version: to_update.iter().find(|u| p.name == u.0).and_then(|u| u.1.clone()), ..p })
1316        .chain(to_update.iter()
1317            .filter(|p| allow_installs && !installed.iter().any(|i| i.name == p.0) && !installed_git.iter().any(|i| i.name == p.0))
1318            .map(|p| {
1319                RegistryPackage {
1320                    name: p.0.clone(),
1321                    registry: p.2.clone(),
1322                    version: None,
1323                    newest_version: None,
1324                    alternative_version: None,
1325                    max_version: p.1.clone(),
1326                    executables: vec![],
1327                }
1328            }))
1329        .collect()
1330}
1331
1332/// Parse the raw crate descriptor from the repository into a collection of `Semver`s and publication times (if present).
1333///
1334/// # Examples
1335///
1336/// ```
1337/// # use cargo_update::ops::crate_versions;
1338/// # use std::fs;
1339/// # let desc_path = "test-data/checksums-versions.json";
1340/// # let package = "checksums";
1341/// let versions = crate_versions(&fs::read(desc_path).unwrap()).expect(package);
1342///
1343/// println!("Released versions of checksums:");
1344/// for (ver, pubtime) in &versions {
1345///     match pubtime {
1346///         None          => println!("  {}",      ver),
1347///         Some(pubtime) => println!("  {} ({})", ver, pubtime),
1348///     }
1349/// }
1350/// ```
1351pub fn crate_versions(buf: &[u8]) -> Result<Vec<(Semver, Option<DateTime<FixedOffset>>)>, Cow<'static, str>> {
1352    buf.split_inclusive(|&b| b == b'\n').map(crate_version_line).flat_map(Result::transpose).collect()
1353}
1354fn crate_version_line(line: &[u8]) -> Result<Option<(Semver, Option<DateTime<FixedOffset>>)>, Cow<'static, str>> {
1355    if line == b"\n" {
1356        return Ok(None);
1357    }
1358    match json::from_slice(line).map_err(|e| e.to_string())? {
1359        json::Value::Object(o) => {
1360            if matches!(o.get("yanked"), Some(&json::Value::Bool(true))) {
1361                return Ok(None);
1362            }
1363
1364            let v = match o.get("vers").ok_or("no \"vers\" key")? {
1365                json::Value::String(ref v) => Semver::parse(&v).map_err(|e| e.to_string())?,
1366                _ => return Err("\"vers\" not string".into()),
1367            };
1368
1369            let pt = match o.get("pubtime") {
1370                None => None,
1371                Some(json::Value::String(ref pt)) => Some(DateTime::parse_from_rfc3339(pt).map_err(|e| e.to_string())?),
1372                Some(_) => return Err("\"pubtime\" not string".into()),
1373            };
1374
1375            Ok(Some((v, pt)))
1376        }
1377        _ => Err("line not object".into()),
1378    }
1379}
1380
1381/// Get the location of the registry index corresponding ot the given URL; if not present – make it and its parents.
1382///
1383/// As odd as it may be, this [can happen (if rarely) and is a supported
1384/// configuration](https://github.com/nabijaczleweli/cargo-update/issues/150).
1385///
1386/// Sparse registries do nothing and return a meaningless value.
1387///
1388/// # Examples
1389///
1390/// ```
1391/// # #[cfg(all(target_pointer_width="64", target_endian="little"))] // github.com/nabijaczleweli/cargo-update/issues/235
1392/// # {
1393/// # use cargo_update::ops::assert_index_path;
1394/// # use std::env::temp_dir;
1395/// # use std::path::Path;
1396/// # let cargo_dir = temp_dir().join("cargo_update-doctest").join("assert_index_path-0");
1397/// # let idx_dir = cargo_dir.join("registry").join("index").join("github.com-1ecc6299db9ec823");
1398/// let index = assert_index_path(&cargo_dir, "https://github.com/rust-lang/crates.io-index", false).unwrap();
1399///
1400/// // Use find_package_data() to look for packages
1401/// # assert_eq!(index, idx_dir);
1402/// # assert_eq!(assert_index_path(&cargo_dir, "https://index.crates.io/", true).unwrap(), Path::new("/ENOENT"));
1403/// # }
1404/// ```
1405pub fn assert_index_path(cargo_dir: &Path, registry_url: &str, sparse: bool) -> Result<PathBuf, Cow<'static, str>> {
1406    if sparse {
1407        return Ok(PathBuf::from("/ENOENT"));
1408    }
1409
1410    let path = cargo_dir.join("registry").join("index").join(registry_shortname(registry_url));
1411    match path.metadata() {
1412        Ok(meta) => {
1413            if meta.is_dir() {
1414                Ok(path)
1415            } else {
1416                Err(format!("{} (index directory for {}) not a directory", path.display(), registry_url).into())
1417            }
1418        }
1419        Err(ref e) if e.kind() == IoErrorKind::NotFound => {
1420            fs::create_dir_all(&path).map_err(|e| format!("Couldn't create {} (index directory for {}): {}", path.display(), registry_url, e))?;
1421            Ok(path)
1422        }
1423        Err(e) => Err(format!("Couldn't read {} (index directory for {}): {}", path.display(), registry_url, e).into()),
1424    }
1425}
1426
1427/// Opens or initialises a git repository at `registry`, or returns a blank sparse registry.
1428///
1429/// Error type distinguishes init error from open error.
1430pub fn open_index_repository(registry: &Path, sparse: bool) -> Result<Registry, (bool, GitError)> {
1431    match sparse {
1432        false => {
1433            Repository::open(&registry).map(Registry::Git).or_else(|e| if e.code() == GitErrorCode::NotFound {
1434                Repository::init(&registry).map(Registry::Git).map_err(|e| (true, e))
1435            } else {
1436                Err((false, e))
1437            })
1438        }
1439        true => Ok(Registry::Sparse(BTreeMap::new())),
1440    }
1441}
1442
1443#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
1444pub struct SparseRegistryAuthProviderBundle<'sr>(pub Cow<'sr, [SparseRegistryAuthProvider]>,
1445                                                 pub &'sr OsStr,
1446                                                 pub &'sr str,
1447                                                 pub Cow<'sr, str>,
1448                                                 pub Option<&'sr str>,
1449                                                 pub Option<&'sr str>);
1450impl<'sr> SparseRegistryAuthProviderBundle<'sr> {
1451    pub fn try(&self) -> Option<Cow<'sr, str>> {
1452        let (install_cargo, repo_name, repo_url, token_env, token) = (self.1, self.2, &self.3, self.4, self.5);
1453        self.0
1454            .iter()
1455            .rev()
1456            .find_map(|p| match p {
1457                SparseRegistryAuthProvider::TokenNoEnvironment => token.map(Cow::from),
1458                SparseRegistryAuthProvider::Token => token_env.or(token).map(Cow::from),
1459                SparseRegistryAuthProvider::Wincred => {
1460                    #[allow(unused_mut)]
1461                    let mut ret = None;
1462                    #[cfg(target_os="windows")]
1463                    unsafe {
1464                        let mut cred = ptr::null_mut();
1465                        if WinCred::CredReadA(PCSTR(format!("cargo-registry:{}\0", repo_url).as_ptr()),
1466                                              WinCred::CRED_TYPE_GENERIC,
1467                                              None,
1468                                              &mut cred)
1469                            .is_ok() {
1470                            ret = str::from_utf8(slice::from_raw_parts((*cred).CredentialBlob, (*cred).CredentialBlobSize as usize))
1471                                .map(str::to_string)
1472                                .map(Cow::from)
1473                                .ok();
1474                            WinCred::CredFree(cred as _);
1475                        }
1476                    }
1477                    ret
1478                }
1479                SparseRegistryAuthProvider::MacosKeychain => {
1480                    #[allow(unused_mut, unused_assignments)]
1481                    let mut ret = None;
1482                    #[cfg(target_vendor = "apple")]
1483                    {
1484                        ret = SecKeychain::default()
1485                            .and_then(|k| k.find_generic_password(&format!("cargo-registry:{}", repo_url), ""))
1486                            .ok()
1487                            .and_then(|(p, _)| str::from_utf8(&*p).map(str::to_string).map(Cow::from).ok());
1488                    }
1489                    ret
1490                }
1491                SparseRegistryAuthProvider::Libsecret => {
1492                    #[allow(unused_mut)]
1493                    let mut ret = None;
1494                    #[cfg(all(unix, not(target_vendor = "apple")))]
1495                    #[allow(non_camel_case_types)]
1496                    unsafe {
1497                        #[repr(C)]
1498                        struct SecretSchemaAttribute {
1499                            name: *const u8,
1500                            flags: libc::c_int, // SECRET_SCHEMA_ATTRIBUTE_STRING = 0
1501                        }
1502                        #[repr(C)]
1503                        struct SecretSchema {
1504                            name: *const u8,
1505                            flags: libc::c_int,
1506                            attributes: [SecretSchemaAttribute; 32],
1507                            reserved: libc::c_int,
1508                            reserved1: *const (),
1509                            reserved2: *const (),
1510                            reserved3: *const (),
1511                            reserved4: *const (),
1512                            reserved5: *const (),
1513                            reserved6: *const (),
1514                            reserved7: *const (),
1515                        }
1516                        unsafe impl Sync for SecretSchema {}
1517                        type secret_password_lookup_sync_t = extern "C" fn(*const SecretSchema, *mut (), *mut (), ...) -> *mut u8;
1518                        type secret_password_free_t = extern "C" fn(*mut u8);
1519
1520                        static LIBSECRET: LazyLock<Option<(secret_password_lookup_sync_t, secret_password_free_t)>> = LazyLock::new(|| unsafe {
1521                            let libsecret = libc::dlopen(b"libsecret-1.so.0\0".as_ptr() as _, libc::RTLD_LAZY);
1522                            if libsecret.is_null() {
1523                                return None;
1524                            }
1525                            let lookup = libc::dlsym(libsecret, b"secret_password_lookup_sync\0".as_ptr() as _);
1526                            let free = libc::dlsym(libsecret, b"secret_password_free\0".as_ptr() as _);
1527                            if lookup.is_null() || free.is_null() {
1528                                libc::dlclose(libsecret);
1529                                return None;
1530                            }
1531                            Some((mem::transmute(lookup), mem::transmute(free)))
1532                        });
1533                        static SCHEMA: SecretSchema = unsafe {
1534                            let mut schema: SecretSchema = mem::zeroed();
1535                            schema.name = b"org.rust-lang.cargo.registry\0".as_ptr() as _;
1536                            schema.attributes[0].name = b"url\0".as_ptr() as _;
1537                            schema
1538                        };
1539
1540                        if let Some((lookup, free)) = *LIBSECRET {
1541                            let pass = lookup(&SCHEMA,
1542                                              ptr::null_mut(),
1543                                              ptr::null_mut(),
1544                                              b"url\0".as_ptr(),
1545                                              format!("{}\0", repo_url).as_ptr(),
1546                                              ptr::null() as *const u8);
1547                            if !pass.is_null() {
1548                                ret = str::from_utf8(slice::from_raw_parts(pass, libc::strlen(pass as _))).map(str::to_string).map(Cow::from).ok();
1549                                free(pass);
1550                            }
1551                        }
1552                    }
1553                    ret
1554                }
1555                SparseRegistryAuthProvider::TokenFromStdout(args) => {
1556                    Command::new(&args[0])
1557                        .args(&args[1..])
1558                        .env("CARGO", install_cargo)
1559                        .env("CARGO_REGISTRY_INDEX_URL", &repo_url[..])
1560                        .env("CARGO_REGISTRY_NAME_OPT", repo_name)
1561                        .stdin(Stdio::inherit())
1562                        .stderr(Stdio::inherit())
1563                        .output()
1564                        .ok()
1565                        .filter(|o| o.status.success())
1566                        .map(|o| o.stdout)
1567                        .and_then(|o| String::from_utf8(o).ok())
1568                        .map(|mut o| {
1569                            o.replace_range(o.rfind(|c| c != '\n').unwrap_or(o.len()) + 1..o.len(), "");
1570                            o.replace_range(0..o.find(|c| c != '\n').unwrap_or(0), "");
1571                            o.into()
1572                        })
1573                }
1574                SparseRegistryAuthProvider::Provider(args) => {
1575                    Command::new(&args[0])
1576                        .arg("--cargo-plugin")
1577                        .stdin(Stdio::piped())
1578                        .stdout(Stdio::piped())
1579                        .spawn()
1580                        .ok()
1581                        .and_then(|mut child| {
1582                            let mut stdin = BufWriter::new(child.stdin.take().unwrap());
1583                            let mut stdout = BufReader::new(child.stdout.take().unwrap());
1584
1585                            let mut l = String::new();
1586                            stdout.read_line(&mut l).map_err(|_| child.kill()).ok()?;
1587                            {
1588                                let mut hello: json::Value = json::from_str(&l).map_err(|_| child.kill()).ok()?;
1589                                hello.as_object_mut()
1590                                    .and_then(|h| h.remove("v"))
1591                                    .and_then(|mut v| v.as_array_mut().filter(|vs| vs.contains(&json::Value::Number(1.into()))).map(drop))
1592                                    .ok_or_else(|| child.kill())
1593                                    .ok()?;
1594                            }
1595
1596                            let req = json::Value::Object({
1597                                let mut kv = json::Map::new();
1598                                kv.insert("v".to_string(), json::Value::Number(1.into()));
1599                                kv.insert("registry".to_string(),
1600                                          json::Value::Object({
1601                                              let mut kv = json::Map::new();
1602                                              kv.insert("index-url".to_string(), json::Value::String(repo_url.to_string()));
1603                                              kv.insert("name".to_string(), json::Value::String(repo_name.to_string()));
1604                                              kv
1605                                          }));
1606                                kv.insert("kind".to_string(), json::Value::String("get".to_string()));
1607                                kv.insert("operation".to_string(), json::Value::String("read".to_string()));
1608                                kv.insert("args".to_string(),
1609                                          json::Value::Array(args.into_iter().skip(1).cloned().map(json::Value::String).collect()));
1610                                kv
1611                            });
1612                            json::to_writer(&mut stdin, &req).map_err(|_| child.kill()).ok()?;
1613                            stdin.write_all(b"\n").map_err(|_| child.kill()).ok()?;
1614                            stdin.flush().map_err(|_| child.kill()).ok()?;
1615
1616                            l.clear();
1617                            stdout.read_line(&mut l).map_err(|_| child.kill()).ok()?;
1618                            let mut res: json::Value = json::from_str(&l).map_err(|_| child.kill()).ok()?;
1619                            match res.as_object_mut()
1620                                .and_then(|h| h.remove("Ok"))
1621                                .and_then(|mut ok| ok.as_object_mut().and_then(|ok| ok.remove("token"))) {
1622                                Some(json::Value::String(tok)) => Some(tok.into()),
1623                                Some(_) => {
1624                                    let _ = child.kill();
1625                                    None
1626                                }
1627                                None => {
1628                                    let _ = io::stderr()
1629                                        .write_all(b"\n")
1630                                        .ok()
1631                                        .and_then(|_| json::to_writer(&mut io::stderr(), &res).ok().and_then(|_| io::stderr().write_all(b"\n").ok()));
1632                                    None
1633                                }
1634                            }
1635                        })
1636                }
1637            })
1638    }
1639}
1640
1641/// Collect everything needed to get an authentication token for the given registry.
1642pub fn auth_providers<'sr>(crates_file: &Path, install_cargo: Option<&'sr OsStr>, sparse_registries: &'sr SparseRegistryConfig, sparse: bool,
1643                           repo_name: &'sr str, repo_url: &'sr str)
1644                           -> SparseRegistryAuthProviderBundle<'sr> {
1645    let cargo = install_cargo.unwrap_or(OsStr::new("cargo"));
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                        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                                return Err(format!("package {}: {} bytes of trailing garbage", pkg, buf.len()))?;
1854                            }
1855                            resp.sort();
1856                            sucker.remove2(c.0.take().unwrap()).map_err(|e| format!("remove2: {}", e))?;
1857                            registry.insert(pkg, resp);
1858                            Ok(false)
1859                        }
1860                        rc @ 404 | rc @ 410 | rc @ 451 => Err(format!("package {} doesn't exist: HTTP {}", pkg, rc)),
1861                        rc @ 408 | rc @ 429 | rc @ 503 | rc @ 504 => {
1862                            if attempt == ATTEMPTS - 1 {
1863                                Err(format!("package {}: HTTP {} after {} attempts", pkg, rc, ATTEMPTS))
1864                            } else {
1865                                c.0.as_mut().unwrap().get_mut().0 = pkg;
1866                                Ok(true)
1867                            }
1868                        }
1869                        rc => Err(format!("package {}: HTTP {}", pkg, rc)),
1870                    }
1871                };
1872                let mut err = Ok(());
1873                conns.retain_mut(|c| {
1874                    if err.is_err() {
1875                        return false;
1876                    }
1877                    match retainer(c) {
1878                        Ok(r) => r,
1879                        Err(e) => {
1880                            if let Ok(mut out) = writussy.lock() {
1881                                let _ = writeln!(out);
1882                            }
1883                            err = Err(e);
1884                            false
1885                        }
1886                    }
1887                });
1888                err?;
1889            }
1890            if let Ok(mut out) = writussy.lock() {
1891                let _ = writeln!(out);
1892            };
1893        }
1894    }
1895    writeln!(out).map_err(|e| format!("failed to write post-update newline: {}", e))?;
1896
1897    Ok(())
1898}
1899// TODO: Mutex wants to be nonpoison
1900struct SparseHandler<'m, 'w: 'm, W: Write>(String,
1901                                           Result<(Vec<(Semver, Option<DateTime<FixedOffset>>)>, Vec<u8>), Cow<'static, str>>,
1902                                           &'m Mutex<&'w mut W>,
1903                                           Option<u8>);
1904
1905impl<'m, 'w: 'm, W: Write> CurlHandler for SparseHandler<'m, 'w, W> {
1906    fn write(&mut self, data: &[u8]) -> Result<usize, CurlWriteError> {
1907        let mut consumed = 0;
1908        self.1 = mem::replace(&mut self.1, Err("write".into())).and_then(|(mut vers, mut buf)| {
1909            for l in data.split_inclusive(|&b| b == b'\n') {
1910                if !l.ends_with(b"\n") {
1911                    buf.extend(l);
1912                    consumed += l.len();
1913                    continue;
1914                }
1915
1916                let line = if buf.is_empty() {
1917                    l
1918                } else {
1919                    buf.extend(l);
1920                    &buf[..]
1921                };
1922                vers.extend(crate_version_line(line)?);
1923                buf.clear();
1924                consumed += l.len();
1925            }
1926            Ok((vers, buf))
1927        });
1928        Ok(consumed)
1929    }
1930
1931    fn progress(&mut self, dltotal: f64, dlnow: f64, _: f64, _: f64) -> bool {
1932        if dltotal != 0.0 && dltotal == dlnow {
1933            if let Some(status) = self.3.take() {
1934                if let Ok(mut out) = self.2.lock() {
1935                    let _ = out.write_all(&[status]).and_then(|_| out.flush());
1936                }
1937            }
1938        }
1939        true
1940    }
1941}
1942
1943
1944/// Either an open git repository with a git registry, or a map of (package, sorted versions), populated by
1945/// [`update_index()`](fn.update_index.html)
1946pub enum Registry {
1947    Git(Repository),
1948    Sparse(BTreeMap<String, Vec<(Semver, Option<DateTime<FixedOffset>>)>>),
1949}
1950
1951/// A git tree corresponding to the latest revision of a git registry.
1952pub enum RegistryTree<'a> {
1953    Git(Tree<'a>),
1954    Sparse,
1955}
1956
1957/// Get `FETCH_HEAD` or `origin/HEAD`, then unwrap it to the tree it points to.
1958pub fn parse_registry_head(registry_repo: &Registry) -> Result<RegistryTree<'_>, GitError> {
1959    match registry_repo {
1960        Registry::Git(registry_repo) => {
1961            registry_repo.revparse_single("FETCH_HEAD")
1962                .or_else(|_| registry_repo.revparse_single("origin/HEAD"))
1963                .map(|h| h.as_commit().unwrap().tree().unwrap())
1964                .map(RegistryTree::Git)
1965        }
1966        Registry::Sparse(_) => Ok(RegistryTree::Sparse),
1967    }
1968}
1969
1970
1971fn proxy_options_from_proxy_url<'a>(repo_url: &str, proxy_url: &str) -> ProxyOptions<'a> {
1972    let mut prx = ProxyOptions::new();
1973    let mut url = Cow::from(proxy_url);
1974
1975    // Cargo allows [protocol://]host[:port], but git needs the protocol, try to crudely add it here if missing;
1976    // confer https://github.com/nabijaczleweli/cargo-update/issues/144.
1977    if Url::parse(proxy_url).is_err() {
1978        if let Ok(rurl) = Url::parse(repo_url) {
1979            let replacement_proxy_url = format!("{}://{}", rurl.scheme(), proxy_url);
1980            if Url::parse(&replacement_proxy_url).is_ok() {
1981                url = Cow::from(replacement_proxy_url);
1982            }
1983        }
1984    }
1985
1986    prx.url(&url);
1987    prx
1988}
1989
1990fn fetch_options_from_proxy_url_and_callbacks<'a>(repo_url: &str, proxy_url: Option<&str>, callbacks: RemoteCallbacks<'a>) -> FetchOptions<'a> {
1991    let mut ret = FetchOptions::new();
1992    if let Some(proxy_url) = proxy_url {
1993        ret.proxy_options(proxy_options_from_proxy_url(repo_url, proxy_url));
1994    }
1995    ret.remote_callbacks(callbacks);
1996    ret
1997}
1998
1999/// Get the URL to update index from, whether it's "sparse", and the cargo name for it from the config file parallel to the
2000/// specified crates file
2001///
2002/// First gets the source name corresponding to the given URL, if appropriate,
2003/// then chases the `source.$SRCNAME.replace-with` chain,
2004/// then retrieves the URL from `source.$SRCNAME.registry` of the final source.
2005///
2006/// Prepopulates with `source.crates-io.registry = "https://github.com/rust-lang/crates.io-index"`,
2007/// as specified in the book
2008///
2009/// If `registries_crates_io_protocol_sparse`, `https://github.com/rust-lang/crates.io-index` is replaced with
2010/// `sparse+https://index.crates.io/`.
2011///
2012/// Consult [#107](https://github.com/nabijaczleweli/cargo-update/issues/107) and
2013/// the Cargo Book for details: https://doc.rust-lang.org/cargo/reference/source-replacement.html,
2014/// https://doc.rust-lang.org/cargo/reference/registries.html.
2015pub fn get_index_url(crates_file: &Path, registry: &str, registries_crates_io_protocol_sparse: bool)
2016                     -> Result<(Cow<'static, str>, bool, Cow<'static, str>), Cow<'static, str>> {
2017    let mut config_file = crates_file.with_file_name("config");
2018    let config = if let Ok(cfg) = fs::read_to_string(&config_file).or_else(|_| {
2019        config_file.set_file_name("config.toml");
2020        fs::read_to_string(&config_file)
2021    }) {
2022        toml::from_str::<toml::Value>(&cfg).map_err(|e| format!("{} not TOML: {}", config_file.display(), e))?
2023    } else {
2024        if registry == "https://github.com/rust-lang/crates.io-index" {
2025            if registries_crates_io_protocol_sparse {
2026                return Ok(("https://index.crates.io/".into(), true, "crates-io".into()));
2027            } else {
2028                return Ok((registry.to_string().into(), false, "crates-io".into()));
2029            }
2030        } else {
2031            Err(format!("Non-crates.io registry specified and no config file found at {} or {}. \
2032                         Due to a Cargo limitation we will not be able to install from there \
2033                         until it's given a [source.NAME] in that file!",
2034                        config_file.with_file_name("config").display(),
2035                        config_file.display()))?
2036        }
2037    };
2038
2039    let mut replacements = BTreeMap::new();
2040    let mut registries = BTreeMap::new();
2041    let mut cur_source = Cow::from(registry);
2042
2043    // Special case, always present
2044    registries.insert("crates-io",
2045                      Cow::from(if registries_crates_io_protocol_sparse {
2046                          "sparse+https://index.crates.io/"
2047                      } else {
2048                          "https://github.com/rust-lang/crates.io-index"
2049                      }));
2050    if cur_source == "https://github.com/rust-lang/crates.io-index" || cur_source == "sparse+https://index.crates.io/" {
2051        cur_source = "crates-io".into();
2052    }
2053
2054    if let Some(source) = config.get("source") {
2055        for (name, v) in source.as_table().ok_or("source not table")? {
2056            if let Some(replacement) = v.get("replace-with") {
2057                replacements.insert(&name[..],
2058                                    replacement.as_str().ok_or_else(|| format!("source.{}.replacement not string", name))?);
2059            }
2060
2061            if let Some(url) = v.get("registry") {
2062                let url = url.as_str().ok_or_else(|| format!("source.{}.registry not string", name))?.to_string().into();
2063                if cur_source == url {
2064                    cur_source = name.into();
2065                }
2066
2067                registries.insert(&name[..], url);
2068            }
2069        }
2070    }
2071
2072    if let Some(registries_tabls) = config.get("registries") {
2073        let table = registries_tabls.as_table().ok_or("registries is not a table")?;
2074        for (name, url) in table.iter().flat_map(|(name, val)| val.as_table()?.get("index")?.as_str().map(|v| (name, v))) {
2075            if cur_source == url.strip_prefix("sparse+").unwrap_or(url) {
2076                cur_source = name.into()
2077            }
2078            registries.insert(name, url.into());
2079        }
2080    }
2081
2082    if Url::parse(&cur_source).is_ok() {
2083        Err(format!("Non-crates.io registry specified and {} couldn't be found in the config file at {}. \
2084                     Due to a Cargo limitation we will not be able to install from there \
2085                     until it's given a [source.NAME] in that file!",
2086                    cur_source,
2087                    config_file.display()))?
2088    }
2089
2090    while let Some(repl) = replacements.get(&cur_source[..]) {
2091        cur_source = Cow::from(&repl[..]);
2092    }
2093
2094    registries.get(&cur_source[..])
2095        .map(|reg| (reg.strip_prefix("sparse+").unwrap_or(reg).to_string().into(), reg.starts_with("sparse+"), cur_source.to_string().into()))
2096        .ok_or_else(|| {
2097            format!("Couldn't find appropriate source URL for {} in {} (resolved to {:?})",
2098                    registry,
2099                    config_file.display(),
2100                    cur_source)
2101                .into()
2102        })
2103}
2104
2105/// Based on
2106/// https://github.com/rust-lang/cargo/blob/bb28e71202260180ecff658cd0fa0c7ba86d0296/src/cargo/sources/git/utils.rs#L344
2107/// and
2108/// https://github.com/rust-lang/cargo/blob/5102de2b7de997b03181063417f20874a06a67c0/src/cargo/sources/git/utils.rs#L644,
2109/// then
2110/// https://github.com/rust-lang/cargo/blob/5102de2b7de997b03181063417f20874a06a67c0/src/cargo/sources/git/utils.rs#L437
2111/// (see that link for full comments)
2112fn with_authentication<T, F>(url: &str, mut f: F) -> Result<T, GitError>
2113    where F: FnMut(&mut git2::Credentials) -> Result<T, GitError>
2114{
2115    let cfg = GitConfig::open_default().unwrap();
2116
2117    let mut cred_helper = git2::CredentialHelper::new(url);
2118    cred_helper.config(&cfg);
2119
2120    let mut ssh_username_requested = false;
2121    let mut cred_helper_bad = None;
2122    let mut ssh_agent_attempts = Vec::new();
2123    let mut any_attempts = false;
2124    let mut tried_ssh_key = false;
2125
2126    let mut res = f(&mut |url, username, allowed| {
2127        any_attempts = true;
2128
2129        if allowed.contains(CredentialType::USERNAME) {
2130            ssh_username_requested = true;
2131
2132            Err(GitError::from_str("username to be tried later"))
2133        } else if allowed.contains(CredentialType::SSH_KEY) && !tried_ssh_key {
2134            tried_ssh_key = true;
2135
2136            let username = username.unwrap();
2137            ssh_agent_attempts.push(username.to_string());
2138
2139            GitCred::ssh_key_from_agent(username)
2140        } else if allowed.contains(CredentialType::USER_PASS_PLAINTEXT) && cred_helper_bad.is_none() {
2141            let ret = GitCred::credential_helper(&cfg, url, username);
2142            cred_helper_bad = Some(ret.is_err());
2143            ret
2144        } else if allowed.contains(CredentialType::DEFAULT) {
2145            GitCred::default()
2146        } else {
2147            Err(GitError::from_str("no authentication available"))
2148        }
2149    });
2150
2151    if ssh_username_requested {
2152        // NOTE: this is the only divergence from the original cargo code: we also try cfg["user.name"]
2153        //       see https://github.com/nabijaczleweli/cargo-update/issues/110#issuecomment-533091965 for explanation
2154        for uname in cred_helper.username
2155            .into_iter()
2156            .chain(cfg.get_string("user.name"))
2157            .chain(["USERNAME", "USER"].iter().flat_map(env::var))
2158            .chain(Some("git".to_string())) {
2159            let mut ssh_attempts = 0;
2160
2161            res = f(&mut |_, _, allowed| {
2162                if allowed.contains(CredentialType::USERNAME) {
2163                    return GitCred::username(&uname);
2164                } else if allowed.contains(CredentialType::SSH_KEY) {
2165                    ssh_attempts += 1;
2166                    if ssh_attempts == 1 {
2167                        ssh_agent_attempts.push(uname.to_string());
2168                        return GitCred::ssh_key_from_agent(&uname);
2169                    }
2170                }
2171
2172                Err(GitError::from_str("no authentication available"))
2173            });
2174
2175            if ssh_attempts != 2 {
2176                break;
2177            }
2178        }
2179    }
2180
2181    if res.is_ok() || !any_attempts {
2182        res
2183    } else {
2184        let err = res.err().map(|e| format!("{}: ", e)).unwrap_or_default();
2185
2186        let mut msg = format!("{}failed to authenticate when downloading repository {}", err, url);
2187        if !ssh_agent_attempts.is_empty() {
2188            msg.push_str(" (tried ssh-agent, but none of the following usernames worked: ");
2189            for (i, uname) in ssh_agent_attempts.into_iter().enumerate() {
2190                if i != 0 {
2191                    msg.push_str(", ");
2192                }
2193                msg.push('\"');
2194                msg.push_str(&uname);
2195                msg.push('\"');
2196            }
2197            msg.push(')');
2198        }
2199
2200        if let Some(failed_cred_helper) = cred_helper_bad {
2201            msg.push_str(" (tried to find username+password via ");
2202            if failed_cred_helper {
2203                msg.push_str("git's credential.helper support, but failed)");
2204            } else {
2205                msg.push_str("credential.helper, but found credentials were incorrect)");
2206            }
2207        }
2208
2209        Err(GitError::from_str(&msg))
2210    }
2211}
2212
2213
2214/// Split and lower-case `cargo-update` into `[ca, rg, cargo-update]`, `jot` into `[3, j, jot]`, &c.
2215pub fn split_package_path(cratename: &str) -> Vec<Cow<'_, str>> {
2216    let mut elems = Vec::new();
2217    if cratename.is_empty() {
2218        panic!("0-length cratename");
2219    }
2220    if cratename.len() <= 3 {
2221        elems.push(["1", "2", "3"][cratename.len() - 1].into())
2222    }
2223    match cratename.len() {
2224        1 | 2 => {}
2225        3 => elems.push(lcase(&cratename[0..1])),
2226        _ => {
2227            elems.push(lcase(&cratename[0..2]));
2228            elems.push(lcase(&cratename[2..4]));
2229        }
2230    }
2231    elems.push(lcase(cratename));
2232    elems
2233}
2234
2235fn lcase(s: &str) -> Cow<'_, str> {
2236    if s.bytes().any(|b| b.is_ascii_uppercase()) {
2237        s.to_ascii_lowercase().into()
2238    } else {
2239        s.into()
2240    }
2241}
2242
2243/// Find package data in the specified cargo git index tree.
2244pub fn find_package_data<'t>(cratename: &str, registry: &Tree<'t>, registry_parent: &'t Repository) -> Option<Blob<'t>> {
2245    let elems = split_package_path(cratename);
2246
2247    let ent = registry.get_name(&elems[0])?;
2248    let obj = ent.to_object(registry_parent).ok()?;
2249    let ent = obj.as_tree()?.get_name(&elems[1])?;
2250    let obj = ent.to_object(registry_parent).ok()?;
2251    let obj = if elems.len() == 3 {
2252        let ent = obj.as_tree()?.get_name(&elems[2])?;
2253        ent.to_object(registry_parent).ok()?
2254    } else {
2255        obj
2256    };
2257    obj.into_blob().ok()
2258}
2259
2260/// Check if there's a proxy specified to be used.
2261///
2262/// Look for `http.proxy` key in the `config` file parallel to the specified crates file.
2263///
2264/// Then look for `git`'s `http.proxy`.
2265///
2266/// Then for the `http_proxy`, `HTTP_PROXY`, `https_proxy`, and `HTTPS_PROXY` environment variables, in that order.
2267///
2268/// Based on Cargo's [`http_proxy_exists()` and
2269/// `http_proxy()`](https://github.com/rust-lang/cargo/blob/eebd1da3a89e9c7788d109b3e615e1e25dc2cfcd/src/cargo/ops/registry.rs)
2270///
2271/// If a proxy is specified, but an empty string, treat it as unspecified.
2272///
2273/// # Examples
2274///
2275/// ```
2276/// # use cargo_update::ops::find_proxy;
2277/// # use std::env::temp_dir;
2278/// # let crates_file = temp_dir().join(".crates.toml");
2279/// match find_proxy(&crates_file) {
2280///     Some(proxy) => println!("Proxy found at {}", proxy),
2281///     None => println!("No proxy detected"),
2282/// }
2283/// ```
2284pub fn find_proxy(crates_file: &Path) -> Option<String> {
2285    if let Ok(crates_file) = fs::read_to_string(crates_file) {
2286        if let Some(toml::Value::String(proxy)) =
2287            toml::from_str::<toml::Value>(&crates_file)
2288                .unwrap()
2289                .get_mut("http")
2290                .and_then(|t| t.as_table_mut())
2291                .and_then(|t| t.remove("proxy")) {
2292            if !proxy.is_empty() {
2293                return Some(proxy);
2294            }
2295        }
2296    }
2297
2298    if let Ok(cfg) = GitConfig::open_default() {
2299        if let Ok(proxy) = cfg.get_string("http.proxy") {
2300            if !proxy.is_empty() {
2301                return Some(proxy);
2302            }
2303        }
2304    }
2305
2306    ["http_proxy", "HTTP_PROXY", "https_proxy", "HTTPS_PROXY"].iter().flat_map(env::var).filter(|proxy| !proxy.is_empty()).next()
2307}
2308
2309/// Find the bare git repository in the specified directory for the specified crate
2310///
2311/// The db directory is usually `$HOME/.cargo/git/db/`
2312///
2313/// The resulting paths are children of this directory in the format
2314/// [`{last_url_segment || "_empty"}-{hash(url)}`]
2315/// (https://github.com/rust-lang/cargo/blob/74f2b400d2be43da798f99f94957d359bc223988/src/cargo/sources/git/source.rs#L62-L73)
2316pub fn find_git_db_repo(git_db_dir: &Path, url: &str) -> Option<PathBuf> {
2317    let path = git_db_dir.join(format!("{}-{}",
2318                                       match Url::parse(url)
2319                                           .ok()?
2320                                           .path_segments()
2321                                           .and_then(|mut segs| segs.next_back())
2322                                           .unwrap_or("") {
2323                                           "" => "_empty",
2324                                           url => url,
2325                                       },
2326                                       cargo_hash(url)));
2327
2328    if path.is_dir() { Some(path) } else { None }
2329}
2330
2331
2332/// The short filesystem name for the repository, as used by `cargo`
2333///
2334/// Must be equivalent to
2335/// https://github.com/rust-lang/cargo/blob/74f2b400d2be43da798f99f94957d359bc223988/src/cargo/sources/registry/mod.rs#L387-L402
2336/// and
2337/// https://github.com/rust-lang/cargo/blob/74f2b400d2be43da798f99f94957d359bc223988/src/cargo/util/hex.rs
2338///
2339/// For main repository it's `github.com-1ecc6299db9ec823`
2340pub fn registry_shortname(url: &str) -> String {
2341    struct RegistryHash<'u>(&'u str);
2342    impl<'u> Hash for RegistryHash<'u> {
2343        fn hash<S: Hasher>(&self, hasher: &mut S) {
2344            SourceKind::Registry.hash(hasher);
2345            self.0.hash(hasher);
2346        }
2347    }
2348
2349    format!("{}-{}",
2350            Url::parse(url).map_err(|e| format!("{} not an URL: {}", url, e)).unwrap().host_str().unwrap_or(""),
2351            cargo_hash(RegistryHash(url)))
2352}
2353
2354/// Stolen from and equivalent to `short_hash()` from
2355/// https://github.com/rust-lang/cargo/blob/74f2b400d2be43da798f99f94957d359bc223988/src/cargo/util/hex.rs
2356#[allow(deprecated)]
2357pub fn cargo_hash<T: Hash>(whom: T) -> String {
2358    use std::hash::SipHasher;
2359
2360    let mut hasher = SipHasher::new_with_keys(0, 0);
2361    whom.hash(&mut hasher);
2362    let hash = hasher.finish();
2363    hex::encode(&[(hash >> 0) as u8,
2364                  (hash >> 8) as u8,
2365                  (hash >> 16) as u8,
2366                  (hash >> 24) as u8,
2367                  (hash >> 32) as u8,
2368                  (hash >> 40) as u8,
2369                  (hash >> 48) as u8,
2370                  (hash >> 56) as u8])
2371}
2372
2373/// These two are stolen verbatim from
2374/// https://github.com/rust-lang/cargo/blob/74f2b400d2be43da798f99f94957d359bc223988/src/cargo/core/source/source_id.rs#L48-L73
2375/// in order to match our hash with
2376/// https://github.com/rust-lang/cargo/blob/74f2b400d2be43da798f99f94957d359bc223988/src/cargo/core/source/source_id.rs#L510
2377#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2378#[allow(unused)]
2379enum SourceKind {
2380    Git(GitReference),
2381    Path,
2382    Registry,
2383    LocalRegistry,
2384    Directory,
2385}
2386
2387#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2388#[allow(unused)]
2389enum GitReference {
2390    Tag(String),
2391    Branch(String),
2392    Rev(String),
2393}
2394
2395
2396trait SemverExt {
2397    fn is_prerelease(&self) -> bool;
2398}
2399impl SemverExt for Semver {
2400    fn is_prerelease(&self) -> bool {
2401        !self.pre.is_empty()
2402    }
2403}