Skip to content

Commit

Permalink
add check shasum
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Jul 19, 2024
1 parent 9c4b47d commit 323ca67
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
31 changes: 0 additions & 31 deletions crates/snm_shim/src/download.rs

This file was deleted.

53 changes: 50 additions & 3 deletions crates/snm_shim/src/ensure_binary_path.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::ops::Not;
use std::{fs, ops::Not};

use snm_atom::atom::AtomTrait;
use snm_download_builder::{DownloadBuilder, WriteStrategy};
use snm_utils::snm_error::SnmError;

use super::download::download;

pub async fn ensure_binary_path<T>(manage: &T, version: &String) -> Result<String, SnmError>
where
T: AtomTrait,
Expand All @@ -26,3 +25,51 @@ where

return Ok(binary);
}

pub async fn download<T>(version: &str, atom: &T) -> Result<(), SnmError>
where
T: AtomTrait,
{
let download_url = atom.get_download_url(version);

let downloaded_file_path_buf = atom.get_downloaded_file_path_buf(version)?;

DownloadBuilder::new()
.retries(3)
.timeout(atom.get_snm_config().get_download_timeout_secs())
.write_strategy(WriteStrategy::Nothing)
.download(&download_url, &downloaded_file_path_buf)
.await?;

let runtime_dir_path_buf = atom.get_runtime_dir_path_buf(version)?;

let expect = atom.get_expect_shasum(version).await?;

let actual = atom.get_actual_shasum(&downloaded_file_path_buf).await?;

if actual.is_none() || expect.is_none() {
fs::remove_file(&downloaded_file_path_buf)?;
return Err(SnmError::ShasumError {
file_path: downloaded_file_path_buf.display().to_string(),
expect: "None".to_string(),
actual: "None".to_string(),
});
}

if actual.eq(&expect).not() {
fs::remove_file(&downloaded_file_path_buf)?;
return Err(SnmError::ShasumError {
file_path: downloaded_file_path_buf.display().to_string(),
expect: expect.unwrap_or("None".to_string()),
actual: actual.unwrap_or("None".to_string()),
});
}

atom.decompress_download_file(&downloaded_file_path_buf, &runtime_dir_path_buf)?;

if let Some(parent) = downloaded_file_path_buf.parent() {
fs::remove_dir_all(parent)?;
}

Ok(())
}
1 change: 0 additions & 1 deletion crates/snm_shim/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod download;
mod ensure_binary_path;
mod get_default_bin_dir;
mod get_node_bin_dir;
Expand Down

0 comments on commit 323ca67

Please sign in to comment.