microsandbox-image 0.7.2

OCI image pulling, layer extraction, and caching for microsandbox.
Documentation
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
//! Global on-disk image and layer cache.

use std::io::Read;
use std::path::{Path, PathBuf};

use oci_client::Reference;
use serde::{Deserialize, Serialize};
use sha2::{Digest as Sha2Digest, Sha256};

use crate::{
    config::ImageConfig,
    digest::Digest,
    erofs::ErofsReader,
    error::{ImageError, ImageResult},
};

//--------------------------------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------------------------------

/// Subdirectory for per-layer EROFS images (keyed by diff_id).
const LAYERS_DIR: &str = "layers";

/// Subdirectory for fsmeta EROFS images (keyed by manifest digest).
const FSMETA_DIR: &str = "fsmeta";

/// Subdirectory for VMDK descriptors (keyed by manifest digest).
const VMDK_DIR: &str = "vmdk";

/// Root directory for reusable flat ext4 artifacts.
const FLAT_DIR: &str = "flat";
const FLAT_REFS_DIR: &str = "refs";
const FLAT_BLOBS_DIR: &str = "blobs";
const FLAT_LOCKS_DIR: &str = "locks";

/// Subdirectory for cached manifest + config metadata.
const MANIFESTS_DIR: &str = "manifests";

/// Subdirectory for transient staging (downloads, work dirs).
const TMP_DIR: &str = "tmp";

/// EROFS images are emitted in 4 KiB filesystem blocks.
const EROFS_ALIGNMENT_BYTES: u64 = 4096;

//--------------------------------------------------------------------------------------------------
// Types
//--------------------------------------------------------------------------------------------------

/// On-disk global cache for OCI layers and EROFS images.
///
/// Layout:
/// ```text
/// ~/.microsandbox/cache/manifests/<sha256-of-ref>.json       # manifest + config metadata
/// ~/.microsandbox/cache/tmp/<blob>.part                      # partial downloads
/// ~/.microsandbox/cache/tmp/<blob>.download.lock             # download flock files
/// ~/.microsandbox/cache/tmp/<blob>.work/                     # materialization work dirs
/// ~/.microsandbox/cache/layers/<diff_id_safe>.erofs          # per-layer EROFS
/// ~/.microsandbox/cache/layers/<diff_id_safe>.erofs.lock     # materialization flock
/// ~/.microsandbox/cache/fsmeta/<manifest_safe>.erofs         # fsmeta EROFS (fsmerge metadata)
/// ~/.microsandbox/cache/fsmeta/<manifest_safe>.erofs.lock    # materialization flock
/// ~/.microsandbox/cache/vmdk/<manifest_safe>.vmdk            # VMDK descriptor
/// ~/.microsandbox/cache/vmdk/<manifest_safe>.vmdk.lock       # materialization flock
/// ```
#[derive(Clone)]
pub struct GlobalCache {
    /// Root of the layer EROFS cache (`~/.microsandbox/cache/layers/`).
    layers_dir: PathBuf,

    /// Root of the fsmeta EROFS cache (`~/.microsandbox/cache/fsmeta/`).
    fsmeta_dir: PathBuf,

    /// Root of the VMDK descriptor cache (`~/.microsandbox/cache/vmdk/`).
    vmdk_dir: PathBuf,

    /// Manifest-keyed references to immutable flat artifacts.
    flat_refs_dir: PathBuf,

    /// Content-addressed immutable raw ext4 artifacts.
    flat_blobs_dir: PathBuf,

    /// Per-derivation materialization locks.
    flat_locks_dir: PathBuf,

    /// Root of the manifest metadata cache (`~/.microsandbox/cache/manifests/`).
    manifests_dir: PathBuf,

    /// Root of the transient staging area (`~/.microsandbox/cache/tmp/`).
    tmp_dir: PathBuf,
}

