Skip to content

Commit

Permalink
fix!: Fixed #23
Browse files Browse the repository at this point in the history
  • Loading branch information
Le0X8 committed Aug 2, 2024
1 parent b152597 commit 6ecbf09
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 32 deletions.
24 changes: 17 additions & 7 deletions src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,33 @@ pub fn extract(
Ok(())
}

pub struct EntrySource<'a> {
pub struct EntrySource {
pub path: String,
pub source: &'a mut FsFile,
pub source: FsFile,
}

impl Clone for EntrySource {
fn clone(&self) -> Self {
Self {
path: self.path.clone(),
source: self.source.clone(),
}
}
}

pub fn create(
format: Formats,
output: String,
input: &mut [EntrySource],
input: Vec<EntrySource>,
buffer_size: u64,
) -> Result<(), String> {
let mut file = FileWriter::new(&output, &false);

match format {
Formats::Zip => {
let files: Vec<ZipFile> = input
.iter_mut()
.iter()
.cloned()
.map(|entry| {
if entry.source.is_directory {
return ZipFile {
Expand All @@ -147,9 +157,9 @@ pub fn create(
};
};
let size = entry.source.size.to_owned();
let reader = entry.source.reader.as_mut().unwrap();
let mut reader = entry.source.reader.unwrap();
ZipFile {
checksum: crc32::hash(reader, &0, &size, &buffer_size),
checksum: crc32::hash(&mut reader, &0, &size, &buffer_size),
path: entry.path.clone(),
offset: 0,
size,
Expand All @@ -161,7 +171,7 @@ pub fn create(
.collect();
formats::zip::writer::write(
&mut file,
&mut formats::zip::ZipArchiveData { files },
formats::zip::ZipArchiveData { files },
&buffer_size,
);
}
Expand Down
21 changes: 21 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ pub struct FsFile {
pub is_directory: bool,
}

impl Clone for FsFile {
fn clone(&self) -> Self {
Self {
size: self.size,
reader: self.reader.clone(),
modified: self.modified,
is_directory: self.is_directory,
}
}
}

impl FsFile {
pub fn new(path: &String) -> Self {
if fs::metadata(path).unwrap().is_dir() {
Expand Down Expand Up @@ -227,6 +238,16 @@ impl<'a> FileReader {
}
}

impl Clone for FileReader {
fn clone(&self) -> Self {
Self {
path: self.path.clone(),
file: OpenOptions::new().read(true).open(&self.path).unwrap(),
pos: self.pos,
}
}
}

#[derive(Debug)]
pub struct FileWriter {
path: String,
Expand Down
12 changes: 6 additions & 6 deletions src/formats/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ pub fn to_zip_entries<'a>(from: Vec<&'a (dyn FileEntry<'a> + 'a)>) -> Vec<ZipFil
}

#[derive(Debug)]
pub struct ZipFile<'a> {
pub struct ZipFile {
pub path: String,
pub offset: u64,
pub size: u64,
pub modified: DateTime<Utc>,
pub is_directory: bool,
pub source: Option<&'a mut FileReader>,
pub source: Option<FileReader>,
pub checksum: u32,
}

impl File for ZipFile<'_> {
impl File for ZipFile {
fn get_path(&self) -> &String {
&self.path
}
Expand All @@ -174,7 +174,7 @@ impl File for ZipFile<'_> {

fn get_source(&mut self) -> Option<&mut FileReader> {
match &mut self.source {
Some(source) => Some(*source),
Some(source) => Some(source),
None => None,
}
}
Expand All @@ -185,6 +185,6 @@ impl File for ZipFile<'_> {
}

#[derive(Debug)]
pub struct ZipArchiveData<'a> {
pub files: Vec<ZipFile<'a>>,
pub struct ZipArchiveData {
pub files: Vec<ZipFile>,
}
8 changes: 5 additions & 3 deletions src/formats/zip/writer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::ZipArchiveData;
use crate::{file::FileWriter, helpers::datetime::msdos};

pub fn write(target: &mut FileWriter, data: &mut ZipArchiveData, buffer_size: &u64) {
for file in &mut data.files {
pub fn write(target: &mut FileWriter, data: ZipArchiveData, buffer_size: &u64) {
for file in data.files {
if file.is_directory {
todo!();
}
Expand Down Expand Up @@ -35,12 +35,14 @@ pub fn write(target: &mut FileWriter, data: &mut ZipArchiveData, buffer_size: &u
target.write_utf8(name);
target.write_u8array(extra);

file.source.as_mut().unwrap().export(
let mut source = file.source.unwrap();
source.export(
&file.offset,
&file.size,
target,
&file.modified,
buffer_size,
);
source.close();
}
}
54 changes: 42 additions & 12 deletions tests/zip-external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ fn create_000_metadata() {
archive::create(
Formats::Zip,
"tests/samples/zip/c000-external.zip".to_string(),
&mut [EntrySource {
vec![EntrySource {
path: "test.txt".to_string(),
source: &mut FsFile::new(&"tests/samples/zip/c000-external/test.txt".to_string()),
source: FsFile::new(&"tests/samples/zip/c000-external/test.txt".to_string()),
}],
1024,
)
Expand Down Expand Up @@ -204,20 +204,12 @@ fn create_000_extract() {
test_txt.write(b"Hello, world!\n");
test_txt.close();

std::fs::create_dir_all("tests/samples/zip/c000-external2").unwrap();
let mut test_txt = FileWriter::new(
&"tests/samples/zip/c000-external2/test.txt".to_string(),
&false,
);
test_txt.write(b"Hello, world!\n");
test_txt.close();

archive::create(
Formats::Zip,
"tests/samples/zip/c000-external2.zip".to_string(),
&mut [EntrySource {
vec![EntrySource {
path: "test.txt".to_string(),
source: &mut FsFile::new(&"tests/samples/zip/c000-external2/test.txt".to_string()),
source: FsFile::new(&"tests/samples/zip/c000-external2/test.txt".to_string()),
}],
1024,
)
Expand All @@ -244,3 +236,41 @@ fn create_000_extract() {
std::fs::remove_dir_all("tests/samples/zip/c000-external2").unwrap();
std::fs::remove_file("tests/samples/zip/c000-external2.zip").unwrap();
}

#[test]
fn create_000_with_iter() {
std::fs::create_dir_all("tests/samples/zip/c000-external3").unwrap(); // this is has another name to avoid conflicts
let mut test_txt = FileWriter::new(
&"tests/samples/zip/c000-external3/test.txt".to_string(),
&false,
);
test_txt.write(b"Hello, world!\n");
test_txt.close();

let input = "tests/samples/zip/c000-external3/test.txt:test.txt";
let files: Vec<EntrySource> = input // implementation from the CLI
.split(';')
.map(|file| {
let file = file.split(':').collect::<Vec<&str>>();
let source_path = file.first().unwrap();
let mut target_path = source_path;
if let Some(path) = file.get(1) {
target_path = path;
}
EntrySource {
path: target_path.to_string(),
source: FsFile::new(&source_path.to_string()),
}
})
.collect();
archive::create(
Formats::Zip,
"tests/samples/zip/c000-external3.zip".to_string(),
files,
1024,
)
.unwrap();

std::fs::remove_dir_all("tests/samples/zip/c000-external3").unwrap();
std::fs::remove_file("tests/samples/zip/c000-external3.zip").unwrap();
}
6 changes: 2 additions & 4 deletions tests/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,19 @@ fn create_000() {
let size = input.get_size();
corelib::formats::zip::writer::write(
&mut output,
&mut ZipArchiveData {
ZipArchiveData {
files: vec![ZipFile {
checksum: crc32::hash(&mut input, &0, &size, &1024),
path: "test.txt".to_string(),
offset: 0,
size,
modified: input.get_times().modified,
is_directory: false,
source: Some(&mut input),
source: Some(input),
}],
},
&1024,
);
input.close();

output.close();

let mut file = FileReader::new(&"tests/samples/zip/c000.zip".to_string());
Expand Down

0 comments on commit 6ecbf09

Please sign in to comment.