oci_util/image/
config.rs

1use crate::filesystem::FileSystem;
2use crate::image::build::config::instructions::Kind;
3use crate::image::build::config::BuildConfig;
4use anyhow::{Context, Result};
5use serde::{Deserialize, Serialize};
6
7pub struct ConfigFileAndData {
8    pub file: ConfigFile,
9    pub data: Vec<u8>,
10}
11impl ConfigFileAndData {
12    pub fn load(digest: &str) -> Result<Self> {
13        let config_path = FileSystem.config_sha256()?.join(digest);
14        let data = std::fs::read(&config_path)
15            .with_context(|| format!("读取镜像config文件{:?}失败", config_path))?;
16        let file: ConfigFile = serde_json::from_slice(&data)?;
17        Ok(Self { file, data })
18    }
19}
20
21#[derive(Serialize, Deserialize)]
22pub struct ConfigFile {
23    pub kind: Kind,
24    pub cmd: String,
25    pub rootf: RootFs,
26}
27
28impl ConfigFile {
29    pub fn load(digest: &String) -> Result<Self> {
30        let config_path = FileSystem.config_sha256()?.join(digest);
31        let data = std::fs::read(&config_path)
32            .with_context(|| format!("读取镜像config文件{:?}失败", config_path))?;
33        let file: ConfigFile = serde_json::from_slice(&data)?;
34        Ok(file)
35    }
36
37    pub fn new(config: &BuildConfig, diff_ids: Vec<String>) -> Result<Self> {
38        let regix = regex::Regex::new("^/")?;
39        let cmd = regix.replace(config.cmd.orgin.as_str(), "").to_string();
40        Ok(Self {
41            kind: config.kind.clone(),
42            cmd: cmd,
43            rootf: RootFs {
44                typ: "layers".to_string(),
45                diff_ids,
46            },
47        })
48    }
49    pub fn data(&self) -> Result<Vec<u8>> {
50        Ok(serde_json::to_vec(&self)?)
51    }
52}
53
54#[derive(Serialize, Deserialize)]
55pub struct RootFs {
56    #[serde(rename = "type")]
57    pub typ: String,
58    pub diff_ids: Vec<String>,
59}