use std::collections::BTreeMap;
use std::process::Command;
use std::process::Stdio;
use hermit::Context;
use hermit::Error;
use serde::Deserialize;
const PODMAN: &str = "podman";
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ResolvedImage {
pub reference: String,
pub id: String,
pub digest: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct ImageSummary {
#[serde(rename = "Id")]
pub id: String,
#[serde(rename = "Names", default)]
pub names: Option<Vec<String>>,
#[serde(rename = "Size", default)]
pub size: u64,
}
impl ImageSummary {
pub fn display_name(&self) -> String {
self.names
.as_ref()
.and_then(|names| names.first().cloned())
.unwrap_or_else(|| "<none>".to_string())
}
}
pub(crate) struct PodmanStore {
graph_root: String,
}
fn podman_output(args: &[&str]) -> Result<String, Error> {
let output = Command::new(PODMAN)
.args(args)
.stdin(Stdio::null())
.output()
.with_context(|| {
format!(
"Failed to run `{PODMAN} {}`. `hermit oci` needs a rootless \
`{PODMAN}` on PATH.",
args.join(" ")
)
})?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(Error::msg(format!(
"`{PODMAN} {}` failed ({}): {}",
args.join(" "),
output.status,
stderr.trim()
)));
}
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}
impl PodmanStore {
pub fn probe() -> Result<Option<Self>, Error> {
match podman_output(&["info", "--format", "{{.Store.GraphRoot}}"]) {
Ok(graph_root) if !graph_root.is_empty() => Ok(Some(Self { graph_root })),
_ => Ok(None),
}
}
pub fn graph_root(&self) -> &str {
&self.graph_root
}
pub fn list(&self, all: bool) -> Result<Vec<ImageSummary>, Error> {
let mut args = vec!["images", "--format", "json"];
if all {
args.push("--all");
}
let stdout = podman_output(&args)?;
if stdout.is_empty() {
return Ok(Vec::new());
}
serde_json::from_str(&stdout)
.context("Failed to parse `podman images --format json` output")
}
pub fn resolve(&self, reference: &str) -> Result<ResolvedImage, Error> {
let id = podman_output(&["pull", "--policy=never", reference]).map_err(|e| {
Error::msg(format!(
"No local image matches {reference:?} in the store at {}. \
Use `hermit oci ls` to see what is available, or \
`hermit oci pull {reference}` to download it.\n ({e})",
self.graph_root
))
})?;
self.identify(reference, &id)
}
pub fn pull(&self, reference: &str) -> Result<ResolvedImage, Error> {
let id = podman_output(&["pull", reference]).map_err(|e| {
Error::msg(format!(
"Failed to pull {reference:?}. If the registry is reachable only \
through a proxy, run hermit under that proxy.\n ({e})"
))
})?;
self.identify(reference, &id)
}
fn identify(&self, reference: &str, id: &str) -> Result<ResolvedImage, Error> {
let id = normalize_image_id(id).ok_or_else(|| {
Error::msg(format!(
"podman returned an unrecognized image id for {reference:?}: {id:?}"
))
})?;
let digest = podman_output(&["image", "inspect", &id, "--format", "{{.Digest}}"])
.ok()
.filter(|d| d.starts_with("sha256:"));
Ok(ResolvedImage {
reference: reference.to_string(),
id,
digest,
})
}
pub fn config(&self, image: &ResolvedImage) -> Result<ImageRunConfig, Error> {
let stdout = podman_output(&[
"image",
"inspect",
&image.id,
"--format",
"{{json .Config}}",
])?;
let raw: RawConfig = serde_json::from_str(&stdout)
.context("Failed to parse `podman image inspect` config output")?;
Ok(ImageRunConfig {
env: raw
.env
.unwrap_or_default()
.iter()
.filter_map(|entry| entry.split_once('='))
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect(),
workdir: raw.working_dir.filter(|w| !w.is_empty() && w != "/"),
cmd: raw.cmd.unwrap_or_default(),
entrypoint: raw.entrypoint.unwrap_or_default(),
})
}
pub fn overlay_dirs(&self, image: &ResolvedImage) -> Result<Vec<String>, Error> {
let stdout = podman_output(&[
"image",
"inspect",
&image.id,
"--format",
"{{json .GraphDriver}}",
])?;
let graph: GraphDriver = serde_json::from_str(&stdout)
.context("Failed to parse `podman image inspect` GraphDriver output")?;
if graph.name != "overlay" {
return Err(Error::msg(format!(
"image {} uses the {:?} storage driver; only `overlay` exposes \
mountable layer directories",
image.id, graph.name
)));
}
Ok(overlay_layer_order(
graph.data.get("UpperDir").map(String::as_str),
graph.data.get("LowerDir").map(String::as_str),
))
}
}
#[derive(Debug, Default, PartialEq, Eq)]
pub(crate) struct ImageRunConfig {
pub env: Vec<(String, String)>,
pub workdir: Option<String>,
pub cmd: Vec<String>,
pub entrypoint: Vec<String>,
}
#[derive(Deserialize)]
struct RawConfig {
#[serde(rename = "Env")]
env: Option<Vec<String>>,
#[serde(rename = "WorkingDir")]
working_dir: Option<String>,
#[serde(rename = "Cmd")]
cmd: Option<Vec<String>>,
#[serde(rename = "Entrypoint")]
entrypoint: Option<Vec<String>>,
}
#[derive(Deserialize)]
struct GraphDriver {
#[serde(rename = "Name")]
name: String,
#[serde(rename = "Data", default)]
data: BTreeMap<String, String>,
}
fn overlay_layer_order(upper: Option<&str>, lower: Option<&str>) -> Vec<String> {
let mut dirs = Vec::new();
if let Some(upper) = upper.map(str::trim).filter(|u| !u.is_empty()) {
dirs.push(upper.to_string());
}
if let Some(lower) = lower {
dirs.extend(
lower
.split(':')
.map(str::trim)
.filter(|d| !d.is_empty())
.map(str::to_string),
);
}
dirs
}
fn normalize_image_id(raw: &str) -> Option<String> {
let candidate = raw.trim();
let candidate = candidate.strip_prefix("sha256:").unwrap_or(candidate);
(candidate.len() == 64 && candidate.chars().all(|c| c.is_ascii_hexdigit()))
.then(|| candidate.to_ascii_lowercase())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn normalize_image_id_accepts_bare_and_prefixed_hex() {
let id = "1ee23df5e47b5b7331304af929f5461f9f516f5c166c3031832d2adfd22f323c";
assert_eq!(normalize_image_id(id).as_deref(), Some(id));
assert_eq!(
normalize_image_id(&format!("sha256:{id}")).as_deref(),
Some(id)
);
assert_eq!(
normalize_image_id(&format!(" {id}\n")).as_deref(),
Some(id)
);
assert_eq!(
normalize_image_id(&id.to_ascii_uppercase()).as_deref(),
Some(id),
"podman ids are case-insensitive hex; normalize to lowercase"
);
}
#[test]
fn normalize_image_id_rejects_short_ids_and_non_hex() {
assert_eq!(normalize_image_id("1ee23df5e47b"), None);
assert_eq!(normalize_image_id(""), None);
assert_eq!(normalize_image_id("not-an-id"), None);
assert_eq!(normalize_image_id(&"z".repeat(64)), None);
}
#[test]
fn overlay_layer_order_handles_single_layer_image() {
assert_eq!(
overlay_layer_order(Some("/store/overlay/aaa/diff"), None),
vec!["/store/overlay/aaa/diff".to_string()]
);
}
#[test]
fn overlay_layer_order_puts_upper_before_the_lower_chain() {
assert_eq!(
overlay_layer_order(
Some("/store/overlay/top/diff"),
Some("/store/overlay/mid/diff:/store/overlay/base/diff"),
),
vec![
"/store/overlay/top/diff".to_string(),
"/store/overlay/mid/diff".to_string(),
"/store/overlay/base/diff".to_string(),
]
);
}
#[test]
fn overlay_layer_order_skips_empty_components() {
assert_eq!(
overlay_layer_order(Some(" "), Some("/a/diff::/b/diff:")),
vec!["/a/diff".to_string(), "/b/diff".to_string()]
);
assert!(overlay_layer_order(None, None).is_empty());
}
#[test]
fn image_summary_display_name_falls_back_for_untagged_images() {
let untagged = ImageSummary {
id: "a".repeat(64),
names: None,
size: 0,
};
assert_eq!(untagged.display_name(), "<none>");
let named = ImageSummary {
id: "b".repeat(64),
names: Some(vec!["localhost/example:1".to_string()]),
size: 1,
};
assert_eq!(named.display_name(), "localhost/example:1");
}
}