1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//! An index from which information/metadata about available packages is obtained.
//!
//! ## Design
//! Indices provide metadata about available packages from a particular source. Multiple indices
//! can be specified at once, and a default index can be specified in configuration for packages
//! with version constraints but no explicitly specified index. By default, the official index is
//! set to be the default.
//!
//! The packages that the index offers must have a direct source: they cannot point to other
//! registries. Because the package doesn't necessarily need to be a tarball stored somewhere,
//! indices can serve to "curate" packages from disparate repositories and other sources (think
//! Purescript package sets). The dependencies of a package in an index must be located either in
//! the same index or a dependent index of the current index (as specified in the index's config).
//!
//! Tarballs are the only source which can contain a checksum, by nature of the way they're
//! constructed internally.
//!
//! A package can only be published to the official index if it only depends on packages in the
//! official index.
//!
//! ## Prior art
//! This design follows closely with that of Cargo's, specifically with their RFC enabling
//! [unofficial registries](https://github.com/rust-lang/rfcs/blob/master/text/2141-alternative-registries.md).

use super::registry::Registry;
use crate::{
    package::*,
    remote::resolution::{DirectRes, IndexRes, Resolution},
    util::{
        errors::{ErrorKind, Res},
        lock::DirLock,
    },
};
use failure::{format_err, Error, ResultExt};
use indexmap::IndexMap;
use semver::Version;
use semver_constraints::Constraint;
use serde::{Deserialize, Serialize};
use serde_json;
use simsearch::{SearchOptions, SimSearch};
use std::{
    fs,
    io::{self, prelude::*, BufReader},
    str::FromStr,
};
use toml;
use walkdir::WalkDir;

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct IndexConfig {
    pub index: IndexConfInner,
}

impl FromStr for IndexConfig {
    type Err = Error;

    fn from_str(raw: &str) -> Result<Self, Self::Err> {
        toml::from_str(raw)
            .context(format_err!("invalid index config"))
            .map_err(Error::from)
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct IndexConfInner {
    pub secure: bool,
    pub dependencies: IndexMap<String, IndexRes>,
    pub registry: Option<Registry>,
}

impl Default for IndexConfInner {
    fn default() -> Self {
        IndexConfInner {
            secure: false,
            dependencies: IndexMap::new(),
            registry: None,
        }
    }
}

/// A dependency.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct Dep<T> {
    pub name: Name,
    pub index: T,
    pub req: Constraint,
}

pub type ResolvedDep = Dep<IndexRes>;
pub type TomlDep = Dep<Option<String>>;

#[derive(Debug, Default)]
pub struct Indices {
    /// The indices being used.
    ///
    /// It is assumed that all dependent indices have been resolved, and that this mapping contains
    /// every index mentioned or depended on.
    pub indices: IndexMap<IndexRes, Index>,
    pub cache: IndexMap<PackageId, IndexMap<Version, ResolvedEntry>>,
}

impl Indices {
    pub fn new(indices: Vec<Index>) -> Self {
        let indices = indices.into_iter().map(|i| (i.id.clone(), i)).collect();
        let cache = IndexMap::new();

        Indices { indices, cache }
    }

    pub fn select_by_spec(&self, spec: &Spec) -> Res<Summary> {
        // For simplicity's sake, we don't do any caching here. It's not really necessary.
        let mut res = None;
        for (ir, ix) in &self.indices {
            if spec.resolution.is_none() || Some(&ir.clone().into()) == spec.resolution.as_ref() {
                if let Ok(es) = ix.entries(&spec.name) {
                    // We don't want to give back yanked packages
                    if let Some(x) = es
                        .into_iter()
                        .filter(|x| {
                            !x.1.yanked
                                && (spec.version.is_none()
                                    || Some(&x.1.version) == spec.version.as_ref())
                        })
                        .last()
                    {
                        if let Some(existing) = res {
                            return Err(format_err!(
                                "spec `{}` is ambiguous, and matches both {} and {}@{}|{}",
                                &spec,
                                existing,
                                &spec.name,
                                ir,
                                x.0
                            ));
                        } else {
                            res = Some(Summary::new(
                                PackageId::new(spec.name.clone(), ir.clone().into()),
                                x.0,
                            ));
                        }
                    }
                }
            }
        }

        Ok(res.ok_or_else(|| ErrorKind::PackageNotFound)?)
    }

    pub fn select(&mut self, pkg: &Summary) -> Res<&ResolvedEntry> {
        let entry = self
            .entries(pkg.id())?
            .get(pkg.version())
            .ok_or_else(|| ErrorKind::PackageNotFound)?;

        Ok(entry)
    }

    // This assumes that the packages have already been loaded into the cache.
    pub fn count_versions(&self, pkg: &PackageId) -> usize {
        self.cache.get(pkg).map(|m| m.len()).unwrap_or(0)
    }

