zlayer-agent 0.14.0

Container runtime agent using libcontainer/youki
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
//! Windows OCI layer unpack orchestrator.
//!
//! Given a resolved manifest and a scratch directory, this module pulls each
//! layer blob from the registry (with MCR `urls[]` redirect fallback for
//! foreign Windows layers), decompresses the tar stream, and materializes the
//! wclayer folder layout via [`BackupStreamWriter`] before calling
//! `HcsImportLayer`. The result is a parent chain ready to be paired with a
//! scratch writable layer and attached to a compute system.
//!
//! # Simplifications (provisional first cut)
//!
//! - Layers are buffered fully in memory. A production-grade pipeline would
//!   stream each blob to disk to cap RAM at large-image boundaries.
//! - Digest verification re-hashes the raw (compressed) blob bytes; we don't
//!   independently verify `DiffIDs` on the decompressed tar.
//! - Tombstones and `Hives/` entries are written as raw byte copies (hcsshim's
//!   `BackupFileWriter` interprets framing on the fly and writes only the body
//!   to disk, so the staging layout on disk is unframed). The `Files/` subtree
//!   is translated through [`backuptar`] which synthesises the full
//!   `WIN32_STREAM_ID`-framed records (`BACKUP_DATA`, plus optional
//!   `BACKUP_SECURITY_DATA` / `BACKUP_REPARSE_DATA` / `BACKUP_EA_DATA` pulled
//!   from the entry's PAX extensions) that [`BackupStreamWriter`] expects.
//!
//! See `hcsshim/internal/wclayer/legacy.go` and `go-winio/backuptar/tar.go`
//! for the reference flow.

#![cfg(target_os = "windows")]

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

use base64::Engine as _;
use oci_client::secrets::RegistryAuth;
use zlayer_hcs::schema::Layer;

use crate::windows::backuptar;
use crate::windows::layer;
use crate::windows::wclayer::{self, LayerChain};

/// Layer descriptor passed to the unpacker. Structurally mirrors the subset of
/// the OCI `Descriptor` fields we actually consume.
#[derive(Debug, Clone)]
pub struct ResolvedLayerDescriptor {
    /// Content-addressable digest (`sha256:...`) of the blob.
    pub digest: String,
    /// OCI media type — drives the decompressor selection and the
    /// foreign-layer `urls[]` fallback in the registry client.
    pub media_type: String,
    /// Size in bytes, used for the redirect-fallback sanity check.
    pub size: i64,
    /// Optional mirror URLs for foreign layers (non-empty for MCR Windows
    /// base layers; empty for ordinary OCI layers).
    pub urls: Vec<String>,
}

/// Result of an unpack: every layer that was materialized on disk.
#[derive(Debug, Clone)]
pub struct UnpackedImage {
    /// HCS-ordered parent chain (child-to-parent). The first element is the
    /// topmost image layer; the last is the base OS layer. Safe to hand
    /// directly to [`wclayer::create_sandbox_layer`] or
    /// [`wclayer::prepare_layer`].
    pub chain: LayerChain,
    /// Root directory that holds all the individual `<layer_id>/` folders.
    pub root: PathBuf,
    /// Per-layer path to the retained **compressed** blob (`<digest>.tar.gz`),
    /// in the SAME order as [`chain`] (child-to-parent). `Some` for ordinary
    /// non-foreign layers — the OCI export / push reads these bytes to upload
    /// the base-image layers a freshly-built Windows image inherits. `None` for
    /// foreign Windows base layers (MCR `urls[]` rehydrates them; the registry
    /// never sees the bytes). Modern `mcr.microsoft.com/windows/nanoserver`
    /// layers are ordinary `rootfs.diff.tar.gzip` (NOT foreign), so without
    /// these the push fails "non-foreign layer ... has no `local_path`".
    pub blob_paths: Vec<Option<PathBuf>>,
}

