kh-runtime 0.1.7

Kakehashi native ARM64 execution context, memory layout, traps, and bottle
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
//! Install Apple **Command Line Tools** into the bottle (source of Apple `git`).
//!
//! Primary path (clean Linux / Docker):
//! 1. Public Software Update catalog (`swscan.apple.com`) → latest CLT
//!    `CLTools_Executables*.pkg` (see [`swscan`])
//! 2. Persistent cache under `KAKEHASHI_DATA_DIR/cache` (Docker bind mount)
//! 3. Extract → `{bottle}/Library/Developer/CommandLineTools/…`
//! 4. Symlink `{bottle}/usr/bin/git` → CLT git
//!
//! Optional: `KAKEHASHI_XCODE_TOOLS_VERSION` pins the catalog title substring.
//! Force re-fetch: `KAKEHASHI_FORCE_DOWNLOAD=1`.
//!
//! Idempotent: if bottle already has `…/usr/bin/git`, install is a no-op (no network).

use std::fs::{self, File};
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use super::download_cache;
use super::guest_tools::{InstallReport, ToolError};
use super::layout::is_bottle_root;
use super::manage;
use super::swscan;

/// Guest-relative root of the CLT tree under the bottle.
pub const GUEST_CLT_REL: &str = "Library/Developer/CommandLineTools";

/// Guest absolute path of Apple `git` after install.
pub const GUEST_GIT_PATH: &str = "/Library/Developer/CommandLineTools/usr/bin/git";

/// Guest-relative path of git inside the CLT tree.
pub const GUEST_GIT_REL: &str = "Library/Developer/CommandLineTools/usr/bin/git";

/// Guest-relative symlink at classic `/usr/bin/git`.
pub(crate) const GUEST_USR_BIN_GIT_REL: &str = "usr/bin/git";

/// Durable extract tree name under the download cache.
const CACHE_EXTRACT_NAME: &str = "command-line-tools";

/// Install CLT into the active bottle (creating the bottle if needed).
pub(crate) fn install_xcode_tools() -> Result<InstallReport, ToolError> {
    let bottle = ensure_active_bottle()?;

    // Idempotent: Docker re-runs must not re-download.
    if !download_cache::force_download() {
        let host_git = bottle.join(GUEST_GIT_REL);
        if host_git.is_file() {
            return Ok(InstallReport {
                package: "xcode-tools",
                host_path: host_git,
                guest_path: GUEST_GIT_PATH,
                bottle,
            });
        }
    }

    let archive = resolve_clt_archive()?;
    let clt_root = extract_archive_to_cache(&archive)?;
    install_clt_into_bottle(&bottle, &clt_root)?;

    let host_git = bottle.join(GUEST_GIT_REL);
    if !host_git.is_file() {
        return Err(ToolError::Command(format!(
            "CLT install finished but git missing at {}",
            host_git.display()
        )));
    }
    set_executable(&host_git)?;

    Ok(InstallReport {
        package: "xcode-tools",
        host_path: host_git,
        guest_path: GUEST_GIT_PATH,
        bottle,
    })
}

/// Whether the bottle already has Apple git from a prior CLT install.
#[must_use]
pub fn bottle_has_git(bottle: &Path) -> bool {
    bottle.join(GUEST_GIT_REL).is_file()
}

/// Discover Apple `git` under an active bottle.
#[must_use]
pub fn discover_git(explicit: Option<&Path>) -> Option<PathBuf> {
    if let Some(p) = explicit {
        if p.is_file() {
            return Some(p.to_path_buf());
        }
        return None;
    }
    if let Ok(Some(root)) = manage::active_root() {
        let p = root.join(GUEST_GIT_REL);
        if p.is_file() {
            return Some(p);
        }
        let alt = root.join(GUEST_USR_BIN_GIT_REL);
        if alt.is_file() {
            return Some(alt);
        }
    }
    None
}

