From b585988c60a13af3c9b49fa8d58a10dd5bb62232 Mon Sep 17 00:00:00 2001 From: kipcode66 Date: Mon, 11 Oct 2021 04:39:42 -0400 Subject: [PATCH] Added recursive addition of resources. --- backend/src/file_source.rs | 49 ++++++++++++++++++++++++++++++++++++++ backend/src/lib.rs | 28 +++++++++++++++------- 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/backend/src/file_source.rs b/backend/src/file_source.rs index 1e76352..006dd9b 100644 --- a/backend/src/file_source.rs +++ b/backend/src/file_source.rs @@ -9,6 +9,10 @@ pub trait FileSource { fn read_to_vec>(&mut self, path: P) -> Result, Error>; fn read_to_string>(&mut self, path: P) -> Result; fn open_image>(&mut self, path: P) -> Result; + fn exist>(&mut self, path: P) -> Result; + fn is_dir>(&mut self, path: P) -> Result; + fn is_file>(&mut self, path: P) -> Result; + fn get_names>(&mut self, path: P) -> Result, Error>; } pub struct FileSystem; @@ -23,6 +27,25 @@ impl FileSource for FileSystem { fn open_image>(&mut self, path: P) -> Result { Ok(image::open(path)?) } + fn exist>(&mut self, path: P) -> Result { + Ok(path.as_ref().exists()) + } + fn is_dir>(&mut self, path: P) -> Result { + Ok(path.as_ref().is_dir()) + } + fn is_file>(&mut self, path: P) -> Result { + Ok(path.as_ref().is_file()) + } + fn get_names>(&mut self, path: P) -> Result, Error> { + let mut names: Vec = Vec::new(); + for entry in fs::read_dir(path)? { + let entry = entry?; + let entry_path = entry.path(); + let file_name = entry_path.file_name().expect("Entry has no name"); + names.push(String::from(file_name.to_str().unwrap())); + } + Ok(names) + } } impl FileSource for ZipArchive { @@ -50,4 +73,30 @@ impl FileSource for ZipArchive { let buf = self.read_to_vec(path)?; Ok(image::load_from_memory(&buf)?) } + fn exist>(&mut self, path: P) -> Result { + Ok(self.by_name( + path.as_ref() + .to_str() + .ok_or_else(|| err_msg("Invalid path"))? + ).is_ok()) + } + fn is_dir>(&mut self, path: P) -> Result { + let file = self.by_name( + path.as_ref() + .to_str() + .ok_or_else(|| err_msg("Invalid path"))? + )?; + Ok(file.is_dir()) + } + fn is_file>(&mut self, path: P) -> Result { + let file = self.by_name( + path.as_ref() + .to_str() + .ok_or_else(|| err_msg("Invalid path"))? + )?; + Ok(file.is_file()) + } + fn get_names>(&mut self, _path: P) -> Result, Error> { + Err(failure::err_msg("Unsupported operation on ZipArchive: get_names")) + } } diff --git a/backend/src/lib.rs b/backend/src/lib.rs index b6ac510..4d006c5 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -334,15 +334,25 @@ fn add_file_to_iso( iso: &mut Directory, files: &mut F ) -> Result<(), Error> { - iso.resolve_and_create_path(&iso_path).data = files - .read_to_vec(actual_path) - .with_context(|_| { - format!( - "Couldn't read the file \"{}\" to store it in the ISO.", - actual_path.display() - ) - })? - .into(); + if files.is_dir(actual_path)? { + let names = files.get_names(actual_path)?; + for name in names { + let iso_path = String::from(iso_path) + &String::from('/') + &name; + let mut actual_path = actual_path.clone(); + actual_path.push(name); + add_file_to_iso(&iso_path, &actual_path, iso, files)?; + } + } else { + iso.resolve_and_create_path(&iso_path).data = files + .read_to_vec(actual_path) + .with_context(|_| { + format!( + "Couldn't read the file \"{}\" to store it in the ISO.", + actual_path.display() + ) + })? + .into(); + } Ok(()) }