Merge branch 'code_cleanups' into 'master'

Various code quality improvements to the cookbook code

See merge request redox-os/cookbook!336
This commit is contained in:
Jeremy Soller 2024-01-08 19:16:01 +00:00
commit d56958abcc
9 changed files with 19 additions and 151 deletions

19
Cargo.lock generated
View File

@ -212,16 +212,15 @@ dependencies = [
[[package]]
name = "blake3"
version = "1.4.1"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "199c42ab6972d92c9f8995f086273d25c42fc0f7b2a1fcefba465c1352d25ba5"
checksum = "0231f06152bf547e9c2b5194f247cd97aacf6dcd8b15d8e5ec0663f64580da87"
dependencies = [
"arrayref",
"arrayvec 0.7.4",
"cc",
"cfg-if 1.0.0",
"constant_time_eq 0.3.0",
"digest 0.10.7",
]
[[package]]
@ -1237,13 +1236,12 @@ dependencies = [
name = "redox_cookbook"
version = "0.1.0"
dependencies = [
"blake3 1.4.1",
"blake3 1.5.0",
"pbr",
"pkgar",
"pkgar-keys",
"redoxer",
"serde",
"sha2",
"termion",
"toml 0.5.11",
"walkdir",
@ -1572,17 +1570,6 @@ dependencies = [
"serde",
]
[[package]]
name = "sha2"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479fb9d862239e610720565ca91403019f2f00410f1864c5aa7479b950a76ed8"
dependencies = [
"cfg-if 1.0.0",
"cpufeatures",
"digest 0.10.7",
]
[[package]]
name = "sha3"
version = "0.8.2"

View File

@ -20,13 +20,12 @@ name = "cookbook"
path = "src/lib.rs"
[dependencies]
blake3 = "1"
blake3 = "1.5"
pbr = "1.0.2"
pkgar = "0.1.7"
pkgar-keys = "0.1.0"
redoxer = "0.2"
serde = { version = "1.0.110", features = ["derive"] }
sha2 = "0.10"
termion = "1.5.5"
toml = "0.5.6"
walkdir = "2.3.1"

View File

@ -1,6 +1,5 @@
use cookbook::blake3::blake3_progress;
use cookbook::recipe::{Recipe, SourceRecipe, BuildKind, BuildRecipe, PackageRecipe};
use cookbook::sha256::sha256_progress;
use cookbook::recipe_find::recipe_find;
use std::{
env,
@ -250,7 +249,7 @@ fi"#);
command.arg("submodule").arg("update").arg("--init").arg("--recursive");
run_command(command)?;
},
Some(SourceRecipe::Tar { tar, blake3, sha256, patches, script }) => {
Some(SourceRecipe::Tar { tar, blake3, patches, script }) => {
if ! source_dir.is_dir() {
// Download tar
//TODO: replace wget
@ -292,26 +291,6 @@ fi"#);
);
}
//TODO: if blake3 is set, remove sha256
if let Some(sha256) = sha256 {
// Calculate sha256
let source_tar_sha256 = sha256_progress(&source_tar).map_err(|err| format!(
"failed to calculate sha256 of '{}': {}\n{:?}",
source_tar.display(),
err,
err
))?;
// Check if it matches recipe
if &source_tar_sha256 != sha256 {
return Err(format!(
"calculated sha256 '{}' does not match recipe sha256 '{}'",
source_tar_sha256,
sha256
));
}
}
// Create source.tmp
let source_dir_tmp = recipe_dir.join("source.tmp");
create_dir_clean(&source_dir_tmp)?;
@ -430,7 +409,7 @@ fn build(recipe_dir: &Path, source_dir: &Path, target_dir: &Path, build: &BuildR
eprintln!("DEBUG: '{}' newer than '{}'", source_dir.display(), stage_dir.display());
remove_all(&stage_dir)?;
}
if ! stage_dir.is_dir() {
// Create stage.tmp
let stage_dir_tmp = target_dir.join("stage.tmp");

View File

@ -1,30 +1,8 @@
use blake3::Hasher;
use std::{
fs,
io::{Read, Result},
path::Path,
time::Duration,
};
use std::{fs, io::Result, path::Path, time::Duration};
use crate::progress_bar::{ProgressBar, ProgressBarRead};
pub fn blake3<R: Read>(r: &mut R) -> Result<String> {
let mut hasher = Hasher::new();
let mut data = vec![0; 4 * 1024 * 1024];
loop {
let count = r.read(&mut data)?;
if count == 0 {
break;
}
hasher.update(&data[..count]);
}
let hash = hasher.finalize();
Ok(format!("{}", hash.to_hex()))
}
pub fn blake3_progress<P: AsRef<Path>>(path: P) -> Result<String> {
let len = fs::metadata(&path)?.len();
@ -35,12 +13,11 @@ pub fn blake3_progress<P: AsRef<Path>>(path: P) -> Result<String> {
pb.set_max_refresh_rate(Some(Duration::new(1, 0)));
pb.set_units(pbr::Units::Bytes);
let res = {
let mut pbr = ProgressBarRead::new(&mut pb, &mut f);
blake3(&mut pbr)
};
let mut pbr = ProgressBarRead::new(&mut pb, &mut f);
let hash = Hasher::new().update_reader(&mut pbr)?.finalize();
let res = format!("{}", hash.to_hex());
pb.finish_println("");
res
Ok(res)
}

View File

@ -1,6 +1,5 @@
pub mod blake3;
pub mod recipe;
pub mod sha256;
pub mod recipe_find;
mod progress_bar;

View File

@ -23,29 +23,3 @@ impl<'p, 'r, P: Write, R: Read> Read for ProgressBarRead<'p, 'r, P, R> {
Ok(count)
}
}
pub struct ProgressBarWrite<'p, 'w, P: Write + 'p, W: Write + 'w> {
pb: &'p mut ProgressBar<P>,
w: &'w mut W,
}
impl<'p, 'w, P: Write, W: Write> ProgressBarWrite<'p, 'w, P, W> {
pub fn _new(pb: &'p mut ProgressBar<P>, w: &'w mut W) -> ProgressBarWrite<'p, 'w, P, W> {
ProgressBarWrite {
pb,
w
}
}
}
impl<'p, 'w, P: Write, W: Write> Write for ProgressBarWrite<'p, 'w, P, W> {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
let count = self.w.write(buf)?;
self.pb.add(count as u64);
Ok(count)
}
fn flush(&mut self) -> Result<()> {
self.w.flush()
}
}

View File

@ -24,8 +24,6 @@ pub enum SourceRecipe {
/// The optional blake3 sum of the tar file. Please specify this to make reproducible
/// builds more reliable
blake3: Option<String>,
/// The optional sha256 sum of the tar file. This is a slower alternative to a blake3 sum
sha256: Option<String>,
/// A list of patch files to apply to the source
#[serde(default)]
patches: Vec<String>,
@ -118,7 +116,7 @@ mod tests {
let recipe: Recipe = toml::from_str(r#"
[source]
tar = "http://downloads.xiph.org/releases/ogg/libogg-1.3.3.tar.xz"
sha256 = "4f3fc6178a533d392064f14776b23c397ed4b9f48f5de297aba73b643f955c08"
sha256 = "8220c0e4082fa26c07b10bfe31f641d2e33ebe1d1bb0b20221b7016bc8b78a3a"
[build]
template = "custom"
@ -128,8 +126,7 @@ mod tests {
assert_eq!(recipe, Recipe {
source: Some(SourceRecipe::Tar {
tar: "http://downloads.xiph.org/releases/ogg/libogg-1.3.3.tar.xz".to_string(),
blake3: None,
sha256: Some("4f3fc6178a533d392064f14776b23c397ed4b9f48f5de297aba73b643f955c08".to_string()),
blake3: Some("8220c0e4082fa26c07b10bfe31f641d2e33ebe1d1bb0b20221b7016bc8b78a3a".to_string()),
patches: Vec::new(),
script: None,
}),

View File

@ -1,3 +1,4 @@
use std::ffi::OsStr;
use std::fs::{self};
use std::path::{Path, PathBuf};
@ -8,11 +9,11 @@ pub fn recipe_find(recipe: &str, dir: &Path) -> Result<Option<PathBuf>, String>
}
for entry in fs::read_dir(dir).map_err(|e| e.to_string())? {
let entry = entry.map_err(|e| e.to_string())?;
if entry.file_name().to_string_lossy() == "recipe.sh"
|| entry.file_name().to_string_lossy() == "recipe.toml"
if entry.file_name() == OsStr::new("recipe.sh")
|| entry.file_name() == OsStr::new("recipe.toml")
{
// println!("recipe is {:?}", dir.file_name());
if dir.file_name().unwrap().to_string_lossy() != recipe {
if dir.file_name().unwrap() != OsStr::new(recipe) {
return Ok(None);
} else {
return Ok(Some(dir.to_path_buf()));
@ -51,8 +52,8 @@ pub fn list_recipes(dir: &Path) -> Result<Vec<String>, String> {
}
for entry in fs::read_dir(dir).map_err(|e| e.to_string())? {
let entry = entry.map_err(|e| e.to_string())?;
if entry.file_name().to_string_lossy() == "recipe.sh"
|| entry.file_name().to_string_lossy() == "recipe.toml"
if entry.file_name() == OsStr::new("recipe.sh")
|| entry.file_name() == OsStr::new("recipe.toml")
{
recipes.push(dir.file_name().ok_or(format!("could not unwrap the filename for {:?}", dir))?.to_string_lossy().to_string());
return Ok(recipes);

View File

@ -1,45 +0,0 @@
use std::{
fs,
io::{Read, Result},
path::Path,
time::Duration,
};
use sha2::{Digest, Sha256};
use crate::progress_bar::{ProgressBar, ProgressBarRead};
pub fn sha256<R: Read>(r: &mut R) -> Result<String> {
let mut hasher = Sha256::default();
let mut data = vec![0; 4 * 1024 * 1024];
loop {
let count = r.read(&mut data)?;
if count == 0 {
break;
}
hasher.update(&data[..count]);
}
Ok(format!("{:x}", hasher.finalize()))
}
pub fn sha256_progress<P: AsRef<Path>>(path: P) -> Result<String> {
let len = fs::metadata(&path)?.len();
let mut f = fs::File::open(&path)?;
let mut pb = ProgressBar::new(len);
pb.message("sha256: ");
pb.set_max_refresh_rate(Some(Duration::new(1, 0)));
pb.set_units(pbr::Units::Bytes);
let res = {
let mut pbr = ProgressBarRead::new(&mut pb, &mut f);
sha256(&mut pbr)
};
pb.finish_println("");
res
}