diff options
author | Daniel Schadt <kingdread@gmx.de> | 2020-06-09 17:05:08 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2020-06-09 17:05:08 +0200 |
commit | 01e6e75645d5c5a611efde957eff0621ad5b1bca (patch) | |
tree | b93618253c90eadd641ad711b97a187b20a38ee9 /src | |
parent | 679addb51e3ac92702c22d643b6ec1df143cf4ac (diff) | |
download | ezau-01e6e75645d5c5a611efde957eff0621ad5b1bca.tar.gz ezau-01e6e75645d5c5a611efde957eff0621ad5b1bca.tar.bz2 ezau-01e6e75645d5c5a611efde957eff0621ad5b1bca.zip |
verify that zip has right content
Diffstat (limited to 'src')
-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(()); |