Skip to main content

hexz_cli/cmd/data/
checkout.rs

1//! Checkout an archive into a writable workspace.
2
3use anyhow::Result;
4use std::path::Path;
5use colored::Colorize;
6use super::workspace::Workspace;
7use super::mount;
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!("{} Initializing workspace at {}", "╭".dimmed(), path.display().to_string().cyan());
19    let ws = Workspace::init(path, Some(archive.to_path_buf()))?;
20    let overlay = ws.overlay_path();
21
22    println!("{} Mounting base archive {}", "╰".dimmed(), archive.display().to_string().bright_black());
23    mount::run(
24        &archive.to_string_lossy(),
25        path,
26        true, // daemon
27        None, // cache_size
28        // SAFETY: getuid() is always safe to call
29        unsafe { libc::getuid() },
30        // SAFETY: getgid() is always safe to call
31        unsafe { libc::getgid() },
32        Some(overlay),
33        false, // editable (we already have an overlay)
34        Some(&ws.metadata_dir()),
35    )?;
36
37    println!("\n  {} Workspace ready.", "✓".green());
38    Ok(())
39}