fn ensure_active_bottle() -> Result<PathBuf, ToolError> {
    if let Ok(Some(root)) = manage::active_root()
        && is_bottle_root(&root)
    {
        return Ok(root);
    }
    let created = manage::ensure(&manage::CreateOptions {
        path: None,
        libsystem: None,
        skip_libsystem: false,
    })
    .map_err(|e| ToolError::Bottle(e.to_string()))?;
    Ok(created.path)
}

fn resolve_clt_archive() -> Result<PathBuf, ToolError> {
    match swscan::download_selected_clt() {
        Ok((_pkg, path)) => Ok(path),
        Err(e) => {
            let mut msg = format!("Apple CLT download failed: {e}\n");
            let mut help = Vec::new();
            drop(swscan::fallback_help(&mut help));
            msg.push_str(&String::from_utf8_lossy(&help));
            Err(ToolError::Command(msg))
        }
    }
}

fn extract_archive_to_cache(archive: &Path) -> Result<PathBuf, ToolError> {
    let extract_root = download_cache::extract_path(CACHE_EXTRACT_NAME).map_err(ToolError::Io)?;
    if !download_cache::force_download()
        && let Some(existing) = find_clt_root(&extract_root)
    {
        return Ok(existing);
    }
    if extract_root.exists() {
        drop(fs::remove_dir_all(&extract_root));
    }
    fs::create_dir_all(&extract_root)?;

    let name = archive
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or("")
        .to_ascii_lowercase();

    let is_pkg = archive
        .extension()
        .is_some_and(|ext| ext.eq_ignore_ascii_case("pkg"));
    let is_tar = name.contains(".tar.")
        || Path::new(&name)
            .extension()
            .is_some_and(|ext| ext.eq_ignore_ascii_case("tar") || ext.eq_ignore_ascii_case("tgz"));

    if is_pkg {
        // Primary: own XAR + pbzx + odc (no p7zip/bsdtar).
        super::pkg_extract::extract_apple_pkg(archive, &extract_root).map_err(|e| {
            ToolError::Command(format!("Apple .pkg extract failed: {e}"))
        })?;
    } else if is_tar {
        extract_tar(archive, &extract_root)?;
    } else {
        // .dmg / odd wrappers — host 7z peel + nested pkg.
        extract_with_7z(archive, &extract_root)?;
        peel_nested_packages(&extract_root)?;
    }

    find_clt_root(&extract_root).ok_or_else(|| {
        ToolError::Command(format!(
            "extracted archive but no usr/bin/git under {} \
             (need CLTools_Executables package from Software Update)",
            extract_root.display()
        ))
    })
}

fn extract_tar(archive: &Path, dest: &Path) -> Result<(), ToolError> {
    let status = Command::new("tar")
        .arg("-xf")
        .arg(archive)
        .current_dir(dest)
        .status()
        .map_err(|e| ToolError::Command(format!("tar: {e}")))?;
    if !status.success() {
        return Err(ToolError::Command(format!(
            "tar extract failed (status {status}) for {}",
            archive.display()
        )));
    }
    Ok(())
}

fn extract_with_7z(archive: &Path, dest: &Path) -> Result<(), ToolError> {
    // Apple flat `.pkg` is XAR. Prefer bsdtar (libarchive) — Debian's p7zip 16.x
    // hardlinks every member to the Payload heap (corrupt Bom/Scripts).
    let is_pkg = archive
        .extension()
        .is_some_and(|ext| ext.eq_ignore_ascii_case("pkg"));
    if is_pkg && extract_pkg_bsdtar(archive, dest).is_ok() && pkg_extract_looks_sane(dest) {
        return Ok(());
    }

    for bin in ["7z", "7zz"] {
        let mut cmd = Command::new(bin);
        cmd.args(["x", "-y", &format!("-o{}", dest.display())]);
        if is_pkg {
            // Avoid nested open of pbzx Payload as a single xz stream.
            cmd.arg("-tXar");
        }
        cmd.arg(archive);
        let status = cmd.status();
        if let Ok(st) = status {
            // XAR extract often warns about a 20-byte trailer but still writes files.
            if (st.success() || dest_has_payload_or_files(dest))
                && (!is_pkg || pkg_extract_looks_sane(dest))
            {
                return Ok(());
            }
            // Corrupt p7zip extract: wipe and try next tool.
            if is_pkg {
                wipe_dir_contents(dest);
            }
        }
    }

    if is_pkg && extract_pkg_bsdtar(archive, dest).is_ok() && pkg_extract_looks_sane(dest) {
        return Ok(());
    }

    Err(ToolError::Command(format!(
        "need host `bsdtar` (libarchive-tools) or a working `7z`/`7zz` to extract {} \
         (Debian p7zip 16.x corrupts flat .pkg XAR members)",
        archive.display()
    )))
}

