From 506d2bdd2d976e537b75805a5f624da2f69e5d2d Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 22 Oct 2021 15:30:47 -0400 Subject: [PATCH] Initial "chunking" code This analyzes an ostree commit and splits it into chunks suitable for output to separate layers in an OCI image. --- lib/src/chunking.rs | 399 +++++++++++++++++++++++++++++++ lib/src/cli.rs | 38 ++- lib/src/container/encapsulate.rs | 51 +++- lib/src/container/ociwriter.rs | 43 ++-- lib/src/container/store.rs | 5 +- lib/src/lib.rs | 2 + lib/src/tar/export.rs | 85 ++++++- 7 files changed, 593 insertions(+), 30 deletions(-) create mode 100644 lib/src/chunking.rs diff --git a/lib/src/chunking.rs b/lib/src/chunking.rs new file mode 100644 index 00000000..516fe2e8 --- /dev/null +++ b/lib/src/chunking.rs @@ -0,0 +1,399 @@ +//! Split an OSTree commit into separate chunks + +// SPDX-License-Identifier: Apache-2.0 OR MIT + +use std::borrow::Borrow; +use std::collections::{BTreeMap, BTreeSet}; +use std::rc::Rc; + +use crate::objgv::*; +use anyhow::Result; +use camino::Utf8PathBuf; +use gvariant::aligned_bytes::TryAsAligned; +use gvariant::{Marker, Structure}; +use ostree; +use ostree::prelude::*; +use ostree::{gio, glib}; + +const FIRMWARE: &str = "/usr/lib/firmware"; +const MODULES: &str = "/usr/lib/modules"; + +const QUERYATTRS: &str = "standard::name,standard::type"; + +/// Size in bytes of the smallest chunk we will emit. +// pub(crate) const MIN_CHUNK_SIZE: u32 = 10 * 1024; +/// Maximum number of layers (chunks) we will use. +// We take half the limit of 128. +// https://github.com/ostreedev/ostree-rs-ext/issues/69 +pub(crate) const MAX_CHUNKS: u32 = 64; + +/// Size in bytes for the minimum size for chunks +#[allow(dead_code)] +pub(crate) const DEFAULT_MIN_CHUNK: usize = 10 * 1024; + +#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone)] +pub(crate) struct RcStr(Rc); + +impl Borrow for RcStr { + fn borrow(&self) -> &str { + &*self.0 + } +} + +impl From<&str> for RcStr { + fn from(s: &str) -> Self { + Self(Rc::from(s)) + } +} + +#[derive(Debug, Default)] +pub(crate) struct Chunk { + pub(crate) name: String, + pub(crate) content: BTreeMap)>, + pub(crate) size: u64, +} + +#[derive(Debug)] +pub(crate) enum Meta { + DirTree(RcStr), + DirMeta(RcStr), +} + +impl Meta { + pub(crate) fn objtype(&self) -> ostree::ObjectType { + match self { + Meta::DirTree(_) => ostree::ObjectType::DirTree, + Meta::DirMeta(_) => ostree::ObjectType::DirMeta, + } + } + + pub(crate) fn checksum(&self) -> &str { + match self { + Meta::DirTree(v) => &*v.0, + Meta::DirMeta(v) => &*v.0, + } + } +} + +#[derive(Debug, Default)] +pub(crate) struct Chunking { + pub(crate) metadata_size: u64, + pub(crate) commit: Box, + pub(crate) meta: Vec, + pub(crate) remainder: Chunk, + pub(crate) chunks: Vec, +} + +// pub(crate) struct ChunkConfig { +// pub(crate) min_size: u32, +// pub(crate) max_chunks: u32, +// } +// +// impl Default for ChunkConfig { +// fn default() -> Self { +// Self { +// min_size: MIN_CHUNK_SIZE, +// max_chunks: MAX_CHUNKS, +// } +// } +// } + +#[derive(Default)] +struct Generation { + path: Utf8PathBuf, + metadata_size: u64, + meta: Vec, + dirtree_found: BTreeSet, + dirmeta_found: BTreeSet, +} + +fn generate_chunking_recurse( + repo: &ostree::Repo, + gen: &mut Generation, + chunk: &mut Chunk, + dt: &glib::Variant, +) -> Result<()> { + let dt = dt.data_as_bytes(); + let dt = dt.try_as_aligned()?; + let dt = gv_dirtree!().cast(dt); + let (files, dirs) = dt.to_tuple(); + // A reusable buffer to avoid heap allocating these + let mut hexbuf = [0u8; 64]; + for file in files { + let (name, csum) = file.to_tuple(); + let fpath = gen.path.join(name.to_str()); + hex::encode_to_slice(csum, &mut hexbuf)?; + let checksum = std::str::from_utf8(&hexbuf)?; + let (_, meta, _) = repo.load_file(checksum, gio::NONE_CANCELLABLE)?; + // SAFETY: We know this API returns this value; it only has a return nullable because the + // caller can pass NULL to skip it. + let meta = meta.unwrap(); + let size = meta.size() as u64; + let entry = chunk.content.entry(RcStr::from(checksum)).or_default(); + entry.0 = size; + let first = entry.1.is_empty(); + if first { + chunk.size += size; + } + entry.1.push(fpath); + } + for item in dirs { + let (name, contents_csum, meta_csum) = item.to_tuple(); + let name = name.to_str(); + // Extend our current path + gen.path.push(name); + hex::encode_to_slice(contents_csum, &mut hexbuf)?; + let checksum_s = std::str::from_utf8(&hexbuf)?; + if !gen.dirtree_found.contains(checksum_s) { + let checksum = RcStr::from(checksum_s); + gen.dirtree_found.insert(RcStr::clone(&checksum)); + gen.meta.push(Meta::DirTree(checksum)); + let child_v = repo.load_variant(ostree::ObjectType::DirTree, checksum_s)?; + gen.metadata_size += child_v.data_as_bytes().as_ref().len() as u64; + generate_chunking_recurse(repo, gen, chunk, &child_v)?; + } + hex::encode_to_slice(meta_csum, &mut hexbuf)?; + let checksum_s = std::str::from_utf8(&hexbuf)?; + if !gen.dirtree_found.contains(checksum_s) { + let checksum = RcStr::from(checksum_s); + gen.dirmeta_found.insert(RcStr::clone(&checksum)); + let child_v = repo.load_variant(ostree::ObjectType::DirMeta, checksum_s)?; + gen.metadata_size += child_v.data_as_bytes().as_ref().len() as u64; + gen.meta.push(Meta::DirMeta(checksum)); + } + // We did a push above, so pop must succeed. + assert!(gen.path.pop()); + } + Ok(()) +} + +impl Chunk { + fn new(name: &str) -> Self { + Chunk { + name: name.to_string(), + ..Default::default() + } + } + + fn move_obj(&mut self, dest: &mut Self, checksum: &str) -> bool { + // In most cases, we expect the object to exist in the source. However, it's + // conveneient here to simply ignore objects which were already moved into + // a chunk. + if let Some((name, (size, paths))) = self.content.remove_entry(checksum) { + let v = dest.content.insert(name, (size, paths)); + debug_assert!(v.is_none()); + self.size -= size; + dest.size += size; + true + } else { + false + } + } + + // fn split(self) -> (Self, Self) { + // todo!() + // } +} + +fn find_kernel_dir( + root: &gio::File, + cancellable: Option<&gio::Cancellable>, +) -> Result> { + let moddir = root.resolve_relative_path(MODULES); + let e = moddir.enumerate_children( + "standard::name", + gio::FileQueryInfoFlags::NOFOLLOW_SYMLINKS, + cancellable, + )?; + let mut r = None; + for child in e.clone() { + let child = &child?; + let childpath = e.child(child); + if child.file_type() == gio::FileType::Directory { + if r.replace(childpath).is_some() { + anyhow::bail!("Found multiple subdirectories in {}", MODULES); + } + } + } + Ok(r) +} + +impl Chunking { + /// Generate an initial single chunk. + pub(crate) fn new(repo: &ostree::Repo, rev: &str) -> Result { + // Find the target commit + let rev = repo.resolve_rev(rev, false)?.unwrap(); + + // Load and parse the commit object + let (commit_v, _) = repo.load_commit(&rev)?; + let commit_v = commit_v.data_as_bytes(); + let commit_v = commit_v.try_as_aligned()?; + let commit = gv_commit!().cast(commit_v); + let commit = commit.to_tuple(); + + // Find the root directory tree + let contents_checksum = &hex::encode(commit.6); + let contents_v = repo.load_variant(ostree::ObjectType::DirTree, contents_checksum)?; + + // Load it all into a single chunk + let mut gen: Generation = Default::default(); + gen.path = Utf8PathBuf::from("/"); + let mut chunk: Chunk = Default::default(); + generate_chunking_recurse(repo, &mut gen, &mut chunk, &contents_v)?; + + let chunking = Chunking { + commit: Box::from(rev.as_str()), + metadata_size: gen.metadata_size, + meta: gen.meta, + remainder: chunk, + ..Default::default() + }; + Ok(chunking) + } + + fn remaining(&self) -> u32 { + MAX_CHUNKS.saturating_sub(self.chunks.len() as u32) + } + + /// Find the object named by `path` in `src`, and move it to `dest`. + fn extend_chunk( + repo: &ostree::Repo, + src: &mut Chunk, + dest: &mut Chunk, + path: &ostree::RepoFile, + ) -> Result<()> { + let cancellable = gio::NONE_CANCELLABLE; + let ft = path.query_file_type(gio::FileQueryInfoFlags::NOFOLLOW_SYMLINKS, cancellable); + if ft == gio::FileType::Directory { + let e = path.enumerate_children( + QUERYATTRS, + gio::FileQueryInfoFlags::NOFOLLOW_SYMLINKS, + cancellable, + )?; + for child in e { + let childi = child?; + let child = path.child(childi.name()); + let child = child.downcast::().unwrap(); + Self::extend_chunk(repo, src, dest, &child)?; + } + } else { + let checksum = path.checksum().unwrap(); + src.move_obj(dest, checksum.as_str()); + } + Ok(()) + } + + /// Create a new chunk from the provided filesystem paths. + pub(crate) fn chunk_paths<'a>( + &mut self, + repo: &ostree::Repo, + paths: impl IntoIterator, + name: &str, + cancellable: Option<&gio::Cancellable>, + ) -> Result<()> { + // Do nothing if we've hit our max. + if self.remaining() == 0 { + return Ok(()); + } + + let mut chunk = Chunk::new(name); + for path in paths { + if !path.query_exists(cancellable) { + continue; + } + let child = path.downcast_ref::().unwrap(); + Self::extend_chunk(repo, &mut self.remainder, &mut chunk, &child)?; + } + if !chunk.content.is_empty() { + self.chunks.push(chunk); + } + Ok(()) + } + + fn chunk_kernel_initramfs( + &mut self, + repo: &ostree::Repo, + root: &gio::File, + cancellable: Option<&gio::Cancellable>, + ) -> Result<()> { + let moddir = if let Some(m) = find_kernel_dir(root, cancellable)? { + m + } else { + return Ok(()); + }; + // The initramfs has a dependency on userspace *and* kernel, so we + // should chunk the kernel separately. + let initramfs = &moddir.resolve_relative_path("initramfs.img"); + self.chunk_paths(repo, [initramfs], "initramfs", cancellable)?; + // Gather all of the rest of the kernel as a single chunk + self.chunk_paths(repo, [&moddir], "kernel", cancellable) + } + + /// Apply built-in heuristics to automatically create chunks. + pub(crate) fn auto_chunk(&mut self, repo: &ostree::Repo) -> Result<()> { + let cancellable = gio::NONE_CANCELLABLE; + let root = &repo.read_commit(&self.commit, cancellable)?.0; + + // Grab all of linux-firmware; it's the largest thing in FCOS. + let firmware = root.resolve_relative_path(FIRMWARE); + self.chunk_paths(repo, [&firmware], "firmware", cancellable)?; + + // Kernel and initramfs + self.chunk_kernel_initramfs(repo, root, cancellable)?; + + self.large_files(20, 1)?; + + Ok(()) + } + + /// Gather large files (up to `max` chunks) as a percentage (1-99) of total size. + pub(crate) fn large_files(&mut self, max: u32, percentage: u32) -> Result<()> { + let max = max.min(self.remaining()); + if max == 0 { + return Ok(()); + } + + let mut large_objects = Vec::new(); + let total_size = self.remainder.size; + let largefile_limit = (total_size * (percentage * 100) as u64) / total_size; + dbg!(largefile_limit); + for (objid, (size, _names)) in &self.remainder.content { + if *size > largefile_limit { + large_objects.push((*size, objid.clone())); + } + } + large_objects.sort_by(|a, b| a.0.cmp(&b.0)); + for (_size, objid) in large_objects.iter().rev().take(max as usize) { + let mut chunk = { + let (_size, names) = self.remainder.content.get(objid).unwrap(); + let name = &names[0]; + Chunk::new(name.as_str()) + }; + let moved = self.remainder.move_obj(&mut chunk, objid.borrow()); + // The object only exists once, so we must have moved it. + assert!(moved); + self.chunks.push(chunk); + } + Ok(()) + } + + pub(crate) fn take_chunks(&mut self) -> Vec { + let mut r = Vec::new(); + std::mem::swap(&mut self.chunks, &mut r); + r + } +} + +pub(crate) fn print(src: &Chunking) { + println!("Metadata: {}", glib::format_size(src.metadata_size)); + for (n, chunk) in src.chunks.iter().enumerate() { + let sz = glib::format_size(chunk.size); + println!("Chunk {}: \"{}\": objects:{} size:{}", n, chunk.name, chunk.content.len(), sz); + } + let sz = glib::format_size(src.remainder.size); + println!( + "Remainder: objects:{} size:{}", + src.remainder.content.len(), + sz + ); +} diff --git a/lib/src/cli.rs b/lib/src/cli.rs index b53fed12..e850ce00 100644 --- a/lib/src/cli.rs +++ b/lib/src/cli.rs @@ -119,6 +119,10 @@ enum ContainerOpts { /// Corresponds to the Dockerfile `CMD` instruction. #[structopt(long)] cmd: Option>, + + #[structopt(long, hidden = true)] + /// Output in multiple blobs + ex_chunked: bool, }, /// Commands for working with (possibly layered, non-encapsulated) container images. @@ -225,6 +229,19 @@ struct ImaSignOpts { key: String, } +/// Experimental options +#[derive(Debug, StructOpt)] +enum ExperimentalOpts { + /// Print chunking + PrintChunks { + /// Path to the repository + #[structopt(long)] + repo: String, + /// The ostree ref or commt + rev: String, + }, +} + /// Toplevel options for extended ostree functionality. #[derive(Debug, StructOpt)] #[structopt(name = "ostree-ext")] @@ -236,6 +253,8 @@ enum Opt { Container(ContainerOpts), /// IMA signatures ImaSign(ImaSignOpts), + /// Experimental/debug CLI + Experimental(ExperimentalOpts), } impl Into for ContainerProxyOpts { @@ -338,13 +357,17 @@ async fn container_export( imgref: &ImageReference, labels: BTreeMap, cmd: Option>, + chunked: bool, ) -> Result<()> { let repo = &ostree::Repo::open_at(libc::AT_FDCWD, repo, gio::NONE_CANCELLABLE)?; let config = Config { labels: Some(labels), cmd, }; - let opts = Some(Default::default()); + let opts = Some(crate::container::ExportOpts { + chunked, + ..Default::default() + }); let pushed = crate::container::encapsulate(repo, rev, &config, opts, &imgref).await?; println!("{}", pushed); Ok(()) @@ -452,6 +475,7 @@ where imgref, labels, cmd, + ex_chunked, } => { let labels: Result> = labels .into_iter() @@ -464,7 +488,8 @@ where Ok((k.to_string(), v.to_string())) }) .collect(); - container_export(&repo, &rev, &imgref, labels?, cmd).await + + container_export(&repo, &rev, &imgref, labels?, cmd, ex_chunked).await } ContainerOpts::Image(opts) => match opts { ContainerImageOpts::List { repo } => { @@ -517,5 +542,14 @@ where }, }, Opt::ImaSign(ref opts) => ima_sign(opts), + Opt::Experimental(ref opts) => match opts { + ExperimentalOpts::PrintChunks { repo, rev } => { + let repo = &ostree::Repo::open_at(libc::AT_FDCWD, &repo, gio::NONE_CANCELLABLE)?; + let mut chunks = crate::chunking::Chunking::new(repo, rev)?; + chunks.auto_chunk(repo)?; + crate::chunking::print(&chunks); + Ok(()) + } + }, } } diff --git a/lib/src/container/encapsulate.rs b/lib/src/container/encapsulate.rs index f9fa719e..6d8c0361 100644 --- a/lib/src/container/encapsulate.rs +++ b/lib/src/container/encapsulate.rs @@ -2,6 +2,7 @@ use super::ociwriter::OciWriter; use super::*; +use crate::chunking::Chunking; use crate::tar as ostree_tar; use anyhow::Context; use fn_error_context::context; @@ -34,6 +35,37 @@ fn export_ostree_ref( w.complete() } +/// Write an ostree commit to an OCI blob +#[context("Writing ostree root to blob")] +fn export_chunked( + repo: &ostree::Repo, + ociw: &mut OciWriter, + mut chunking: Chunking, + compression: Option, +) -> Result<()> { + let layers: Result> = chunking + .take_chunks() + .into_iter() + .enumerate() + .map(|(i, chunk)| -> Result<_> { + let mut w = ociw.create_layer(compression)?; + ostree_tar::export_chunk(repo, &chunk, &mut w) + .with_context(|| format!("Exporting chunk {}", i))?; + let w = w.into_inner()?; + Ok((w.complete()?, chunk.name)) + }) + .collect(); + for (layer, name) in layers? { + ociw.push_layer(layer, &name); + } + let mut w = ociw.create_layer(compression)?; + ostree_tar::export_final_chunk(repo, &chunking, &mut w)?; + let w = w.into_inner()?; + let final_layer = w.complete()?; + ociw.push_layer(final_layer, "Remaining objects"); + Ok(()) +} + /// Generate an OCI image from a given ostree root #[context("Building oci")] fn build_oci( @@ -54,6 +86,15 @@ fn build_oci( let commit_meta = &commit_v.child_value(0); let commit_meta = glib::VariantDict::new(Some(commit_meta)); + let chunking = if opts.chunked { + // compression = Some(flate2::Compression::none()); + let mut c = crate::chunking::Chunking::new(repo, commit)?; + c.auto_chunk(repo)?; + Some(c) + } else { + None + }; + if let Some(version) = commit_meta.lookup_value("version", Some(glib::VariantTy::new("s").unwrap())) { @@ -78,8 +119,12 @@ fn build_oci( flate2::Compression::none() }; - let rootfs_blob = export_ostree_ref(repo, commit, &mut writer, Some(compression))?; - writer.push_layer(rootfs_blob); + if let Some(chunking) = chunking { + export_chunked(repo, &mut writer, chunking, Some(compression))?; + } else { + let rootfs_blob = export_ostree_ref(repo, commit, &mut writer, Some(compression))?; + writer.push_layer(rootfs_blob, &format!("export of commit {}", commit)); + } writer.complete()?; Ok(ImageReference { @@ -159,6 +204,8 @@ async fn build_impl( pub struct ExportOpts { /// If true, perform gzip compression of the tar layers. pub compress: bool, + /// Whether or not to generate multiple layers + pub chunked: bool, } /// Given an OSTree repository and ref, generate a container image. diff --git a/lib/src/container/ociwriter.rs b/lib/src/container/ociwriter.rs index 38e56761..e1f37832 100644 --- a/lib/src/container/ociwriter.rs +++ b/lib/src/container/ociwriter.rs @@ -76,6 +76,7 @@ pub(crate) struct OciWriter<'a> { cmd: Option>, + history: Vec, layers: Vec, } @@ -102,6 +103,7 @@ impl<'a> OciWriter<'a> { config_annotations: Default::default(), manifest_annotations: Default::default(), layers: Vec::new(), + history: Vec::new(), cmd: None, }) } @@ -123,17 +125,18 @@ impl<'a> OciWriter<'a> { Ok(tar::Builder::new(self.create_raw_layer(c)?)) } - #[allow(dead_code)] - /// Finish all I/O for a layer writer, and add it to the layers in the image. - pub(crate) fn finish_and_push_layer(&mut self, w: RawLayerWriter) -> Result<()> { - let w = w.complete()?; - self.push_layer(w); - Ok(()) - } + // #[allow(dead_code)] + // /// Finish all I/O for a layer writer, and add it to the layers in the image. + // pub(crate) fn finish_and_push_layer(&mut self, w: RawLayerWriter) -> Result<()> { + // let w = w.complete()?; + // self.push_layer(w); + // Ok(()) + // } /// Add a layer to the top of the image stack. The firsh pushed layer becomes the root. - pub(crate) fn push_layer(&mut self, layer: Layer) { - self.layers.push(layer) + pub(crate) fn push_layer(&mut self, layer: Layer, description: &str) { + self.layers.push(layer); + self.history.push(description.to_string()); } pub(crate) fn set_cmd(&mut self, e: &[&str]) { @@ -182,20 +185,22 @@ impl<'a> OciWriter<'a> { } .build() .unwrap(); - let history = oci_image::HistoryBuilder::default() - .created_by(format!( - "created by {} {}", - env!("CARGO_PKG_NAME"), - env!("CARGO_PKG_VERSION") - )) - .build() - .unwrap(); + let history: Vec<_> = self + .history + .into_iter() + .map(|h| { + oci_image::HistoryBuilder::default() + .created_by(h) + .build() + .unwrap() + }) + .collect(); let config = oci_image::ImageConfigurationBuilder::default() .architecture(arch.clone()) .os(oci_image::Os::Linux) .config(ctrconfig) .rootfs(rootfs) - .history(vec![history]) + .history(history) .build() .unwrap(); let config_blob = write_json_blob(self.dir, &config, MediaType::ImageConfig)?; @@ -371,7 +376,7 @@ mod tests { root_layer.uncompressed_sha256, "349438e5faf763e8875b43de4d7101540ef4d865190336c2cc549a11f33f8d7c" ); - w.push_layer(root_layer); + w.push_layer(root_layer, "root"); w.complete()?; Ok(()) } diff --git a/lib/src/container/store.rs b/lib/src/container/store.rs index c973f270..a2e5037a 100644 --- a/lib/src/container/store.rs +++ b/lib/src/container/store.rs @@ -137,7 +137,10 @@ pub struct PreparedImport { } // Given a manifest, compute its ostree ref name and cached ostree commit -fn query_layer(repo: &ostree::Repo, layer: oci_image::Descriptor) -> Result { +pub(crate) fn query_layer( + repo: &ostree::Repo, + layer: oci_image::Descriptor, +) -> Result { let ostree_ref = ref_for_layer(&layer)?; let commit = repo.resolve_rev(&ostree_ref, true)?.map(|s| s.to_string()); Ok(ManifestLayerState { diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 88793b1b..3dec12b8 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -31,8 +31,10 @@ pub mod refescape; pub mod tar; pub mod tokio_util; +mod chunking; mod cmdext; pub(crate) mod objgv; + /// Prelude, intended for glob import. pub mod prelude { #[doc(hidden)] diff --git a/lib/src/tar/export.rs b/lib/src/tar/export.rs index 06b33e93..d28c81e0 100644 --- a/lib/src/tar/export.rs +++ b/lib/src/tar/export.rs @@ -1,5 +1,7 @@ //! APIs for creating container images from OSTree commits +use crate::chunking; +use crate::chunking::Chunking; use crate::objgv::*; use anyhow::Result; use camino::{Utf8Path, Utf8PathBuf}; @@ -9,6 +11,7 @@ use gio::prelude::*; use gvariant::aligned_bytes::TryAsAligned; use gvariant::{Marker, Structure}; use ostree::gio; +use std::borrow::Borrow; use std::borrow::Cow; use std::collections::HashSet; use std::io::BufReader; @@ -193,7 +196,7 @@ impl<'a, W: std::io::Write> OstreeTarWriter<'a, W> { /// Write a content object, returning the path/header that should be used /// as a hard link to it in the target path. This matches how ostree checkouts work. - fn append_content(&mut self, checksum: &str) -> Result<(Utf8PathBuf, tar::Header)> { + fn append_content_obj(&mut self, checksum: &str) -> Result<(Utf8PathBuf, tar::Header)> { let path = object_path(ostree::ObjectType::File, checksum); let (instream, meta, xattrs) = self.repo.load_file(checksum, gio::NONE_CANCELLABLE)?; @@ -236,6 +239,18 @@ impl<'a, W: std::io::Write> OstreeTarWriter<'a, W> { Ok((path, target_header)) } + fn append_content_hardlink( + &mut self, + srcpath: &Utf8Path, + mut h: tar::Header, + dest: &Utf8Path, + ) -> Result<()> { + h.set_entry_type(tar::EntryType::Link); + h.set_link_name(srcpath)?; + self.out.append_data(&mut h, dest, &mut std::io::empty())?; + Ok(()) + } + /// Write a dirtree object. fn append_dirtree>( &mut self, @@ -264,13 +279,10 @@ impl<'a, W: std::io::Write> OstreeTarWriter<'a, W> { let name = name.to_str(); hex::encode_to_slice(csum, &mut hexbuf)?; let checksum = std::str::from_utf8(&hexbuf)?; - let (objpath, mut h) = self.append_content(checksum)?; - h.set_entry_type(tar::EntryType::Link); - h.set_link_name(&objpath)?; + let (objpath, h) = self.append_content_obj(checksum)?; let subpath = &dirpath.join(name); let subpath = map_path(subpath); - self.out - .append_data(&mut h, &*subpath, &mut std::io::empty())?; + self.append_content_hardlink(&objpath, h, &*subpath)?; } for item in dirs { @@ -319,6 +331,67 @@ pub fn export_commit(repo: &ostree::Repo, rev: &str, out: impl std::io::Write) - Ok(()) } +/// Output a chunk. +pub(crate) fn export_chunk( + repo: &ostree::Repo, + chunk: &chunking::Chunk, + out: &mut tar::Builder, +) -> Result<()> { + let writer = &mut OstreeTarWriter::new(repo, out); + writer.write_initial_directories()?; + for (checksum, (_size, paths)) in chunk.content.iter() { + let (objpath, h) = writer.append_content_obj(checksum.borrow())?; + for path in paths.iter() { + let path = path.strip_prefix("/").unwrap_or(path); + let h = h.clone(); + writer.append_content_hardlink(&objpath, h, path)?; + } + } + Ok(()) +} + +/// Output the last chunk in a chunking. +#[context("Exporting final chunk")] +pub(crate) fn export_final_chunk( + repo: &ostree::Repo, + chunking: &Chunking, + out: &mut tar::Builder, +) -> Result<()> { + let cancellable = gio::NONE_CANCELLABLE; + let writer = &mut OstreeTarWriter::new(repo, out); + writer.write_initial_directories()?; + + let (commit_v, _) = repo.load_commit(&chunking.commit)?; + let commit_v = &commit_v; + writer.append(ostree::ObjectType::Commit, &chunking.commit, commit_v)?; + if let Some(commitmeta) = repo.read_commit_detached_metadata(&chunking.commit, cancellable)? { + writer.append( + ostree::ObjectType::CommitMeta, + &chunking.commit, + &commitmeta, + )?; + } + + // In the chunked case, the final layer has all ostree metadata objects. + for meta in &chunking.meta { + let objtype = meta.objtype(); + let checksum = meta.checksum(); + let v = repo.load_variant(objtype, checksum)?; + writer.append(objtype, checksum, &v)?; + } + + for (checksum, (_size, paths)) in chunking.remainder.content.iter() { + let (objpath, h) = writer.append_content_obj(checksum.borrow())?; + for path in paths.iter() { + let path = path.strip_prefix("/").unwrap_or(path); + let h = h.clone(); + writer.append_content_hardlink(&objpath, h, path)?; + } + } + + Ok(()) +} + #[cfg(test)] mod tests { use super::*;