From 323ca67114588308a6dd6ec6820deb90ab981e0e Mon Sep 17 00:00:00 2001 From: ityuany <519495771@qq.com> Date: Fri, 19 Jul 2024 20:13:54 +0800 Subject: [PATCH] add check shasum --- crates/snm_shim/src/download.rs | 31 ------------- crates/snm_shim/src/ensure_binary_path.rs | 53 +++++++++++++++++++++-- crates/snm_shim/src/lib.rs | 1 - 3 files changed, 50 insertions(+), 35 deletions(-) delete mode 100644 crates/snm_shim/src/download.rs diff --git a/crates/snm_shim/src/download.rs b/crates/snm_shim/src/download.rs deleted file mode 100644 index 995a198f..00000000 --- a/crates/snm_shim/src/download.rs +++ /dev/null @@ -1,31 +0,0 @@ -use std::fs; - -use snm_atom::atom::AtomTrait; -use snm_download_builder::{DownloadBuilder, WriteStrategy}; -use snm_utils::snm_error::SnmError; - -pub async fn download(version: &str, manage: &T) -> Result<(), SnmError> -where - T: AtomTrait, -{ - let download_url = manage.get_download_url(version); - - let downloaded_file_path_buf = manage.get_downloaded_file_path_buf(version)?; - - DownloadBuilder::new() - .retries(3) - .timeout(manage.get_snm_config().get_download_timeout_secs()) - .write_strategy(WriteStrategy::Nothing) - .download(&download_url, &downloaded_file_path_buf) - .await?; - - let runtime_dir_path_buf = manage.get_runtime_dir_path_buf(version)?; - - manage.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(()) -} diff --git a/crates/snm_shim/src/ensure_binary_path.rs b/crates/snm_shim/src/ensure_binary_path.rs index df5ce92d..828feafd 100644 --- a/crates/snm_shim/src/ensure_binary_path.rs +++ b/crates/snm_shim/src/ensure_binary_path.rs @@ -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(manage: &T, version: &String) -> Result where T: AtomTrait, @@ -26,3 +25,51 @@ where return Ok(binary); } + +pub async fn download(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(()) +} diff --git a/crates/snm_shim/src/lib.rs b/crates/snm_shim/src/lib.rs index 70ffad6f..24e3c0f8 100644 --- a/crates/snm_shim/src/lib.rs +++ b/crates/snm_shim/src/lib.rs @@ -1,4 +1,3 @@ -mod download; mod ensure_binary_path; mod get_default_bin_dir; mod get_node_bin_dir;