/// Unpack a Windows OCI image into `dest_root`.
///
/// `layers` is the descriptor list from a platform-resolved manifest, in
/// **manifest order** (base first). For each layer, this function:
///
/// 1. Pulls the blob via [`zlayer_registry::client::ImagePuller::pull_blob_with_urls`]
///    (with MCR redirect fallback when a foreign-layer 404 lands).
/// 2. Decompresses the blob according to its media type (`gzip`, `zstd`, or
///    raw tar).
/// 3. Re-hashes and verifies the digest.
/// 4. Allocates a fresh layer id + directory under `dest_root/<uuid>/`.
/// 5. Walks the tar, writing `Files/` entries through [`BackupStreamWriter`]
///    and everything else as raw byte copies.
/// 6. Calls [`wclayer::import_layer`] with the parents collected so far
///    (child-to-parent order).
///
/// Callers must have already resolved a manifest that matches the node's
/// target platform (see [`zlayer_registry::client::ImagePuller::with_platform`]).
///
/// # Errors
///
/// Returns [`io::Error`] on any failure — blob pull, decompress, digest
/// mismatch, `BackupWrite`, or `HcsImportLayer`.
#[allow(clippy::too_many_lines)]
pub async fn unpack_windows_image(
    puller: &zlayer_registry::client::ImagePuller,
    image: &str,
    auth: &RegistryAuth,
    layers: &[ResolvedLayerDescriptor],
    dest_root: &Path,
) -> io::Result<UnpackedImage> {
    // HcsImportLayer requires SeBackupPrivilege + SeRestorePrivilege; enable
    // them once up-front so every subsequent call inherits the adjustment.
    layer::enable_backup_restore_privileges()?;

    std::fs::create_dir_all(dest_root)?;

    // `chain_so_far` grows base-first (manifest order). For each new import
    // we reverse it into child-to-parent to satisfy HCS.
    let mut chain_so_far: Vec<Layer> = Vec::with_capacity(layers.len());

    // Retained compressed-blob path per layer (base-first, parallel to
    // `chain_so_far`). `Some` for non-foreign layers whose bytes the OCI
    // export / push must upload; `None` for foreign layers (rehydrated from
    // the manifest `urls[]`).
    let mut blob_paths_acc: Vec<Option<PathBuf>> = Vec::with_capacity(layers.len());

    // Host stage for streamed layer files (cleared at the end of the unpack).
    let layer_stage = dest_root.join(".layers");

    for (layer_idx, desc) in layers.iter().enumerate() {
        let layer_id = new_layer_id();
        let layer_path = dest_root.join(&layer_id);
        std::fs::create_dir_all(&layer_path)?;

        // 1. Stream the blob to a file (MCR `urls[]` redirect fallback lives
        //    inside the registry client). The streaming pull verifies the
        //    SHA-256 inline as it writes, so no separate digest pass is needed.
        let layer_file = puller
            .pull_blob_to_file(
                image,
                &desc.digest,
                auth,
                &desc.urls,
                Some(desc.size),
                &layer_stage,
            )
            .await
            .map_err(|e| io::Error::other(format!("pull blob {}: {e}", desc.digest)))?;

        // 2. Stream-decompress from the file (never buffering the whole layer).
        let raw = decompress_reader(std::fs::File::open(&layer_file)?, &desc.media_type)?;

        let is_base_layer = chain_so_far.is_empty();
        if is_base_layer {
            // hcsshim's `baseLayerWriter` writes real NTFS files directly into
            // the final layer dir and finalizes via `ProcessBaseLayer` — no
            // `HcsImportLayer` involved (that's only for `legacyLayerWriter`
            // staging dirs, i.e. diff layers).
            extract_tar_as_base_layer(raw, &layer_path)?;
            wclayer::process_base_layer(&layer_path).map_err(|e| {
                io::Error::other(format!(
                    "ProcessBaseLayer(layer={layer_idx} digest={} dest={}): {e}",
                    desc.digest,
                    layer_path.display()
                ))
            })?;

            // Materialize UVM artifacts if this layer ships a UtilityVM payload.
            // nanoserver/servercore base layers ship `UtilityVM\Files\` and need
            // `ProcessUtilityImage` to produce the `UtilityVM\SystemTemplate*.vhdx`
            // artifacts the consumer's `Uvm::create` later copies into a per-UVM
            // sandbox. Sideloaded process-only images without a UVM payload are
            // skipped silently.
            let uvm_dir = layer_path.join("UtilityVM");
            if uvm_dir.join("Files").is_dir() {
                wclayer::process_utility_vm_image(&uvm_dir).map_err(|e| {
                    io::Error::other(format!(
                        "ProcessUtilityVMImage(layer={layer_idx} digest={} dest={}): {e}",
                        desc.digest,
                        uvm_dir.display()
                    ))
                })?;
            }
        } else {
            // Diff layer: materialize the legacy framed staging format, then
            // hand it to `HcsImportLayer` (source must differ from dest — see
            // `wclayer.rs:91-94`).
            //
            // Hardlink targets that point at files living in PARENT layers
            // (every catroot `.cat`, every DriverStore `.inf`/`.sys`/`.dll`,
            // including `\windows\system32\nanocontainersbridge.dll` —
            // the in-guest GCS bridge DLL) are resolved here against the
            // parents in reverse (newest-first) order. The actual link
            // creation is deferred until after `HcsImportLayer` returns and
            // the merged view exists at `layer_path` — see the post-import
            // replay block below. Mirrors hcsshim's
            // `legacyLayerWriter.AddLink` (target-resolution) +
            // `legacyLayerWriterWrapper.Close` (post-import replay) split.
            let staging_path = dest_root.join(format!("{layer_id}.staging"));
            std::fs::create_dir_all(&staging_path)?;
            let parent_chain = build_parent_chain(&chain_so_far);
            let pending_links =
                extract_tar_as_diff_layer(raw, &staging_path, parent_chain.0.as_slice())?;
            wclayer::import_layer(&layer_path, &staging_path, &parent_chain).map_err(|e| {
                io::Error::other(format!(
                    "HcsImportLayer(layer={layer_idx} digest={} dest={}): {e}",
                    desc.digest,
                    layer_path.display()
                ))
            })?;
            // Post-import hardlink replay. The link target must exist in the
            // merged destination dir (HcsImportLayer materialises the parent
            // chain's files into `layer_path` before returning). We log + fail
            // on missing targets rather than silently degrade, since the
            // alternative (no link in the destination layer) is precisely the
            // bug this fix addresses — silent file loss.
            for link in &pending_links {
                let dest_link = layer_path.join(&link.link_rel);
                let dest_target = layer_path.join(&link.target_rel);
                if let Err(e) = wclayer::link_relative(&layer_path, &dest_target, &dest_link) {
                    return Err(io::Error::other(format!(
                        "post-import hard_link({} -> {}) in layer {}: {e} \
                         (target resolved from {} during tar walk)",
                        dest_link.display(),
                        dest_target.display(),
                        layer_path.display(),
                        link.target_origin,
                    )));
                }
            }
            let _ = std::fs::remove_dir_all(&staging_path);
        }

        // The staged compressed layer file has been consumed for HCS import.
        // For NON-foreign layers we must retain the compressed bytes so the
        // OCI export / push can upload them (a freshly-built Windows image
        // inherits the base layers; modern nanoserver layers are ordinary
        // `rootfs.diff.tar.gzip`, not foreign, so they are NOT rehydrated from
        // `urls[]`). Persist to a stable, process-owned path under `dest_root`
        // (which is the build's working-layer-chain dir, outliving this fn).
        // Foreign layers keep the old behaviour: drop the staging file, push
        // `None` (rehydrated from the manifest `urls[]` on pull).
        let is_foreign = desc.media_type.contains("foreign.diff.tar.gzip")
            || (!desc.urls.is_empty() && desc.media_type.contains("foreign"));
        if is_foreign {
            let _ = std::fs::remove_file(&layer_file);
            blob_paths_acc.push(None);
        } else {
            let digest_hex = desc.digest.strip_prefix("sha256:").unwrap_or(&desc.digest);
            let retained = dest_root.join(format!("{digest_hex}.tar.gz"));
            // Move the already-pulled+verified staging file into place (same
            // volume → rename; fall back to copy across volumes).
            if std::fs::rename(&layer_file, &retained).is_err() {
                std::fs::copy(&layer_file, &retained)?;
                let _ = std::fs::remove_file(&layer_file);
            }
            blob_paths_acc.push(Some(retained));
        }

        // HCS keys parent layers by `NameToGuid(basename(path))`, not by the
        // directory's UUID name. Derive the canonical id so HCS can chain-walk.
        chain_so_far.push(Layer {
            id: wclayer::layer_id_for_path(&layer_path)?,
            path: layer_path.to_string_lossy().into_owned(),
        });
    }

    let _ = std::fs::remove_dir_all(&layer_stage);

    // Final external chain is child-to-parent; our accumulators are base-first.
    let mut final_chain = chain_so_far;
    final_chain.reverse();
    let mut final_blob_paths = blob_paths_acc;
    final_blob_paths.reverse();

    Ok(UnpackedImage {
        chain: LayerChain::new(final_chain),
        root: dest_root.to_path_buf(),
        blob_paths: final_blob_paths,
    })
}

