windows-headless 0.3.7-alpha

Provision an SSH-ready headless Windows 11 VM from a retail installer ISO in one command. Unattended setup, portable QEMU, no Docker.
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
//! Portable QEMU provisioning from `jzombie/lab-qemu-portable` releases
//! (feature `downloader`).
//!
//! Layout on disk (managed by `cache-manager`, never evicted):
//! `<cache>/qemu-portable/<version>/qemu-portable/{bin,lib,share}/`.
//! The bundled dylibs/so link via `@executable_path`/`$ORIGIN`-relative
//! paths and QEMU finds `share/qemu` (OVMF firmware) relative to the
//! executable, so the tree works from any CWD with no env vars.
use anyhow::{Context, Result};
use std::env::consts;
use std::io::Read;
use std::path::{Component, Path, PathBuf};

const UA: &str = "windows-headless/0.1";

/// Env var granting explicit consent to skip SHA-256 verification when a
/// release ships no `SHA256SUMS.txt` (or no entry for our asset). Absent/false
/// = fail closed. Mirrored by the `--allow-unverified` CLI flags.
pub const ALLOW_UNVERIFIED_ENV: &str = "WINDOWS_HEADLESS_ALLOW_UNVERIFIED";

/// Verification escape hatch: explicit CLI flag wins, else the env var.
fn unverified_allowed(explicit: bool) -> bool {
    if explicit {
        return true;
    }
    let v = std::env::var(ALLOW_UNVERIFIED_ENV)
        .unwrap_or_default()
        .to_ascii_lowercase();
    matches!(v.as_str(), "1" | "true" | "yes" | "on")
}