/// Cached metadata for a pulled image reference.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedImageMetadata {
    /// Content-addressable digest of the resolved manifest.
    pub manifest_digest: String,
    /// Content-addressable digest of the config blob.
    pub config_digest: String,
    /// Raw resolved image manifest JSON.
    pub raw_manifest_json: String,
    /// Raw image config JSON.
    pub raw_config_json: String,
    /// Parsed OCI image configuration.
    pub config: ImageConfig,
    /// Layer metadata in bottom-to-top order.
    pub layers: Vec<CachedLayerMetadata>,
}

/// Cached metadata for a single layer descriptor.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedLayerMetadata {
    /// Compressed layer digest from the manifest (blob digest).
    pub digest: String,
    /// OCI media type of the layer blob.
    pub media_type: Option<String>,
    /// Compressed blob size in bytes.
    pub size_bytes: Option<u64>,
    /// Uncompressed diff ID from the image config.
    pub diff_id: String,
}

/// Manifest-keyed reference to one validated immutable flat rootfs artifact.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FlatRootfsRef {
    /// Reference schema version.
    pub schema: u32,
    /// Resolved OCI manifest digest used as the requested input.
    pub manifest_digest: String,
    /// Complete derivation digest including platform and materializer profile.
    pub derivation_digest: String,
    /// SHA-256 digest of the validated raw ext4 bytes.
    pub artifact_digest: String,
    /// Pure-Rust materializer ABI.
    pub materializer_abi: u32,
    /// Deterministic ext4 UUID as lowercase hexadecimal.
    pub uuid: String,
    /// Logical sparse image size.
    pub virtual_size_bytes: u64,
    /// Unique inode count in the materialized rootfs.
    pub inode_count: u64,
    /// Unique regular-file content bytes.
    pub content_bytes: u64,
}

//--------------------------------------------------------------------------------------------------
// Methods
//--------------------------------------------------------------------------------------------------

impl GlobalCache {
    /// Create a new GlobalCache using the provided cache directory.
    ///
    /// Creates all subdirectories if they don't exist.
    pub fn new(cache_dir: &Path) -> ImageResult<Self> {
        let layers_dir = cache_dir.join(LAYERS_DIR);
        let fsmeta_dir = cache_dir.join(FSMETA_DIR);
        let vmdk_dir = cache_dir.join(VMDK_DIR);
        let flat_dir = cache_dir.join(FLAT_DIR);
        let flat_refs_dir = flat_dir.join(FLAT_REFS_DIR);
        let flat_blobs_dir = flat_dir.join(FLAT_BLOBS_DIR);
        let flat_locks_dir = flat_dir.join(FLAT_LOCKS_DIR);
        let manifests_dir = cache_dir.join(MANIFESTS_DIR);
        let tmp_dir = cache_dir.join(TMP_DIR);

        for dir in [
            &layers_dir,
            &fsmeta_dir,
            &vmdk_dir,
            &flat_refs_dir,
            &flat_blobs_dir,
            &flat_locks_dir,
            &manifests_dir,
            &tmp_dir,
        ] {
            std::fs::create_dir_all(dir).map_err(|e| ImageError::Cache {
                path: dir.clone(),
                source: e,
            })?;
        }

        Ok(Self {
            layers_dir,
            fsmeta_dir,
            vmdk_dir,
            flat_refs_dir,
            flat_blobs_dir,
            flat_locks_dir,
            manifests_dir,
            tmp_dir,
        })
    }

    /// Create a new GlobalCache using async filesystem operations.
    pub async fn new_async(cache_dir: &Path) -> ImageResult<Self> {
        let layers_dir = cache_dir.join(LAYERS_DIR);
        let fsmeta_dir = cache_dir.join(FSMETA_DIR);
        let vmdk_dir = cache_dir.join(VMDK_DIR);
        let flat_dir = cache_dir.join(FLAT_DIR);
        let flat_refs_dir = flat_dir.join(FLAT_REFS_DIR);
        let flat_blobs_dir = flat_dir.join(FLAT_BLOBS_DIR);
        let flat_locks_dir = flat_dir.join(FLAT_LOCKS_DIR);
        let manifests_dir = cache_dir.join(MANIFESTS_DIR);
        let tmp_dir = cache_dir.join(TMP_DIR);

        for dir in [
            &layers_dir,
            &fsmeta_dir,
            &vmdk_dir,
            &flat_refs_dir,
            &flat_blobs_dir,
            &flat_locks_dir,
            &manifests_dir,
            &tmp_dir,
        ] {
            tokio::fs::create_dir_all(dir)
                .await
                .map_err(|e| ImageError::Cache {
                    path: dir.clone(),
                    source: e,
                })?;
        }

        Ok(Self {
            layers_dir,
            fsmeta_dir,
            vmdk_dir,
            flat_refs_dir,
            flat_blobs_dir,
            flat_locks_dir,
            manifests_dir,
            tmp_dir,
        })
    }

