diff --git a/src/archive.rs b/src/archive.rs index cf4edb5..ff318ee 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -29,14 +29,14 @@ pub fn metadata<'a>( let metadata = match format { Formats::Zip => { let metadata = formats::zip::parser::metadata(&mut file); - if check_integrity { - if !formats::zip::parser::check_integrity_all( + if check_integrity + && !formats::zip::parser::check_integrity_all( &mut file, &metadata.files, &buffer_size, - ) { - return Err("Integrity check failed".to_string()); - } + ) + { + return Err("Integrity check failed".to_string()); } OriginalArchiveMetadata::Zip(metadata) } @@ -61,14 +61,14 @@ pub fn extract( let metadata: &dyn ArchiveMetadata = match format { Formats::Zip => { let metadata = formats::zip::parser::metadata(&mut file); - if check_integrity { - if !formats::zip::parser::check_integrity_all( + if check_integrity + && !formats::zip::parser::check_integrity_all( &mut file, &metadata.files, &buffer_size, - ) { - return Err("Integrity check failed".to_string()); - } + ) + { + return Err("Integrity check failed".to_string()); } &metadata.clone() as &dyn ArchiveMetadata } @@ -85,35 +85,33 @@ pub fn extract( }); } } - } else { - if index != None { - let index = index.unwrap(); - if index >= files.len() as u32 { - return Err("Index out of range".to_string()); - } - formats::zip::parser::extract( - &mut file, - &formats::zip::to_zip_entries(files), - &buffer_size, - &|path| format!("{}/{}", &output, &path), - ); - } else { - let path = path.unwrap(); - let files: Vec = metadata - .get_files() - .iter() - .filter_map(|file| { - if file.get_path().starts_with(&path) { - Some(formats::zip::to_zip_entry(*file)) - } else { - None - } - }) - .collect(); - formats::zip::parser::extract(&mut file, &files, &buffer_size, &|path| { - format!("{}/{}", &output, &path) - }); + } else if index.is_some() { + let index = index.unwrap(); + if index >= files.len() as u32 { + return Err("Index out of range".to_string()); } + formats::zip::parser::extract( + &mut file, + &formats::zip::to_zip_entries(files), + &buffer_size, + &|path| format!("{}/{}", &output, &path), + ); + } else { + let path = path.unwrap(); + let files: Vec = metadata + .get_files() + .iter() + .filter_map(|file| { + if file.get_path().starts_with(&path) { + Some(formats::zip::to_zip_entry(*file)) + } else { + None + } + }) + .collect(); + formats::zip::parser::extract(&mut file, &files, &buffer_size, &|path| { + format!("{}/{}", &output, &path) + }); }; Ok(()) @@ -127,7 +125,7 @@ pub struct EntrySource<'a> { pub fn create( format: Formats, output: String, - input: &mut Vec, + input: &mut [EntrySource], buffer_size: u64, ) -> Result<(), String> { let mut file = FileWriter::new(&output, &false); diff --git a/src/file.rs b/src/file.rs index 9b8b453..4da6a99 100644 --- a/src/file.rs +++ b/src/file.rs @@ -105,7 +105,7 @@ impl<'a> FileReader { let mut remaining = *len; while remaining > 0 { - let to_read = min(*buffer_size as u64, remaining) as usize; + let to_read = min(*buffer_size, remaining) as usize; let read = self.read(&mut buf[..to_read]); target.write(read); remaining -= to_read as u64; @@ -122,7 +122,7 @@ impl<'a> FileReader { Times { created: metadata .created() - .unwrap_or_else(|_| metadata.modified().unwrap().into()) + .unwrap_or_else(|_| metadata.modified().unwrap()) .into(), accessed: metadata.accessed().unwrap().into(), modified: metadata.modified().unwrap().into(), @@ -238,7 +238,6 @@ impl<'a> FileWriter { pub fn new(path: &'a String, append: &bool) -> Self { if *append { let mut file = OpenOptions::new() - .write(true) .create(true) .append(true) .open(path) @@ -254,6 +253,7 @@ impl<'a> FileWriter { let mut file = OpenOptions::new() .write(true) .create(true) + .truncate(true) .open(path) .unwrap(); file.rewind().unwrap(); diff --git a/src/formats.rs b/src/formats.rs index 1241e31..ce14a5f 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -6,8 +6,8 @@ pub enum Formats { Zip, } -pub fn from_string(format: &String) -> Formats { - match format.as_str() { +pub fn from_string(format: &str) -> Formats { + match format { "zip" => Formats::Zip, _ => panic!("Unsupported format"), } diff --git a/src/formats/zip.rs b/src/formats/zip.rs index 54aacd1..41e86e2 100644 --- a/src/formats/zip.rs +++ b/src/formats/zip.rs @@ -98,7 +98,7 @@ impl<'a> FileEntry<'a> for ZipFileEntry<'a> { } fn get_original(&'a self) -> OriginalFileEntry<'a> { - OriginalFileEntry::Zip(&self) + OriginalFileEntry::Zip(self) } } diff --git a/src/formats/zip/parser.rs b/src/formats/zip/parser.rs index d51dc0b..57da620 100644 --- a/src/formats/zip/parser.rs +++ b/src/formats/zip/parser.rs @@ -36,7 +36,7 @@ pub fn extract( &entry.offset, &entry.size, &mut target, - &entry.modified.into(), + &entry.modified, buffer_size, ); } else { diff --git a/src/formats/zip/writer.rs b/src/formats/zip/writer.rs index d5067d3..41fdb44 100644 --- a/src/formats/zip/writer.rs +++ b/src/formats/zip/writer.rs @@ -39,7 +39,7 @@ pub fn write(target: &mut FileWriter, data: &mut ZipArchiveData, buffer_size: &u &file.offset, &file.size, target, - &file.modified.into(), + &file.modified, buffer_size, ); } diff --git a/src/helpers/hash/crc32.rs b/src/helpers/hash/crc32.rs index 4a0f5a8..10ccde1 100644 --- a/src/helpers/hash/crc32.rs +++ b/src/helpers/hash/crc32.rs @@ -14,9 +14,9 @@ pub fn hash(file: &mut FileReader, offset: &u64, size: &u64, buffer_size: &u64) let mut remaining = *size; while remaining > 0 { - let to_read = min(*buffer_size as u64, remaining) as usize; + let to_read = min(*buffer_size, remaining) as usize; let read = file.read(&mut buf[..to_read]); - hasher.update(&read); + hasher.update(read); remaining -= to_read as u64; } diff --git a/src/lib.rs b/src/lib.rs index 9bdf20b..8ae2795 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(clippy::too_many_arguments)] + pub mod archive; pub mod file; pub mod formats; diff --git a/tests/other.rs b/tests/other.rs index 7c728e1..ad1b9ab 100644 --- a/tests/other.rs +++ b/tests/other.rs @@ -1,5 +1,3 @@ -use corelib; - #[test] fn returns_real_version() { assert_eq!(corelib::get_version(), env!("CARGO_PKG_VERSION")); diff --git a/tests/zip-external.rs b/tests/zip-external.rs index 1cdae52..6bd3eb9 100644 --- a/tests/zip-external.rs +++ b/tests/zip-external.rs @@ -6,16 +6,13 @@ use corelib::{ #[test] fn sample_000_metadata() { - let metadata = match *archive::metadata( + let OriginalArchiveMetadata::Zip(metadata) = *archive::metadata( Formats::Zip, "tests/samples/zip/000.zip".to_string(), true, 1024, ) - .unwrap() - { - OriginalArchiveMetadata::Zip(metadata) => metadata, - }; + .unwrap(); assert_eq!(metadata.files.len(), 1); assert_eq!(metadata.files[0].path, "test.txt"); assert_eq!(metadata.files[0].size, 14); @@ -49,16 +46,13 @@ fn sample_000_extract() { #[test] fn sample_001_metadata() { - let metadata = match *archive::metadata( + let OriginalArchiveMetadata::Zip(metadata) = *archive::metadata( Formats::Zip, "tests/samples/zip/001.zip".to_string(), true, 1024, ) - .unwrap() - { - OriginalArchiveMetadata::Zip(metadata) => metadata, - }; + .unwrap(); assert_eq!(metadata.files.len(), 2); assert_eq!(metadata.files[0].path, "test.txt"); assert_eq!(metadata.files[0].size, 14); @@ -103,16 +97,13 @@ fn sample_001_extract() { #[test] fn sample_002_metadata() { - let metadata = match *archive::metadata( + let OriginalArchiveMetadata::Zip(metadata) = *archive::metadata( Formats::Zip, "tests/samples/zip/002.zip".to_string(), true, 1024, ) - .unwrap() - { - OriginalArchiveMetadata::Zip(metadata) => metadata, - }; + .unwrap(); assert_eq!(metadata.files.len(), 3); assert_eq!(metadata.files[0].path, "test/"); assert_eq!(metadata.files[0].size, 0); @@ -176,7 +167,7 @@ fn create_000_metadata() { archive::create( Formats::Zip, "tests/samples/zip/c000-external.zip".to_string(), - &mut vec![EntrySource { + &mut [EntrySource { path: "test.txt".to_string(), source: &mut FsFile::new(&"tests/samples/zip/c000-external/test.txt".to_string()), }], @@ -186,16 +177,13 @@ fn create_000_metadata() { std::fs::remove_dir_all("tests/samples/zip/c000-external").unwrap(); - let metadata = match *archive::metadata( + let OriginalArchiveMetadata::Zip(metadata) = *archive::metadata( Formats::Zip, "tests/samples/zip/c000-external.zip".to_string(), true, 1024, ) - .unwrap() - { - OriginalArchiveMetadata::Zip(metadata) => metadata, - }; + .unwrap(); assert_eq!(metadata.files.len(), 1); assert_eq!(metadata.files[0].path, "test.txt"); @@ -227,7 +215,7 @@ fn create_000_extract() { archive::create( Formats::Zip, "tests/samples/zip/c000-external2.zip".to_string(), - &mut vec![EntrySource { + &mut [EntrySource { path: "test.txt".to_string(), source: &mut FsFile::new(&"tests/samples/zip/c000-external2/test.txt".to_string()), }],