/// Build the HCS parent chain (child-to-parent) from the base-first accumulator.
fn build_parent_chain(base_first: &[Layer]) -> LayerChain {
    let parents: Vec<Layer> = base_first.iter().rev().cloned().collect();
    LayerChain::new(parents)
}

/// Allocate a fresh layer identifier.
fn new_layer_id() -> String {
    uuid::Uuid::new_v4().to_string()
}

/// Decompress `bytes` according to `media_type`, returning the raw tar stream.
///
/// Unknown media types are treated as already-uncompressed tar.
///
/// Test-only: production unpacking uses the streaming [`decompress_reader`] so a
/// multi-GB layer is never fully buffered; this buffered form is kept for unit
/// tests of the media-type matching.
#[cfg(test)]
fn decompress(bytes: &[u8], media_type: &str) -> io::Result<Vec<u8>> {
    use std::io::Read as _;
    let mt = media_type.to_ascii_lowercase();
    if mt.ends_with("+gzip") || mt.ends_with(".tar.gzip") {
        let mut d = flate2::read::GzDecoder::new(bytes);
        let mut out = Vec::new();
        d.read_to_end(&mut out)?;
        Ok(out)
    } else if mt.ends_with("+zstd") || mt.ends_with(".tar.zstd") {
        let mut d = zstd::stream::read::Decoder::new(bytes)?;
        let mut out = Vec::new();
        d.read_to_end(&mut out)?;
        Ok(out)
    } else {
        Ok(bytes.to_vec())
    }
}

/// A streaming decompressing reader over a layer FILE, so the (de)compressed
/// layer is never fully buffered in RAM. Mirrors [`decompress`]'s media-type
/// matching.
fn decompress_reader(file: std::fs::File, media_type: &str) -> io::Result<Box<dyn std::io::Read>> {
    let mt = media_type.to_ascii_lowercase();
    let buffered = std::io::BufReader::new(file);
    if mt.ends_with("+gzip") || mt.ends_with(".tar.gzip") {
        Ok(Box::new(flate2::read::GzDecoder::new(buffered)))
    } else if mt.ends_with("+zstd") || mt.ends_with(".tar.zstd") {
        Ok(Box::new(zstd::stream::read::Decoder::new(buffered)?))
    } else {
        Ok(Box::new(buffered))
    }
}

/// SHA-256 the blob bytes and compare against `expected` (`sha256:<hex>`).
///
/// Test-only: the streaming unpack path verifies layer digests as it reads, so
/// this whole-buffer form is retained only for unit tests.
#[cfg(test)]
fn verify_digest(bytes: &[u8], expected: &str) -> io::Result<()> {
    use sha2::{Digest, Sha256};
    let expected_hex = expected.trim_start_matches("sha256:");
    let mut hasher = Sha256::new();
    hasher.update(bytes);
    let got = hex::encode(hasher.finalize());
    if !got.eq_ignore_ascii_case(expected_hex) {
        return Err(io::Error::other(format!(
            "blob digest mismatch: expected sha256:{expected_hex}, got sha256:{got}"
        )));
    }
    Ok(())
}

/// Walk an OCI Windows layer tar and materialize each entry under `layer_path`.
///
/// Three categories of entry get special treatment beyond the plain
/// `Files/`-vs-other split:
///
/// 1. **Directory entries** (`tar::EntryType::Dir`). These MUST be created on
///    disk — `mcr.microsoft.com/windows/nanoserver` ships thousands of empty
///    leaf directories (`Files/Windows/INF/`, `Files/Windows/System32/Catroot/`,
///    registry/UtilityVM scaffolding) that HCS's `NtQueryDirectoryFile` walk
///    expects to find during `HcsImportLayer`. Silently dropping them yields
///    `STATUS_OBJECT_NAME_NOT_FOUND` → `ERROR_FILE_NOT_FOUND` (`0x80070002`).
/// 2. **Hardlink entries** (`tar::EntryType::Link`). We replay them after the
///    main walk because the tar ordering of `Link` vs. the target it references
///    is not guaranteed in OCI layers.
/// 3. **Whiteouts** — basename-prefixed `.wh.<name>` entries (the OCI
///    overlayfs-style convention). These are NOT extracted as files; instead
///    we append the target path (sibling with `.wh.` stripped) to
///    `tombstones.txt`, which is the on-disk whiteout manifest the wclayer
///    importer consumes. If a raw `tombstones.txt` entry was also present in
///    the tar we APPEND to (not overwrite) it.
///
/// `Files/`-prefixed regular files carry raw file bodies + PAX-encoded NTFS
/// metadata in the OCI format; we hand them to
/// [`backuptar::write_oci_entry_to_backup_stream`] which synthesises the
/// `WIN32_STREAM_ID`-framed records expected by `BackupWrite`. Everything
/// else (raw `tombstones.txt`, `Hives/*`, `UtilityVM/`) is written as a raw
/// byte copy via the long-path-aware helper — hcsshim's `BackupFileWriter`
/// interprets framing on the fly, so the staging-dir layout on disk is
/// unframed.
/// A hardlink that could not be materialised during the tar walk because the
/// link must be created in the IMPORTED layer dir (post-`HcsImportLayer`),
/// not in the staging dir. Carries the resolved target so the post-import
/// replay can fail loudly if the merged destination view is missing the file.
///
/// Mirrors hcsshim's `pendingLink { Path, Target, TargetRoot }` (see
/// `internal/wclayer/legacy.go:740-784`). Our `target_origin` is the
/// human-readable equivalent of hcsshim's `TargetRoot *os.File` handle —
/// either "current staging dir" (the link target was added in this same
/// layer) or the parent layer path it was resolved against.
#[derive(Debug, Clone)]
pub(crate) struct PendingLink {
    /// Forward-slash, archive-relative path of the link to create
    /// (e.g. `Files/Windows/System32/foo.dll`).
    pub link_rel: PathBuf,
    /// Forward-slash, archive-relative path of the link target (e.g.
    /// `Files/Windows/WinSxS/.../foo.dll`). Resolved in the destination
    /// layer dir (post-import) against `layer_path / target_rel`.
    pub target_rel: PathBuf,
    /// Where the target was found during the tar walk. Used in error
    /// messages to identify the layer the missing file was expected to
    /// come from.
    pub target_origin: TargetOrigin,
}