/// Fail-closed verification of `archive_path` against the release
/// `SHA256SUMS.txt`. Missing sums sheet or missing entry bails unless the
/// operator explicitly opted into unverified downloads. On hash mismatch the
/// corrupt archive is removed before returning the error. The sums sheet
/// itself is always removed (it is staging litter, never cache).
///
/// `expected_pin`: when `Some` (pinned tag + known platform), the sums-file
/// hash must additionally equal the code-pinned hash — this is what defeats a
/// release-infrastructure compromise that forges sums and archive together.
/// `None` for explicit test tags (bytes legitimately differ from the pin).
async fn verify_archive_against_sums(
    assets: &[(String, String)],
    asset_name: &str,
    archive_path: &Path,
    base: &Path,
    allow_unverified: bool,
    expected_pin: Option<&str>,
) -> Result<()> {
    let sums_url = assets
        .iter()
        .find(|(n, _)| n == crate::paths::SHA256SUMS_FILE)
        .map(|(_, u)| u.clone());
    let Some(sums_url) = sums_url else {
        if unverified_allowed(allow_unverified) {
            tracing::warn!(
                "{asset_name}: no {} in release; proceeding unverified (explicitly allowed via {ALLOW_UNVERIFIED_ENV} or --allow-unverified)",
                crate::paths::SHA256SUMS_FILE
            );
            return Ok(());
        }
        let _ = std::fs::remove_file(archive_path);
        anyhow::bail!(
            "{asset_name}: release ships no {sums} — refusing unverified download. \
             Set {ALLOW_UNVERIFIED_ENV}=1 or pass --allow-unverified to proceed anyway (not recommended)",
            sums = crate::paths::SHA256SUMS_FILE
        );
    };
    let sums_path = base.join(format!("{}.download", crate::paths::SHA256SUMS_FILE));
    fetch_to(&sums_url, &sums_path).await?;
    let body = std::fs::read_to_string(&sums_path)?;
    let _ = std::fs::remove_file(&sums_path);
    match parse_sums(&body, asset_name) {
        Some(expected) => {
            let actual = crate::downloader::sha256_of(archive_path).await?;
            if !actual.eq_ignore_ascii_case(&expected) {
                let _ = std::fs::remove_file(archive_path);
                anyhow::bail!(
                    "SHA-256 mismatch for {asset_name}: expected {expected}, got {actual}"
                );
            }
            if let Some(pin) = expected_pin
                && !expected.eq_ignore_ascii_case(pin)
            {
                let _ = std::fs::remove_file(archive_path);
                anyhow::bail!(
                    "SHA-256 sums entry for {asset_name} does not match the pinned hash \
                     (expected pin {pin}, release sums say {expected}): refusing possibly \
                     compromised release asset"
                );
            }
            Ok(())
        }
        None => {
            if unverified_allowed(allow_unverified) {
                tracing::warn!(
                    "{asset_name} not listed in {}; proceeding unverified (explicitly allowed)",
                    crate::paths::SHA256SUMS_FILE
                );
                return Ok(());
            }
            let _ = std::fs::remove_file(archive_path);
            anyhow::bail!(
                "{asset_name} not listed in {} — refusing unverified download. \
                 Set {ALLOW_UNVERIFIED_ENV}=1 or pass --allow-unverified to proceed anyway (not recommended)",
                crate::paths::SHA256SUMS_FILE
            );
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum AssetKind {
    TarGz,
    TarXz,
}

/// A provisioned portable QEMU tree. `root` is the extracted `qemu-portable`
/// dir (the one containing `bin/`, `lib/`, `share/`); `version` is the
/// full release tag (`qemu-v11.1.1-57`), never a bare version.
#[derive(Debug, Clone)]
pub struct PortableQemu {
    pub root: PathBuf,
    pub version: String,
}

impl PortableQemu {
    pub fn qemu_img(&self) -> PathBuf {
        self.root
            .join(crate::paths::PORTABLE_BIN_DIR)
            .join(crate::paths::QEMU_IMG_NAME)
    }

    pub fn qemu_system(&self, binary: &str) -> PathBuf {
        self.root.join("bin").join(binary)
    }

    /// All `qemu-system-*` binaries actually present in this tree
    /// (what the release shipped for this host — e.g. macOS ships only
    /// `qemu-system-aarch64`, Linux ships `qemu-system-x86_64`).
    pub fn system_binaries(&self) -> Vec<PathBuf> {
        let mut out = Vec::new();
        if let Ok(rd) = std::fs::read_dir(self.root.join("bin")) {
            for entry in rd.flatten() {
                let name = entry.file_name().to_string_lossy().into_owned();
                if name.starts_with("qemu-system-") {
                    out.push(entry.path());
                }
            }
        }
        out.sort();
        out
    }
}

/// `(asset-name marker, kind)` for this host, or an error when no portable
/// build exists (Intel Macs, Windows hosts) telling the caller to use system
/// QEMU instead.
fn asset_for_host() -> Result<(&'static str, AssetKind)> {
    match (consts::OS, consts::ARCH) {
        ("macos", "aarch64") => Ok((crate::paths::MARKER_MACOS_ARM64, AssetKind::TarGz)),
        ("linux", "x86_64") => Ok((crate::paths::MARKER_LINUX_X86_64, AssetKind::TarXz)),
        ("linux", "aarch64") => Ok((crate::paths::MARKER_LINUX_AARCH64, AssetKind::TarXz)),
        (os, arch) => anyhow::bail!(
            "no portable QEMU build for {os}/{arch} (only macos-arm64 and linux x86_64/aarch64 are published); install system QEMU instead"
        ),
    }
}

/// Pinned SHA-256 for a QEMU portable asset marker at the pinned tag.
/// `None` for unknown markers (fail-closed downstream treats this as unverified,
/// which bails without the escape hatch — markers come from `asset_for_host`,
/// so `None` here means a new platform was added without pinning its hash).
/// `pub(crate)` for the `verify-pins` command.
pub(crate) fn pinned_qemu_sha256(marker: &str) -> Option<&'static str> {
    match marker {
        crate::paths::MARKER_MACOS_ARM64 => Some(crate::pins::QEMU_PORTABLE_SHA256_MACOS_ARM64),
        crate::paths::MARKER_LINUX_X86_64 => Some(crate::pins::QEMU_PORTABLE_SHA256_LINUX_X86_64),
        crate::paths::MARKER_LINUX_AARCH64 => Some(crate::pins::QEMU_PORTABLE_SHA256_LINUX_AARCH64),
        _ => None,
    }
}

/// Pinned SHA-256 for a wimlib portable asset marker at the pinned tag.
/// Same contract as [`pinned_qemu_sha256`].
/// `pub(crate)` for the `verify-pins` command.
pub(crate) fn pinned_wimlib_sha256(marker: &str) -> Option<&'static str> {
    match marker {
        crate::paths::MARKER_MACOS_ARM64 => Some(crate::pins::WIMLIB_PORTABLE_SHA256_MACOS_ARM64),
        crate::paths::MARKER_LINUX_X86_64 => Some(crate::pins::WIMLIB_PORTABLE_SHA256_LINUX_X86_64),
        crate::paths::MARKER_LINUX_AARCH64 => {
            Some(crate::pins::WIMLIB_PORTABLE_SHA256_LINUX_AARCH64)
        }
        _ => None,
    }
}

/// Pinned QEMU release tag: `run`/`install` use exactly this, `fetch-qemu`
/// downloads exactly this unless `--qemu-tag` overrides. Value lives in
/// [`crate::pins`]; this alias keeps existing call sites.
pub const DEFAULT_QEMU_PORTABLE_TAG: &str = crate::pins::QEMU_PORTABLE_TAG;

/// Parse a `SHA256SUMS.txt`-style body (`<hex>  <filename>` per line).
/// `pub(crate)` for the `verify-pins` command, which reuses release parsing
/// without downloading archives.
pub(crate) fn parse_sums(body: &str, asset: &str) -> Option<String> {
    body.lines().find_map(|line| {
        let mut parts = line.split_whitespace();
        match (parts.next(), parts.next()) {
            (Some(hash), Some(name)) if name.trim_start_matches('*') == asset => {
                Some(hash.to_string())
            }
            _ => None,
        }
    })
}

/// Download `url` to `dest` (via `.part` + rename, as in `downloader`),
/// with throttled stderr progress (see `downloader::Progress`).
async fn fetch_to(url: &str, dest: &Path) -> Result<()> {
    let client = reqwest::Client::builder()
        .user_agent(UA)
        .build()
        .context("reqwest client")?;
    let mut resp = client
        .get(url)
        .header("Accept", "application/vnd.github+json")
        .send()
        .await?
        .error_for_status()
        .with_context(|| format!("GET {url}"))?;
    if let Some(parent) = dest.parent()
        && !parent.as_os_str().is_empty()
    {
        tokio::fs::create_dir_all(parent).await?;
    }
    // Display the asset name, not our `.download` staging suffix.
    let name = dest
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_else(|| dest.display().to_string());
    let label = name.strip_suffix(".download").unwrap_or(&name).to_string();
    let mut progress = crate::downloader::Progress::new(&label, resp.content_length());
    let tmp = dest.with_extension("part");
    let mut file = tokio::fs::File::create(&tmp).await?;
    use tokio::io::AsyncWriteExt;
    while let Some(chunk) = resp.chunk().await? {
        progress.add(chunk.len());
        file.write_all(&chunk).await?;
    }
    file.flush().await?;
    progress.finish();
    drop(file);
    tokio::fs::rename(&tmp, dest).await?;
    Ok(())
}

/// GitHub API client (JSON + User-Agent), shared by the tag endpoints.
/// Attaches `GITHUB_TOKEN`/`GH_TOKEN` as a bearer token when set:
/// unauthenticated calls share 60 req/hr per egress IP (routinely
/// exhausted on CI runners → flaky 403s), authenticated calls get 5,000.
/// `GITHUB_TOKEN` is automatic in GitHub Actions; without it the client
/// behaves exactly as before. The token lives only in the request
/// headers and is never logged.
fn github_client() -> Result<reqwest::Client> {
    let mut headers = reqwest::header::HeaderMap::new();
    if let Some(token) = std::env::var("GITHUB_TOKEN")
        .or_else(|_| std::env::var("GH_TOKEN"))
        .ok()
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
    {
        headers.insert(
            reqwest::header::AUTHORIZATION,
            format!("Bearer {token}").parse().context("token header")?,
        );
    }
    reqwest::Client::builder()
        .user_agent(UA)
        .default_headers(headers)
        .build()
        .context("reqwest client")
}

/// Unpack a `.tar.gz` / `.tar.xz` archive into `dest`, rejecting absolute
/// paths and `..` components (defense in depth over `unpack_in`).
// `inner_bin_dir` only feeds the `#[cfg(unix)]` chmod pass below; allow
// the unused-variable lint where that block compiles out.
#[cfg_attr(windows, allow(unused_variables))]
fn extract(kind: AssetKind, archive: &Path, dest: &Path, inner_bin_dir: &str) -> Result<()> {
    // dest must already exist (created via the cache group); `unpack_in`
    // canonicalizes it as a traversal guard and fails otherwise.
    let file =
        std::fs::File::open(archive).with_context(|| format!("open {}", archive.display()))?;
    match kind {
        AssetKind::TarGz => {
            extract_tar(flate2::read::GzDecoder::new(file), dest)?;
        }
        AssetKind::TarXz => {
            extract_tar(liblzma::read::XzDecoder::new(file), dest)?;
        }
    }
    // tar preserves mode bits, but ensure the tools are executable in case
    // the archive or platform dropped them.
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if let Ok(rd) = std::fs::read_dir(dest.join(inner_bin_dir)) {
            for entry in rd.flatten() {
                let p = entry.path();
                if p.is_file() {
                    let mut perm = std::fs::metadata(&p)?.permissions();
                    perm.set_mode(perm.mode() | 0o111);
                    std::fs::set_permissions(&p, perm)?;
                }
            }
        }
    }
    Ok(())
}