    // ── Layer EROFS paths (keyed by diff_id) ─────────────────────────

    /// Root layer EROFS cache directory.
    pub fn layers_dir(&self) -> &Path {
        &self.layers_dir
    }

    /// Path to the per-layer EROFS image for a given diff_id.
    pub fn layer_erofs_path(&self, diff_id: &Digest) -> PathBuf {
        self.layers_dir
            .join(format!("{}.erofs", diff_id.to_path_safe()))
    }

    /// Path to the materialization lock for a layer EROFS image.
    pub fn layer_erofs_lock_path(&self, diff_id: &Digest) -> PathBuf {
        self.layers_dir
            .join(format!("{}.erofs.lock", diff_id.to_path_safe()))
    }

    /// Check if a layer EROFS image exists.
    pub fn is_layer_materialized(&self, diff_id: &Digest) -> bool {
        is_valid_erofs_artifact(&self.layer_erofs_path(diff_id))
    }

    /// Check if all given layer diff_ids have materialized EROFS images.
    pub fn all_layers_materialized(&self, diff_ids: &[Digest]) -> bool {
        diff_ids.iter().all(|d| self.is_layer_materialized(d))
    }

    // ── fsmeta EROFS paths (keyed by manifest digest) ─────────────────

    /// Root fsmeta EROFS cache directory.
    pub fn fsmeta_dir(&self) -> &Path {
        &self.fsmeta_dir
    }

    /// Path to the fsmeta EROFS image for a given manifest digest.
    pub fn fsmeta_erofs_path(&self, manifest_digest: &Digest) -> PathBuf {
        self.fsmeta_dir
            .join(format!("{}.erofs", manifest_digest.to_path_safe()))
    }

    /// Path to the materialization lock for a fsmeta EROFS image.
    pub fn fsmeta_erofs_lock_path(&self, manifest_digest: &Digest) -> PathBuf {
        self.fsmeta_dir
            .join(format!("{}.erofs.lock", manifest_digest.to_path_safe()))
    }

    /// Check if a fsmeta EROFS image exists.
    pub fn is_fsmeta_materialized(&self, manifest_digest: &Digest) -> bool {
        is_valid_erofs_artifact(&self.fsmeta_erofs_path(manifest_digest))
    }

    // ── VMDK descriptor paths (keyed by manifest digest) ────────────

    /// Root VMDK cache directory.
    pub fn vmdk_dir(&self) -> &Path {
        &self.vmdk_dir
    }

    /// Path to the VMDK descriptor for a given manifest digest.
    pub fn vmdk_path(&self, manifest_digest: &Digest) -> PathBuf {
        self.vmdk_dir
            .join(format!("{}.vmdk", manifest_digest.to_path_safe()))
    }

    /// Path to the materialization lock for a VMDK descriptor.
    pub fn vmdk_lock_path(&self, manifest_digest: &Digest) -> PathBuf {
        self.vmdk_dir
            .join(format!("{}.vmdk.lock", manifest_digest.to_path_safe()))
    }

    /// Check if a VMDK descriptor exists for a given manifest digest.
    pub fn is_vmdk_materialized(&self, manifest_digest: &Digest) -> bool {
        self.vmdk_path(manifest_digest).exists()
    }

    // ── Flat ext4 artifact paths (manifest ref → content blob) ───────

    /// Path to the manifest-keyed flat rootfs reference.
    pub fn flat_ref_path(&self, manifest_digest: &Digest) -> PathBuf {
        self.flat_refs_dir
            .join(format!("{}.json", manifest_digest.to_path_safe()))
    }

