Skip to content

Commit dc6e625

Browse files
committed
store: Only print "Fetching bound image" if it's not there
Also print how many bound images we have, since it's interesting and relevant information. Signed-off-by: Colin Walters <[email protected]>
1 parent 2b61c24 commit dc6e625

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

lib/src/boundimage.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
//! pre-pulled (and in the future, pinned) before a new image root
66
//! is considered ready.
77
8+
use std::num::NonZeroUsize;
9+
810
use anyhow::{Context, Result};
911
use camino::Utf8Path;
1012
use cap_std_ext::cap_std::fs::Dir;
@@ -145,16 +147,22 @@ fn parse_container_file(file_contents: &tini::Ini) -> Result<BoundImage> {
145147
#[context("Pulling bound images")]
146148
pub(crate) async fn pull_images(sysroot: &Storage, bound_images: Vec<BoundImage>) -> Result<()> {
147149
tracing::debug!("Pulling bound images: {}", bound_images.len());
148-
// Only initialize the image storage if we have images to pull
149-
let imgstore = if !bound_images.is_empty() {
150-
sysroot.get_ensure_imgstore()?
151-
} else {
150+
// Yes, the usage of NonZeroUsize here is...maybe odd looking, but I find
151+
// it an elegant way to divide (empty vector, non empty vector) since
152+
// we want to print the length too below.
153+
let Some(n) = NonZeroUsize::new(bound_images.len()) else {
152154
return Ok(());
153155
};
154-
//TODO: do this in parallel
156+
// Only do work like initializing the image storage if we have images to pull.
157+
let imgstore = sysroot.get_ensure_imgstore()?;
158+
// TODO: do this in parallel
155159
for bound_image in bound_images {
156160
let image = &bound_image.image;
157-
let desc = format!("Updating bound image: {image}");
161+
if imgstore.exists(image).await? {
162+
tracing::debug!("Bound image already present: {image}");
163+
continue;
164+
}
165+
let desc = format!("Fetching bound image: {image}");
158166
crate::utils::async_task_with_spinner(&desc, async move {
159167
imgstore
160168
.pull(&bound_image.image, PullMode::IfNotExists)
@@ -163,6 +171,8 @@ pub(crate) async fn pull_images(sysroot: &Storage, bound_images: Vec<BoundImage>
163171
.await?;
164172
}
165173

174+
println!("Bound images stored: {n}");
175+
166176
Ok(())
167177
}
168178

lib/src/imgstorage.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,21 @@ impl Storage {
245245
Ok(garbage)
246246
}
247247

248+
/// Return true if the image exists in the storage.
249+
pub(crate) async fn exists(&self, image: &str) -> Result<bool> {
250+
// Sadly https://docs.rs/containers-image-proxy/latest/containers_image_proxy/struct.ImageProxy.html#method.open_image_optional
251+
// doesn't work with containers-storage yet
252+
let mut cmd = AsyncCommand::from(self.new_image_cmd()?);
253+
cmd.args(["exists", image]);
254+
Ok(cmd.status().await?.success())
255+
}
256+
248257
/// Fetch the image if it is not already present; return whether
249258
/// or not the image was fetched.
250259
pub(crate) async fn pull(&self, image: &str, mode: PullMode) -> Result<bool> {
251260
match mode {
252261
PullMode::IfNotExists => {
253-
// Sadly https://docs.rs/containers-image-proxy/latest/containers_image_proxy/struct.ImageProxy.html#method.open_image_optional
254-
// doesn't work with containers-storage yet
255-
let mut cmd = AsyncCommand::from(self.new_image_cmd()?);
256-
cmd.args(["exists", image]);
257-
let exists = cmd.status().await?.success();
258-
if exists {
262+
if self.exists(image).await? {
259263
tracing::debug!("Image is already present: {image}");
260264
return Ok(false);
261265
}

0 commit comments

Comments
 (0)