v0.2
This commit is contained in:
173
src/zspatch.rs
173
src/zspatch.rs
@@ -1,80 +1,164 @@
|
||||
mod utils;
|
||||
|
||||
use clap::Parser;
|
||||
use std::fs::read;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use std::{fs, io, time};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{io, time};
|
||||
use tokio::fs;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use utils::Zsdiff;
|
||||
|
||||
async fn create_tmp_dir(dir_name: String) -> Result<String, io::Error> {
|
||||
let name = format!("{}.tmp", dir_name);
|
||||
fs::remove_dir_all(name.clone()).ok();
|
||||
fs::DirBuilder::new().create(name.clone())?;
|
||||
Ok(name)
|
||||
let name = PathBuf::from(format!("{}_tmp", dir_name));
|
||||
if name.exists() {
|
||||
fs::remove_dir_all(&name).await?;
|
||||
}
|
||||
fs::create_dir(&name).await?;
|
||||
name.to_str().map(|s| s.to_string()).ok_or_else(|| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"Path contains invalid UTF-8 characters",
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
async fn load_file(filename: String) -> Result<Zsdiff, io::Error> {
|
||||
let filename = &format!("{}.zdiff", filename);
|
||||
let parts = utils::decompress_parts(read(filename)?).await?;
|
||||
Ok(Zsdiff::from_vec(parts).await?)
|
||||
let full_filename = format!("{}.zdiff", filename);
|
||||
let compressed_data = fs::read(&full_filename).await?;
|
||||
let data = utils::decompress(compressed_data).await?;
|
||||
let zsdiff = Zsdiff::from_vec(data).await?;
|
||||
println!(
|
||||
">>> Metadata files to remove: {}",
|
||||
zsdiff.metadata.remove_files.len()
|
||||
);
|
||||
println!(
|
||||
">>> Metadata hashes to check: {}",
|
||||
zsdiff.metadata.hashes.len()
|
||||
);
|
||||
|
||||
Ok(zsdiff)
|
||||
}
|
||||
|
||||
async fn extract_files(zsdiff: &Zsdiff, filename: String) -> Result<String, io::Error> {
|
||||
let tmp_dir_name = create_tmp_dir(filename.to_string()).await?;
|
||||
let path = Path::new(&tmp_dir_name);
|
||||
fs::remove_dir_all(path).ok();
|
||||
for (f, c) in zsdiff.content.iter() {
|
||||
|
||||
for (i, (f, c)) in zsdiff.content.iter().enumerate() {
|
||||
println!(
|
||||
">>> Processing file {}/{}: '{}'",
|
||||
i + 1,
|
||||
zsdiff.content.len(),
|
||||
f
|
||||
);
|
||||
let filepath = path.join(f);
|
||||
fs::create_dir_all(filepath.parent().unwrap())?;
|
||||
fs::File::create(&filepath)?.write_all(c)?;
|
||||
if let Some(parent) = filepath.parent() {
|
||||
fs::create_dir_all(parent).await?;
|
||||
}
|
||||
|
||||
let mut file = fs::File::create(&filepath).await?;
|
||||
file.write_all(c).await?;
|
||||
}
|
||||
Ok(tmp_dir_name)
|
||||
}
|
||||
|
||||
async fn check_hash(filename: String) -> Result<(), io::Error> {
|
||||
let file_data = read(format!("{}.zdiff", filename))?;
|
||||
let hash_file = String::from_utf8(read(format!("{}.zdiff.md5", filename))?).unwrap();
|
||||
let file_data = fs::read(format!("{}.zdiff", filename)).await?;
|
||||
let mut hash_file =
|
||||
String::from_utf8(fs::read(format!("{}.zdiff.md5", filename)).await?).unwrap();
|
||||
let hash = utils::get_hash(file_data).await;
|
||||
if !hash_file.split(" ").next().unwrap().eq(&hash) {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "Hash mismatch"));
|
||||
hash_file = hash_file.split(" ").next().unwrap().parse().unwrap();
|
||||
if !hash_file.eq(&hash) {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("Hash mismatch. Expected {}, got {}", hash_file, hash),
|
||||
));
|
||||
}
|
||||
println!("Zsdiff hash: {}", hash);
|
||||
println!(">>> Zsdiff hash: {}", hash);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn zspatch(filename: String, dest_dir: String) -> Result<(), io::Error> {
|
||||
let diff = load_file(filename.clone()).await?;
|
||||
let cloned = filename.clone();
|
||||
let diff = load_file(cloned).await.ok().unwrap();
|
||||
let tmp_dir_name = extract_files(&diff, filename).await?;
|
||||
let now = time::Instant::now();
|
||||
for name in diff.content.keys().collect::<Vec<&String>>() {
|
||||
|
||||
fs::File::create("metadata.json")
|
||||
.await?
|
||||
.write_all(serde_json::to_vec(&diff.metadata).unwrap().as_slice())
|
||||
.await?;
|
||||
let files_to_copy: Vec<String> = diff.content.keys().cloned().collect();
|
||||
for (_, name) in files_to_copy.iter().enumerate() {
|
||||
let from_path = Path::new(&tmp_dir_name).join(name);
|
||||
let to_path = Path::new(&dest_dir).join(name);
|
||||
fs::create_dir_all(to_path.parent().unwrap())?;
|
||||
fs::copy(from_path, to_path)?;
|
||||
|
||||
if !from_path.exists() {
|
||||
println!("ERROR: Source file doesn't exist: {:?}", from_path);
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(parent) = to_path.parent() {
|
||||
fs::create_dir_all(parent).await?;
|
||||
}
|
||||
|
||||
fs::copy(from_path.clone(), to_path.clone()).await?;
|
||||
}
|
||||
|
||||
for file in diff.metadata.remove_files {
|
||||
for file in &diff.metadata.remove_files {
|
||||
let path = Path::new(&dest_dir).join(file);
|
||||
fs::remove_file(path).ok();
|
||||
println!(">>> Removing file {}", path.display());
|
||||
|
||||
if !path.exists() {
|
||||
println!("File doesn't exist, skipping");
|
||||
continue;
|
||||
}
|
||||
|
||||
fs::remove_file(path.clone()).await?
|
||||
}
|
||||
|
||||
for folder in diff.metadata.remove_folders {
|
||||
println!(">>> Starting folder removal process <<<");
|
||||
println!(
|
||||
">>> Folders to remove: {}",
|
||||
diff.metadata.remove_folders.len()
|
||||
);
|
||||
|
||||
for folder in &diff.metadata.remove_folders {
|
||||
let path = Path::new(&dest_dir).join(folder);
|
||||
fs::remove_dir_all(path).ok();
|
||||
|
||||
if !path.exists() {
|
||||
println!("Folder doesn't exist, skipping");
|
||||
continue;
|
||||
}
|
||||
|
||||
fs::remove_dir_all(path.clone()).await?
|
||||
}
|
||||
|
||||
for (k, hash) in diff.metadata.hashes {
|
||||
println!(">>> Starting hash verification <<<");
|
||||
println!(">>> Files to verify: {}", diff.metadata.hashes.len());
|
||||
|
||||
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;
|
||||
if !fs_hash.eq(&hash) {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "Hash mismatch"))?
|
||||
|
||||
match fs::read(path.clone()).await {
|
||||
Ok(content) => {
|
||||
let fs_hash = utils::get_hash(content).await;
|
||||
if !fs_hash.eq(hash) {
|
||||
println!(
|
||||
"Hash mismatch. Expected {}, got {}. Path: {}",
|
||||
hash,
|
||||
fs_hash,
|
||||
path.display()
|
||||
);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Can't read file for hash verification: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
fs::remove_dir_all(tmp_dir_name).ok();
|
||||
println!("Patching done!");
|
||||
println!("Elapsed time: {:.2?}", now.elapsed());
|
||||
|
||||
// fs::remove_dir_all(tmp_dir_name).await?;
|
||||
println!(">>> Patching done! <<<");
|
||||
println!(">>> Elapsed time: {:.2?}", now.elapsed());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -87,19 +171,24 @@ struct Args {
|
||||
#[arg(short, long)]
|
||||
metadata: bool,
|
||||
#[arg(short, long)]
|
||||
hash_check: bool,
|
||||
check_hash: bool,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
let args = Args::parse();
|
||||
if args.hash_check {
|
||||
check_hash(args.filename.clone()).await?;
|
||||
|
||||
let filename = args.filename.clone();
|
||||
let dest_dir = args.dest_dir.clone();
|
||||
|
||||
if args.check_hash {
|
||||
check_hash(args.filename.clone()).await.ok();
|
||||
}
|
||||
if args.metadata {
|
||||
let diff = load_file(args.filename).await?;
|
||||
println!("{}", serde_json::to_string(&diff.metadata)?);
|
||||
let diff = load_file(filename).await?;
|
||||
let metadata = diff.metadata;
|
||||
println!(">>> Compress level: {}", metadata.compress_level);
|
||||
return Ok(());
|
||||
}
|
||||
zspatch(args.filename, args.dest_dir).await
|
||||
zspatch(filename, dest_dir).await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user