    /// Path to the immutable content-addressed raw ext4 artifact.
    pub fn flat_blob_path(&self, artifact_digest: &Digest) -> PathBuf {
        self.flat_blobs_dir
            .join(format!("{}.raw", artifact_digest.to_path_safe()))
    }

    /// Path to the per-derivation flat materialization lock.
    pub fn flat_lock_path(&self, derivation_digest: &Digest) -> PathBuf {
        self.flat_locks_dir
            .join(format!("{}.lock", derivation_digest.to_path_safe()))
    }

    /// Same-filesystem work directory for one flat-rootfs derivation.
    pub fn flat_work_dir(&self, derivation_digest: &Digest) -> PathBuf {
        self.tmp_dir
            .join(format!("{}.flat.work", derivation_digest.to_path_safe()))
    }

    /// Publish a synchronized candidate as an immutable content-addressed blob.
    pub fn publish_flat_blob(
        &self,
        candidate: &Path,
        artifact_digest: &Digest,
        expected_size: u64,
    ) -> ImageResult<PathBuf> {
        let destination = self.flat_blob_path(artifact_digest);
        if let Ok(metadata) = std::fs::metadata(&destination) {
            if metadata.len() != expected_size {
                return Err(ImageError::Cache {
                    path: destination,
                    source: std::io::Error::new(
                        std::io::ErrorKind::InvalidData,
                        "content-addressed flat blob has an unexpected size",
                    ),
                });
            }
            if flat_blob_matches_digest(&destination, artifact_digest)? {
                let _ = std::fs::remove_file(candidate);
                return Ok(destination);
            }

            // A content-addressed name must never retain different bytes. The
            // candidate has already been synchronized and validated, while a
            // missing destination makes every existing ref safely miss after
            // a crash between removal and rename.
            std::fs::remove_file(&destination).map_err(|source| ImageError::Cache {
                path: destination.clone(),
                source,
            })?;
        }
        std::fs::rename(candidate, &destination).map_err(|source| ImageError::Cache {
            path: destination.clone(),
            source,
        })?;
        sync_directory(&self.flat_blobs_dir)?;
        Ok(destination)
    }

    /// Read and validate the manifest-keyed flat rootfs reference.
    pub fn read_flat_ref(&self, manifest_digest: &Digest) -> ImageResult<Option<FlatRootfsRef>> {
        let path = self.flat_ref_path(manifest_digest);
        let data = match std::fs::read_to_string(&path) {
            Ok(data) => data,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(source) => return Err(ImageError::Cache { path, source }),
        };
        let reference = match serde_json::from_str::<FlatRootfsRef>(&data) {
            Ok(reference)
                if reference.schema == 1
                    && reference.manifest_digest == manifest_digest.to_string() =>
            {
                reference
            }
            Ok(_) => return Ok(None),
            Err(error) => {
                tracing::warn!(path = %path.display(), %error, "corrupt flat rootfs ref, ignoring");
                return Ok(None);
            }
        };
        let artifact_digest = match reference.artifact_digest.parse::<Digest>() {
            Ok(digest) => digest,
            Err(_) => return Ok(None),
        };
        let blob_path = self.flat_blob_path(&artifact_digest);
        match std::fs::metadata(&blob_path) {
            Ok(metadata) if metadata.len() == reference.virtual_size_bytes => Ok(Some(reference)),
            Ok(_) | Err(_) => Ok(None),
        }
    }

