diff options
-rw-r--r-- | src/main.rs | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 199cd9d..ff86f4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::{ fs::{self, File}, - io::{BufWriter, Write}, + io::{BufReader, BufWriter, Read, Write}, path::{Path, PathBuf}, sync::mpsc::channel, time::Duration, @@ -9,11 +9,11 @@ use std::{ use anyhow::{anyhow, Result}; use clap::Clap; use evtclib::{Boss, Compression, Log}; -use log::{debug, error, info}; +use log::{debug, error, info, warn}; use notify::{self, DebouncedEvent, RecursiveMode, Watcher}; use regex::Regex; use serde::Deserialize; -use zip::{CompressionMethod, ZipWriter}; +use zip::{CompressionMethod, ZipArchive, ZipWriter}; mod categories; use categories::Categorizable; @@ -120,12 +120,27 @@ fn zip_file(filepath: &Path) -> Result<()> { zip.start_file(filename, options)?; zip.write_all(&evtc_content)?; - zip.finish()?; + zip.finish()?.flush()?; + + if !verify_zip(filepath, &outname)? { + warn!("ZIP content mismatch, keeping original file"); + return Ok(()); + } fs::remove_file(filepath)?; Ok(()) } +fn verify_zip(original: &Path, zip_path: &Path) -> Result<bool> { + let expected_content = fs::read(original)?; + let mut archive = ZipArchive::new(BufReader::new(File::open(zip_path)?))?; + let mut inner = archive.by_index(0)?; + let mut actual_content = Vec::new(); + inner.read_to_end(&mut actual_content)?; + + Ok(expected_content == actual_content) +} + fn handle_file(config: &Config, filename: &Path) -> Result<()> { if !config.upload { return Ok(()); |