initial commit
This commit is contained in:
53
src/main.rs
Normal file
53
src/main.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
mod utils;
|
||||
mod zdiff;
|
||||
mod zpatch;
|
||||
|
||||
use std::fs;
|
||||
use std::fs::read;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
|
||||
async fn zdiff(filename: &str, old: &str, new: &str) -> Result<(), io::Error> {
|
||||
let output_filename = &format!("{}.zdiff", filename);
|
||||
let old_hashes = zdiff::walk_dir(old.to_string()).await;
|
||||
let new_hashes = zdiff::walk_dir(new.to_string()).await;
|
||||
let compare_hashes = zdiff::compare_hashes(old_hashes, new_hashes).await;
|
||||
let parts = compare_hashes.to_vec().await;
|
||||
utils::compress_parts(parts, fs::File::create(output_filename)?, 11).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn zpatch(filename: &str, dest_dir: &str) -> Result<(), io::Error> {
|
||||
let filename = &format!("{}.zdiff", filename);
|
||||
let parts = utils::decompress_parts(read(filename)?).await?;
|
||||
let zdiff = zdiff::Zdiff::from_vec(parts).await?;
|
||||
let tmp_dir_name = zpatch::extract_files(&zdiff, filename).await?;
|
||||
for name in zdiff.content.keys().collect::<Vec<&String>>() {
|
||||
let from_path = Path::new(&tmp_dir_name).join(name);
|
||||
let to_path = Path::new(&dest_dir).join(name);
|
||||
// println!("{:?} {:?}", from_path, to_path);
|
||||
fs::create_dir_all(to_path.parent().unwrap())?;
|
||||
fs::copy(from_path, to_path)?;
|
||||
}
|
||||
for file in zdiff.metadata.remove_files {
|
||||
let path = Path::new(&dest_dir).join(file);
|
||||
fs::remove_file(path)?;
|
||||
}
|
||||
|
||||
for (k, hash) in zdiff.metadata.hashes {
|
||||
let path = Path::new(&dest_dir).join(k);
|
||||
println!("path: {:?}", path);
|
||||
let content = read(path)?;
|
||||
let fs_hash = zdiff::get_hash(&content).await;
|
||||
println!("{:?} {:?}", hash, fs_hash);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
let filename = "test";
|
||||
zdiff(filename, "test/old", "test/new").await?;
|
||||
zpatch(filename, "old").await?;
|
||||
Ok(())
|
||||
}
|
||||
33
src/utils.rs
Normal file
33
src/utils.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use std::{fs, io};
|
||||
use zstd::{Decoder, Encoder};
|
||||
|
||||
pub async fn compress_parts(input: Vec<Vec<u8>>, output: fs::File, level: i32) {
|
||||
let mut encoder = Encoder::new(output, level).unwrap();
|
||||
for part in input.iter() {
|
||||
io::copy(&mut &part[..], &mut encoder).unwrap();
|
||||
}
|
||||
encoder.finish().unwrap();
|
||||
}
|
||||
|
||||
pub async fn decompress_parts(input: Vec<u8>) -> Result<Vec<Vec<u8>>, io::Error> {
|
||||
let mut decoder = Decoder::new(&input[..])?;
|
||||
let mut buf = Vec::new();
|
||||
|
||||
io::copy(&mut decoder, &mut buf)?;
|
||||
let mut index = 0;
|
||||
let mut parts: Vec<Vec<u8>> = Vec::new();
|
||||
|
||||
while index < buf.len() {
|
||||
let filename_size = u32::from_be_bytes(buf[index..index + 4].try_into().unwrap()) as usize;
|
||||
let filename = buf[index..index + filename_size + 4].to_vec();
|
||||
index += 4 + filename_size;
|
||||
|
||||
let content_size = u32::from_be_bytes(buf[index..index + 4].try_into().unwrap()) as usize;
|
||||
let content = buf[index..index + content_size + 4].to_vec();
|
||||
index += content_size + 4;
|
||||
|
||||
let part = vec![filename, content].concat();
|
||||
parts.push(part);
|
||||
}
|
||||
Ok(parts)
|
||||
}
|
||||
127
src/zdiff.rs
Normal file
127
src/zdiff.rs
Normal file
@@ -0,0 +1,127 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use walkdir::WalkDir;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Zdiff {
|
||||
pub content: HashMap<String, Vec<u8>>,
|
||||
pub metadata: Metadata,
|
||||
}
|
||||
|
||||
impl Zdiff {
|
||||
pub async fn from_vec(_data: Vec<Vec<u8>>) -> Result<Self, std::io::Error> {
|
||||
let mut content = HashMap::new();
|
||||
for part in _data {
|
||||
let filename_size = u32::from_be_bytes(part[0..4].try_into().unwrap()) as usize;
|
||||
let filename = String::from_utf8(part[4..filename_size + 4].to_vec()).unwrap();
|
||||
let cont = part[filename_size + 8..].to_vec();
|
||||
content.insert(filename, cont);
|
||||
}
|
||||
let meta = content.get("metadata.json").unwrap();
|
||||
let metadata: Metadata = serde_json::from_slice(meta.as_slice())?;
|
||||
content.remove("metadata.json");
|
||||
|
||||
Ok(Zdiff { content, metadata })
|
||||
}
|
||||
|
||||
pub async fn to_vec(&self) -> Vec<Vec<u8>> {
|
||||
let mut parts: Vec<Vec<u8>> = Vec::new();
|
||||
for (filename, content) in &self.content {
|
||||
let filename_size: [u8; 4] = (filename.len() as u32).to_be_bytes();
|
||||
let filename_encoded = vec![filename_size.as_slice(), filename.as_bytes()].concat();
|
||||
|
||||
let content_size: [u8; 4] = (content.len() as u32).to_be_bytes();
|
||||
let content_encoded = vec![content_size.as_slice(), content.as_slice()].concat();
|
||||
parts.push(vec![filename_encoded, content_encoded].concat())
|
||||
}
|
||||
|
||||
let meta = serde_json::to_vec(&self.metadata).unwrap();
|
||||
let meta_filename = "metadata.json";
|
||||
let meta_filename_size = (meta_filename.len() as u32).to_be_bytes();
|
||||
let meta_filename_encoded =
|
||||
vec![meta_filename_size.as_slice(), meta_filename.as_bytes()].concat();
|
||||
|
||||
let meta_size = (meta.len() as u32).to_be_bytes();
|
||||
let meta_encoded = vec![meta_size.as_slice(), meta.as_slice()].concat();
|
||||
parts.push(vec![meta_filename_encoded, meta_encoded].concat());
|
||||
|
||||
parts
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Metadata {
|
||||
diff_files: Vec<String>,
|
||||
pub hashes: HashMap<String, String>,
|
||||
pub remove_files: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FileInfo {
|
||||
path: String,
|
||||
relative_path: String, // Without dir prefix
|
||||
hash: String,
|
||||
}
|
||||
|
||||
pub async fn get_hash(data: &Vec<u8>) -> String {
|
||||
let hash = md5::compute(&data[..]);
|
||||
format!("{:x}", hash)
|
||||
}
|
||||
|
||||
pub async fn walk_dir(dir: String) -> HashMap<String, FileInfo> {
|
||||
let mut hash_list: HashMap<String, FileInfo> = HashMap::new();
|
||||
for e in WalkDir::new(&dir) {
|
||||
let e = e.unwrap();
|
||||
let path = e.path();
|
||||
if path.is_dir() {
|
||||
continue;
|
||||
}
|
||||
let content = fs::read(path).unwrap();
|
||||
let hash = get_hash(&content).await;
|
||||
// let filename = path.file_name().unwrap().to_str().unwrap().to_string();
|
||||
let path_str = path.display().to_string();
|
||||
let file_info = FileInfo {
|
||||
relative_path: path_str[dir.len() + 1..].to_string(),
|
||||
path: path_str,
|
||||
hash: hash.clone(),
|
||||
};
|
||||
hash_list.insert(hash, file_info);
|
||||
}
|
||||
hash_list
|
||||
}
|
||||
|
||||
pub async fn compare_hashes(
|
||||
old: HashMap<String, FileInfo>,
|
||||
new: HashMap<String, FileInfo>,
|
||||
) -> Zdiff {
|
||||
let mut diff_files: HashMap<String, Vec<u8>> = HashMap::new();
|
||||
let mut remove_files: Vec<String> = vec![];
|
||||
let mut hashes: HashMap<String, String> = HashMap::new();
|
||||
for (_, info) in &old {
|
||||
remove_files.push(info.relative_path.clone());
|
||||
}
|
||||
|
||||
for (new_hash, new_fileinfo) in &new {
|
||||
let old_fileinfo = old.get(new_hash);
|
||||
remove_files.retain(|filename| !filename.eq(&new_fileinfo.relative_path));
|
||||
|
||||
if old_fileinfo.is_none() {
|
||||
let path = new_fileinfo.relative_path.clone();
|
||||
diff_files.insert(path.clone(), fs::read(new_fileinfo.path.clone()).unwrap());
|
||||
hashes.insert(
|
||||
new_fileinfo.relative_path.clone(),
|
||||
new_fileinfo.hash.clone(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Zdiff {
|
||||
content: diff_files.clone(),
|
||||
metadata: Metadata {
|
||||
diff_files: diff_files.keys().cloned().collect(),
|
||||
hashes,
|
||||
remove_files,
|
||||
},
|
||||
}
|
||||
}
|
||||
24
src/zpatch.rs
Normal file
24
src/zpatch.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use crate::zdiff::Zdiff;
|
||||
use crate::zpatch;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
|
||||
pub async fn create_tmp_dir(dir_name: String) -> Result<String, std::io::Error> {
|
||||
let name = format!("{}.tmp", dir_name);
|
||||
fs::remove_dir_all(name.clone()).map_err(|_| std::io::ErrorKind::NotFound)?;
|
||||
fs::DirBuilder::new().create(name.clone())?;
|
||||
Ok(name)
|
||||
}
|
||||
|
||||
pub async fn extract_files(zdiff: &Zdiff, filename: &String) -> Result<String, std::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)?;
|
||||
for (f, c) in zdiff.content.iter() {
|
||||
let filepath = path.join(f);
|
||||
fs::create_dir_all(filepath.parent().unwrap())?;
|
||||
fs::File::create(&filepath)?.write_all(c)?;
|
||||
}
|
||||
Ok(tmp_dir_name)
|
||||
}
|
||||
Reference in New Issue
Block a user