Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Refactor checksum calculation #8

Merged
merged 1 commit into from
May 18, 2024
Merged
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
14 changes: 3 additions & 11 deletions buildpacks/dotnet/src/layers/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ use semver::Version;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha512};
use std::env::temp_dir;
use std::fs::File;
use std::io::Read;
use std::fs::{self, File};
use std::path::Path;

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -135,9 +134,8 @@ fn verify_checksum<D>(checksum: &Checksum<D>, path: impl AsRef<Path>) -> Result<
where
D: Digest,
{
let calculated_checksum = File::open(path.as_ref())
.map_err(SdkLayerError::OpenTempFile)
.map(calculate_checksum::<D>)?
let calculated_checksum = fs::read(path.as_ref())
.map(|data| D::digest(data).to_vec())
.map_err(SdkLayerError::ReadTempFile)?;

if calculated_checksum == checksum.value {
Expand All @@ -147,12 +145,6 @@ where
}
}

fn calculate_checksum<D: Digest>(data: impl Read) -> std::io::Result<Vec<u8>> {
data.bytes()
.collect::<Result<Vec<_>, _>>()
.map(|data| D::digest(data).to_vec())
}

#[derive(thiserror::Error, Debug)]
pub(crate) enum SdkLayerError {
#[error("Couldn't download .NET SDK: {0}")]
Expand Down