fn extract_pkg_bsdtar(archive: &Path, dest: &Path) -> Result<(), ToolError> {
    for bin in ["bsdtar", "tar"] {
        let status = Command::new(bin)
            .args(["-xf"])
            .arg(archive)
            .current_dir(dest)
            .status();
        if let Ok(st) = status
            && st.success()
            && dest_has_payload_or_files(dest)
        {
            return Ok(());
        }
    }
    Err(ToolError::Command("bsdtar/tar pkg extract failed".into()))
}

fn dest_has_payload_or_files(dest: &Path) -> bool {
    dest.join("Payload").is_file()
        || dest
            .read_dir()
            .is_ok_and(|mut d| d.next().is_some())
}

/// Reject the known p7zip-16 XAR bug: every member hardlinked to Payload size.
fn pkg_extract_looks_sane(dest: &Path) -> bool {
    let payload = dest.join("Payload");
    if !payload.is_file() {
        return false;
    }
    let Ok(payload_len) = payload.metadata().map(|m| m.len()) else {
        return false;
    };
    if payload_len == 0 {
        return false;
    }
    // Bom / PackageInfo must be smaller than Payload for real CLT packages.
    for name in ["Bom", "PackageInfo", "Scripts"] {
        let p = dest.join(name);
        if !p.is_file() {
            continue;
        }
        if p.metadata().is_ok_and(|m| m.len() == payload_len) {
            return false;
        }
    }
    // Payload itself should be pbzx / gzip / cpio — not empty.
    let mut head = [0_u8; 4];
    if File::open(&payload)
        .and_then(|mut f| f.read(&mut head))
        .is_ok_and(|n| n >= 4)
    {
        let gzip = matches!((head.first(), head.get(1)), (Some(&0x1f), Some(&0x8b)));
        if &head == b"pbzx" || gzip {
            return true;
        }
        // Accept any non-trivial payload; cpio may start with various magic.
        return payload_len > 1024;
    }
    false
}

fn wipe_dir_contents(dir: &Path) {
    if let Ok(entries) = fs::read_dir(dir) {
        for ent in entries.flatten() {
            let p = ent.path();
            if p.is_dir() {
                drop(fs::remove_dir_all(&p));
            } else {
                drop(fs::remove_file(&p));
            }
        }
    }
}

fn peel_nested_packages(root: &Path) -> Result<(), ToolError> {
    for _ in 0..6 {
        if find_clt_root(root).is_some() {
            return Ok(());
        }
        if let Some(nested) = find_first_with_suffix(root, &[".pkg", ".dmg"]) {
            let out = nested.with_extension("extracted");
            if !out.exists() {
                fs::create_dir_all(&out)?;
                extract_with_7z(&nested, &out)?;
            }
            continue;
        }
        if let Some(payload) = find_named_file(root, "Payload") {
            let out = payload
                .parent()
                .unwrap_or(root)
                .join("Payload.extracted");
            if !out.exists() {
                fs::create_dir_all(&out)?;
                extract_payload(&payload, &out)?;
            }
            continue;
        }
        break;
    }
    Ok(())
}

