Commit 53bd3d56 authored by SamwiseFilmore's avatar SamwiseFilmore
Browse files

Simplify copy_and_hash to take any writer;

Hashing a file should just pass it io::sink() and that'll probably be
optimized away at compile. Significantly simplifies every call site too.
parent 1a9d144c
......@@ -150,7 +150,7 @@ pub fn create(
file: path.clone(),
source: e,
})?;
copy_and_hash(&mut entry_file, Some(&mut archive_file), &mut buf)
copy_and_hash(&mut entry_file, &mut archive_file, &mut buf)
.map_err(|source| Error::Io {
reason: format!("Writing entry to archive: '{}'", relative.display()),
file: path.clone(),
......@@ -165,7 +165,7 @@ pub fn create(
source: e,
})?;
let mut data = destination.as_os_str().as_bytes();
copy_and_hash(&mut data, Some(&mut archive_file), &mut buf)
copy_and_hash(&mut data, &mut archive_file, &mut buf)
.map_err(|source| Error::Io {
reason: format!("Writing entry to archive: '{}'", relative.display()),
file: path.clone(),
......
......@@ -80,7 +80,7 @@ where Src: PackageSrc<Err = Error>,
/// outputs the blake3 hash of the data streamed, and also does not allocate.
pub(crate) fn copy_and_hash<R: Read, W: Write>(
mut read: R,
mut write: Option<W>,
mut write: W,
buf: &mut [u8]
) -> Result<(u64, Hash), io::Error> {
let mut hasher = Hasher::new();
......@@ -93,12 +93,7 @@ pub(crate) fn copy_and_hash<R: Read, W: Write>(
written += count as u64;
hasher.update_with_join::<blake3::join::RayonJoin>(&buf[..count]);
//TODO: Progress
// After some naive testing it doesn't seem to make much difference to
// use dynamic dispatch instead of checking every iteration
if let Some(ref mut write) = write {
write.write_all(&buf[..count])?;
}
write.write_all(&buf[..count])?;
}
Ok((written, hasher.finalize()))
}
......
......@@ -101,7 +101,7 @@ impl Transaction {
file: PathBuf::from(&tmp_path),
source: e,
})?;
copy_and_hash(src.entry_reader(entry), Some(&mut tmp_file), &mut buf)
copy_and_hash(src.entry_reader(entry), &mut tmp_file, &mut buf)
.map_err(|source| Error::Io {
reason: format!("Copying entry to tempfile: '{}'", relative_path.display()),
file: tmp_path.to_path_buf(),
......@@ -110,7 +110,7 @@ impl Transaction {
},
MODE_SYMLINK => {
let mut data = Vec::new();
let (size, hash) = copy_and_hash(src.entry_reader(entry), Some(&mut data), &mut buf)
let (size, hash) = copy_and_hash(src.entry_reader(entry), &mut data, &mut buf)
.map_err(|source| Error::Io {
reason: format!("Copying entry to tempfile: '{}'", relative_path.display()),
file: tmp_path.to_path_buf(),
......@@ -193,7 +193,7 @@ impl Transaction {
file: PathBuf::from(&target_path),
source: e,
})?;
copy_and_hash::<_, io::Sink>(candidate, None, &mut buf)
copy_and_hash(candidate, io::sink(), &mut buf)
.map_err(|source| Error::Io {
reason: format!("Hashing file for entry: '{}'", relative_path.display()),
file: PathBuf::from(&target_path),
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment