diff options
Diffstat (limited to '')
-rw-r--r-- | src/archive.rs | 71 |
1 files changed, 34 insertions, 37 deletions
diff --git a/src/archive.rs b/src/archive.rs index e52fc49..a058f20 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -8,7 +8,7 @@ use strum::{Display, EnumIter, EnumString}; use tar::Builder; use zip::{write, ZipWriter}; -use crate::errors::ContextualError; +use crate::errors::RuntimeError; /// Available archive methods #[derive(Deserialize, Clone, Copy, EnumIter, EnumString, Display)] @@ -62,7 +62,7 @@ impl ArchiveMethod { dir: T, skip_symlinks: bool, out: W, - ) -> Result<(), ContextualError> + ) -> Result<(), RuntimeError> where T: AsRef<Path>, W: std::io::Write, @@ -77,17 +77,17 @@ impl ArchiveMethod { } /// Write a gzipped tarball of `dir` in `out`. -fn tar_gz<W>(dir: &Path, skip_symlinks: bool, out: W) -> Result<(), ContextualError> +fn tar_gz<W>(dir: &Path, skip_symlinks: bool, out: W) -> Result<(), RuntimeError> where W: std::io::Write, { - let mut out = Encoder::new(out).map_err(|e| ContextualError::IoError("GZIP".to_string(), e))?; + let mut out = Encoder::new(out).map_err(|e| RuntimeError::IoError("GZIP".to_string(), e))?; tar_dir(dir, skip_symlinks, &mut out)?; out.finish() .into_result() - .map_err(|e| ContextualError::IoError("GZIP finish".to_string(), e))?; + .map_err(|e| RuntimeError::IoError("GZIP finish".to_string(), e))?; Ok(()) } @@ -115,22 +115,22 @@ where /// ├── f /// └── g /// ``` -fn tar_dir<W>(dir: &Path, skip_symlinks: bool, out: W) -> Result<(), ContextualError> +fn tar_dir<W>(dir: &Path, skip_symlinks: bool, out: W) -> Result<(), RuntimeError> where W: std::io::Write, { let inner_folder = dir.file_name().ok_or_else(|| { - ContextualError::InvalidPathError("Directory name terminates in \"..\"".to_string()) + RuntimeError::InvalidPathError("Directory name terminates in \"..\"".to_string()) })?; let directory = inner_folder.to_str().ok_or_else(|| { - ContextualError::InvalidPathError( + RuntimeError::InvalidPathError( "Directory name contains invalid UTF-8 characters".to_string(), ) })?; tar(dir, directory.to_string(), skip_symlinks, out) - .map_err(|e| ContextualError::ArchiveCreationError("tarball".to_string(), Box::new(e))) + .map_err(|e| RuntimeError::ArchiveCreationError("tarball".to_string(), Box::new(e))) } /// Writes a tarball of `dir` in `out`. @@ -141,7 +141,7 @@ fn tar<W>( inner_folder: String, skip_symlinks: bool, out: W, -) -> Result<(), ContextualError> +) -> Result<(), RuntimeError> where W: std::io::Write, { @@ -153,7 +153,7 @@ where tar_builder .append_dir_all(inner_folder, src_dir) .map_err(|e| { - ContextualError::IoError( + RuntimeError::IoError( format!( "Failed to append the content of {} to the TAR archive", src_dir.to_str().unwrap_or("file") @@ -164,7 +164,7 @@ where // Finish the archive tar_builder.into_inner().map_err(|e| { - ContextualError::IoError("Failed to finish writing the TAR archive".to_string(), e) + RuntimeError::IoError("Failed to finish writing the TAR archive".to_string(), e) })?; Ok(()) @@ -197,28 +197,28 @@ fn create_zip_from_directory<W>( out: W, directory: &Path, skip_symlinks: bool, -) -> Result<(), ContextualError> +) -> Result<(), RuntimeError> where W: std::io::Write + std::io::Seek, { let options = write::FileOptions::default().compression_method(zip::CompressionMethod::Stored); let mut paths_queue: Vec<PathBuf> = vec![directory.to_path_buf()]; let zip_root_folder_name = directory.file_name().ok_or_else(|| { - ContextualError::InvalidPathError("Directory name terminates in \"..\"".to_string()) + RuntimeError::InvalidPathError("Directory name terminates in \"..\"".to_string()) })?; let mut zip_writer = ZipWriter::new(out); let mut buffer = Vec::new(); while !paths_queue.is_empty() { let next = paths_queue.pop().ok_or_else(|| { - ContextualError::ArchiveCreationDetailError("Could not get path from queue".to_string()) + RuntimeError::ArchiveCreationDetailError("Could not get path from queue".to_string()) })?; let current_dir = next.as_path(); let directory_entry_iterator = std::fs::read_dir(current_dir) - .map_err(|e| ContextualError::IoError("Could not read directory".to_string(), e))?; + .map_err(|e| RuntimeError::IoError("Could not read directory".to_string(), e))?; let zip_directory = Path::new(zip_root_folder_name).join( current_dir.strip_prefix(directory).map_err(|_| { - ContextualError::ArchiveCreationDetailError( + RuntimeError::ArchiveCreationDetailError( "Could not append base directory".to_string(), ) })?, @@ -228,37 +228,36 @@ where let entry_path = entry .ok() .ok_or_else(|| { - ContextualError::InvalidPathError( + RuntimeError::InvalidPathError( "Directory name terminates in \"..\"".to_string(), ) })? .path(); - let entry_metadata = std::fs::metadata(entry_path.clone()).map_err(|e| { - ContextualError::IoError("Could not get file metadata".to_string(), e) - })?; + let entry_metadata = std::fs::metadata(entry_path.clone()) + .map_err(|e| RuntimeError::IoError("Could not get file metadata".to_string(), e))?; if entry_metadata.file_type().is_symlink() && skip_symlinks { continue; } let current_entry_name = entry_path.file_name().ok_or_else(|| { - ContextualError::InvalidPathError("Invalid file or directory name".to_string()) + RuntimeError::InvalidPathError("Invalid file or directory name".to_string()) })?; if entry_metadata.is_file() { let mut f = File::open(&entry_path) - .map_err(|e| ContextualError::IoError("Could not open file".to_string(), e))?; + .map_err(|e| RuntimeError::IoError("Could not open file".to_string(), e))?; f.read_to_end(&mut buffer).map_err(|e| { - ContextualError::IoError("Could not read from file".to_string(), e) + RuntimeError::IoError("Could not read from file".to_string(), e) })?; let relative_path = zip_directory.join(current_entry_name).into_os_string(); zip_writer .start_file(relative_path.to_string_lossy(), options) .map_err(|_| { - ContextualError::ArchiveCreationDetailError( + RuntimeError::ArchiveCreationDetailError( "Could not add file path to ZIP".to_string(), ) })?; zip_writer.write(buffer.as_ref()).map_err(|_| { - ContextualError::ArchiveCreationDetailError( + RuntimeError::ArchiveCreationDetailError( "Could not write file to ZIP".to_string(), ) })?; @@ -268,7 +267,7 @@ where zip_writer .add_directory(relative_path.to_string_lossy(), options) .map_err(|_| { - ContextualError::ArchiveCreationDetailError( + RuntimeError::ArchiveCreationDetailError( "Could not add directory path to ZIP".to_string(), ) })?; @@ -278,9 +277,7 @@ where } zip_writer.finish().map_err(|_| { - ContextualError::ArchiveCreationDetailError( - "Could not finish writing ZIP archive".to_string(), - ) + RuntimeError::ArchiveCreationDetailError("Could not finish writing ZIP archive".to_string()) })?; Ok(()) } @@ -288,39 +285,39 @@ where /// Writes a zip of `dir` in `out`. /// /// The content of `src_dir` will be saved in the archive as the folder named . -fn zip_data<W>(src_dir: &Path, skip_symlinks: bool, mut out: W) -> Result<(), ContextualError> +fn zip_data<W>(src_dir: &Path, skip_symlinks: bool, mut out: W) -> Result<(), RuntimeError> where W: std::io::Write, { let mut data = Vec::new(); let memory_file = Cursor::new(&mut data); create_zip_from_directory(memory_file, src_dir, skip_symlinks).map_err(|e| { - ContextualError::ArchiveCreationError( + RuntimeError::ArchiveCreationError( "Failed to create the ZIP archive".to_string(), Box::new(e), ) })?; out.write_all(data.as_mut_slice()) - .map_err(|e| ContextualError::IoError("Failed to write the ZIP archive".to_string(), e))?; + .map_err(|e| RuntimeError::IoError("Failed to write the ZIP archive".to_string(), e))?; Ok(()) } -fn zip_dir<W>(dir: &Path, skip_symlinks: bool, out: W) -> Result<(), ContextualError> +fn zip_dir<W>(dir: &Path, skip_symlinks: bool, out: W) -> Result<(), RuntimeError> where W: std::io::Write, { let inner_folder = dir.file_name().ok_or_else(|| { - ContextualError::InvalidPathError("Directory name terminates in \"..\"".to_string()) + RuntimeError::InvalidPathError("Directory name terminates in \"..\"".to_string()) })?; inner_folder.to_str().ok_or_else(|| { - ContextualError::InvalidPathError( + RuntimeError::InvalidPathError( "Directory name contains invalid UTF-8 characters".to_string(), ) })?; zip_data(dir, skip_symlinks, out) - .map_err(|e| ContextualError::ArchiveCreationError("zip".to_string(), Box::new(e))) + .map_err(|e| RuntimeError::ArchiveCreationError("zip".to_string(), Box::new(e))) } |