Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Commit

Permalink
ocidir: Change create -> ensure
Browse files Browse the repository at this point in the history
It's handy to have this be idempotent; we previously said
"must be empty" but that wasn't true.
  • Loading branch information
cgwalters committed Jan 18, 2024
1 parent 15e3f98 commit 5a2bdbe
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/src/container/encapsulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn build_oci(
std::fs::create_dir(ocidir_path).context("Creating OCI dir")?;
}
let ocidir = Dir::open_ambient_dir(ocidir_path, cap_std::ambient_authority())?;
let mut writer = ocidir::OciDir::create(&ocidir)?;
let mut writer = ocidir::OciDir::ensure(&ocidir)?;

let commit = repo.require_rev(rev)?;
let commit = commit.as_str();
Expand Down
13 changes: 8 additions & 5 deletions lib/src/container/ocidir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,23 @@ pub fn new_empty_manifest() -> oci_image::ImageManifestBuilder {
}

impl OciDir {
/// Create a new, empty OCI directory at the target path, which should be empty.
pub fn create(dir: &Dir) -> Result<Self> {
/// Open the OCI directory at the target path; if it does not already
/// have the standard OCI metadata, it is created.
pub fn ensure(dir: &Dir) -> Result<Self> {
let mut db = cap_std::fs::DirBuilder::new();
db.recursive(true).mode(0o755);
dir.ensure_dir_with(BLOBDIR, &db)?;
dir.atomic_write("oci-layout", r#"{"imageLayoutVersion":"1.0.0"}"#)?;
if !dir.try_exists("oci-layout")? {
dir.atomic_write("oci-layout", r#"{"imageLayoutVersion":"1.0.0"}"#)?;
}
Self::open(dir)
}

/// Clone an OCI directory, using reflinks for blobs.
pub fn clone_to(&self, destdir: &Dir, p: impl AsRef<Path>) -> Result<Self> {
let p = p.as_ref();
destdir.create_dir(p)?;
let cloned = Self::create(&destdir.open_dir(p)?)?;
let cloned = Self::ensure(&destdir.open_dir(p)?)?;
for blob in self.dir.read_dir(BLOBDIR)? {
let blob = blob?;
let path = Path::new(BLOBDIR).join(blob.file_name());
Expand Down Expand Up @@ -519,7 +522,7 @@ mod tests {
#[test]
fn test_build() -> Result<()> {
let td = cap_tempfile::tempdir(cap_std::ambient_authority())?;
let w = OciDir::create(&td)?;
let w = OciDir::ensure(&td)?;
let mut layerw = w.create_raw_layer(None)?;
layerw.write_all(b"pretend this is a tarball")?;
let root_layer = layerw.complete()?;
Expand Down

0 comments on commit 5a2bdbe

Please sign in to comment.