fn extract_tar<R: Read>(reader: R, dest: &Path) -> Result<()> {
    let mut archive = tar::Archive::new(reader);
    for entry in archive.entries().context("read tar entries")? {
        let mut entry = entry.context("read tar entry")?;
        let path = entry.path().context("tar entry path")?.into_owned();
        if path.is_absolute() || path.components().any(|c| matches!(c, Component::ParentDir)) {
            anyhow::bail!("refusing unsafe tar entry: {}", path.display());
        }
        entry
            .unpack_in(dest)
            .with_context(|| format!("unpack {}", path.display()))?;
    }
    Ok(())
}

/// Ensure a portable QEMU tree in the project cache (never evicted).
///
/// - `dir`: explicit cache base, else `<project>/.cache/qemu-portable`
///   (same discovery as the ISO cache; `$WINDOWS_HEADLESS_QEMU_DIR`
///   overrides the base).
/// - `version`: exact release tag (for new-build testing); else the
///   pinned [`DEFAULT_QEMU_PORTABLE_TAG`]. Tags are used verbatim —
///   no normalization. Already-extracted trees are reused without
///   touching the network.
/// - `allow_unverified`: permit proceeding when the release ships no
///   `SHA256SUMS.txt` (or no entry for our asset). Fails closed by default;
///   also honored via `$WINDOWS_HEADLESS_ALLOW_UNVERIFIED`.
pub async fn ensure(
    dir: Option<PathBuf>,
    version: Option<String>,
    allow_unverified: bool,
) -> Result<PortableQemu> {
    // All directories via the shared crate::cache helper: explicit --dir /
    // env override win, else workspace discovery, else the OS user cache.
    // Every subdirectory (group, version) goes through the group API —
    // no raw create_dir_all.
    let root = crate::cache::resolve_cache_root(dir, "WINDOWS_HEADLESS_QEMU_DIR")?;
    // Ensure the base group exists even before we know the version, so the
    // fast path below can scan it.
    root.ensure_group(crate::paths::QEMU_GROUP)
        .context("create qemu-portable cache group")?;

    let (marker, kind) = asset_for_host()?;

    let tag: String = match version {
        Some(v) => v,
        None => DEFAULT_QEMU_PORTABLE_TAG.to_string(),
    };
    let group = root.group(format!("{}/{tag}", crate::paths::QEMU_GROUP));
    group.ensure_dir().context("create version cache group")?;
    let tree = group.path().join(crate::paths::QEMU_INNER);
    if tree
        .join(crate::paths::PORTABLE_BIN_DIR)
        .join(crate::paths::QEMU_IMG_NAME)
        .is_file()
    {
        return Ok(PortableQemu {
            root: tree,
            version: tag,
        });
    }

    // Pin the exact asset URLs from the release metadata so a renamed
    // asset fails loudly instead of 404ing a guessed URL.
    let assets = release_by_tag(&tag).await?;
    let asset_name = assets
        .iter()
        .map(|(n, _)| n.as_str())
        .find(|n| n.contains(marker) && n.starts_with(crate::paths::QEMU_ASSET_PREFIX))
        .context(format!(
            "release has no portable asset for this host ({marker})"
        ))?
        .to_string();
    let url = assets
        .iter()
        .find(|(n, _)| n == &asset_name)
        .map(|(_, u)| u.clone())
        .unwrap();

    let base = root.group_path(crate::paths::QEMU_GROUP);
    let archive_path = base.join(format!("{asset_name}.download"));
    fetch_to(&url, &archive_path).await?;

    // Fail-closed verification: missing sums sheet or entry bails unless the
    // operator explicitly allowed unverified downloads. Mismatches remove the
    // corrupt archive before returning. The pin additionally binds the sums
    // entry for the pinned tag, defeating forged sums+archive pairs.
    let expected_pin = (tag == DEFAULT_QEMU_PORTABLE_TAG).then(|| pinned_qemu_sha256(marker));
    let expected_pin = expected_pin.flatten();
    verify_archive_against_sums(
        &assets,
        &asset_name,
        &archive_path,
        &base,
        allow_unverified,
        expected_pin,
    )
    .await?;

    // No staging: extract straight into the group dir. Safe because the
    // sentinel check above guarantees no good tree lives here — `dest` is
    // absent or stale — and a killed fetch leaves a sentinel-less tree the
    // next run simply refetches. Validity = sentinel presence, nothing else.
    // (Concurrent double-fetch can interleave in one dir; single-user CLI,
    // and any interleaving still ends sentinel-less on failure.)
    let dest = group.path().to_path_buf();
    if dest.exists() {
        std::fs::remove_dir_all(&dest)
            .with_context(|| format!("clear stale {}", dest.display()))?;
    }
    // Group creation normalizes; the re-sweep after extract clears the
    // read-only modes tar writes per member. Single call, owned by
    // cache-manager — no local permission logic anywhere in this flow.
    // WHY: release tarballs ship 0444 lib files (verified live against
    // the macos-arm64 asset); without the re-sweep below, Windows tree
    // deletion breaks on rotation. Belt (ensure_dir) and suspenders
    // (re-sweep): tar re-applies read-only modes during unpack, after
    // creation normalized.
    group.ensure_dir()?;
    extract(
        kind,
        &archive_path,
        &dest,
        &format!(
            "{}/{}",
            crate::paths::QEMU_INNER,
            crate::paths::PORTABLE_BIN_DIR
        ),
    )?;
    // WHY (explicitly): tar just re-applied 0444 modes per member
    // during unpack, undoing what creation normalized. This re-sweep —
    // through the group interface, owned by cache-manager — clears them.
    // The comment stays so nobody "simplifies" it away.
    group.ensure_dir()?;
    let _ = std::fs::remove_file(&archive_path);
    let root = dest.join(crate::paths::QEMU_INNER);

    Ok(PortableQemu { root, version: tag })
}

