From 01e6e75645d5c5a611efde957eff0621ad5b1bca Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 9 Jun 2020 17:05:08 +0200 Subject: verify that zip has right content --- src/main.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'src') 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 { + 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(()); -- cgit v1.2.3