/// Provenance of a resolved hardlink target.
#[derive(Debug, Clone)]
pub(crate) enum TargetOrigin {
    /// Target was added in the same layer's staging dir before the link
    /// entry appeared. Post-import the file lives at `layer_path / rel`.
    CurrentLayer,
    /// Target was found in a parent layer's `Files/` tree. Post-import the
    /// file lives at `layer_path / rel` because `HcsImportLayer` merges the
    /// parent chain into the destination. The inner `PathBuf` records which
    /// parent layer satisfied the lookup, surfaced verbatim in error
    /// messages so a missing post-import target can be traced back to the
    /// parent layer the file was expected to come from.
    ParentLayer(PathBuf),
}

impl std::fmt::Display for TargetOrigin {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::CurrentLayer => f.write_str("current layer's staging dir"),
            Self::ParentLayer(p) => write!(f, "parent layer {}", p.display()),
        }
    }
}

/// Diff-layer (parent-chain non-empty) tar walk — emits the hcsshim
/// `legacyLayerWriter` on-disk staging format: `.$wcidirs$` markers,
/// 4-byte LE `FileAttributes` headers, verbatim `WIN32_STREAM_ID` records,
/// raw `Hives/*` byte copies, and a `tombstones.txt` whiteout manifest.
///
/// Returns a vector of pending hardlinks the caller MUST replay against the
/// imported layer dir (NOT the staging dir) after `wclayer::import_layer`
/// returns Ok. Hardlinks whose target lives in a parent layer cannot be
/// created during extraction (the target file doesn't exist on disk yet —
/// it only materialises into the destination after `HcsImportLayer` merges
/// the parent chain). Mirrors hcsshim's `legacyLayerWriter.AddLink` +
/// `legacyLayerWriterWrapper.Close` split (`internal/wclayer/legacy.go:733`
/// + `internal/wclayer/importlayer.go:69-122`).
///
/// `parents` is the same child-to-parent ordered slice you'd hand to
/// `wclayer::import_layer`; target resolution walks it in reverse (newest
/// parent first) when the link's target was not added in this layer.
#[allow(clippy::too_many_lines)]
fn extract_tar_as_diff_layer(
    reader: impl std::io::Read,
    layer_path: &Path,
    parents: &[Layer],
) -> io::Result<Vec<PendingLink>> {
    use std::collections::HashSet;

    let mut archive = tar::Archive::new(reader);
    // Hardlink replay queue. Each entry carries (a) the link's archive-relative
    // path and (b) the resolved target — either in this same layer's staging
    // dir or in a parent layer's `Files/` tree.
    let mut pending_links: Vec<PendingLink> = Vec::new();
    // Track every regular-file (and hardlink) `Files/` entry materialised in
    // this layer's staging dir. Mirrors hcsshim's `addedFiles map[string]bool`.
    // Link targets check this set first before falling back to parent layers.
    let mut added_files: HashSet<String> = HashSet::new();
    // Whiteout collector: relative target paths (forward-slash, no `.wh.`).
    let mut pending_tombstones: Vec<String> = Vec::new();
    // Track raw `tombstones.txt` presence so we APPEND rather than overwrite.
    let mut raw_tombstones_written = false;

    for entry in archive.entries()? {
        let mut entry = entry?;
        let rel_path = entry.path()?.into_owned();
        let entry_type = entry.header().entry_type();

        // (1) Directory entries: create the directory (with all ancestors via
        // the long-path-aware helper) and move on. We deliberately do NOT
        // skip them anymore.
        if entry_type.is_dir() {
            let dest = layer_path.join(&rel_path);
            create_long_path_dir_all(&dest)?;
            write_wcidirs_sidecar(&mut entry, &rel_path, &dest)?;
            continue;
        }

        // (3) Whiteout detection — check basename for `.wh.` prefix. We do
        // this before path construction so we never materialise a `.wh.*`
        // file on disk (HCS would otherwise see it as a literal payload).
        if let Some(basename) = rel_path.file_name().and_then(|s| s.to_str()) {
            if let Some(stripped) = basename.strip_prefix(".wh.") {
                // Reconstruct the tombstone target as the sibling path with
                // `.wh.` stripped, normalised to forward slashes.
                let sibling: PathBuf = match rel_path.parent() {
                    Some(p) if !p.as_os_str().is_empty() => p.join(stripped),
                    _ => PathBuf::from(stripped),
                };
                let normalised: String = sibling
                    .to_string_lossy()
                    .replace('\\', "/")
                    .trim_start_matches('/')
                    .to_string();
                if !normalised.is_empty() {
                    pending_tombstones.push(normalised);
                }
                continue;
            }
        }

        let dest = layer_path.join(&rel_path);
        if let Some(parent) = dest.parent() {
            create_long_path_dir_all(parent)?;
        }

        // (2) Hardlink entries: NEVER materialise in the staging dir. They
        // must be created in the IMPORTED layer dir after `HcsImportLayer`
        // returns. Here we just resolve the target's provenance — current
        // layer's staging dir vs a parent layer's `Files/` tree — and queue
        // it for the post-import replay. Mirrors hcsshim's
        // `legacyLayerWriter.AddLink` (`internal/wclayer/legacy.go:733-784`).
        if entry_type == tar::EntryType::Link {
            let link_target = entry.link_name()?.ok_or_else(|| {
                io::Error::other(format!(
                    "tar hardlink entry missing link_name: {}",
                    rel_path.display()
                ))
            })?;
            let target_rel = link_target.into_owned();

            // Normalise the target for `added_files` lookup. OCI tar uses
            // forward slashes; `added_files` keys are stored the same way.
            let target_key = path_to_forward_slash(&target_rel);
            let link_key = path_to_forward_slash(&rel_path);

            let origin = if added_files.contains(&target_key) {
                TargetOrigin::CurrentLayer
            } else {
                // Walk parents in reverse (newest parent first → base layer
                // last) mirroring how `legacyLayerWriter.AddLink` iterates
                // its `parentRoots` slice. The chain is already
                // child-to-parent ordered (first elem = immediate parent),
                // so a forward iteration is "newest-first".
                let mut found: Option<PathBuf> = None;
                for parent in parents {
                    let parent_root = PathBuf::from(&parent.path);
                    let candidate = parent_root.join(&target_rel);
                    if candidate.exists() {
                        found = Some(parent_root);
                        break;
                    }
                }
                match found {
                    Some(root) => TargetOrigin::ParentLayer(root),
                    None => {
                        // Match hcsshim's `AddLink`: a link with an
                        // unresolvable target is a hard error, not a
                        // silently-dropped entry. This is the exact bug we
                        // are fixing — degrading here would mean shipping
                        // an incomplete layer.
                        return Err(io::Error::other(format!(
                            "hardlink {} -> {}: target not found in this \
                             layer's staging dir nor in any of the {} parent \
                             layer(s)",
                            link_key,
                            target_key,
                            parents.len(),
                        )));
                    }
                }
            };

            // Record the link itself as "added" so a subsequent link whose
            // target is THIS link's path still resolves locally. Matches
            // hcsshim's `w.addedFiles[name] = true` at the end of
            // `AddLink`.
            added_files.insert(link_key.clone());
            pending_links.push(PendingLink {
                link_rel: rel_path.clone(),
                target_rel: target_rel.clone(),
                target_origin: origin,
            });
            continue;
        }

        // OCI Windows layer tar layout:
        //   Files/...            -> raw file body + PAX-encoded NTFS metadata
        //                           (translated to BackupWrite framing)
        //   Hives/...            -> raw NTFS registry hive exports
        //   tombstones.txt       -> plain-text whiteout manifest
        //   UtilityVM/...        -> raw byte copies (Hyper-V UVM scratch)
        let rel_str = rel_path.to_string_lossy();
        let is_files_payload = rel_str.starts_with("Files/") || rel_str.starts_with("Files\\");
        if is_files_payload {
            backuptar::write_oci_entry_to_backup_stream(&mut entry, &dest)?;
            added_files.insert(path_to_forward_slash(&rel_path));
        } else {
            // Non-`Files/` payloads (`tombstones.txt`, `Hives/*`, `UtilityVM/`)
            // are raw byte copies. `std::fs::File::create` here would hit
            // ERROR_PATH_NOT_FOUND (0x80070003) on the deep UtilityVM/WinSxS
            // paths embedded in nanoserver layers, so we route through the
            // long-path-aware helper which adds the `\\?\` prefix.
            let mut f = layer::create_long_path_file(&dest)?;
            std::io::copy(&mut entry, &mut f)?;
            if rel_str == "tombstones.txt" || rel_str == "tombstones" {
                raw_tombstones_written = true;
            }
            // UtilityVM files can also be hardlink targets (the in-guest GCS
            // bridge DLL itself lives under `Files/Windows/System32/`, but
            // some auxiliary entries live under `UtilityVM/Files/`).
            // Tracking them in `added_files` keeps the resolution table
            // complete.
            added_files.insert(path_to_forward_slash(&rel_path));
        }
    }

    // Replay whiteouts into `tombstones.txt`. De-duplicate so a malformed
    // tar that lists the same `.wh.foo` twice doesn't bloat the manifest.
    if !pending_tombstones.is_empty() {
        let tombstones_path = layer_path.join("tombstones.txt");
        let mut existing: Vec<String> = Vec::new();
        if raw_tombstones_written && tombstones_path.exists() {
            // Pull current contents back via the long-path helper to avoid
            // a `0x80070003` re-read of a deep path. tombstones.txt itself
            // lives at the layer root so this is mostly defensive.
            let bytes = std::fs::read(&tombstones_path)?;
            for line in bytes.split(|&b| b == b'\n') {
                if line.is_empty() {
                    continue;
                }
                existing.push(String::from_utf8_lossy(line).trim().to_string());
            }
        }
        let mut seen: HashSet<String> = existing.iter().cloned().collect();
        let mut all_lines = existing;
        for line in pending_tombstones {
            if seen.insert(line.clone()) {
                all_lines.push(line);
            }
        }
        let body = all_lines.join("\n") + "\n";
        let mut f = layer::create_long_path_file(&tombstones_path)?;
        std::io::Write::write_all(&mut f, body.as_bytes())?;
    }

    Ok(pending_links)
}

