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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Tracks inputs and outputs by digest to help caching.
//!
//! When a package is built, we keep track of all the inputs
//! used to construct that package, as well as the output file
//! name. This information is then converted into an [ArtifactManifest],
//! which tracks the digests of all those inputs, and this manifest
//! is written to the [CACHE_SUBDIRECTORY] within the output directory.
//!
//! When re-building, we can look up this manifest: if all the inputs
//! to build a package are the same, the output should be the same, so
//! we can use the cached output to avoid an unnecessary package construction
//! step.

use crate::digest::{DefaultDigest, Digest, FileDigester};
use crate::input::{BuildInput, BuildInputs};

use anyhow::{anyhow, bail, Context};
use camino::{Utf8Path, Utf8PathBuf};
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
use thiserror::Error;
use tokio::fs::File;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

pub const CACHE_SUBDIRECTORY: &str = "manifest-cache";

pub type Inputs = Vec<BuildInput>;

// It's not actually a map, because serde doesn't like enum keys.
//
// This has the side-effect that changing the order of input files
// changes the package.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct InputMap(Vec<InputEntry>);

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct InputEntry {
    key: BuildInput,
    value: Option<Digest>,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArtifactManifest<D = DefaultDigest> {
    // All inputs, which create this artifact
    inputs: InputMap,

    // Output, created by this artifact
    output_path: Utf8PathBuf,

    // Which digest is being used?
    phantom: PhantomData<D>,
}

impl<D: FileDigester> ArtifactManifest<D> {
    /// Reads all inputs and outputs, collecting their digests.
    async fn new(inputs: &BuildInputs, output_path: Utf8PathBuf) -> anyhow::Result<Self> {
        let result = Self::new_internal(inputs, output_path, None).await?;
        Ok(result)
    }

    // If the optional "compare_with" field is supplied, construction
    // of the ArtifactManifest exits early if any of the inputs are not
    // equal to the digests found in "compare_with". This helps improve
    // the "cache miss" case, by allowing us to stop calculating hashes
    // as soon as we find any divergence.
    async fn new_internal(
        inputs: &BuildInputs,
        output_path: Utf8PathBuf,
        compare_with: Option<&Self>,
    ) -> Result<Self, CacheError> {
        let input_entry_tasks = inputs.0.iter().cloned().enumerate().map(|(i, input)| {
            let expected_input = compare_with.map(|manifest| &manifest.inputs.0[i]);
            async move {
                let digest = if let Some(input_path) = input.input_path() {
                    Some(D::get_digest(input_path).await?)
                } else {
                    None
                };
                let input = InputEntry {
                    key: input.clone(),
                    value: digest,
                };

                if let Some(expected_input) = expected_input {
                    if *expected_input != input {
                        CacheError::miss(format!(
                            "Differing build inputs.\nSaw {:#?}\nExpected {:#?})",
                            input, expected_input
                        ));
                    }
                };

                Ok::<_, CacheError>(input)
            }
        });

        let inputs = InputMap(futures::future::try_join_all(input_entry_tasks).await?);

        Ok(Self {
            inputs,
            output_path,
            phantom: PhantomData,
        })
    }

    // Writes a manifest file to a particular location.
    async fn write_to(&self, path: &Utf8PathBuf) -> anyhow::Result<()> {
        let Some(extension) = path.extension() else {
            bail!("Missing extension?");
        };
        if extension != "json" {
            bail!("JSON encoding is all we know. Write to a '.json' file?");
        }
        let serialized =
            serde_json::to_string(&self).context("Failed to serialize ArtifactManifest to JSON")?;

        let mut f = File::create(path).await?;
        f.write_all(serialized.as_bytes()).await?;
        Ok(())
    }

    // Reads a manifest file to a particular location.
    //
    // Does not validate whether or not any corresponding artifacts exist.
    async fn read_from(path: &Utf8PathBuf) -> Result<Self, CacheError> {
        let Some(extension) = path.extension() else {
            return Err(anyhow!("Missing extension?").into());
        };
        if extension != "json" {
            return Err(anyhow!("JSON encoding is all we know. Read from a '.json' file?").into());
        }

        let mut f = match File::open(path).await {
            Ok(f) => f,
            Err(e) => {
                if matches!(e.kind(), std::io::ErrorKind::NotFound) {
                    return Err(CacheError::miss(format!("File {} not found", path)));
                } else {
                    return Err(anyhow!(e).into());
                }
            }
        };
        let mut buffer = String::new();
        f.read_to_string(&mut buffer)
            .await
            .map_err(|e| anyhow!(e))?;

        // In the case that we cannot read the manifest, treat it as "missing".
        // This will force a rebuild anyway.
        let Ok(manifest) = serde_json::from_str(&buffer) else {
            return Err(CacheError::miss(format!(
                "Cannot parse manifest at {}",
                path
            )));
        };
        Ok(manifest)
    }
}

/// Errors that can be returned when looking up cached artifacts.
#[derive(Error, Debug)]
pub enum CacheError {
    /// Identifies that cache lookup has failed, for a wide number of reasons,
    /// but that we should probably try to continue with package building
    /// anyway.
    #[error("Cache Miss: {reason}")]
    CacheMiss { reason: String },

    /// Other errors, which could indicate a more fundamental problem.
    ///
    /// These errors encourage callers to exit immediately, rather than
    /// treating the failure like a "miss".
    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

impl CacheError {
    // Convenience wrapper
    fn miss<T: Into<String>>(t: T) -> Self {
        CacheError::CacheMiss { reason: t.into() }
    }
}

/// Provides access to a set of manifests describing packages.
///
/// Provides two primary operations:
/// - [Self::lookup]: Support for finding previously-built packages
/// - [Self::update]: Support for updating a package's latest manifest
pub struct Cache {
    disabled: bool,
    cache_directory: Utf8PathBuf,
}

impl Cache {
    /// Ensures the cache directory exists within the output directory
    pub async fn new(output_directory: &Utf8Path) -> anyhow::Result<Self> {
        let cache_directory = output_directory.join(CACHE_SUBDIRECTORY);
        tokio::fs::create_dir_all(&cache_directory).await?;
        Ok(Self {
            disabled: false,
            cache_directory,
        })
    }

    /// If "disable" is true, causes cache operations to be no-ops.
    /// Otherwise, causes the cache to act normally.
    pub fn set_disable(&mut self, disable: bool) {
        self.disabled = disable;
    }

    /// Looks up an entry from the cache.
    ///
    /// Confirms that the artifact exists.
    pub async fn lookup(
        &self,
        inputs: &BuildInputs,
        output_path: &Utf8Path,
    ) -> Result<ArtifactManifest, CacheError> {
        if self.disabled {
            return Err(CacheError::miss("Cache disabled"));
        }

        let artifact_filename = output_path
            .file_name()
            .ok_or_else(|| CacheError::Other(anyhow!("Output has no file name")))?;
        let mut manifest_filename = String::from(artifact_filename);
        manifest_filename.push_str(".json");

        let manifest_path = self.cache_directory.join(manifest_filename);

        // Look up the manifest file in the cache
        let manifest = ArtifactManifest::read_from(&manifest_path).await?;

        // Do a quick check if the input files are different.
        //
        // We'll actually validate the digests later, but this lets us bail
        // early if any files were added or removed.
        if inputs
            .0
            .iter()
            .ne(manifest.inputs.0.iter().map(|entry| &entry.key))
        {
            return Err(CacheError::miss("Set of inputs has changed"));
        }
        if output_path != manifest.output_path {
            return Err(CacheError::miss(format!(
                "Output path changed from {} -> {}",
                manifest.output_path, output_path,
            )));
        }

        // Confirm the output file exists
        if !tokio::fs::try_exists(&output_path)
            .await
            .map_err(|e| CacheError::miss(format!("Cannot locate output artifact: {e}")))?
        {
            return Err(CacheError::miss("Output does not exist"));
        }

        // Confirm the output matches.
        let Some(observed_filename) = manifest.output_path.file_name() else {
            return Err(CacheError::miss(format!(
                "Missing output file name from manifest {}",
                manifest.output_path
            )));
        };
        if observed_filename != artifact_filename {
            return Err(CacheError::miss(format!(
                "Wrong output name in manifest (saw {}, expected {})",
                observed_filename, artifact_filename
            )));
        }

        // Finally, compare the manifests, including their digests.
        //
        // This calculation bails out early if any inputs don't match.
        let calculated_manifest =
            ArtifactManifest::new_internal(inputs, output_path.to_path_buf(), Some(&manifest))
                .await?;

        // This is a hard stop-gap against any other differences in the
        // manifests. The error message here is worse (we don't know "why"),
        // but it's a quick check that's protective.
        if calculated_manifest != manifest {
            return Err(CacheError::miss("Manifests appear different"));
        }

        Ok(manifest)
    }

    /// Updates an artifact's entry within the cache
    pub async fn update(
        &self,
        inputs: &BuildInputs,
        output_path: &Utf8Path,
    ) -> Result<(), CacheError> {
        if self.disabled {
            // Return immediately, regardless of the input. We have nothing to
            // calculate, and nothing to save.
            return Ok(());
        }

        // This call actually acquires the digests for all inputs
        let manifest =
            ArtifactManifest::<DefaultDigest>::new(inputs, output_path.to_path_buf()).await?;

        let Some(artifact_filename) = manifest.output_path.file_name() else {
            return Err(anyhow!("Bad manifest: Missing output name").into());
        };

        let mut manifest_filename = String::from(artifact_filename);
        manifest_filename.push_str(".json");
        let manifest_path = self.cache_directory.join(manifest_filename);
        manifest.write_to(&manifest_path).await?;

        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::input::MappedPath;
    use camino::Utf8PathBuf;
    use camino_tempfile::{tempdir, Utf8TempDir};

    struct CacheTest {
        _input_dir: Utf8TempDir,
        output_dir: Utf8TempDir,

        input_path: Utf8PathBuf,
        output_path: Utf8PathBuf,
    }

    impl CacheTest {
        fn new() -> Self {
            let input_dir = tempdir().unwrap();
            let output_dir = tempdir().unwrap();
            let input_path = input_dir.path().join("binary.exe");
            let output_path = output_dir.path().join("output.tar.gz");
            Self {
                _input_dir: input_dir,
                output_dir,
                input_path,
                output_path,
            }
        }

        async fn create_input(&self, contents: &str) {
            tokio::fs::write(&self.input_path, contents).await.unwrap()
        }

        async fn create_output(&self, contents: &str) {
            tokio::fs::write(&self.output_path, contents).await.unwrap()
        }

        async fn remove_output(&self) {
            tokio::fs::remove_file(&self.output_path).await.unwrap()
        }
    }

    fn expect_missing_manifest(err: &CacheError, file: &str) {
        match &err {
            CacheError::CacheMiss { reason } => {
                let expected = format!("{file}.json not found");
                assert!(reason.contains(&expected), "{}", reason);
            }
            _ => panic!("Unexpected error: {}", err),
        }
    }

    fn expect_cache_disabled(err: &CacheError) {
        match &err {
            CacheError::CacheMiss { reason } => {
                assert!(reason.contains("Cache disabled"), "{}", reason);
            }
            _ => panic!("Unexpected error: {}", err),
        }
    }

    fn expect_changed_manifests(err: &CacheError) {
        match &err {
            CacheError::CacheMiss { reason } => {
                assert!(reason.contains("Manifests appear different"), "{}", reason);
            }
            _ => panic!("Unexpected error: {}", err),
        }
    }

    fn expect_missing_output(err: &CacheError) {
        match &err {
            CacheError::CacheMiss { reason } => {
                assert!(reason.contains("Output does not exist"), "{}", reason);
            }
            _ => panic!("Unexpected error: {}", err),
        }
    }

    #[tokio::test]
    async fn test_cache_lookup_misses_before_update() {
        let test = CacheTest::new();

        test.create_input("Hi I'm the input file").await;
        let inputs = BuildInputs(vec![BuildInput::add_file(MappedPath {
            from: test.input_path.to_path_buf(),
            to: Utf8PathBuf::from("/very/important/file"),
        })
        .unwrap()]);

        let cache = Cache::new(test.output_dir.path()).await.unwrap();

        // Look for the package in the cache. It shouldn't exist.
        let err = cache.lookup(&inputs, &test.output_path).await.unwrap_err();
        expect_missing_manifest(&err, "output.tar.gz");

        // Create the output we're expecting
        test.create_output("Hi I'm the output file").await;

        // Still expect a failure; we haven't called "cache.update".
        let err = cache.lookup(&inputs, &test.output_path).await.unwrap_err();
        expect_missing_manifest(&err, "output.tar.gz");
    }

    #[tokio::test]
    async fn test_cache_lookup_hits_after_update() {
        let test = CacheTest::new();

        test.create_input("Hi I'm the input file").await;
        let inputs = BuildInputs(vec![BuildInput::add_file(MappedPath {
            from: test.input_path.to_path_buf(),
            to: Utf8PathBuf::from("/very/important/file"),
        })
        .unwrap()]);

        // Create the output we're expecting
        test.create_output("Hi I'm the output file").await;

        let cache = Cache::new(test.output_dir.path()).await.unwrap();

        // If we update the cache, we expect a hit.
        cache.update(&inputs, &test.output_path).await.unwrap();
        cache.lookup(&inputs, &test.output_path).await.unwrap();

        // If we update the input again, we expect a miss.
        test.create_input("hi i'M tHe InPuT fIlE").await;
        let err = cache.lookup(&inputs, &test.output_path).await.unwrap_err();
        expect_changed_manifests(&err);
    }

    #[tokio::test]
    async fn test_cache_lookup_misses_after_removing_output() {
        let test = CacheTest::new();

        test.create_input("Hi I'm the input file").await;
        let inputs = BuildInputs(vec![BuildInput::add_file(MappedPath {
            from: test.input_path.to_path_buf(),
            to: Utf8PathBuf::from("/very/important/file"),
        })
        .unwrap()]);

        // Create the output we're expecting
        test.create_output("Hi I'm the output file").await;

        let cache = Cache::new(test.output_dir.path()).await.unwrap();

        // If we update the cache, we expect a hit.
        cache.update(&inputs, &test.output_path).await.unwrap();
        cache.lookup(&inputs, &test.output_path).await.unwrap();

        // If we remove the output file, we expect a miss.
        // This is somewhat of a "special case", as all the inputs are the same.
        test.remove_output().await;
        let err = cache.lookup(&inputs, &test.output_path).await.unwrap_err();
        expect_missing_output(&err);
    }

    #[tokio::test]
    async fn test_cache_disabled_always_misses() {
        let test = CacheTest::new();

        test.create_input("Hi I'm the input file").await;
        let inputs = BuildInputs(vec![BuildInput::add_file(MappedPath {
            from: test.input_path.to_path_buf(),
            to: Utf8PathBuf::from("/very/important/file"),
        })
        .unwrap()]);

        // Create the output we're expecting
        test.create_output("Hi I'm the output file").await;

        let mut cache = Cache::new(test.output_dir.path()).await.unwrap();
        cache.set_disable(true);

        // Updating the cache should still succeed, though it'll do nothing.
        cache.update(&inputs, &test.output_path).await.unwrap();

        // The lookup will miss, as the cache has been disabled.
        let err = cache.lookup(&inputs, &test.output_path).await.unwrap_err();
        expect_cache_disabled(&err);
    }
}