Skip to content

Commit 7d1e95a

Browse files
committed
feat: replace curl and sha256sum with reqwest and sha2
1 parent 7e8c847 commit 7d1e95a

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

xtask/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ clap = { version = "4.4", features = ["derive"] }
1111
colored = "3"
1212
ostool = "0.8"
1313
jkconfig = "0.1"
14+
reqwest = "0.11"
1415
schemars = { version = "1", features = ["derive"] }
1516
serde = { version = "1.0", features = ["derive"] }
1617
serde_json = "1"
18+
sha2 = "0.10"
1719
tokio = { version = "1", features = ["full"] }
1820
toml.workspace = true
1921

xtask/src/image.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@
1313
//! xtask image ls
1414
//! // Download a specific image and automatically extract it
1515
//! xtask image download evm3588_arceos --output-dir ./images
16-
//! // Pull a specific image (alias for download) and automatically extract it
17-
//! xtask image pull evm3588_arceos --output-dir ./images
16+
//! // Download a specific image without extracting
17+
//! xtask image download evm3588_arceos --output-dir ./images --no-extract
1818
//! // Remove a specific image from temp directory
1919
//! xtask image rm evm3588_arceos
2020
//! ```
2121
2222
use anyhow::{Result, anyhow};
2323
use clap::{Parser, Subcommand};
24+
use sha2::{Sha256, Digest};
2425
use std::path::{Path};
2526
use std::process::Command;
2627
use std::fs;
2728
use std::env;
29+
use std::io::Read;
30+
use tokio::fs::File;
31+
use tokio::io::AsyncWriteExt;
2832

2933
/// Base URL for downloading images
3034
const IMAGE_URL_BASE: &str = "https://github.com/arceos-hypervisor/axvisor-guest/releases/download/v0.0.16/";
@@ -216,17 +220,20 @@ impl Image {
216220
/// # Errors
217221
/// * `anyhow::Error` - If any error occurs during the verification process
218222
fn image_verify_sha256(file_path: &Path, expected_sha256: &str) -> Result<bool> {
219-
let output = Command::new("sha256sum")
220-
.arg(file_path)
221-
.output()?;
222-
223-
if !output.status.success() {
224-
return Err(anyhow!("Failed to calculate SHA256"));
223+
let mut file = fs::File::open(file_path)?;
224+
let mut hasher = Sha256::new();
225+
let mut buffer = [0; 8192];
226+
227+
loop {
228+
let bytes_read = file.read(&mut buffer)?;
229+
if bytes_read == 0 {
230+
break;
231+
}
232+
hasher.update(&buffer[..bytes_read]);
225233
}
226234

227-
let stdout = String::from_utf8(output.stdout)?;
228-
let actual_sha256 = stdout.split_whitespace().next()
229-
.ok_or_else(|| anyhow!("Unable to parse SHA256 output"))?;
235+
let result = hasher.finalize();
236+
let actual_sha256 = format!("{:x}", result);
230237

231238
Ok(actual_sha256 == expected_sha256)
232239
}
@@ -281,7 +288,7 @@ fn image_list() -> Result<()> {
281288
/// // Or use the pull alias
282289
/// xtask image pull evm3588_arceos --output-dir ./images
283290
/// ```
284-
fn image_download(image_name: &str, output_dir: Option<String>, extract: bool) -> Result<()> {
291+
async fn image_download(image_name: &str, output_dir: Option<String>, extract: bool) -> Result<()> {
285292
let image = Image::find_by_name(image_name)
286293
.ok_or_else(|| anyhow!("Image not found: {}. Use 'xtask image ls' to view available images", image_name))?;
287294

@@ -339,19 +346,19 @@ fn image_download(image_name: &str, output_dir: Option<String>, extract: bool) -
339346

340347
println!("Downloading file from {}...", download_url);
341348

342-
let mut child = Command::new("curl")
343-
.arg("-L") // Follow redirects
344-
.arg("-o")
345-
.arg(&output_path)
346-
.arg(&download_url)
347-
.spawn()?;
348-
349-
let status = child.wait()?;
350-
if !status.success() {
351-
return Err(anyhow!("Download failed, curl exit code: {}", status));
349+
// Use reqwest to download the file
350+
let response = reqwest::get(&download_url).await?;
351+
if !response.status().is_success() {
352+
return Err(anyhow!("Failed to download file: HTTP {}", response.status()));
352353
}
353354

354-
println!("Download completed");
355+
let bytes = response.bytes().await?;
356+
357+
// Write all bytes at once
358+
let mut file = File::create(&output_path).await?;
359+
file.write_all(&bytes).await?;
360+
361+
println!("Download completed ({} bytes)", bytes.len());
355362

356363
// Verify downloaded file
357364
println!("Verifying SHA256 of downloaded file...");
@@ -463,13 +470,13 @@ fn image_remove(image_name: &str) -> Result<()> {
463470
/// xtask image pull evm3588_arceos --output-dir ./images
464471
/// xtask image rm evm3588_arceos
465472
/// ```
466-
pub fn run_image(args: ImageArgs) -> Result<()> {
473+
pub async fn run_image(args: ImageArgs) -> Result<()> {
467474
match args.command {
468475
ImageCommands::Ls => {
469476
image_list()?;
470477
}
471478
ImageCommands::Download { image_name, output_dir, extract } => {
472-
image_download(&image_name, output_dir, extract.unwrap_or(true))?;
479+
image_download(&image_name, output_dir, extract.unwrap_or(true)).await?;
473480
}
474481
ImageCommands::Rm { image_name } => {
475482
image_remove(&image_name)?;

xtask/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ async fn main() -> Result<()> {
117117
ctx.run_menuconfig().await?;
118118
}
119119
Commands::Image(args) => {
120-
image::run_image(args)?;
120+
image::run_image(args).await?;
121121
}
122122
}
123123

0 commit comments

Comments
 (0)