/// Expand a macOS package `Payload` (pbzx / gzip / plain cpio) into `dest`.
fn extract_payload(payload: &Path, dest: &Path) -> Result<(), ToolError> {
    let mut head = [0_u8; 4];
    {
        let mut f = File::open(payload).map_err(ToolError::Io)?;
        let n = f.read(&mut head).map_err(ToolError::Io)?;
        if n < 4 {
            return Err(ToolError::Command(format!(
                "Payload too small: {}",
                payload.display()
            )));
        }
    }
    if &head == b"pbzx" {
        return extract_payload_pbzx(payload, dest);
    }
    // gzip magic 1f 8b
    if head[0] == 0x1f && head[1] == 0x8b {
        return extract_payload_gzip_cpio(payload, dest);
    }
    // Try 7z, then raw cpio.
    if extract_with_7z(payload, dest).is_ok() && find_clt_root(dest).is_some() {
        return Ok(());
    }
    extract_payload_raw_cpio(payload, dest)
}

/// Apple **pbzx** multi-stream xz wrapper used by modern CLT / Xcode packages.
///
/// Layout (big-endian):
/// `pbzx` + u64 flags + repeated (u64 chunk_flags, u64 xz_size, xz_bytes…).
/// Concatenated xz streams decompress to a single cpio archive.
fn extract_payload_pbzx(payload: &Path, dest: &Path) -> Result<(), ToolError> {
    let mut input = File::open(payload).map_err(ToolError::Io)?;
    let mut magic = [0_u8; 4];
    input.read_exact(&mut magic).map_err(ToolError::Io)?;
    if &magic != b"pbzx" {
        return Err(ToolError::Command("Payload missing pbzx magic".into()));
    }
    let mut flags = [0_u8; 8];
    input.read_exact(&mut flags).map_err(ToolError::Io)?;

    let mut cpio = Command::new("cpio")
        .args(["-idm"])
        .current_dir(dest)
        .stdin(Stdio::piped())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .map_err(|e| ToolError::Command(format!("cpio: {e}")))?;
    let mut cpio_in = cpio.stdin.take().ok_or_else(|| {
        ToolError::Command("cpio produced no stdin for pbzx Payload".into())
    })?;

    let mut chunks = 0_u32;
    loop {
        let mut hdr = [0_u8; 16];
        match input.read_exact(&mut hdr) {
            Ok(()) => {}
            Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => break,
            Err(e) => return Err(ToolError::Io(e)),
        }
        let Some(size_bytes) = hdr.get(8..16) else {
            return Err(ToolError::Command("pbzx header truncated".into()));
        };
        let mut size_arr = [0_u8; 8];
        size_arr.copy_from_slice(size_bytes);
        let xz_size = u64::from_be_bytes(size_arr);
        if xz_size == 0 {
            break;
        }
        let Ok(xz_len) = usize::try_from(xz_size) else {
            return Err(ToolError::Command(format!(
                "pbzx xz chunk too large ({xz_size} bytes)"
            )));
        };

        let mut xz = Command::new("xz")
            .args(["-dc"])
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .map_err(|e| {
                ToolError::Command(format!("xz: {e} (need host xz-utils for pbzx Payload)")
                )
            })?;
        let mut xz_in = xz.stdin.take().ok_or_else(|| {
            ToolError::Command("xz produced no stdin".into())
        })?;
        let mut xz_out = xz.stdout.take().ok_or_else(|| {
            ToolError::Command("xz produced no stdout".into())
        })?;

        // Stream this xz frame into xz(1).
        let mut remaining = xz_len;
        let mut buf = vec![0_u8; 64 * 1024];
        while remaining > 0 {
            let n = remaining.min(buf.len());
            input
                .read_exact(
                    buf.get_mut(..n).ok_or_else(|| {
                        ToolError::Command("pbzx buffer slice failed".into())
                    })?,
                )
                .map_err(ToolError::Io)?;
            xz_in
                .write_all(buf.get(..n).unwrap_or(&[]))
                .map_err(ToolError::Io)?;
            remaining = remaining.saturating_sub(n);
        }
        drop(xz_in);

        io::copy(&mut xz_out, &mut cpio_in).map_err(ToolError::Io)?;
        let xz_status = xz
            .wait()
            .map_err(|e| ToolError::Command(format!("xz wait: {e}")))?;
        if !xz_status.success() {
            return Err(ToolError::Command(format!(
                "xz decompress of pbzx chunk {chunks} failed (status {xz_status})"
            )));
        }
        chunks = chunks.saturating_add(1);
    }
    drop(cpio_in);
    let status = cpio
        .wait()
        .map_err(|e| ToolError::Command(format!("cpio wait: {e}")))?;
    // cpio often exits 1 on trailer warnings; accept if we got a tree.
    if !status.success() && find_clt_root(dest).is_none() {
        return Err(ToolError::Command(format!(
            "cpio extract of pbzx Payload failed (status {status}, chunks={chunks})"
        )));
    }
    if chunks == 0 {
        return Err(ToolError::Command(
            "pbzx Payload contained no xz chunks".into(),
        ));
    }
    Ok(())
}