    /// Atomically replace a flat rootfs reference after its immutable blob is durable.
    pub fn write_flat_ref(
        &self,
        manifest_digest: &Digest,
        reference: &FlatRootfsRef,
    ) -> ImageResult<()> {
        let path = self.flat_ref_path(manifest_digest);
        let temp_path = path.with_extension("json.part");
        let payload = serde_json::to_vec_pretty(reference).map_err(|error| {
            ImageError::ConfigParse(format!("failed to serialize flat rootfs ref: {error}"))
        })?;
        let mut temp = std::fs::File::create(&temp_path).map_err(|source| ImageError::Cache {
            path: temp_path.clone(),
            source,
        })?;
        use std::io::Write;
        temp.write_all(&payload)
            .map_err(|source| ImageError::Cache {
                path: temp_path.clone(),
                source,
            })?;
        temp.sync_all().map_err(|source| ImageError::Cache {
            path: temp_path.clone(),
            source,
        })?;
        std::fs::rename(&temp_path, &path).map_err(|source| ImageError::Cache {
            path: path.clone(),
            source,
        })?;
        sync_directory(&self.flat_refs_dir)?;
        Ok(())
    }

    // ── Staging/tmp paths (downloads, work dirs) ─────────────────────

    /// Root staging directory.
    pub fn tmp_dir(&self) -> &Path {
        &self.tmp_dir
    }

    /// Path to the partial download file for a blob.
    pub fn part_path(&self, blob_digest: &Digest) -> PathBuf {
        self.tmp_dir
            .join(format!("{}.part", blob_digest.to_path_safe()))
    }

    /// Path to the download lock file for a blob.
    pub fn download_lock_path(&self, blob_digest: &Digest) -> PathBuf {
        self.tmp_dir
            .join(format!("{}.download.lock", blob_digest.to_path_safe()))
    }

    /// Path to the materialization work directory for an EROFS build.
    pub fn work_dir(&self, key: &Digest) -> PathBuf {
        self.tmp_dir.join(format!("{}.work", key.to_path_safe()))
    }

    // ── Manifest metadata cache ──────────────────────────────────────

    /// Root manifest metadata directory.
    pub fn manifests_dir(&self) -> &Path {
        &self.manifests_dir
    }

    /// Path to the pull lock file for an image reference.
    pub fn image_lock_path(&self, reference: &Reference) -> PathBuf {
        self.manifests_dir
            .join(format!("{}.lock", image_cache_key(reference)))
    }

    /// Read cached metadata for an image reference.
    pub fn read_image_metadata(
        &self,
        reference: &Reference,
    ) -> ImageResult<Option<CachedImageMetadata>> {
        let path = self.image_metadata_path(reference);

        let data = match std::fs::read_to_string(&path) {
            Ok(data) => data,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(e) => return Err(ImageError::Cache { path, source: e }),
        };

        parse_cached_image_metadata(&path, &data)
    }

    /// Read cached metadata for an image reference using async filesystem I/O.
    pub async fn read_image_metadata_async(
        &self,
        reference: &Reference,
    ) -> ImageResult<Option<CachedImageMetadata>> {
        let path = self.image_metadata_path(reference);

        let data = match tokio::fs::read_to_string(&path).await {
            Ok(data) => data,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(e) => return Err(ImageError::Cache { path, source: e }),
        };

        parse_cached_image_metadata(&path, &data)
    }

    /// Write cached metadata for an image reference.
    #[cfg_attr(not(test), allow(dead_code))]
    pub(crate) fn write_image_metadata(
        &self,
        reference: &Reference,
        metadata: &CachedImageMetadata,
    ) -> ImageResult<()> {
        let path = self.image_metadata_path(reference);
        let temp_path = path.with_extension("json.part");
        let payload = serde_json::to_vec(metadata).map_err(|e| {
            ImageError::ConfigParse(format!("failed to serialize cached image metadata: {e}"))
        })?;

        std::fs::write(&temp_path, payload).map_err(|e| ImageError::Cache {
            path: temp_path.clone(),
            source: e,
        })?;
        std::fs::rename(&temp_path, &path).map_err(|e| ImageError::Cache { path, source: e })?;

        Ok(())
    }

    /// Write cached metadata for an image reference using async filesystem I/O.
    pub async fn write_image_metadata_async(
        &self,
        reference: &Reference,
        metadata: &CachedImageMetadata,
    ) -> ImageResult<()> {
        let path = self.image_metadata_path(reference);
        let temp_path = path.with_extension("json.part");
        let payload = serde_json::to_vec(metadata).map_err(|e| {
            ImageError::ConfigParse(format!("failed to serialize cached image metadata: {e}"))
        })?;

        tokio::fs::write(&temp_path, payload)
            .await
            .map_err(|e| ImageError::Cache {
                path: temp_path.clone(),
                source: e,
            })?;
        tokio::fs::rename(&temp_path, &path)
            .await
            .map_err(|e| ImageError::Cache { path, source: e })?;

        Ok(())
    }