/// Pinned portable tree in the project cache, without touching the
/// network. Used by `run`/`install` so end users never pass `--qemu-dir`
/// — and never silently run a different QEMU than the pin. Anything else
/// (e.g. a newer build under test) must be selected explicitly via
/// `--qemu-dir` or `fetch-qemu --qemu-tag`.
pub fn pinned_cached() -> Result<PortableQemu> {
    let root = crate::cache::resolve_cache_root(None, "WINDOWS_HEADLESS_QEMU_DIR")?;
    let base = root.group_path(crate::paths::QEMU_GROUP);
    let tag = DEFAULT_QEMU_PORTABLE_TAG.to_string();
    let tree = base.join(&tag).join(crate::paths::QEMU_INNER);
    if tree
        .join(crate::paths::PORTABLE_BIN_DIR)
        .join(crate::paths::QEMU_IMG_NAME)
        .is_file()
    {
        return Ok(PortableQemu {
            root: tree,
            version: tag,
        });
    }
    let found = cached_versions(&base);
    anyhow::bail!(
        "pinned portable QEMU {tag} not cached (found: {found}); run `fetch-qemu` (fetches the pin) or pass `--qemu-dir` to use another tree explicitly",
    )
}

/// Versions with an extracted tree under a `qemu-portable` cache base
/// (full tags, or `none`), for the `pinned_cached` error message.
fn cached_versions(base: &Path) -> String {
    let mut found: Vec<String> = std::fs::read_dir(base)
        .map(|rd| {
            rd.flatten()
                .filter(|e| {
                    e.path()
                        .join(crate::paths::QEMU_INNER)
                        .join(crate::paths::PORTABLE_BIN_DIR)
                        .join(crate::paths::QEMU_IMG_NAME)
                        .is_file()
                })
                .map(|e| e.file_name().to_string_lossy().into_owned())
                .collect()
        })
        .unwrap_or_default();
    found.sort();
    if found.is_empty() {
        "none".to_string()
    } else {
        found.join(", ")
    }
}