    pub fn entries(&mut self, pkg: &PackageId) -> Res<&IndexMap<Version, ResolvedEntry>> {
        if self.cache.contains_key(pkg) {
            return Ok(&self.cache[pkg]);
        }

        let res = pkg.resolution();
        if let Resolution::Index(ir) = res {
            let ix = self.indices.get(ir);

            if let Some(ix) = ix {
                let mut v = ix.entries(pkg.name())?;
                v.sort_keys();
                self.cache.insert(pkg.clone(), v);
                Ok(&self.cache[pkg])
            } else {
                Err(Error::from(ErrorKind::PackageNotFound))
            }
        } else {
            Err(Error::from(ErrorKind::PackageNotFound))
        }
    }

    pub fn search(&self, query: &str) -> Res<Vec<(Name, Version, &IndexRes)>> {
        let mut engine: SimSearch<(&IndexRes, &str)> =
            SimSearch::new_with(SearchOptions::new().stop_words(&["/", "\\"]));
        let x = self
            .indices
            .iter()
            .map(|x| x.1.packages().map(move |p| (x.0, p)))
            .flatten()
            .collect::<Vec<_>>();

        for (ir, pkg) in &x {
            engine.insert((ir, pkg), pkg);
        }
        let pkgs = engine.search(query);

        pkgs.iter().map(|(ir, pkg)| {
            let name = Name::from_str(pkg).unwrap();
            let ix = &self.indices[*ir];
            let ver: Version = ix.entries(&name)?.into_iter().map(|x| x.0).last().unwrap();

            Ok((name, ver, *ir))
        }).collect::<Res<Vec<_>>>()
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct IndexEntry<T, L> {
    pub name: Name,
    pub version: Version,
    pub dependencies: Vec<Dep<T>>,
    pub yanked: bool,
    pub location: L,
}

pub type ResolvedEntry = IndexEntry<IndexRes, DirectRes>;
pub type TomlEntry = IndexEntry<Option<String>, Option<DirectRes>>;

/// Struct `Index` defines a single index.
///
/// Indices must be sharded by group name.
#[derive(Debug)]
pub struct Index {
    /// Indicates identifying information about the index
    pub id: IndexRes,
    /// Indicates where this index is stored on-disk.
    pub path: DirLock,
    /// The configuration of this index.
    pub config: IndexConfig,
}

impl Index {
    /// Creates a new empty package index directly from a Url and a local path.
    pub fn from_disk(res: DirectRes, path: DirLock) -> Res<Self> {
        let id = IndexRes { res };
        let pn = path.path().join("index.toml");
        let file = fs::File::open(&pn)
            .with_context(|e| format_err!("couldn't open index config {}: {}", pn.display(), e))?;
        let mut file = BufReader::new(file);
        let mut contents = String::new();
        file.read_to_string(&mut contents)
            .with_context(|e| format_err!("couldn't read index config {}: {}", pn.display(), e))?;
        let config = IndexConfig::from_str(&contents)?;

        Ok(Index { id, path, config })
    }

    pub fn entries(&self, name: &Name) -> Res<IndexMap<Version, ResolvedEntry>> {
        let mut res = IndexMap::new();
        let path = self.path.path().join(name.as_normalized());
        let file = fs::File::open(path).context(ErrorKind::PackageNotFound)?;
        let r = io::BufReader::new(&file);

        for (lix, line) in r.lines().enumerate() {
            let entry: TomlEntry = serde_json::from_str(&line?).context(format_err!(
                "index entry {} for package {} is invalid",
                lix + 1,
                name
            ))?;

            let dependencies = entry
                .dependencies
                .into_iter()
                .map(|x| {
                    let index = x
                        .index
                        .and_then(|ix| self.config.index.dependencies.get(&ix))
                        .cloned()
                        .unwrap_or_else(|| self.id.clone());
                    Dep {
                        index,
                        name: x.name,
                        req: x.req,
                    }
                })
                .collect::<Vec<_>>();

            let location = if let Some(url) = &self.config.index.registry {
                if let Some(eloc) = entry.location {
                    Ok(eloc)
                } else {
                    Ok(DirectRes::Registry {
                        registry: url.clone(),
                        name: name.clone(),
                        version: entry.version.clone(),
                    })
                }
            } else {
                entry.location.ok_or_else(|| {
                    format_err!(
                        "no location for index entry {} of package {}",
                        lix + 1,
                        name
                    )
                })
            }?;

            let entry: ResolvedEntry = IndexEntry {
                name: entry.name,
                version: entry.version,
                dependencies,
                yanked: entry.yanked,
                location,
            };

            res.insert(entry.version.clone(), entry);
        }

        Ok(res)
    }

    pub fn packages(&self) -> impl Iterator<Item = String> {
        let root_path = self.path.path().to_path_buf();
        let git_path = root_path.join(".git");
        WalkDir::new(self.path.path())
            .min_depth(2)
            .max_depth(3)
            .into_iter()
            .filter_entry(move |x| x.path().parent().unwrap() != git_path)
            .filter_map(|x| x.ok())
            .map(move |x| {
                let stripped = x.path().strip_prefix(&root_path).unwrap();
                stripped.to_string_lossy().replace("\\", "/").to_string()
            })
    }

    pub fn depends(&self) -> impl Iterator<Item = &IndexRes> {
        self.config.index.dependencies.iter().map(|x| x.1)
    }

    pub fn registry(&self) -> Option<&Registry> {
        self.config.index.registry.as_ref()
    }
}