patch hash check
This commit is contained in:
@@ -26,7 +26,17 @@ async fn extract_files(zsdiff: &Zsdiff, filename: &String) -> Result<String, io:
|
||||
Ok(tmp_dir_name)
|
||||
}
|
||||
|
||||
async fn zpatch(filename: String, dest_dir: String) -> Result<(), io::Error> {
|
||||
async fn check_hash(filename: String) -> Result<(), io::Error> {
|
||||
let file_data = read(format!("{}.zdiff", filename))?;
|
||||
let hash_file = read(format!("{}.zdiff.md5", filename))?;
|
||||
let hash = utils::get_hash(file_data).await;
|
||||
if !String::from_utf8(hash_file).unwrap().eq(&hash) {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "Hash mismatch"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn zspatch(filename: String, dest_dir: String) -> Result<(), io::Error> {
|
||||
let filename = &format!("{}.zdiff", filename);
|
||||
let parts = utils::decompress_parts(read(filename)?).await?;
|
||||
let diff = Zsdiff::from_vec(parts).await?;
|
||||
@@ -46,11 +56,12 @@ async fn zpatch(filename: String, dest_dir: String) -> Result<(), io::Error> {
|
||||
for (k, hash) in diff.metadata.hashes {
|
||||
let path = Path::new(&dest_dir).join(k);
|
||||
let content = read(path)?;
|
||||
let fs_hash = utils::get_hash(&content).await;
|
||||
let fs_hash = utils::get_hash(content).await;
|
||||
if !fs_hash.eq(&hash) {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "Hash mismatch"))?
|
||||
}
|
||||
}
|
||||
fs::remove_dir_all(tmp_dir_name).ok();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -60,11 +71,16 @@ struct Args {
|
||||
filename: String,
|
||||
#[arg(short, long)]
|
||||
dest_dir: String,
|
||||
#[arg(short, long)]
|
||||
hash_check: bool,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
let args = Args::parse();
|
||||
zpatch(args.filename, args.dest_dir).await?;
|
||||
Ok(())
|
||||
println!("{}", args.hash_check);
|
||||
if args.hash_check {
|
||||
check_hash(args.filename.clone()).await?;
|
||||
}
|
||||
zspatch(args.filename, args.dest_dir).await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user