/// Stage a plain host `Files/` tree — raw files and directories with no NTFS
/// metadata, as materialised by a `COPY`/`ADD` instruction — into the
/// `legacyLayerWriter` on-disk format that [`wclayer::import_layer`] consumes,
/// writing the result under `staging_root`.
///
/// This is the COPY/ADD analogue of [`extract_tar_as_diff_layer`]: that one
/// converts an OCI layer *tar* (with PAX-carried NTFS metadata) into the
/// staging format; this one converts an already-materialised host directory.
/// Committing a COPY/ADD layer this way mirrors how the unpacker commits every
/// base-image diff layer and how hcsshim's `docker build` commits a `COPY` —
/// **no scratch sandbox, no WCIFS host mount, no compute system, no
/// `HcsExportLayer`**. A plain `Files/` tree handed straight to
/// `HcsImportLayer` (or routed through a host-written HCS scratch +
/// `HcsExportLayer`) fails: `0x80070002` at export (the sandbox was never
/// flushed by a running container) and `0x80071126`
/// (`ERROR_NOT_A_REPARSE_POINT`) at compute-system construction (HCS's
/// layer-combine walks the host-written scratch and rejects entries that are
/// not the reparse-point placeholders it expects). Synthesising the framing
/// here and importing directly sidesteps both.
///
/// Host-sourced files carry no security descriptor / reparse / EA streams, so
/// every regular file gets the default `FILE_ATTRIBUTE_ARCHIVE` (0x20) header +
/// a single `BACKUP_DATA` record (via
/// [`backuptar::write_host_file_to_backup_stream`]), and every directory gets a
/// `<name>.$wcidirs$` marker with `FILE_ATTRIBUTE_DIRECTORY` (0x10) — exactly
/// what `HcsImportLayer`'s NTFS walker requires.
///
/// `files_src_root` is the plain `<scratch>/Files` directory (it may not exist,
/// for an `ADD` that resolved to zero files — that yields an empty layer).
/// `staging_root` must be a fresh directory; on return it contains `Files/...`
/// in framed form, ready to pass as the `source_folder` of
/// [`wclayer::import_layer`].
///
/// # Errors
///
/// Returns an [`io::Error`] on any filesystem or framing failure.
pub fn stage_host_files_as_diff_layer(
    files_src_root: &Path,
    staging_root: &Path,
) -> io::Result<()> {
    // Every WCOW layer in the on-disk `legacyLayerWriter` format that
    // `HcsImportLayer` consumes carries a `Hives\` registry-delta directory
    // alongside `Files\`. `HcsExportLayer` always emits one (so the RUN
    // step's export→reimport round-trip — the proven import path — always
    // hands `HcsImportLayer` a folder containing `Hives\`), and every MCR
    // base/diff layer tar ships a `Hives\` entry which the unpacker frames
    // like any other directory (`extract_tar_as_diff_layer`). A COPY/ADD adds
    // no registry deltas, but `HcsImportLayer` still enumerates
    // `<importFolder>\Hives\*` while applying the layer and fails with
    // `ERROR_PATH_NOT_FOUND` (`0x80070003`) when the directory is absent. So
    // synthesise an empty, fully-framed `Hives\` here to match the layout
    // that every real (exported or tar-sourced) layer presents. This is
    // unconditional — even a payload-less COPY/ADD must still hand the
    // importer a `Hives\`-bearing folder. The directory gets the same
    // `<name>.$wcidirs$` marker the unpacker writes for the `Hives` tar entry,
    // so the importer's NTFS walker sees an identically-framed tree.
    let hives_dest_root = staging_root.join("Hives");
    create_long_path_dir_all(&hives_dest_root)?;
    write_host_dir_wcidirs_marker(&hives_dest_root)?;

    // No staged payload — a COPY/ADD with nothing to add still produces a
    // valid (empty) diff layer: `Hives\`-only, no `Files\` tree.
    if !files_src_root.exists() {
        return Ok(());
    }

    // The top-level `Files` directory is itself an entry in a real OCI layer
    // tar (and so the unpacker emits its `.$wcidirs$` marker); reproduce that
    // for parity so the importer's NTFS walker sees a fully-framed tree.
    let files_dest_root = staging_root.join("Files");
    create_long_path_dir_all(&files_dest_root)?;
    write_host_dir_wcidirs_marker(&files_dest_root)?;
    stage_host_dir_recursive(files_src_root, &files_dest_root)?;
    Ok(())
}

