Skip to main content

hexz_cli/cmd/data/
checkout.rs

1//! Checkout an archive into a writable workspace.
2
3use super::mount;
4use super::workspace::Workspace;
5use anyhow::Result;
6use colored::Colorize;
7use std::path::Path;
8
9/// Initializes a workspace and mounts the base archive.
10#[allow(unsafe_code)]
11pub fn run(archive: &Path, path: &Path) -> Result<()> {
12    if path.exists() && std::fs::read_dir(path)?.next().is_some() {
13        anyhow::bail!("Directory {} is not empty.", path.display());
14    }
15
16    std::fs::create_dir_all(path)?;
17
18    println!(
19        "{} Initializing workspace at {}",
20        "╭".dimmed(),
21        path.display().to_string().cyan()
22    );
23    let ws = Workspace::init(path, Some(archive.to_path_buf()))?;
24    let overlay = ws.overlay_path();
25
26    println!(
27        "{} Mounting base archive {}",
28        "╰".dimmed(),
29        archive.display().to_string().bright_black()
30    );
31    mount::run(
32        &archive.to_string_lossy(),
33        path,
34        true, // daemon
35        None, // cache_size
36        // SAFETY: getuid() is always safe to call
37        unsafe { libc::getuid() },
38        // SAFETY: getgid() is always safe to call
39        unsafe { libc::getgid() },
40        Some(overlay),
41        false, // editable (we already have an overlay)
42        Some(&ws.metadata_dir()),
43    )?;
44
45    println!("\n  {} Workspace ready.", "✓".green());
46    Ok(())
47}