kh-runtime 0.1.14

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
//! Optional guest tools installed **into the bottle** at real macOS paths.
//!
//! Example: Darwin `7zz` → `{bottle}/usr/local/bin/7zz`, which is guest path
//! `/usr/local/bin/7zz` — the same place a real Mac would keep a CLI install.
//! Kakehashi is not a 7-Zip product; packages are opt-in via `kh install`.
//!
//! Downloads go through [`super::download_cache`] so Docker/Colima re-runs
//! (bind-mounted `KAKEHASHI_DATA_DIR`) do not re-fetch archives. Install is
//! **idempotent** when the guest binary is already present in the bottle.

use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::process::Command;

use super::download_cache;
use super::layout::is_bottle_root;
use super::manage;
use super::xcode_tools;

/// Env override: host path of a Darwin `7zz` binary (skip download).
pub const ENV_7ZZ: &str = "KAKEHASHI_7ZZ";

/// Env override: host path of a Darwin `curl` binary (skip download).
pub const ENV_CURL: &str = "KAKEHASHI_CURL";

/// Legacy ad-hoc drop location (still checked by discovery helpers).
pub const DEFAULT_7ZZ_PATH: &str = "/tmp/7zz";

/// Official macOS console 7-Zip (universal arm64 / x86_64 Mach-O).
pub const DARWIN_7ZZ_URL: &str =
    "https://github.com/ip7z/7zip/releases/download/26.02/7z2602-mac.tar.xz";

/// Public Darwin arm64 static-ish curl (stunnel/static-curl release).
///
/// Archive root contains a single `curl` Mach-O. Prefer this over Apple
/// `/usr/bin/curl` (which needs `libcurl.4.dylib`). Still may link Apple
/// frameworks for TLS — probe will surface that; see `docs/curl.md`.
pub const DARWIN_CURL_URL: &str = "https://github.com/stunnel/static-curl/releases/download/8.21.0/curl-macos-arm64-8.21.0.tar.xz";

/// Cache object names under `data/cache/downloads/`.
const CACHE_7ZZ_ARCHIVE: &str = "7z2602-mac.tar.xz";
const CACHE_CURL_ARCHIVE: &str = "curl-macos-arm64-8.21.0.tar.xz";

/// Guest-relative install path for Darwin 7zz (under bottle root).
///
/// On a real Mac this is `/usr/local/bin/7zz`.
pub const GUEST_7ZZ_REL: &str = "usr/local/bin/7zz";

/// Guest-relative install path for Darwin curl (under bottle root).
///
/// On a real Mac this is `/usr/local/bin/curl`.
pub const GUEST_CURL_REL: &str = "usr/local/bin/curl";

/// Known installable package ids (`kh install <name>`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InstallPackage {
    /// Darwin 7-Zip console (`7zz`).
    SevenZip,
    /// Darwin `curl` (downloaded arm64 Mach-O; see `docs/curl.md`).
    Curl,
    /// Apple Command Line Tools (includes Apple `git`; see `docs/git.md`).
    XcodeTools,
}

impl InstallPackage {
    /// Parse CLI package name (case-insensitive).
    #[must_use]
    pub fn parse(name: &str) -> Option<Self> {
        match name.to_ascii_lowercase().as_str() {
            "7zip" | "7zz" | "sevenzip" | "p7zip" => Some(Self::SevenZip),
            "curl" => Some(Self::Curl),
            "xcode-tools" | "xcodetools" | "clt" | "command-line-tools"
            | "commandlinetools" | "git" => Some(Self::XcodeTools),
            _ => None,
        }
    }

    /// Canonical name for messages.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::SevenZip => "7zip",
            Self::Curl => "curl",
            Self::XcodeTools => "xcode-tools",
        }
    }

    /// Guest absolute path after install (with leading `/`).
    #[must_use]
    pub const fn guest_path(self) -> &'static str {
        match self {
            Self::SevenZip => "/usr/local/bin/7zz",
            Self::Curl => "/usr/local/bin/curl",
            Self::XcodeTools => xcode_tools::GUEST_GIT_PATH,
        }
    }
}