/// Fetch release metadata for an exact release tag (`qemu-v11.1.1-57`).
/// No latest-release fallback: under never-overwrite rebuild tags, latest
/// is never the answer to "give me the pin" — a missing tag is a hard
/// error naming the tag.
/// `pub(crate)` for the `verify-pins` command, which checks asset
/// availability without downloading archives.
pub(crate) async fn release_by_tag(tag: &str) -> Result<Vec<(String, String)>> {
    let url = format!(
        "https://api.github.com/repos/{}/releases/tags/{tag}",
        crate::pins::RELEASE_REPO,
    );
    let body = github_get_json(&url, &format!("portable QEMU release {tag}")).await?;
    let (_, assets) = parse_release_body(&body)?;
    Ok(assets)
}

/// GET `url` via [`github_client`], returning the decoded JSON body.
/// `what` names the lookup (e.g. `portable QEMU release qemu-v11.1.3`) for
/// error context. The HTTP status is captured before `error_for_status()`
/// so failures name the likely cause: a 403 here is almost always the
/// shared-IP 60 req/hr unauthenticated rate limit (not a missing tag) —
/// the workflows pass GITHUB_TOKEN (5,000/hr) for exactly this reason.
async fn github_get_json(url: &str, what: &str) -> Result<serde_json::Value> {
    let resp = github_client()?
        .get(url)
        .header("Accept", "application/vnd.github+json")
        .send()
        .await?;
    let status = resp.status();
    resp.error_for_status()
        .with_context(|| {
            format!("{what} lookup failed (HTTP {status}; 403 usually means GitHub API rate limit — retry with GITHUB_TOKEN set)")
        })?
        .json()
        .await
        .context("decode GitHub release JSON")
}

