1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use crate::{
    image::{Image, ImageBuilder},
    Digest, ImageName,
};
use anyhow::{bail, Context, Result};
use maplit::hashmap;
use oci_spec::image::{
    DescriptorBuilder, ImageIndex, ImageIndexBuilder, ImageManifest, MediaType, OciLayout,
};
use std::{
    fs,
    path::{Path, PathBuf},
};

use super::get_name_from_index;

/// Build an [OciDir]
pub struct OciDirBuilder {
    image_name: Option<ImageName>,
    oci_dir_root: PathBuf,
    is_finished: bool,
}

impl Drop for OciDirBuilder {
    fn drop(&mut self) {
        // Remove oci-dir if it is not finished.
        if !self.is_finished {
            fs::remove_dir_all(&self.oci_dir_root).unwrap_or_else(|e| {
                log::error!(
                    "Failed to remove oci-dir {}: {}",
                    self.oci_dir_root.display(),
                    e
                )
            });
        }
    }
}

impl OciDirBuilder {
    pub fn new_unnamed(oci_dir_root: PathBuf) -> Result<Self> {
        if oci_dir_root.exists() {
            bail!("oci-dir {} already exists", oci_dir_root.display());
        }
        fs::create_dir_all(&oci_dir_root)?;
        Ok(Self {
            image_name: None,
            oci_dir_root,
            is_finished: false,
        })
    }

    pub fn new(oci_dir_root: PathBuf, image_name: ImageName) -> Result<Self> {
        if oci_dir_root.exists() {
            bail!("oci-dir {} already exists", oci_dir_root.display());
        }
        fs::create_dir_all(&oci_dir_root)?;
        Ok(Self {
            image_name: Some(image_name),
            oci_dir_root,
            is_finished: false,
        })
    }
}

impl ImageBuilder for OciDirBuilder {
    type Image = OciDir;

    fn add_blob(&mut self, data: &[u8]) -> Result<(Digest, i64)> {
        let digest = Digest::from_buf_sha256(data);
        let out = self.oci_dir_root.join(digest.as_path());
        fs::create_dir_all(out.parent().unwrap())?;
        fs::write(out, data)?;
        Ok((digest, data.len() as i64))
    }

    fn build(mut self, manifest: ImageManifest) -> Result<OciDir> {
        let manifest_json = serde_json::to_string(&manifest)?;
        let (digest, size) = self.add_blob(manifest_json.as_bytes())?;
        let descriptor = DescriptorBuilder::default()
            .media_type(MediaType::ImageManifest)
            .size(size)
            .digest(digest.to_string())
            .annotations(if let Some(name) = &self.image_name {
                hashmap! {
                    "org.opencontainers.image.ref.name".to_string() => name.to_string()
                }
            } else {
                hashmap! {}
            })
            .build()?;
        let index = ImageIndexBuilder::default()
            .schema_version(2_u32)
            .manifests(vec![descriptor])
            .build()?;
        fs::write(
            self.oci_dir_root.join("oci-layout"),
            r#"{"imageLayoutVersion":"1.0.0"}"#,
        )?;
        fs::write(
            self.oci_dir_root.join("index.json"),
            serde_json::to_string(&index)?,
        )?;
        self.is_finished = true;
        Ok(OciDir {
            oci_dir_root: self.oci_dir_root.clone(),
        })
    }
}

/// `oci-dir` image layout, a directory in the form of [OCI Image Layout](https://github.com/opencontainers/image-spec/blob/v1.1.0/image-layout.md).
///
/// The name "oci-dir" comes from [`podman save`](https://docs.podman.io/en/latest/markdown/podman-save.1.html).
pub struct OciDir {
    oci_dir_root: PathBuf,
}

impl OciDir {
    pub fn new(oci_dir_root: &Path) -> Result<Self> {
        if !oci_dir_root.is_dir() {
            bail!("{} is not a directory", oci_dir_root.display());
        }
        let oci_layout: OciLayout = fs::read(oci_dir_root.join("oci-layout"))
            .and_then(|bytes| Ok(serde_json::from_slice(&bytes)?))
            .context("The directory is not a oci-dir; oci-layout is not found.")?;
        if oci_layout.image_layout_version() != "1.0.0" {
            bail!(
                "Incompatible oci-layout version in {}",
                oci_dir_root.display()
            );
        }
        Ok(Self {
            oci_dir_root: oci_dir_root.to_owned(),
        })
    }

    fn get_index(&mut self) -> Result<ImageIndex> {
        let index_path = self.oci_dir_root.join("index.json");
        let index_json = fs::read_to_string(index_path)?;
        Ok(serde_json::from_str(&index_json)?)
    }
}

impl Image for OciDir {
    fn get_name(&mut self) -> Result<ImageName> {
        get_name_from_index(&self.get_index()?)
    }

    fn get_blob(&mut self, digest: &Digest) -> Result<Vec<u8>> {
        Ok(fs::read(self.oci_dir_root.join(digest.as_path()))?)
    }

    fn get_manifest(&mut self) -> Result<ImageManifest> {
        let index = self.get_index()?;
        let desc = index
            .manifests()
            .first()
            .context("No manifest found in index.json")?;
        let digest = Digest::from_descriptor(desc)?;
        let manifest = serde_json::from_slice(self.get_blob(&digest)?.as_slice())?;
        Ok(manifest)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::image::OciArtifactBuilder;

    #[test]
    fn test_artifact_over_oci_dir() -> Result<()> {
        let tmp_dir = tempfile::tempdir()?;
        let path = tmp_dir.path().join("oci-dir");
        let image_name = ImageName::parse("test")?;
        let oci_dir = OciDirBuilder::new(path, image_name.clone())?;
        let mut artifact =
            OciArtifactBuilder::new(oci_dir, MediaType::Other("test".to_string()))?.build()?;

        let name = artifact.get_name()?;
        let manifest = artifact.get_manifest()?;
        assert_eq!(name, image_name);
        assert_eq!(
            manifest.artifact_type().as_ref().unwrap(),
            &MediaType::Other("test".to_string())
        );

        let (config_desc, config) = artifact.get_config()?;
        assert_eq!(config_desc.media_type(), &MediaType::EmptyJSON);
        assert_eq!(config, "{}".as_bytes());

        Ok(())
    }
}