/// Errors from package install / download.
#[derive(Debug, thiserror::Error)]
pub enum ToolError {
    /// Bottle lifecycle / registry.
    #[error("{0}")]
    Bottle(String),
    /// I/O while creating dirs or writing the binary.
    #[error("tool I/O: {0}")]
    Io(#[from] io::Error),
    /// `curl` / `tar` missing or failed.
    #[error("{0}")]
    Command(String),
    /// Downloaded archive did not contain the expected binary.
    #[error("expected binary not found in downloaded archive")]
    MissingBinary,
    /// Unknown package name.
    #[error("unknown package `{0}` (known: 7zip, curl, xcode-tools)")]
    UnknownPackage(String),
}

/// Host path of an installed package inside a bottle, if present.
#[must_use]
pub fn package_host_path(bottle: &Path, package: InstallPackage) -> PathBuf {
    match package {
        InstallPackage::SevenZip => bottle.join(GUEST_7ZZ_REL),
        InstallPackage::Curl => bottle.join(GUEST_CURL_REL),
        InstallPackage::XcodeTools => bottle.join(xcode_tools::GUEST_GIT_REL),
    }
}

/// Discover Darwin `7zz` for convenience helpers (not required for `kh run`).
///
/// Order: explicit → `KAKEHASHI_7ZZ` → active bottle `/usr/local/bin/7zz` →
/// legacy `/tmp/7zz` → dev-tree fallbacks.
#[must_use]
pub fn discover_7zz(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(raw) = std::env::var(ENV_7ZZ) {
        let p = PathBuf::from(raw);
        if p.is_file() {
            return Some(p);
        }
    }

    if let Ok(Some(root)) = manage::active_root() {
        let p = package_host_path(&root, InstallPackage::SevenZip);
        if p.is_file() {
            return Some(p);
        }
    }

    let default = Path::new(DEFAULT_7ZZ_PATH);
    if default.is_file() {
        return Some(default.to_path_buf());
    }

    for rel in ["tests/clang-probe/7zz.bin", "tests/clang-probe/7zz"] {
        let p = PathBuf::from(rel);
        if p.is_file() {
            return Some(p);
        }
    }

    None
}

/// Install a named package into the active bottle (creating the bottle if needed).
pub fn install_package(name: &str) -> Result<InstallReport, ToolError> {
    let package =
        InstallPackage::parse(name).ok_or_else(|| ToolError::UnknownPackage(name.to_owned()))?;
    match package {
        InstallPackage::SevenZip => install_sevenzip(),
        InstallPackage::Curl => install_curl(),
        InstallPackage::XcodeTools => xcode_tools::install_xcode_tools(),
    }
}

/// Result of a successful [`install_package`].
#[derive(Debug, Clone)]
pub struct InstallReport {
    /// Package id.
    pub package: &'static str,
    /// Absolute host path of the installed binary.
    pub host_path: PathBuf,
    /// Guest absolute path (macOS layout).
    pub guest_path: &'static str,
    /// Bottle root used.
    pub bottle: PathBuf,
}

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 install_sevenzip() -> Result<InstallReport, ToolError> {
    let bottle = ensure_active_bottle()?;
    let dest = package_host_path(&bottle, InstallPackage::SevenZip);
    if let Some(parent) = dest.parent() {
        fs::create_dir_all(parent)?;
    }

    // Prefer env override (already a binary) over download.
    if let Ok(raw) = std::env::var(ENV_7ZZ) {
        let src = PathBuf::from(raw);
        if src.is_file() {
            fs::copy(&src, &dest)?;
            set_executable(&dest)?;
            return Ok(InstallReport {
                package: InstallPackage::SevenZip.as_str(),
                host_path: dest,
                guest_path: InstallPackage::SevenZip.guest_path(),
                bottle,
            });
        }
    }

    // Idempotent: bottle already has the binary (Docker re-run).
    if !download_cache::force_download() && dest.is_file() {
        return Ok(InstallReport {
            package: InstallPackage::SevenZip.as_str(),
            host_path: dest,
            guest_path: InstallPackage::SevenZip.guest_path(),
            bottle,
        });
    }

    let archive =
        download_cache::ensure_named_url(DARWIN_7ZZ_URL, CACHE_7ZZ_ARCHIVE).map_err(|e| {
            ToolError::Command(e.to_string())
        })?;

    let tmp_root = download_cache::cache_dir()
        .map_err(ToolError::Io)?
        .join("tmp-extract-7zz");
    drop(fs::remove_dir_all(&tmp_root));
    fs::create_dir_all(&tmp_root)?;

    let status = Command::new("tar")
        .args(["-xJf"])
        .arg(&archive)
        .current_dir(&tmp_root)
        .status()
        .map_err(|e| ToolError::Command(format!("tar: {e}")))?;
    if !status.success() {
        drop(fs::remove_dir_all(&tmp_root));
        return Err(ToolError::Command(format!(
            "tar extract failed (status {status})"
        )));
    }

    let found = find_named_file(&tmp_root, "7zz").ok_or(ToolError::MissingBinary)?;
    if dest.exists() {
        drop(fs::remove_file(&dest));
    }
    fs::copy(&found, &dest)?;
    set_executable(&dest)?;
    drop(fs::remove_dir_all(&tmp_root));

    Ok(InstallReport {
        package: InstallPackage::SevenZip.as_str(),
        host_path: dest,
        guest_path: InstallPackage::SevenZip.guest_path(),
        bottle,
    })
}

/// Install Darwin `curl` into the bottle at `/usr/local/bin/curl`.
///
/// Order: `KAKEHASHI_CURL` (already a binary) → download [`DARWIN_CURL_URL`]
/// and extract the `curl` Mach-O from the tarball.
fn install_curl() -> Result<InstallReport, ToolError> {
    let bottle = ensure_active_bottle()?;
    let dest = package_host_path(&bottle, InstallPackage::Curl);
    if let Some(parent) = dest.parent() {
        fs::create_dir_all(parent)?;
    }

    // Prefer env override (already a binary) over download.
    if let Ok(raw) = std::env::var(ENV_CURL) {
        let src = PathBuf::from(raw);
        if src.is_file() {
            if dest.exists() {
                drop(fs::remove_file(&dest));
            }
            fs::copy(&src, &dest)?;
            set_executable(&dest)?;
            return Ok(InstallReport {
                package: InstallPackage::Curl.as_str(),
                host_path: dest,
                guest_path: InstallPackage::Curl.guest_path(),
                bottle,
            });
        }
    }

    // Idempotent: bottle already has the binary (Docker re-run).
    if !download_cache::force_download() && dest.is_file() {
        return Ok(InstallReport {
            package: InstallPackage::Curl.as_str(),
            host_path: dest,
            guest_path: InstallPackage::Curl.guest_path(),
            bottle,
        });
    }

    let archive =
        download_cache::ensure_named_url(DARWIN_CURL_URL, CACHE_CURL_ARCHIVE).map_err(|e| {
            ToolError::Command(e.to_string())
        })?;

    let tmp_root = download_cache::cache_dir()
        .map_err(ToolError::Io)?
        .join("tmp-extract-curl");
    drop(fs::remove_dir_all(&tmp_root));
    fs::create_dir_all(&tmp_root)?;

    let status = Command::new("tar")
        .args(["-xJf"])
        .arg(&archive)
        .current_dir(&tmp_root)
        .status()
        .map_err(|e| ToolError::Command(format!("tar: {e}")))?;
    if !status.success() {
        drop(fs::remove_dir_all(&tmp_root));
        return Err(ToolError::Command(format!(
            "tar extract failed (status {status})"
        )));
    }

    let found = find_named_file(&tmp_root, "curl").ok_or(ToolError::MissingBinary)?;
    // Avoid copying the archive itself if it were ever named `curl`.
    if found == archive {
        drop(fs::remove_dir_all(&tmp_root));
        return Err(ToolError::MissingBinary);
    }
    if dest.exists() {
        drop(fs::remove_file(&dest));
    }
    fs::copy(&found, &dest)?;
    set_executable(&dest)?;
    drop(fs::remove_dir_all(&tmp_root));

    Ok(InstallReport {
        package: InstallPackage::Curl.as_str(),
        host_path: dest,
        guest_path: InstallPackage::Curl.guest_path(),
        bottle,
    })
}

/// Discover Darwin `curl` for convenience helpers.
///
/// Order: explicit → `KAKEHASHI_CURL` → active bottle `/usr/local/bin/curl`.
#[must_use]
pub fn discover_curl(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(raw) = std::env::var(ENV_CURL) {
        let p = PathBuf::from(raw);
        if p.is_file() {
            return Some(p);
        }
    }

    if let Ok(Some(root)) = manage::active_root() {
        let p = package_host_path(&root, InstallPackage::Curl);
        if p.is_file() {
            return Some(p);
        }
    }

    None
}

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(())
}

