Skip to content

Commit ac5cf7c

Browse files
committed
composefs: Add Repository.ensure_object_from_reader()
This is basically ensure_object_from_fd(), but for anything implementing Read. basically ensure_object_from_fd() is reimplemented based on this. We will need this in the ostree support code for streaming a zlib compressed file to the repo. Signed-off-by: Alexander Larsson <alexl@redhat.com>
1 parent db8ba59 commit ac5cf7c

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

crates/composefs/src/repository.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,14 +1528,22 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
15281528
/// avoiding a second read pass.
15291529
#[context("Ensuring object from file descriptor")]
15301530
pub(crate) fn ensure_object_from_fd(&self, source: OwnedFd, size: u64) -> Result<ObjectID> {
1531+
self.ensure_object_from_reader(File::from(source), size)
1532+
}
1533+
1534+
/// Ensures that the given data is stored as an object in the repository,
1535+
/// reading from any source that implements [`Read`].
1536+
///
1537+
/// In insecure mode, the fs-verity digest is computed while copying,
1538+
/// avoiding a second read pass.
1539+
#[context("Ensuring object from reader")]
1540+
pub fn ensure_object_from_reader(&self, mut source: impl Read, size: u64) -> Result<ObjectID> {
15311541
let writable = self.ensure_writable_token()?;
15321542
let tmpfile_fd = self.create_object_tmpfile_impl(&writable)?;
15331543

15341544
if self.insecure {
1535-
// Insecure mode: compute verity digest while copying, avoiding
1536-
// a second read of the data in finalize_object_tmpfile_impl.
15371545
let mut hasher = FsVerityHasher::<ObjectID>::new();
1538-
let mut src = std::io::BufReader::with_capacity(IO_BUF_CAPACITY, File::from(source));
1546+
let mut src = std::io::BufReader::with_capacity(IO_BUF_CAPACITY, &mut source);
15391547
let mut dst = File::from(tmpfile_fd.try_clone()?);
15401548

15411549
loop {
@@ -1561,9 +1569,8 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
15611569
// Secure mode: let std::io::copy use copy_file_range for
15621570
// potential reflinks, then finalize_object_tmpfile_impl
15631571
// enables kernel verity and measures the digest.
1564-
let mut src = File::from(source);
15651572
let mut dst = File::from(tmpfile_fd.try_clone()?);
1566-
let copied = std::io::copy(&mut src, &mut dst)?;
1573+
let copied = std::io::copy(&mut source, &mut dst)?;
15671574
ensure!(copied == size, "Expected {size} bytes, got {copied}");
15681575
drop(dst);
15691576

0 commit comments

Comments
 (0)