Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/commands/build/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ fn upload_file(
pb.set_style(progress_style);

let chunk_size = chunk_upload_options.chunk_size as usize;
let (checksum, checksums) = get_sha1_checksums(bytes, chunk_size)?;
let (checksum, checksums) = get_sha1_checksums(bytes, chunk_size.try_into()?);
let mut chunks = bytes
.chunks(chunk_size)
.zip(checksums.iter())
Expand Down
2 changes: 1 addition & 1 deletion src/utils/chunks/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where
/// Creates a new `ChunkedObject` from the given object, using
/// the given chunk size.
pub fn from(object: T, chunk_size: usize) -> Result<Self> {
let (checksum, chunks) = fs::get_sha1_checksums(object.as_ref(), chunk_size)?;
let (checksum, chunks) = fs::get_sha1_checksums(object.as_ref(), chunk_size.try_into()?);
Ok(Self {
object,
checksum,
Expand Down
3 changes: 2 additions & 1 deletion src/utils/file_upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,8 @@ fn upload_files_chunked(
pb.set_style(progress_style);

let view = ByteView::open(archive.path())?;
let (checksum, checksums) = get_sha1_checksums(&view, options.chunk_size as usize)?;
let (checksum, checksums) =
get_sha1_checksums(&view, (options.chunk_size as usize).try_into()?);
let mut chunks = view
.chunks(options.chunk_size as usize)
.zip(checksums.iter())
Expand Down
29 changes: 11 additions & 18 deletions src/utils/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::env;
use std::fs;
use std::io;
use std::io::{Read, Seek};
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};

use anyhow::{bail, Result};
use anyhow::Result;
use flate2::read::GzDecoder;
use log::error;
use sha1_smol::{Digest, Sha1};
Expand Down Expand Up @@ -159,22 +160,18 @@ pub fn get_sha1_checksum<R: Read>(rdr: R) -> Result<Digest> {

/// Returns the SHA1 hash for the entire input, as well as each chunk of it. The
/// `chunk_size` must be non-zero.
pub fn get_sha1_checksums(data: &[u8], chunk_size: usize) -> Result<(Digest, Vec<Digest>)> {
if chunk_size == 0 {
bail!("Chunk size may not be zero.");
}

pub fn get_sha1_checksums(data: &[u8], chunk_size: NonZeroUsize) -> (Digest, Vec<Digest>) {
let mut total_sha = Sha1::new();
let mut chunks = Vec::new();

for chunk in data.chunks(chunk_size) {
for chunk in data.chunks(chunk_size.into()) {
let mut chunk_sha = Sha1::new();
chunk_sha.update(chunk);
total_sha.update(chunk);
chunks.push(chunk_sha.digest());
}

Ok((total_sha.digest(), chunks))
(total_sha.digest(), chunks)
}

/// Checks if provided slice contains gzipped data.
Expand Down Expand Up @@ -245,9 +242,10 @@ mod tests {

#[test]
fn sha1_checksums_power_of_two() {
const NONZERO_16: NonZeroUsize = NonZeroUsize::new(16).unwrap();

let data = b"this is some binary data for the test";
let (total_sha, chunks) =
get_sha1_checksums(data, 16).expect("Method should not fail because 16 is not zero");
let (total_sha, chunks) = get_sha1_checksums(data, NONZERO_16);

assert_eq!(
total_sha.to_string(),
Expand All @@ -268,10 +266,11 @@ mod tests {

#[test]
fn sha1_checksums_not_power_of_two() {
const NONZERO_17: NonZeroUsize = NonZeroUsize::new(17).unwrap();

let data = b"this is some binary data for the test";

let (total_sha, chunks) =
get_sha1_checksums(data, 17).expect("Method should not fail because 17 is not zero");
let (total_sha, chunks) = get_sha1_checksums(data, NONZERO_17);

assert_eq!(
total_sha.to_string(),
Expand All @@ -289,10 +288,4 @@ mod tests {
]
);
}

#[test]
fn sha1_checksums_zero() {
let data = b"this is some binary data for the test";
get_sha1_checksums(data, 0).expect_err("Method should fail because 0 is zero");
}
}