/// Recursively reframe `src_dir` (plain host files/dirs) into `dest_dir` in the
/// diff-layer staging format. Each directory gets a `.$wcidirs$` marker; each
/// regular file gets the attribute header + `BACKUP_DATA` framing.
fn stage_host_dir_recursive(src_dir: &Path, dest_dir: &Path) -> io::Result<()> {
    for entry in std::fs::read_dir(src_dir)? {
        let entry = entry?;
        let file_type = entry.file_type()?;
        let src = entry.path();
        let dest = dest_dir.join(entry.file_name());
        if file_type.is_dir() {
            create_long_path_dir_all(&dest)?;
            write_host_dir_wcidirs_marker(&dest)?;
            stage_host_dir_recursive(&src, &dest)?;
        } else {
            // Regular file. `apply_filesystem_writes` stages COPY/ADD payloads
            // as plain file copies (symlinks are dereferenced upstream), so
            // every non-dir entry here is an ordinary file body.
            backuptar::write_host_file_to_backup_stream(&src, &dest)?;
        }
    }
    Ok(())
}

/// Write the `<dirname>.$wcidirs$` sibling marker for a host-sourced directory:
/// a 4-byte LE `FILE_ATTRIBUTE_DIRECTORY` (0x10) header with no reparse record.
/// The host-file twin of [`write_wcidirs_sidecar`] (which derives attrs +
/// reparse from a tar entry's PAX headers); a COPY directory has neither, so
/// the plain directory attribute is correct.
fn write_host_dir_wcidirs_marker(dir_dest: &Path) -> io::Result<()> {
    const FILE_ATTRIBUTE_DIRECTORY: u32 = 0x0000_0010;
    let dirname = dir_dest.file_name().ok_or_else(|| {
        io::Error::other(format!(
            "staged dir has no file_name: {}",
            dir_dest.display()
        ))
    })?;
    let mut marker_name = dirname.to_os_string();
    marker_name.push(".$wcidirs$");
    let marker_path = match dir_dest.parent() {
        Some(p) => p.join(&marker_name),
        None => PathBuf::from(&marker_name),
    };
    let mut f = layer::create_long_path_file(&marker_path)?;
    std::io::Write::write_all(&mut f, &FILE_ATTRIBUTE_DIRECTORY.to_le_bytes())?;
    Ok(())
}

/// Convert an archive-relative `Path` into the forward-slash, no-leading-slash
/// string form used as the `added_files` lookup key and as the canonical OCI
/// tar path representation. Matches Go's `filepath.Clean` + `filepath.ToSlash`
/// pipeline that hcsshim runs over `hdr.Name` / `hdr.Linkname`.
fn path_to_forward_slash(p: &Path) -> String {
    p.to_string_lossy()
        .replace('\\', "/")
        .trim_start_matches('/')
        .to_string()
}