fn parse_release_body(body: &serde_json::Value) -> Result<(String, Vec<(String, String)>)> {
    let tag = body
        .get("tag_name")
        .and_then(|t| t.as_str())
        .context("release has no tag_name")?
        .to_string();
    let assets = body
        .get("assets")
        .and_then(|a| a.as_array())
        .context("release has no assets array")?
        .iter()
        .filter_map(|a| {
            Some((
                a.get("name")?.as_str()?.to_string(),
                a.get("browser_download_url")?.as_str()?.to_string(),
            ))
        })
        .collect();
    Ok((tag, assets))
}

/// A provisioned portable wimlib tree (`wimlib-portable` release asset).
/// `root` is the extracted inner dir holding `bin/wimlib-imagex`.
#[derive(Debug, Clone)]
pub struct PortableWimlib {
    pub root: PathBuf,
    pub version: String,
}

impl PortableWimlib {
    pub fn wimlib_bin(&self) -> PathBuf {
        self.root
            .join(crate::paths::PORTABLE_BIN_DIR)
            .join(crate::paths::WIMLIB_IMGX_NAME)
    }
}

/// Pinned wimlib release tag: `build-media` uses exactly this,
/// `fetch-wimlib` downloads exactly this unless `--wimlib-tag` overrides.
/// Value lives in [`crate::pins`]; this alias keeps existing call sites.
pub const DEFAULT_WIMLIB_PORTABLE_TAG: &str = crate::pins::WIMLIB_PORTABLE_TAG;

/// Release metadata for an exact tag in `repo` (`owner/name`).
async fn release_for_tag(repo: &str, tag: &str) -> Result<(String, Vec<(String, String)>)> {
    let url = format!("https://api.github.com/repos/{repo}/releases/tags/{tag}");
    let body = github_get_json(&url, &format!("release {tag} in {repo}")).await?;
    parse_release_body(&body)
}

/// Download URL of `asset` in release `tag` of `repo` (fails loudly on rename).
pub async fn release_asset_url(repo: &str, tag: &str, asset: &str) -> Result<String> {
    let (_, assets) = release_for_tag(repo, tag).await?;
    assets
        .iter()
        .find(|(n, _)| n == asset)
        .map(|(_, u)| u.clone())
        .with_context(|| format!("release {tag} has no asset {asset}"))
}

/// Ensure a portable wimlib tree in the project cache (never evicted).
/// Same shape as [`ensure`]: `$WINDOWS_HEADLESS_WIMLIB_DIR` overrides,
/// else `<project>/.cache/wimlib-portable`; exact release tag or the
/// pinned [`DEFAULT_WIMLIB_PORTABLE_TAG`]. Tags are used verbatim.
/// Already-extracted trees are reused without touching the network.
/// `allow_unverified` mirrors [`ensure`]: fail closed unless set (or
/// `$WINDOWS_HEADLESS_ALLOW_UNVERIFIED`).
pub async fn ensure_wimlib(
    dir: Option<PathBuf>,
    version: Option<String>,
    allow_unverified: bool,
) -> Result<PortableWimlib> {
    let root = crate::cache::resolve_cache_root(dir, "WINDOWS_HEADLESS_WIMLIB_DIR")?;
    root.ensure_group(crate::paths::WIMLIB_GROUP)
        .context("create wimlib-portable cache group")?;
    let (marker, kind) = asset_for_host()?;
    let tag: String = match version {
        Some(v) => v,
        None => DEFAULT_WIMLIB_PORTABLE_TAG.to_string(),
    };
    let group = root.group(format!("{}/{tag}", crate::paths::WIMLIB_GROUP));
    group.ensure_dir().context("create version cache group")?;
    let tree = group.path().join(crate::paths::WIMLIB_INNER);
    if tree
        .join(crate::paths::PORTABLE_BIN_DIR)
        .join(crate::paths::WIMLIB_IMGX_NAME)
        .is_file()
    {
        return Ok(PortableWimlib {
            root: tree,
            version: tag,
        });
    }
    let (_, assets) = release_for_tag(crate::pins::RELEASE_REPO, &tag).await?;
    let asset_name = assets
        .iter()
        .map(|(n, _)| n.as_str())
        .find(|n| n.contains(marker) && n.starts_with(crate::paths::WIMLIB_ASSET_PREFIX))
        .with_context(|| format!("release {tag} has no portable asset for this host ({marker})"))?
        .to_string();
    let url = assets
        .iter()
        .find(|(n, _)| n == &asset_name)
        .map(|(_, u)| u.clone())
        .unwrap();
    let base = root.group_path(crate::paths::WIMLIB_GROUP);
    let archive_path = base.join(format!("{asset_name}.download"));
    fetch_to(&url, &archive_path).await?;
    let expected_pin = (tag == DEFAULT_WIMLIB_PORTABLE_TAG).then(|| pinned_wimlib_sha256(marker));
    let expected_pin = expected_pin.flatten();
    verify_archive_against_sums(
        &assets,
        &asset_name,
        &archive_path,
        &base,
        allow_unverified,
        expected_pin,
    )
    .await?;
    let dest = group.path().to_path_buf();
    // No staging (see QEMU flow above): sentinel check at entry guarantees
    // no good tree lives here; validity is sentinel presence alone.
    if dest.exists() {
        std::fs::remove_dir_all(&dest)
            .with_context(|| format!("clear stale {}", dest.display()))?;
    }
    // Group creation normalizes; the re-sweep after extract clears the
    // read-only modes tar writes per member. Owned by cache-manager.
    group.ensure_dir()?;
    extract(
        kind,
        &archive_path,
        &dest,
        &format!(
            "{}/{}",
            crate::paths::WIMLIB_INNER,
            crate::paths::PORTABLE_BIN_DIR
        ),
    )?;
    // WHY (explicitly): tar just re-applied 0444 modes per member
    // during unpack, undoing what creation normalized. This re-sweep —
    // through the group interface, owned by cache-manager — clears them.
    // The comment stays so nobody "simplifies" it away.
    group.ensure_dir()?;
    let _ = std::fs::remove_file(&archive_path);
    let root = dest.join(crate::paths::WIMLIB_INNER);
    Ok(PortableWimlib { root, version: tag })
}

