summaryrefslogtreecommitdiffstats
path: root/src/repository/repository.rs
blob: 5b358f331b504ae06548991cd571e5e66169c18f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//
// Copyright (c) 2020-2021 science+computing ag and other contributors
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//

use std::collections::BTreeMap;
use std::path::Path;
use std::path::PathBuf;

use anyhow::anyhow;
use anyhow::Context;
use anyhow::Error;
use anyhow::Result;
use log::trace;
use resiter::Map;

use crate::package::Package;
use crate::package::PackageName;
use crate::package::PackageVersion;
use crate::package::PackageVersionConstraint;
use crate::repository::fs::FileSystemAccessor;

/// A repository represents a collection of packages
pub struct Repository {
    inner: BTreeMap<(PackageName, PackageVersion), Package>,
}

#[cfg(test)]
impl From<BTreeMap<(PackageName, PackageVersion), Package>> for Repository {
    fn from(inner: BTreeMap<(PackageName, PackageVersion), Package>) -> Self {
        Repository { inner }
    }
}

impl Repository {
    pub async fn load(path: &Path, progress: &indicatif::ProgressBar) -> Result<Self> {
        use futures::StreamExt;
        use futures::FutureExt;

        async fn all_subdirs(p: PathBuf, accessor: FileSystemAccessor) -> Result<impl futures::stream::Stream<Item = Result<PathBuf>>> {
            let stream = accessor.read_dir(p)
                .await?
                .then(move |entry| {
                    async move {
                        let de = entry?;
                        let is_dir = de.file_type().await?.is_dir();
                        let is_hidden = de
                            .path()
                            .file_name()
                            .and_then(|s| s.to_str())
                            .map(|s| s.starts_with('.'))
                            .unwrap_or(false);

                        if is_dir && !is_hidden {
                            Ok(Some(de.path()))
                        } else {
                            Ok(None)
                        }
                    }
                })
                .filter_map(|t| async {
                    match t {
                        Err(e) => Some(Err(e)),
                        Ok(None) => None,
                        Ok(Some(o)) => Some(Ok(o)),
                    }
                });

            Ok(stream)
        }

        fn load_recursive<'a>(
            root: &'a Path,
            path: &'a Path,
            mut config: config::Config,
            progress: &'a indicatif::ProgressBar,
            accessor: FileSystemAccessor,
        ) -> futures::future::BoxFuture<'a, Result<Vec<Result<Package>>>> {
            async move {
                let pkg_file = path.join("pkg.toml");

                if accessor.is_file(&pkg_file).await? {
                    log::trace!("Reading: {}", pkg_file.display());
                    let buf = accessor.read_to_string(&pkg_file)
                        .await
                        .with_context(|| format!("Reading {}", pkg_file.display()))?;

                    // This function has an issue: It loads packages recursively, but if there are
                    // patches set for a package, these patches are set _relative_ to the current
                    // pkg.toml file.
                    //
                    // E.G.:
                    // (1) /pkg.toml
                    // (2) /a/pkg.toml
                    // (3) /a/1.0/pkg.toml
                    // (4) /a/2.0/pkg.toml
                    //
                    // If (2) defines a patches = ["./1.patch"], the patch exists at /a/1.patch.
                    // We can fix that by modifying the Config object after loading (2) and fixing the
                    // path of the patch to be relative to the repostory root.
                    //
                    // But if we continue loading the /a/ subdirectory recursively, this value gets
                    // overwritten by Config::refresh(), which is called by Config::merge, for example.
                    //
                    // The trick is, to get the list of patches _before_ the merge, and later
                    // re-setting them after the merge, if there were no new patches set (which itself
                    // is tricky to find out, because the `Config` object _looks like_ there is a new
                    // array set).
                    //
                    // If (3), for example, does set a new patches=[] array, the old array is
                    // invalidated and no longer relevant for that package!
                    // Thus, we can savely throw it away and continue with the new array, fixing the
                    // pathes to be relative to repo root again.
                    //
                    // If (4) does _not_ set any patches, we must ensure that the patches from the
                    // loading of (2) are used and not overwritten by the Config::refresh() call
                    // happening during Config::merge().
                    //

                    // first of all, we get the patches array.
                    // This is either the patches array from the last recursion or the newly set one,
                    // that doesn't matter here.
                    log::trace!("Fetching patches before merge");
                    let patches_before_merge = match config.get_array("patches") {
                        Ok(v)  => {
                            log::trace!("Array found");
                            v.into_iter()
                                .map(|p| {
                                    log::trace!("Patch path to string: {:?}", p);
                                    p.into_str()
                                        .map(PathBuf::from)
                                        .with_context(|| anyhow!("patches must be strings"))
                                        .map_err(Error::from)
                                })
                                .collect::<Result<Vec<_>>>()
                                .context("Collecting patches")?
                        },
                        Err(config::ConfigError::NotFound(_)) => vec![],
                        Err(e)                                => return Err(e).map_err(Error::from),
                    };
                    trace!("[{}] Patches before merging: {:?}", path.display(), patches_before_merge);

                    // Merge the new pkg.toml file over the already loaded configuration
                    config
                        .merge(config::File::from_str(&buf, config::FileFormat::Toml))
                        .with_context(|| format!("Loading contents of {}", pkg_file.display()))?;

                    let path_relative_to_root = path.strip_prefix(root)?;

                    // get the patches that are in the `config` object after the merge
                    log::trace!("Fetching patches after merge");
                    let patches = match config.get_array("patches") {
                        Ok(v) => {
                            trace!("[{}] Patches after merging: {:?}", path.display(), v);
                            v
                        },

                        // if there was none, we simply use an empty array
                        // This is cheap because Vec::with_capacity(0) does not allocate
                        Err(config::ConfigError::NotFound(_)) => Vec::with_capacity(0),
                        Err(e)                                => return Err(e).map_err(Error::from),
                    }
                    .into_iter()

                    // Map all `Value`s to String and then join them on the path that is relative to
                    // the root directory of the repository.
                    .map(|patch| patch.into_str().map_err(Error::from))
                    .map_ok(|patch| path_relative_to_root.join(patch))
                    .inspect(|patch| trace!("[{}] Patch relative to root: {:?}", path.display(), patch.as_ref().map(|p| p.display())));

                    // now, we need to "manually" iter over the iterator here, because we need to
                    // call FileSystemAccessor::exists for each patch, and the method is async.
                    //
                    // We collect the patches "manually" here into a preallocated vector of decent
                    // size
                    log::trace!("Allocating collection for found patches with size {}", patches.size_hint().0);
                    let mut collected_patches = Vec::with_capacity(patches.size_hint().0);
                    log::trace!("Collecting patches...");
                    for patch in patches {
                        match patch {
                            Ok(patch) => if accessor.exists(&patch).await? {
                                trace!("[{}] Path to patch exists: {}", path.display(), patch.display());

                                collected_patches.push(patch);
                            } else if patches_before_merge.iter().any(|pb| pb.file_name() == patch.file_name()) {
                                // We have a patch already in the array that is named equal to the patch
                                // we have in the current recursion.
                                // It seems like this patch was already in the list and we re-found it
                                // because we loaded a deeper pkg.toml file.
                                /* do nothing */
                            } else {
                                trace!("[{}] Path to patch does not exist: {}", path.display(), patch.display());
                                Err(anyhow!("Patch does not exist: {}", patch.display()))
                                    .context("Collecting patches after merge")?
                            },

                            Err(e) => Err(e).context("Collecting patches after merge")?,
                        }
                    }
                    log::trace!("Collected patches");
                    let patches = collected_patches; // rebind variable

                    // If we found any patches, use them. Otherwise use the array from before the merge
                    // (which already has the correct pathes from the previous recursion).
                    //
                    // We don't need to check whether the patches all exist, because we checked
                    // that above when collecting them
                    let patches = if !patches.is_empty() /* && patches.iter().all(|p| accessor.exists(p).await?) */ {
                        patches
                    } else {
                        patc