    /// Delete cached metadata for an image reference.
    pub fn delete_image_metadata(&self, reference: &Reference) -> ImageResult<()> {
        let path = self.image_metadata_path(reference);
        match std::fs::remove_file(&path) {
            Ok(()) => Ok(()),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
            Err(e) => Err(ImageError::Cache { path, source: e }),
        }
    }

    /// Delete cached metadata for an image reference using async filesystem I/O.
    pub async fn delete_image_metadata_async(&self, reference: &Reference) -> ImageResult<()> {
        let path = self.image_metadata_path(reference);
        match tokio::fs::remove_file(&path).await {
            Ok(()) => Ok(()),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
            Err(e) => Err(ImageError::Cache { path, source: e }),
        }
    }

    /// Path to the cached metadata file for an image reference.
    pub fn image_metadata_path(&self, reference: &Reference) -> PathBuf {
        self.manifests_dir
            .join(format!("{}.json", image_cache_key(reference)))
    }

    // ── Blob cache paths ──────────────────────────────────────────────

    /// Path to the cached compressed tarball for a layer blob.
    pub fn tar_path(&self, digest: &Digest) -> PathBuf {
        self.layers_dir
            .join(format!("{}.tar.gz", digest.to_path_safe()))
    }
}

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------

fn image_cache_key(reference: &Reference) -> String {
    let mut hasher = Sha256::new();
    hasher.update(reference.to_string().as_bytes());
    hex::encode(hasher.finalize())
}

#[cfg(unix)]
fn sync_directory(path: &Path) -> ImageResult<()> {
    let directory = std::fs::File::open(path).map_err(|source| ImageError::Cache {
        path: path.to_path_buf(),
        source,
    })?;
    directory.sync_all().map_err(|source| ImageError::Cache {
        path: path.to_path_buf(),
        source,
    })
}

#[cfg(not(unix))]
fn sync_directory(_path: &Path) -> ImageResult<()> {
    Ok(())
}

pub(crate) fn parse_cached_image_metadata(
    path: &Path,
    data: &str,
) -> ImageResult<Option<CachedImageMetadata>> {
    match serde_json::from_str::<CachedImageMetadata>(data) {
        Ok(metadata) => Ok(Some(metadata)),
        Err(e) => {
            tracing::warn!(
                path = %path.display(),
                error = %e,
                "corrupt image metadata cache, ignoring"
            );
            Ok(None)
        }
    }
}

pub(crate) fn is_valid_erofs_artifact(path: &Path) -> bool {
    let Ok(meta) = std::fs::metadata(path) else {
        return false;
    };
    let len = meta.len();
    if !meta.is_file() || len == 0 || len % EROFS_ALIGNMENT_BYTES != 0 {
        return false;
    }

    // Length alone accepts any aligned garbage as a cache hit. Parse the
    // superblock and root inode so corrupt cached layers are re-materialized
    // instead of failing later while composing a flat rootfs or booting a VM.
    let Ok(file) = std::fs::File::open(path) else {
        return false;
    };
    let Ok(mut reader) = ErofsReader::new(file) else {
        return false;
    };
    reader.root_directory_metadata().is_ok()
}

pub(crate) async fn is_valid_erofs_artifact_async(path: &Path) -> bool {
    let path = path.to_path_buf();
    tokio::task::spawn_blocking(move || is_valid_erofs_artifact(&path))
        .await
        .unwrap_or(false)
}

