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