/// Base-layer (parent-chain empty) tar walk — emits the hcsshim
/// `baseLayerWriter` on-disk format: REAL NTFS directories (no
/// `.$wcidirs$` markers), real NTFS files with metadata stamped via
/// `BackupWrite` (no 4-byte attr header, no verbatim framing), and NO
/// tombstones (`baseLayerWriter.Remove` explicitly rejects them).
///
/// Hardlinks are deferred to a replay pass exactly like the diff layer so
/// out-of-order tar entries still resolve.
fn extract_tar_as_base_layer(reader: impl std::io::Read, layer_path: &Path) -> io::Result<()> {
    let mut archive = tar::Archive::new(reader);
    let mut pending_links: Vec<(PathBuf, PathBuf)> = Vec::new();

    for entry in archive.entries()? {
        let mut entry = entry?;
        let rel_path = entry.path()?.into_owned();
        let entry_type = entry.header().entry_type();

        // Directory entries: materialize a real NTFS directory. No
        // `.$wcidirs$` sibling marker — baseLayerWriter does not emit one.
        if entry_type.is_dir() {
            let dest = layer_path.join(&rel_path);
            create_long_path_dir_all(&dest)?;
            continue;
        }

        // Whiteouts are illegal in base layers — hcsshim's baseLayerWriter
        // returns `errors.New("base layer cannot have tombstones")`.
        if let Some(basename) = rel_path.file_name().and_then(|s| s.to_str()) {
            if basename.starts_with(".wh.") {
                return Err(io::Error::other(format!(
                    "base layer cannot have tombstones (got whiteout entry {})",
                    rel_path.display()
                )));
            }
        }

        let dest = layer_path.join(&rel_path);
        if let Some(parent) = dest.parent() {
            create_long_path_dir_all(parent)?;
        }

        // Defer hardlinks until all primary entries have been materialized.
        if entry_type == tar::EntryType::Link {
            let link_target = entry.link_name()?.ok_or_else(|| {
                io::Error::other(format!(
                    "tar hardlink entry missing link_name: {}",
                    rel_path.display()
                ))
            })?;
            pending_links.push((rel_path.clone(), link_target.into_owned()));
            continue;
        }

        let rel_str = rel_path.to_string_lossy();
        let is_files_payload = rel_str.starts_with("Files/")
            || rel_str.starts_with("Files\\")
            || rel_str.starts_with("UtilityVM/")
            || rel_str.starts_with("UtilityVM\\");
        if is_files_payload {
            // Real NTFS file: metadata stamped via BackupWrite, no verbatim
            // framing on disk.
            backuptar::write_oci_entry_as_base_layer(&mut entry, &dest)?;
        } else {
            // `Hives/*` and any auxiliary base-layer files: raw byte copies
            // through the long-path-aware helper (hcsshim's baseLayerWriter
            // also raw-copies Hives — they are NTFS registry hive exports,
            // not BackupStream blobs).
            let mut f = layer::create_long_path_file(&dest)?;
            std::io::copy(&mut entry, &mut f)?;
        }
    }

    // Replay pending hardlinks now that every primary file is on disk.
    for (link_rel, target_rel) in pending_links {
        let link_abs = layer_path.join(&link_rel);
        let target_abs = layer_path.join(&target_rel);
        if let Some(parent) = link_abs.parent() {
            create_long_path_dir_all(parent)?;
        }
        if let Err(e) = std::fs::hard_link(&target_abs, &link_abs) {
            return Err(io::Error::other(format!(
                "hard_link({} -> {}): {e}",
                link_abs.display(),
                target_abs.display()
            )));
        }
    }

    Ok(())
}

/// Write the `<dirname>.$wcidirs$` sibling marker that hcsshim's
/// `legacyLayerWriter.Add` emits for every non-UVM directory. Without it,
/// `HcsImportLayer`'s NTFS walker returns `0x80070002`. The marker carries
/// a 4-byte LE `FileAttributes` header, optionally followed by a
/// `BACKUP_REPARSE_DATA` record when the tar entry has `MSWINDOWS.reparse`.
fn write_wcidirs_sidecar<R: std::io::Read>(
    entry: &mut tar::Entry<'_, R>,
    rel_path: &Path,
    dest: &Path,
) -> io::Result<()> {
    let rel_norm = rel_path.to_string_lossy().replace('\\', "/");
    let is_uvm =
        rel_norm == "UtilityVM" || rel_norm == "UtilityVM/" || rel_norm.starts_with("UtilityVM/");
    if is_uvm {
        return Ok(());
    }

    let mut attrs: u32 = 0x0000_0010;
    let mut reparse: Option<Vec<u8>> = None;
    if let Some(pax) = entry.pax_extensions()? {
        let engine = base64::engine::general_purpose::STANDARD;
        for ext in pax {
            let ext = ext?;
            match ext.key().unwrap_or("") {
                "MSWINDOWS.fileattr" => {
                    if let Some(parsed) = std::str::from_utf8(ext.value_bytes())
                        .ok()
                        .and_then(|s| s.trim().parse::<u32>().ok())
                    {
                        attrs = parsed;
                    }
                }
                "MSWINDOWS.reparse" => {
                    reparse = Some(engine.decode(ext.value_bytes()).map_err(|e| {
                        io::Error::other(format!("PAX MSWINDOWS.reparse base64 decode: {e}"))
                    })?);
                }
                _ => {}
            }
        }
    }

    let dirname = dest.file_name().ok_or_else(|| {
        io::Error::other(format!(
            "tar dir entry has no file_name: {}",
            rel_path.display()
        ))
    })?;
    let mut marker_name = dirname.to_os_string();
    marker_name.push(".$wcidirs$");
    let marker_path = match dest.parent() {
        Some(p) => p.join(&marker_name),
        None => PathBuf::from(&marker_name),
    };

    let mut f = layer::create_long_path_file(&marker_path)?;
    std::io::Write::write_all(&mut f, &attrs.to_le_bytes())?;
    if let Some(rp) = reparse {
        backuptar::write_stream_header(&mut f, backuptar::BACKUP_REPARSE_DATA, 0, rp.len() as u64)?;
        std::io::Write::write_all(&mut f, &rp)?;
    }
    Ok(())
}

/// `std::fs::create_dir_all` analogue that uses the `\\?\`-prefixed long-path
/// helper so we don't trip `ERROR_PATH_NOT_FOUND` (`0x80070003`) on the deep
/// `WinSxS`/`Catroot`/`UtilityVM` directories embedded in nanoserver layers.
///
/// This walks ancestors top-down and calls a `CreateDirectoryW`-equivalent
/// for each; we re-use the file helper to materialise a placeholder when the
/// directory does not yet exist, except we route to the dedicated dir API.
fn create_long_path_dir_all(dir: &Path) -> io::Result<()> {
    if dir.as_os_str().is_empty() {
        return Ok(());
    }
    // Fast path: std handles short paths fine. Try it first; on
    // `ERROR_PATH_NOT_FOUND` / `ERROR_FILENAME_EXCED_RANGE` fall back.
    match std::fs::create_dir_all(dir) {
        Ok(()) => Ok(()),
        Err(e) => {
            // Walk ancestors and create them one-by-one through the long-path
            // helper. We accumulate the components first because `Path` doesn't
            // give us a top-down ancestor iterator without an extra collect.
            let mut to_create: Vec<&Path> = dir.ancestors().collect();
            to_create.reverse();
            for component in to_create {
                if component.as_os_str().is_empty() {
                    continue;
                }
                if component.is_dir() {
                    continue;
                }
                layer::create_long_path_dir(component).map_err(|inner| {
                    io::Error::other(format!(
                        "create_dir_all fallback for {} (initial {e}): {inner}",
                        component.display()
                    ))
                })?;
            }
            Ok(())
        }
    }
}