fn extract_payload_gzip_cpio(payload: &Path, dest: &Path) -> Result<(), ToolError> {
    let gzip = Command::new("gzip")
        .args(["-dc"])
        .arg(payload)
        .stdout(Stdio::piped())
        .spawn()
        .map_err(|e| ToolError::Command(format!("gzip: {e}")))?;
    let Some(stdout) = gzip.stdout else {
        return Err(ToolError::Command(
            "gzip produced no stdout for Payload extract".into(),
        ));
    };
    let status = Command::new("cpio")
        .args(["-idm"])
        .current_dir(dest)
        .stdin(stdout)
        .status()
        .map_err(|e| ToolError::Command(format!("cpio: {e}")))?;
    if !status.success() && find_clt_root(dest).is_none() {
        return Err(ToolError::Command(format!(
            "cpio extract of gzip Payload failed (status {status})"
        )));
    }
    Ok(())
}

fn extract_payload_raw_cpio(payload: &Path, dest: &Path) -> Result<(), ToolError> {
    let file = File::open(payload).map_err(ToolError::Io)?;
    let status = Command::new("cpio")
        .args(["-idm"])
        .current_dir(dest)
        .stdin(file)
        .status()
        .map_err(|e| ToolError::Command(format!("cpio: {e}")))?;
    if !status.success() && find_clt_root(dest).is_none() {
        return Err(ToolError::Command(format!(
            "raw cpio extract of Payload failed (status {status})"
        )));
    }
    Ok(())
}

fn find_clt_root(root: &Path) -> Option<PathBuf> {
    if root.join("usr/bin/git").is_file() {
        return Some(root.to_path_buf());
    }
    let mut stack = vec![root.to_path_buf()];
    let mut visited = 0_usize;
    while let Some(dir) = stack.pop() {
        visited = visited.saturating_add(1);
        if visited > 50_000 {
            break;
        }
        if dir.join("usr/bin/git").is_file() {
            return Some(dir);
        }
        let Ok(entries) = fs::read_dir(&dir) else {
            continue;
        };
        for ent in entries.flatten() {
            let p = ent.path();
            if p.is_dir() {
                let name = p.file_name().and_then(|n| n.to_str()).unwrap_or("");
                if name == "CommandLineTools" && p.join("usr/bin/git").is_file() {
                    return Some(p);
                }
                stack.push(p);
            }
        }
    }
    None
}

fn find_first_with_suffix(root: &Path, suffixes: &[&str]) -> Option<PathBuf> {
    let mut stack = vec![root.to_path_buf()];
    let mut visited = 0_usize;
    while let Some(dir) = stack.pop() {
        visited = visited.saturating_add(1);
        if visited > 20_000 {
            break;
        }
        let Ok(entries) = fs::read_dir(&dir) else {
            continue;
        };
        for ent in entries.flatten() {
            let p = ent.path();
            if p.is_dir() {
                stack.push(p);
                continue;
            }
            let name = p.file_name().and_then(|n| n.to_str()).unwrap_or("");
            let lower = name.to_ascii_lowercase();
            if suffixes.iter().any(|s| lower.ends_with(s)) {
                return Some(p);
            }
        }
    }
    None
}

