Files
krata/crates/oci/src/cache.rs

75 lines
2.9 KiB
Rust
Raw Normal View History

use crate::packer::OciPackerFormat;
2024-03-08 14:44:45 +00:00
use super::compiler::ImageInfo;
use anyhow::Result;
2024-01-18 00:02:21 -08:00
use log::debug;
2024-01-18 00:15:36 -08:00
use oci_spec::image::{ImageConfiguration, ImageManifest};
2024-01-18 00:02:21 -08:00
use std::path::{Path, PathBuf};
2024-03-07 05:26:35 +00:00
use tokio::fs;
2024-01-18 00:02:21 -08:00
2024-04-02 00:56:18 +00:00
#[derive(Clone)]
2024-01-18 00:02:21 -08:00
pub struct ImageCache {
cache_dir: PathBuf,
}
impl ImageCache {
pub fn new(cache_dir: &Path) -> Result<ImageCache> {
Ok(ImageCache {
cache_dir: cache_dir.to_path_buf(),
})
}
pub async fn recall(&self, digest: &str, format: OciPackerFormat) -> Result<Option<ImageInfo>> {
let mut fs_path = self.cache_dir.clone();
2024-01-18 00:15:36 -08:00
let mut config_path = self.cache_dir.clone();
2024-01-18 00:02:21 -08:00
let mut manifest_path = self.cache_dir.clone();
fs_path.push(format!("{}.{}", digest, format.extension()));
2024-01-18 00:15:36 -08:00
manifest_path.push(format!("{}.manifest.json", digest));
config_path.push(format!("{}.config.json", digest));
Ok(
if fs_path.exists() && manifest_path.exists() && config_path.exists() {
let image_metadata = fs::metadata(&fs_path).await?;
2024-03-07 05:26:35 +00:00
let manifest_metadata = fs::metadata(&manifest_path).await?;
let config_metadata = fs::metadata(&config_path).await?;
if image_metadata.is_file()
2024-01-18 00:15:36 -08:00
&& manifest_metadata.is_file()
&& config_metadata.is_file()
{
2024-03-07 05:26:35 +00:00
let manifest_text = fs::read_to_string(&manifest_path).await?;
2024-01-18 00:15:36 -08:00
let manifest: ImageManifest = serde_json::from_str(&manifest_text)?;
2024-03-07 05:26:35 +00:00
let config_text = fs::read_to_string(&config_path).await?;
2024-01-18 00:15:36 -08:00
let config: ImageConfiguration = serde_json::from_str(&config_text)?;
debug!("cache hit digest={}", digest);
Some(ImageInfo::new(fs_path.clone(), manifest, config)?)
2024-01-18 00:15:36 -08:00
} else {
None
}
2024-01-18 00:02:21 -08:00
} else {
2024-01-18 00:15:36 -08:00
debug!("cache miss digest={}", digest);
2024-01-18 00:02:21 -08:00
None
2024-01-18 00:15:36 -08:00
},
)
2024-01-18 00:02:21 -08:00
}
pub async fn store(
&self,
digest: &str,
info: &ImageInfo,
format: OciPackerFormat,
) -> Result<ImageInfo> {
2024-01-18 00:02:21 -08:00
debug!("cache store digest={}", digest);
let mut fs_path = self.cache_dir.clone();
2024-01-18 00:02:21 -08:00
let mut manifest_path = self.cache_dir.clone();
2024-01-18 00:15:36 -08:00
let mut config_path = self.cache_dir.clone();
fs_path.push(format!("{}.{}", digest, format.extension()));
2024-01-18 00:15:36 -08:00
manifest_path.push(format!("{}.manifest.json", digest));
config_path.push(format!("{}.config.json", digest));
fs::copy(&info.image, &fs_path).await?;
2024-01-18 00:02:21 -08:00
let manifest_text = serde_json::to_string_pretty(&info.manifest)?;
2024-03-07 05:26:35 +00:00
fs::write(&manifest_path, manifest_text).await?;
2024-01-18 00:15:36 -08:00
let config_text = serde_json::to_string_pretty(&info.config)?;
2024-03-07 05:26:35 +00:00
fs::write(&config_path, config_text).await?;
ImageInfo::new(fs_path.clone(), info.manifest.clone(), info.config.clone())
2024-01-18 00:02:21 -08:00
}
}