/// Paths to a Windows image's bundled Utility VM boot artifacts.
///
/// Hyper-V-isolated Windows containers boot a UVM whose root filesystem is
/// the image's own `UtilityVM\Files` tree (surfaced over a VSMB share named
/// `"os"`) and whose initial scratch VHDX is a copy of the image's
/// `UtilityVM\SystemTemplate.vhdx`. The UEFI firmware then chain-loads
/// `\EFI\Microsoft\Boot\bootmgfw.efi` over the `VmbFs` surface.
///
/// `locate_uvm_boot_files` resolves the absolute paths for the current
/// process; the consumer (HCS doc builder, scratch copier) uses them
/// verbatim — no further path arithmetic required.
#[derive(Debug, Clone)]
pub struct UvmBootFiles {
    /// Absolute path to `<layer>\UtilityVM` (the layer subdir).
    pub uvm_layer_dir: std::path::PathBuf,
    /// Absolute path to `<layer>\UtilityVM\Files` — the OS root surfaced
    /// over the `"os"` VSMB share.
    pub os_files_dir: std::path::PathBuf,
    /// Absolute path to `<layer>\UtilityVM\SystemTemplate.vhdx` — the read-only
    /// template the caller copies to produce a per-UVM `sandbox.vhdx`.
    pub system_template_vhdx: std::path::PathBuf,
    /// Guest-relative UEFI boot path (`\EFI\Microsoft\Boot\bootmgfw.efi`).
    /// Fed verbatim into `Chipset.Uefi.BootThis.DevicePath`.
    pub boot_rel_path: &'static str,
}

/// Walk `chain` (child-to-parent — base layer is `.last()`) and return the
/// first layer that contains a `UtilityVM\Files\EFI\Microsoft\Boot\bootmgfw.efi`
/// file. Iterates in reverse so the OS base layer is checked first, matching
/// hcsshim's `internal/uvm/uvmfolder.go::LocateUVMFolder`.
///
/// # Errors
///
/// Returns [`io::ErrorKind::NotFound`] with a descriptive message if no layer
/// in the chain carries a UVM payload. Daemons should map this to a typed
/// "Hyper-V isolation unavailable: image has no `UtilityVM` payload" error so
/// non-Windows images cannot be silently routed into the Hyper-V path.
pub fn locate_uvm_boot_files(
    chain: &crate::windows::wclayer::LayerChain,
) -> std::io::Result<UvmBootFiles> {
    const BOOT_REL: &str = r"\EFI\Microsoft\Boot\bootmgfw.efi";
    for layer in chain.0.iter().rev() {
        let layer_dir = std::path::PathBuf::from(&layer.path);
        let uvm_dir = layer_dir.join("UtilityVM");
        let files_dir = uvm_dir.join("Files");
        let bootmgfw = files_dir
            .join("EFI")
            .join("Microsoft")
            .join("Boot")
            .join("bootmgfw.efi");
        if bootmgfw.is_file() {
            let template = uvm_dir.join("SystemTemplate.vhdx");
            if !template.is_file() {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    format!(
                        "layer {:?} has UtilityVM\\Files but is missing SystemTemplate.vhdx — \
                         image's UVM payload is incomplete",
                        layer.path
                    ),
                ));
            }
            return Ok(UvmBootFiles {
                uvm_layer_dir: uvm_dir,
                os_files_dir: files_dir,
                system_template_vhdx: template,
                boot_rel_path: BOOT_REL,
            });
        }
    }
    Err(std::io::Error::new(
        std::io::ErrorKind::NotFound,
        "no layer in chain contains a UtilityVM payload \
         (image is not a Hyper-V-bootable Windows base image)",
    ))
}

// ---------------------------------------------------------------------------
// Tests (no HCS calls, no registry)
// ---------------------------------------------------------------------------

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

    #[test]
    fn digest_verify_accepts_matching_hash() {
        let bytes = b"hello world";
        // Known SHA-256 of "hello world".
        let digest = "sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
        verify_digest(bytes, digest).expect("should match");
    }

    #[test]
    fn digest_verify_rejects_mismatch() {
        let err = verify_digest(
            b"hello world",
            "sha256:0000000000000000000000000000000000000000000000000000000000000000",
        )
        .expect_err("should reject");
        assert!(err.to_string().contains("digest mismatch"));
    }

    #[test]
    fn digest_verify_is_case_insensitive() {
        let bytes = b"hello world";
        let upper = "sha256:B94D27B9934D3E08A52E52D7DA7DABFAC484EFE37A5380EE9088F7ACE2EFCDE9";
        verify_digest(bytes, upper).expect("should match");
    }

    #[test]
    fn decompress_passthrough_for_unknown_media_type() {
        let out = decompress(b"not compressed", "application/octet-stream").expect("ok");
        assert_eq!(out, b"not compressed");
    }

    #[test]
    fn decompress_gzip_roundtrip() {
        use std::io::Write as _;
        let mut gz = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
        gz.write_all(b"hello tar").unwrap();
        let compressed = gz.finish().unwrap();
        let out = decompress(&compressed, "application/vnd.oci.image.layer.v1.tar+gzip")
            .expect("decompress");
        assert_eq!(out, b"hello tar");
    }

    #[test]
    fn build_parent_chain_reverses_base_first_to_child_first() {
        let base_first = vec![
            Layer {
                id: "base".into(),
                path: r"C:\l\base".into(),
            },
            Layer {
                id: "mid".into(),
                path: r"C:\l\mid".into(),
            },
            Layer {
                id: "top".into(),
                path: r"C:\l\top".into(),
            },
        ];
        let chain = build_parent_chain(&base_first);
        assert_eq!(chain.0.len(), 3);
        assert_eq!(chain.0[0].id, "top");
        assert_eq!(chain.0[1].id, "mid");
        assert_eq!(chain.0[2].id, "base");
    }

    #[test]
    fn build_parent_chain_handles_empty() {
        let chain = build_parent_chain(&[]);
        assert!(chain.0.is_empty());
    }

    #[test]
    fn new_layer_id_is_unique_and_uuid_shaped() {
        let a = new_layer_id();
        let b = new_layer_id();
        assert_ne!(a, b);
        assert_eq!(a.len(), 36); // 8-4-4-4-12 with hyphens
    }
}