fn find_named_file(root: &Path, name: &str) -> Option<PathBuf> {
    let mut stack = vec![root.to_path_buf()];
    let mut visited = 0_usize;
    while let Some(dir) = stack.pop() {
        visited = visited.saturating_add(1);
        if visited > 20_000 {
            break;
        }
        let Ok(entries) = fs::read_dir(&dir) else {
            continue;
        };
        for ent in entries.flatten() {
            let p = ent.path();
            if p.is_dir() {
                stack.push(p);
            } else if p.file_name().is_some_and(|n| n == name) {
                return Some(p);
            }
        }
    }
    None
}

fn install_clt_into_bottle(bottle: &Path, clt_root: &Path) -> Result<(), ToolError> {
    let dest = bottle.join(GUEST_CLT_REL);
    if dest.exists() {
        drop(fs::remove_dir_all(&dest));
    }
    if let Some(parent) = dest.parent() {
        fs::create_dir_all(parent)?;
    }
    copy_dir_recursive(clt_root, &dest)?;

    let usr_bin = bottle.join("usr/bin");
    fs::create_dir_all(&usr_bin)?;
    let link = bottle.join(GUEST_USR_BIN_GIT_REL);
    if link.exists() || link.symlink_metadata().is_ok() {
        drop(fs::remove_file(&link));
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::symlink;
        symlink(
            "../../Library/Developer/CommandLineTools/usr/bin/git",
            &link,
        )?;
    }
    #[cfg(not(unix))]
    {
        fs::copy(dest.join("usr/bin/git"), &link)?;
    }
    Ok(())
}

fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<(), ToolError> {
    fs::create_dir_all(dst)?;
    let mut stack = vec![(src.to_path_buf(), dst.to_path_buf())];
    while let Some((from, to)) = stack.pop() {
        let entries = fs::read_dir(&from)?;
        for ent in entries {
            let ent = ent?;
            let from_path = ent.path();
            let to_path = to.join(ent.file_name());
            let ft = ent.file_type()?;
            if ft.is_dir() {
                fs::create_dir_all(&to_path)?;
                stack.push((from_path, to_path));
            } else if ft.is_symlink() {
                #[cfg(unix)]
                {
                    use std::os::unix::fs::symlink;
                    let target = fs::read_link(&from_path)?;
                    if to_path.exists() || to_path.symlink_metadata().is_ok() {
                        drop(fs::remove_file(&to_path));
                    }
                    symlink(target, &to_path)?;
                }
                #[cfg(not(unix))]
                {
                    fs::copy(&from_path, &to_path)?;
                }
            } else {
                if let Some(parent) = to_path.parent() {
                    fs::create_dir_all(parent)?;
                }
                fs::copy(&from_path, &to_path)?;
            }
        }
    }
    Ok(())
}

fn set_executable(path: &Path) -> io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(path)?.permissions();
        perms.set_mode(0o755);
        fs::set_permissions(path, perms)?;
    }
    let _ = path;
    Ok(())
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicU64, Ordering};

    fn unique(prefix: &str) -> PathBuf {
        static N: AtomicU64 = AtomicU64::new(0);
        let n = N.fetch_add(1, Ordering::Relaxed);
        std::env::temp_dir().join(format!(
            "kh-clt-{}-{}-{n}",
            prefix,
            std::process::id()
        ))
    }

    #[test]
    fn find_clt_root_nested() {
        let root = unique("tree");
        let clt = root.join("Library/Developer/CommandLineTools");
        fs::create_dir_all(clt.join("usr/bin")).expect("dirs");
        fs::write(clt.join("usr/bin/git"), b"fake").expect("write");
        assert_eq!(find_clt_root(&root), Some(clt));
        drop(fs::remove_dir_all(&root));
    }
}