/// Pinned portable wimlib tree, without touching the network. Used by
/// `build-media` so end users never pass `--wimlib-bin` — and never
/// silently split with a different wimlib than the pin.
pub fn pinned_cached_wimlib() -> Result<PortableWimlib> {
    let root = crate::cache::resolve_cache_root(None, "WINDOWS_HEADLESS_WIMLIB_DIR")?;
    let base = root.group_path(crate::paths::WIMLIB_GROUP);
    let tag = DEFAULT_WIMLIB_PORTABLE_TAG.to_string();
    let tree = base.join(&tag).join(crate::paths::WIMLIB_INNER);
    if tree
        .join(crate::paths::PORTABLE_BIN_DIR)
        .join(crate::paths::WIMLIB_IMGX_NAME)
        .is_file()
    {
        return Ok(PortableWimlib {
            root: tree,
            version: tag,
        });
    }
    let found = cached_wimlib_versions(&base);
    anyhow::bail!(
        "pinned portable wimlib {tag} not cached (found: {found}); run `fetch-wimlib` (fetches the pin)"
    )
}

/// Versions with an extracted tree under a `wimlib-portable` cache base,
/// for the `pinned_cached_wimlib` error message.
fn cached_wimlib_versions(base: &Path) -> String {
    let mut found: Vec<String> = std::fs::read_dir(base)
        .map(|rd| {
            rd.flatten()
                .filter(|e| {
                    e.path()
                        .join(crate::paths::WIMLIB_INNER)
                        .join(crate::paths::PORTABLE_BIN_DIR)
                        .join(crate::paths::WIMLIB_IMGX_NAME)
                        .is_file()
                })
                .map(|e| e.file_name().to_string_lossy().into_owned())
                .collect()
        })
        .unwrap_or_default();
    found.sort();
    if found.is_empty() {
        "none".to_string()
    } else {
        found.join(", ")
    }
}

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

    #[test]
    fn sums_parsing() {
        let body = "abc123  qemu-portable-linux-x86_64-11.1.1.tar.xz\ndef456 *other.zip\n";
        assert_eq!(
            parse_sums(body, "qemu-portable-linux-x86_64-11.1.1.tar.xz"),
            Some("abc123".to_string())
        );
        assert_eq!(parse_sums(body, "missing"), None);
    }

    #[test]
    fn default_qemu_tag_is_the_pin() {
        assert_eq!(DEFAULT_QEMU_PORTABLE_TAG, crate::pins::QEMU_PORTABLE_TAG);
    }

    #[test]
    fn pinned_cached_finds_pin_and_rejects_others() {
        let tmp = tempfile::tempdir().unwrap();
        // Cache dirs are tags, verbatim.
        let pin_bin = tmp
            .path()
            .join("qemu-portable")
            .join(DEFAULT_QEMU_PORTABLE_TAG)
            .join("qemu-portable/bin");
        std::fs::create_dir_all(&pin_bin).unwrap();
        std::fs::write(pin_bin.join("qemu-img"), b"x").unwrap();
        // A newer tree must NOT shadow the pin.
        let other_bin = tmp
            .path()
            .join("qemu-portable/qemu-v99.99.99/qemu-portable/bin");
        std::fs::create_dir_all(&other_bin).unwrap();
        std::fs::write(other_bin.join("qemu-img"), b"x").unwrap();
        // SAFETY: process-local env, restored below; no other test reads
        // this variable (only pinned_cached, called solely here).
        unsafe { std::env::set_var("WINDOWS_HEADLESS_QEMU_DIR", tmp.path()) };
        let got = pinned_cached().unwrap();
        assert_eq!(got.version, DEFAULT_QEMU_PORTABLE_TAG);
        assert!(
            got.root
                .ends_with(format!("{}/qemu-portable", DEFAULT_QEMU_PORTABLE_TAG))
        );
        std::fs::remove_dir_all(
            tmp.path()
                .join("qemu-portable")
                .join(DEFAULT_QEMU_PORTABLE_TAG),
        )
        .unwrap();
        assert!(pinned_cached().is_err());
        unsafe { std::env::remove_var("WINDOWS_HEADLESS_QEMU_DIR") };
    }

    #[test]
    fn default_wimlib_tag_is_the_pin() {
        assert_eq!(
            DEFAULT_WIMLIB_PORTABLE_TAG,
            crate::pins::WIMLIB_PORTABLE_TAG
        );
    }

    #[test]
    fn pinned_cached_wimlib_finds_pin_and_rejects_others() {
        let tmp = tempfile::tempdir().unwrap();
        let pin_bin = tmp
            .path()
            .join("wimlib-portable")
            .join(DEFAULT_WIMLIB_PORTABLE_TAG)
            .join("wimlib-portable/bin");
        std::fs::create_dir_all(&pin_bin).unwrap();
        std::fs::write(pin_bin.join("wimlib-imagex"), b"x").unwrap();
        let other_bin = tmp
            .path()
            .join("wimlib-portable/wimlib-v9.9.9/wimlib-portable/bin");
        std::fs::create_dir_all(&other_bin).unwrap();
        std::fs::write(other_bin.join("wimlib-imagex"), b"x").unwrap();
        // SAFETY: process-local env, restored below; no other test reads
        // this variable (only pinned_cached_wimlib, called solely here).
        unsafe { std::env::set_var("WINDOWS_HEADLESS_WIMLIB_DIR", tmp.path()) };
        let got = pinned_cached_wimlib().unwrap();
        assert_eq!(got.version, DEFAULT_WIMLIB_PORTABLE_TAG);
        assert!(
            got.root
                .ends_with(format!("{}/wimlib-portable", DEFAULT_WIMLIB_PORTABLE_TAG))
        );
        std::fs::remove_dir_all(
            tmp.path()
                .join("wimlib-portable")
                .join(DEFAULT_WIMLIB_PORTABLE_TAG),
        )
        .unwrap();
        assert!(pinned_cached_wimlib().is_err());
        unsafe { std::env::remove_var("WINDOWS_HEADLESS_WIMLIB_DIR") };
    }

    #[test]
    fn host_asset_selection_matches_this_machine() {
        // Must not error on the dev/CI hosts we support (macos-arm64,
        // linux x86_64/aarch64); other hosts get the actionable error.
        let _ = asset_for_host();
    }

    #[test]
    fn unverified_gate_truth_table() {
        // SAFETY: this test solely owns ALLOW_UNVERIFIED_ENV for its body;
        // no other test reads it.
        let prior = std::env::var(ALLOW_UNVERIFIED_ENV).ok();
        unsafe { std::env::remove_var(ALLOW_UNVERIFIED_ENV) };
        assert!(!unverified_allowed(false));
        assert!(unverified_allowed(true));
        unsafe { std::env::set_var(ALLOW_UNVERIFIED_ENV, "1") };
        assert!(unverified_allowed(false));
        unsafe { std::env::set_var(ALLOW_UNVERIFIED_ENV, "0") };
        assert!(!unverified_allowed(false));
        assert!(unverified_allowed(true));
        match prior {
            Some(v) => unsafe { std::env::set_var(ALLOW_UNVERIFIED_ENV, v) },
            None => unsafe { std::env::remove_var(ALLOW_UNVERIFIED_ENV) },
        }
    }
}