fn flat_blob_matches_digest(path: &Path, expected: &Digest) -> ImageResult<bool> {
    let mut file = std::fs::File::open(path).map_err(|source| ImageError::Cache {
        path: path.to_path_buf(),
        source,
    })?;
    let mut hasher = Sha256::new();
    let mut buffer = [0u8; 1024 * 1024];
    loop {
        let read = file.read(&mut buffer).map_err(|source| ImageError::Cache {
            path: path.to_path_buf(),
            source,
        })?;
        if read == 0 {
            break;
        }
        hasher.update(&buffer[..read]);
    }
    Ok(format!("sha256:{}", hex::encode(hasher.finalize())) == expected.to_string())
}

//--------------------------------------------------------------------------------------------------
// Tests
//--------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    fn digest(byte: char) -> Digest {
        format!("sha256:{}", byte.to_string().repeat(64))
            .parse()
            .unwrap()
    }

    #[test]
    fn flat_cache_separates_manifest_refs_from_content_blobs() {
        let directory = tempfile::tempdir().unwrap();
        let cache = GlobalCache::new(directory.path()).unwrap();
        let manifest = digest('a');
        let derivation = digest('b');
        let artifact = digest('c');

        assert!(cache.flat_ref_path(&manifest).ends_with(
            "flat/refs/sha256_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"
        ));
        assert!(cache.flat_blob_path(&artifact).ends_with(
            "flat/blobs/sha256_cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc.raw"
        ));
        assert!(cache.flat_lock_path(&derivation).ends_with(
            "flat/locks/sha256_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.lock"
        ));

        let blob_path = cache.flat_blob_path(&artifact);
        let blob = std::fs::File::create(&blob_path).unwrap();
        blob.set_len(4096).unwrap();
        let reference = FlatRootfsRef {
            schema: 1,
            manifest_digest: manifest.to_string(),
            derivation_digest: derivation.to_string(),
            artifact_digest: artifact.to_string(),
            materializer_abi: 1,
            uuid: "00".repeat(16),
            virtual_size_bytes: 4096,
            inode_count: 2,
            content_bytes: 7,
        };
        cache.write_flat_ref(&manifest, &reference).unwrap();

        assert_eq!(cache.read_flat_ref(&manifest).unwrap(), Some(reference));
    }

    #[test]
    fn aligned_garbage_is_not_a_valid_erofs_cache_artifact() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("corrupt.erofs");
        let file = std::fs::File::create(&path).unwrap();
        file.set_len(EROFS_ALIGNMENT_BYTES).unwrap();

        assert!(!is_valid_erofs_artifact(&path));
    }

    #[test]
    fn flat_ref_must_name_the_manifest_that_indexes_it() {
        let directory = tempfile::tempdir().unwrap();
        let cache = GlobalCache::new(directory.path()).unwrap();
        let indexed_manifest = digest('a');
        let wrong_manifest = digest('b');
        let artifact = digest('c');
        std::fs::write(cache.flat_blob_path(&artifact), [0u8; 8]).unwrap();
        let reference = FlatRootfsRef {
            schema: 1,
            manifest_digest: wrong_manifest.to_string(),
            derivation_digest: digest('d').to_string(),
            artifact_digest: artifact.to_string(),
            materializer_abi: 1,
            uuid: "00".repeat(16),
            virtual_size_bytes: 8,
            inode_count: 2,
            content_bytes: 0,
        };
        cache.write_flat_ref(&indexed_manifest, &reference).unwrap();

        assert_eq!(cache.read_flat_ref(&indexed_manifest).unwrap(), None);
    }

    #[test]
    fn publishing_replaces_same_size_blob_with_wrong_content() {
        let directory = tempfile::tempdir().unwrap();
        let cache = GlobalCache::new(directory.path()).unwrap();
        let candidate = directory.path().join("candidate.raw");
        let expected_bytes = b"good";
        std::fs::write(&candidate, expected_bytes).unwrap();
        let expected: Digest = format!("sha256:{}", hex::encode(Sha256::digest(expected_bytes)))
            .parse()
            .unwrap();
        let destination = cache.flat_blob_path(&expected);
        std::fs::write(&destination, b"evil").unwrap();

        cache
            .publish_flat_blob(&candidate, &expected, expected_bytes.len() as u64)
            .unwrap();

        assert_eq!(std::fs::read(destination).unwrap(), expected_bytes);
    }
}