fn find_named_file(root: &Path, name: &str) -> Option<PathBuf> {
    let mut stack = vec![root.to_path_buf()];
    while let Some(dir) = stack.pop() {
        let entries = fs::read_dir(&dir).ok()?;
        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
}

/// Default guest `PATH` components (macOS-like) used when resolving bare names.
pub const GUEST_PATH_DIRS: &[&str] = &[
    "/usr/local/bin",
    "/Library/Developer/CommandLineTools/usr/bin",
    "/usr/bin",
    "/bin",
    "/usr/sbin",
    "/sbin",
];

/// Resolve a program path for `kh run`.
///
/// * Absolute or relative with `/` → unchanged (host path or guest path string).
/// * Bare name → search bottle under [`GUEST_PATH_DIRS`] (host files that exist).
#[must_use]
pub fn resolve_guest_program(spec: &Path, bottle: Option<&Path>) -> PathBuf {
    let s = spec.as_os_str();
    // Has path separator → user gave an explicit path.
    if spec.components().count() > 1 || s.to_string_lossy().contains('/') {
        return spec.to_path_buf();
    }
    let name = spec.file_name().unwrap_or(s);
    let Some(root) = bottle else {
        return spec.to_path_buf();
    };
    for dir in GUEST_PATH_DIRS {
        // dir is "/usr/local/bin" → relative "usr/local/bin"
        let rel = dir.trim_start_matches('/');
        let candidate = root.join(rel).join(name);
        if candidate.is_file() {
            // Return guest absolute path so loader + path translation stay consistent.
            return PathBuf::from(dir).join(name);
        }
    }
    spec.to_path_buf()
}

/// Map guest absolute path to host under bottle (for opening the Mach-O file).
#[must_use]
pub fn guest_path_to_host(bottle: &Path, guest: &Path) -> PathBuf {
    let g = guest.to_string_lossy();
    if g.starts_with('/') {
        bottle.join(g.trim_start_matches('/'))
    } else {
        guest.to_path_buf()
    }
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::panic, unsafe_code)]
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-7zz-{}-{}-{n}", prefix, std::process::id()))
    }

    #[test]
    fn parse_package_names() {
        assert_eq!(
            InstallPackage::parse("7zip"),
            Some(InstallPackage::SevenZip)
        );
        assert_eq!(InstallPackage::parse("7ZZ"), Some(InstallPackage::SevenZip));
        assert_eq!(InstallPackage::parse("curl"), Some(InstallPackage::Curl));
        assert_eq!(InstallPackage::parse("CURL"), Some(InstallPackage::Curl));
        assert_eq!(
            InstallPackage::parse("xcode-tools"),
            Some(InstallPackage::XcodeTools)
        );
        assert_eq!(InstallPackage::parse("clt"), Some(InstallPackage::XcodeTools));
        assert_eq!(InstallPackage::parse("git"), Some(InstallPackage::XcodeTools));
        assert!(InstallPackage::parse("foo").is_none());
    }

    #[test]
    fn resolve_bare_name_in_bottle() {
        let root = unique("path");
        let bin = root.join("usr/local/bin");
        fs::create_dir_all(&bin).expect("dirs");
        let tool = bin.join("7zz");
        fs::write(&tool, b"x").expect("write");
        let resolved = resolve_guest_program(Path::new("7zz"), Some(&root));
        assert_eq!(resolved, PathBuf::from("/usr/local/bin/7zz"));
        drop(fs::remove_dir_all(&root));
    }

    #[test]
    fn discover_explicit_path() {
        let path = unique("bin");
        std::fs::write(&path, b"not-a-real-macho").expect("write");
        let found = discover_7zz(Some(&path)).expect("explicit");
        assert_eq!(found, path);
        drop(std::fs::remove_file(&path));
        assert!(discover_7zz(Some(&path)).is_none());
    }

    #[test]
    fn find_nested_name() {
        let root = unique("tree");
        fs::create_dir_all(root.join("a/b")).expect("dirs");
        let target = root.join("a/b/7zz");
        fs::write(&target, b"x").expect("write");
        assert_eq!(find_named_file(&root, "7zz"), Some(target));
        drop(fs::remove_dir_all(&root));
    }
}