From 9857f26bdd4e9e87b13a9755b1e00569e9459238 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Fri, 8 Mar 2019 19:59:59 +0100 Subject: Download folder as a tar working --- src/archive.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/archive.rs (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs new file mode 100644 index 0000000..cc39207 --- /dev/null +++ b/src/archive.rs @@ -0,0 +1,80 @@ +use bytes::Bytes; +use serde::Deserialize; +use std::fs::{File, OpenOptions}; +use std::io::Read; +use std::path::PathBuf; +use tar::Builder; +use tempfile::tempdir; + +#[derive(Debug)] +pub enum CompressionError { + IOError(std::io::Error), + NoneError(std::option::NoneError), +} + +impl From for CompressionError { + fn from(err: std::option::NoneError) -> CompressionError { + CompressionError::NoneError(err) + } +} + +impl From for CompressionError { + fn from(err: std::io::Error) -> CompressionError { + CompressionError::IOError(err) + } +} + +/// Available compression methods +#[derive(Debug, Deserialize, Clone)] +pub enum CompressionMethod { + /// ZIP + #[serde(alias = "targz")] + TarGz, +} + +impl CompressionMethod { + pub fn to_string(&self) -> String { + match &self { + CompressionMethod::TarGz => "targz", + } + .to_string() + } + + pub fn extension(&self) -> String { + match &self { + CompressionMethod::TarGz => "tar.gz", + } + .to_string() + } +} + +pub fn create_archive_file( + method: &CompressionMethod, + dir: &PathBuf, +) -> Result<(String, usize, Bytes), CompressionError> { + match method { + CompressionMethod::TarGz => tgz_compress(&dir), + } +} + +fn tgz_compress(dir: &PathBuf) -> Result<(String, usize, Bytes), CompressionError> { + let src_dir = dir.display().to_string(); + let inner_folder = dir.file_name()?.to_str()?; + let dst_filename = format!("{}.tar", inner_folder); + let tmp_dir = tempdir()?; + + let dst_filepath = tmp_dir.path().join(dst_filename.clone()); + let tar_file = File::create(&dst_filepath)?; + let mut tar_builder = Builder::new(&tar_file); + tar_builder.append_dir_all(inner_folder, src_dir)?; + tar_builder.finish()?; + + let mut tar_file = OpenOptions::new().read(true).open(&dst_filepath)?; + let mut contents = Vec::new(); + let content_length = tar_file.read_to_end(&mut contents).unwrap(); + + let mut data = Bytes::new(); + data.extend_from_slice(&contents); + + Ok((dst_filename, content_length, data)) +} -- cgit v1.2.3 From e7c269b12ec8168671e61787227d0fecc2756590 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Fri, 8 Mar 2019 20:25:02 +0100 Subject: Working example of tar.gz --- src/archive.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index cc39207..47af660 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -1,7 +1,8 @@ use bytes::Bytes; +use libflate::gzip::Encoder; use serde::Deserialize; use std::fs::{File, OpenOptions}; -use std::io::Read; +use std::io::{self, Read}; use std::path::PathBuf; use tar::Builder; use tempfile::tempdir; @@ -61,6 +62,7 @@ fn tgz_compress(dir: &PathBuf) -> Result<(String, usize, Bytes), CompressionErro let src_dir = dir.display().to_string(); let inner_folder = dir.file_name()?.to_str()?; let dst_filename = format!("{}.tar", inner_folder); + let dst_tgz_filename = format!("{}.gz", dst_filename); let tmp_dir = tempdir()?; let dst_filepath = tmp_dir.path().join(dst_filename.clone()); @@ -70,11 +72,21 @@ fn tgz_compress(dir: &PathBuf) -> Result<(String, usize, Bytes), CompressionErro tar_builder.finish()?; let mut tar_file = OpenOptions::new().read(true).open(&dst_filepath)?; - let mut contents = Vec::new(); - let content_length = tar_file.read_to_end(&mut contents).unwrap(); + let mut tar_content = Vec::new(); + let content_length = tar_file.read_to_end(&mut tar_content).unwrap(); + + let gz_data = gzip(&mut tar_content)?; let mut data = Bytes::new(); - data.extend_from_slice(&contents); + data.extend_from_slice(&gz_data); + + Ok((dst_tgz_filename, content_length, data)) +} + +fn gzip(mut data: &[u8]) -> Result, CompressionError> { + let mut encoder = Encoder::new(Vec::new())?; + io::copy(&mut data, &mut encoder)?; + let data = encoder.finish().into_result()?; - Ok((dst_filename, content_length, data)) + Ok(data) } -- cgit v1.2.3 From eec5f353ffe584fcdcd7e43670fbd30d9e9f04e3 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Fri, 8 Mar 2019 22:23:33 +0100 Subject: Refactored some code --- src/archive.rs | 74 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 29 deletions(-) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index 47af660..d9ca6e9 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -7,24 +7,6 @@ use std::path::PathBuf; use tar::Builder; use tempfile::tempdir; -#[derive(Debug)] -pub enum CompressionError { - IOError(std::io::Error), - NoneError(std::option::NoneError), -} - -impl From for CompressionError { - fn from(err: std::option::NoneError) -> CompressionError { - CompressionError::NoneError(err) - } -} - -impl From for CompressionError { - fn from(err: std::io::Error) -> CompressionError { - CompressionError::IOError(err) - } -} - /// Available compression methods #[derive(Debug, Deserialize, Clone)] pub enum CompressionMethod { @@ -49,40 +31,74 @@ impl CompressionMethod { } } +/// Possible errors +#[derive(Debug)] +pub enum CompressionError { + IOError(std::io::Error), + NoneError(std::option::NoneError), +} + +impl From for CompressionError { + fn from(err: std::option::NoneError) -> CompressionError { + CompressionError::NoneError(err) + } +} + +impl From for CompressionError { + fn from(err: std::io::Error) -> CompressionError { + CompressionError::IOError(err) + } +} + pub fn create_archive_file( method: &CompressionMethod, dir: &PathBuf, -) -> Result<(String, usize, Bytes), CompressionError> { +) -> Result<(String, Bytes), CompressionError> { match method { CompressionMethod::TarGz => tgz_compress(&dir), } } -fn tgz_compress(dir: &PathBuf) -> Result<(String, usize, Bytes), CompressionError> { +/// Compresses a given folder in .tar.gz format +fn tgz_compress(dir: &PathBuf) -> Result<(String, Bytes), CompressionError> { let src_dir = dir.display().to_string(); let inner_folder = dir.file_name()?.to_str()?; let dst_filename = format!("{}.tar", inner_folder); let dst_tgz_filename = format!("{}.gz", dst_filename); - let tmp_dir = tempdir()?; + let tar_content = tar(src_dir, dst_filename, inner_folder.to_string())?; + let gz_data = gzip(&tar_content)?; + + let mut data = Bytes::new(); + data.extend_from_slice(&gz_data); + + Ok((dst_tgz_filename, data)) +} + +/// Creates a temporary tar file of a given directory, reads it and returns its content as bytes +fn tar( + src_dir: String, + dst_filename: String, + inner_folder: String, +) -> Result, CompressionError> { + let tmp_dir = tempdir()?; let dst_filepath = tmp_dir.path().join(dst_filename.clone()); let tar_file = File::create(&dst_filepath)?; + + // Create a TAR file of src_dir let mut tar_builder = Builder::new(&tar_file); tar_builder.append_dir_all(inner_folder, src_dir)?; - tar_builder.finish()?; + tar_builder.into_inner()?; + // Read the content of the TAR file and store it as bytes let mut tar_file = OpenOptions::new().read(true).open(&dst_filepath)?; let mut tar_content = Vec::new(); - let content_length = tar_file.read_to_end(&mut tar_content).unwrap(); - - let gz_data = gzip(&mut tar_content)?; - - let mut data = Bytes::new(); - data.extend_from_slice(&gz_data); + tar_file.read_to_end(&mut tar_content)?; - Ok((dst_tgz_filename, content_length, data)) + Ok(tar_content) } +/// Compresses a stream of bytes using the GZIP algorithm fn gzip(mut data: &[u8]) -> Result, CompressionError> { let mut encoder = Encoder::new(Vec::new())?; io::copy(&mut data, &mut encoder)?; -- cgit v1.2.3 From e04fc9675433f695b37104e57b28bd33d37ad5ab Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Fri, 8 Mar 2019 23:25:01 +0100 Subject: Corrected comment --- src/archive.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index d9ca6e9..0f09005 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -10,7 +10,7 @@ use tempfile::tempdir; /// Available compression methods #[derive(Debug, Deserialize, Clone)] pub enum CompressionMethod { - /// ZIP + /// TAR GZ #[serde(alias = "targz")] TarGz, } -- cgit v1.2.3 From 46932e7d5664c97ad65aefc3f670f7e64e6f8e0d Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Fri, 8 Mar 2019 23:57:05 +0100 Subject: Improved HTTP headers --- src/archive.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index 0f09005..1b31d08 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -1,3 +1,4 @@ +use actix_web::http::ContentEncoding; use bytes::Bytes; use libflate::gzip::Encoder; use serde::Deserialize; @@ -29,6 +30,19 @@ impl CompressionMethod { } .to_string() } + + pub fn content_type(&self) -> String { + match &self { + CompressionMethod::TarGz => "application/gzip", + } + .to_string() + } + + pub fn content_encoding(&self) -> ContentEncoding { + match &self { + CompressionMethod::TarGz => ContentEncoding::Gzip, + } + } } /// Possible errors -- cgit v1.2.3 From b077424d68659923e6ac13f364434314085a0376 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Sat, 9 Mar 2019 11:42:45 +0100 Subject: Temporary workaround for symlinks issue --- src/archive.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index 1b31d08..bc3a1c8 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -101,6 +101,11 @@ fn tar( // Create a TAR file of src_dir let mut tar_builder = Builder::new(&tar_file); + + // Temporary workaround for known issue: + // https://github.com/alexcrichton/tar-rs/issues/147 + // https://github.com/alexcrichton/tar-rs/issues/174 + tar_builder.follow_symlinks(false); tar_builder.append_dir_all(inner_folder, src_dir)?; tar_builder.into_inner()?; -- cgit v1.2.3 From aeb51dcf43665741a3438360151a4424e9b243e0 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Tue, 12 Mar 2019 00:25:56 +0100 Subject: Started to add helpful messages for errors which could occur during archiving --- src/archive.rs | 82 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 34 deletions(-) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index bc3a1c8..afbcc6b 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -1,5 +1,6 @@ use actix_web::http::ContentEncoding; use bytes::Bytes; +use failure::ResultExt; use libflate::gzip::Encoder; use serde::Deserialize; use std::fs::{File, OpenOptions}; @@ -7,6 +8,9 @@ use std::io::{self, Read}; use std::path::PathBuf; use tar::Builder; use tempfile::tempdir; +use yansi::Color; + +use crate::errors; /// Available compression methods #[derive(Debug, Deserialize, Clone)] @@ -45,43 +49,25 @@ impl CompressionMethod { } } -/// Possible errors -#[derive(Debug)] -pub enum CompressionError { - IOError(std::io::Error), - NoneError(std::option::NoneError), -} - -impl From for CompressionError { - fn from(err: std::option::NoneError) -> CompressionError { - CompressionError::NoneError(err) - } -} - -impl From for CompressionError { - fn from(err: std::io::Error) -> CompressionError { - CompressionError::IOError(err) - } -} - pub fn create_archive_file( method: &CompressionMethod, dir: &PathBuf, -) -> Result<(String, Bytes), CompressionError> { +) -> Result<(String, Bytes), errors::CompressionError> { match method { CompressionMethod::TarGz => tgz_compress(&dir), } } /// Compresses a given folder in .tar.gz format -fn tgz_compress(dir: &PathBuf) -> Result<(String, Bytes), CompressionError> { +fn tgz_compress(dir: &PathBuf) -> Result<(String, Bytes), errors::CompressionError> { let src_dir = dir.display().to_string(); let inner_folder = dir.file_name()?.to_str()?; let dst_filename = format!("{}.tar", inner_folder); let dst_tgz_filename = format!("{}.gz", dst_filename); - let tar_content = tar(src_dir, dst_filename, inner_folder.to_string())?; - let gz_data = gzip(&tar_content)?; + let tar_content = tar(src_dir, dst_filename, inner_folder.to_string()) + .context(errors::CompressionErrorKind::TarContentError)?; + let gz_data = gzip(&tar_content).context(errors::CompressionErrorKind::GZipContentError)?; let mut data = Bytes::new(); data.extend_from_slice(&gz_data); @@ -94,10 +80,13 @@ fn tar( src_dir: String, dst_filename: String, inner_folder: String, -) -> Result, CompressionError> { - let tmp_dir = tempdir()?; +) -> Result, errors::CompressionError> { + let tmp_dir = tempdir().context(errors::CompressionErrorKind::CreateTemporaryFileError)?; let dst_filepath = tmp_dir.path().join(dst_filename.clone()); - let tar_file = File::create(&dst_filepath)?; + let tar_file = + File::create(&dst_filepath).context(errors::CompressionErrorKind::CreateFileError { + path: color_path(&dst_filepath.display().to_string()), + })?; // Create a TAR file of src_dir let mut tar_builder = Builder::new(&tar_file); @@ -106,22 +95,47 @@ fn tar( // https://github.com/alexcrichton/tar-rs/issues/147 // https://github.com/alexcrichton/tar-rs/issues/174 tar_builder.follow_symlinks(false); - tar_builder.append_dir_all(inner_folder, src_dir)?; - tar_builder.into_inner()?; + tar_builder.append_dir_all(inner_folder, &src_dir).context( + errors::CompressionErrorKind::TarBuildingError { + message: format!( + "failed to append the content of {} in the TAR archive", + color_path(&src_dir) + ), + }, + )?; + tar_builder + .into_inner() + .context(errors::CompressionErrorKind::TarBuildingError { + message: "failed to finish writing the TAR archive".to_string(), + })?; // Read the content of the TAR file and store it as bytes - let mut tar_file = OpenOptions::new().read(true).open(&dst_filepath)?; + let mut tar_file = OpenOptions::new().read(true).open(&dst_filepath).context( + errors::CompressionErrorKind::OpenFileError { + path: color_path(&dst_filepath.display().to_string()), + }, + )?; let mut tar_content = Vec::new(); - tar_file.read_to_end(&mut tar_content)?; + tar_file + .read_to_end(&mut tar_content) + .context(errors::CompressionErrorKind::TarContentError)?; Ok(tar_content) } /// Compresses a stream of bytes using the GZIP algorithm -fn gzip(mut data: &[u8]) -> Result, CompressionError> { - let mut encoder = Encoder::new(Vec::new())?; - io::copy(&mut data, &mut encoder)?; - let data = encoder.finish().into_result()?; +fn gzip(mut data: &[u8]) -> Result, errors::CompressionError> { + let mut encoder = + Encoder::new(Vec::new()).context(errors::CompressionErrorKind::GZipBuildingError)?; + io::copy(&mut data, &mut encoder).context(errors::CompressionErrorKind::GZipBuildingError)?; + let data = encoder + .finish() + .into_result() + .context(errors::CompressionErrorKind::GZipBuildingError)?; Ok(data) } + +fn color_path(path: &str) -> String { + Color::White.paint(path).bold().to_string() +} -- cgit v1.2.3 From 67d771fc3a954ea439e77afac092c84c5489d074 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Tue, 12 Mar 2019 19:23:22 +0100 Subject: Added some error messages + reworked the print_error_chain method --- src/archive.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index afbcc6b..556fd69 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -126,12 +126,17 @@ fn tar( /// Compresses a stream of bytes using the GZIP algorithm fn gzip(mut data: &[u8]) -> Result, errors::CompressionError> { let mut encoder = - Encoder::new(Vec::new()).context(errors::CompressionErrorKind::GZipBuildingError)?; - io::copy(&mut data, &mut encoder).context(errors::CompressionErrorKind::GZipBuildingError)?; - let data = encoder - .finish() - .into_result() - .context(errors::CompressionErrorKind::GZipBuildingError)?; + Encoder::new(Vec::new()).context(errors::CompressionErrorKind::GZipBuildingError { + message: "failed to create GZIP encoder".to_string(), + })?; + io::copy(&mut data, &mut encoder).context(errors::CompressionErrorKind::GZipBuildingError { + message: "failed to write GZIP data".to_string(), + })?; + let data = encoder.finish().into_result().context( + errors::CompressionErrorKind::GZipBuildingError { + message: "failed to write GZIP trailer".to_string(), + }, + )?; Ok(data) } -- cgit v1.2.3 From 122a949ec49f84a49e7a5bec657a93a65faadce1 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Tue, 12 Mar 2019 20:16:45 +0100 Subject: Better error messages for invalid path --- src/archive.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index 556fd69..9df1e5e 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -61,7 +61,21 @@ pub fn create_archive_file( /// Compresses a given folder in .tar.gz format fn tgz_compress(dir: &PathBuf) -> Result<(String, Bytes), errors::CompressionError> { let src_dir = dir.display().to_string(); - let inner_folder = dir.file_name()?.to_str()?; + let inner_folder = match dir.file_name() { + Some(directory_name) => match directory_name.to_str() { + Some(directory) => directory, + None => { + return Err(errors::CompressionError::new( + errors::CompressionErrorKind::InvalidUTF8DirectoryName, + )) + } + }, + None => { + return Err(errors::CompressionError::new( + errors::CompressionErrorKind::InvalidDirectoryName, + )) + } + }; let dst_filename = format!("{}.tar", inner_folder); let dst_tgz_filename = format!("{}.gz", dst_filename); -- cgit v1.2.3 From 2723babb9b8ddef120dfeb9b671e18f1a46dfb96 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Wed, 13 Mar 2019 18:08:49 +0100 Subject: Build tar in buffer instead of in tempfile --- src/archive.rs | 42 ++++++++++-------------------------------- 1 file changed, 10 insertions(+), 32 deletions(-) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index 9df1e5e..c535c50 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -3,11 +3,9 @@ use bytes::Bytes; use failure::ResultExt; use libflate::gzip::Encoder; use serde::Deserialize; -use std::fs::{File, OpenOptions}; -use std::io::{self, Read}; +use std::io; use std::path::PathBuf; use tar::Builder; -use tempfile::tempdir; use yansi::Color; use crate::errors; @@ -79,7 +77,7 @@ fn tgz_compress(dir: &PathBuf) -> Result<(String, Bytes), errors::CompressionErr let dst_filename = format!("{}.tar", inner_folder); let dst_tgz_filename = format!("{}.gz", dst_filename); - let tar_content = tar(src_dir, dst_filename, inner_folder.to_string()) + let tar_content = tar(src_dir, inner_folder.to_string()) .context(errors::CompressionErrorKind::TarContentError)?; let gz_data = gzip(&tar_content).context(errors::CompressionErrorKind::GZipContentError)?; @@ -90,20 +88,9 @@ fn tgz_compress(dir: &PathBuf) -> Result<(String, Bytes), errors::CompressionErr } /// Creates a temporary tar file of a given directory, reads it and returns its content as bytes -fn tar( - src_dir: String, - dst_filename: String, - inner_folder: String, -) -> Result, errors::CompressionError> { - let tmp_dir = tempdir().context(errors::CompressionErrorKind::CreateTemporaryFileError)?; - let dst_filepath = tmp_dir.path().join(dst_filename.clone()); - let tar_file = - File::create(&dst_filepath).context(errors::CompressionErrorKind::CreateFileError { - path: color_path(&dst_filepath.display().to_string()), - })?; - +fn tar(src_dir: String, inner_folder: String) -> Result, errors::CompressionError> { // Create a TAR file of src_dir - let mut tar_builder = Builder::new(&tar_file); + let mut tar_builder = Builder::new(Vec::new()); // Temporary workaround for known issue: // https://github.com/alexcrichton/tar-rs/issues/147 @@ -117,22 +104,13 @@ fn tar( ), }, )?; - tar_builder - .into_inner() - .context(errors::CompressionErrorKind::TarBuildingError { - message: "failed to finish writing the TAR archive".to_string(), - })?; - // Read the content of the TAR file and store it as bytes - let mut tar_file = OpenOptions::new().read(true).open(&dst_filepath).context( - errors::CompressionErrorKind::OpenFileError { - path: color_path(&dst_filepath.display().to_string()), - }, - )?; - let mut tar_content = Vec::new(); - tar_file - .read_to_end(&mut tar_content) - .context(errors::CompressionErrorKind::TarContentError)?; + let tar_content = + tar_builder + .into_inner() + .context(errors::CompressionErrorKind::TarBuildingError { + message: "failed to finish writing the TAR archive".to_string(), + })?; Ok(tar_content) } -- cgit v1.2.3 From 17ef61a126e81c9ecfd1ebdd89537e854a06cae6 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Wed, 13 Mar 2019 19:30:54 +0100 Subject: Switched to standard Rust logging system --- src/archive.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index c535c50..e1460b9 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -6,7 +6,6 @@ use serde::Deserialize; use std::io; use std::path::PathBuf; use tar::Builder; -use yansi::Color; use crate::errors; @@ -100,7 +99,7 @@ fn tar(src_dir: String, inner_folder: String) -> Result, errors::Compres errors::CompressionErrorKind::TarBuildingError { message: format!( "failed to append the content of {} in the TAR archive", - color_path(&src_dir) + &src_dir ), }, )?; @@ -132,7 +131,3 @@ fn gzip(mut data: &[u8]) -> Result, errors::CompressionError> { Ok(data) } - -fn color_path(path: &str) -> String { - Color::White.paint(path).bold().to_string() -} -- cgit v1.2.3 From e7fadb7d631824841fa61cb2373c7e260e3b7b6a Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Thu, 14 Mar 2019 00:08:19 +0100 Subject: Improved grammar of TarBuildingError error message --- src/archive.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index e1460b9..b71d50f 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -98,7 +98,7 @@ fn tar(src_dir: String, inner_folder: String) -> Result, errors::Compres tar_builder.append_dir_all(inner_folder, &src_dir).context( errors::CompressionErrorKind::TarBuildingError { message: format!( - "failed to append the content of {} in the TAR archive", + "failed to append the content of {} to the TAR archive", &src_dir ), }, -- cgit v1.2.3 From 20283096380f86595c959de8d5f045437e84c964 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Thu, 14 Mar 2019 20:30:06 +0100 Subject: Switched to tar-rs 0.4.22 and propagate no-symlink argument to tar generation --- src/archive.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index b71d50f..b351fb9 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -49,14 +49,15 @@ impl CompressionMethod { pub fn create_archive_file( method: &CompressionMethod, dir: &PathBuf, + skip_symlinks: bool ) -> Result<(String, Bytes), errors::CompressionError> { match method { - CompressionMethod::TarGz => tgz_compress(&dir), + CompressionMethod::TarGz => tgz_compress(&dir, skip_symlinks), } } /// Compresses a given folder in .tar.gz format -fn tgz_compress(dir: &PathBuf) -> Result<(String, Bytes), errors::CompressionError> { +fn tgz_compress(dir: &PathBuf, skip_symlinks: bool) -> Result<(String, Bytes), errors::CompressionError> { let src_dir = dir.display().to_string(); let inner_folder = match dir.file_name() { Some(directory_name) => match directory_name.to_str() { @@ -76,7 +77,7 @@ fn tgz_compress(dir: &PathBuf) -> Result<(String, Bytes), errors::CompressionErr let dst_filename = format!("{}.tar", inner_folder); let dst_tgz_filename = format!("{}.gz", dst_filename); - let tar_content = tar(src_dir, inner_folder.to_string()) + let tar_content = tar(src_dir, inner_folder.to_string(), skip_symlinks) .context(errors::CompressionErrorKind::TarContentError)?; let gz_data = gzip(&tar_content).context(errors::CompressionErrorKind::GZipContentError)?; @@ -87,14 +88,11 @@ fn tgz_compress(dir: &PathBuf) -> Result<(String, Bytes), errors::CompressionErr } /// Creates a temporary tar file of a given directory, reads it and returns its content as bytes -fn tar(src_dir: String, inner_folder: String) -> Result, errors::CompressionError> { +fn tar(src_dir: String, inner_folder: String, skip_symlinks: bool) -> Result, errors::CompressionError> { // Create a TAR file of src_dir let mut tar_builder = Builder::new(Vec::new()); - // Temporary workaround for known issue: - // https://github.com/alexcrichton/tar-rs/issues/147 - // https://github.com/alexcrichton/tar-rs/issues/174 - tar_builder.follow_symlinks(false); + tar_builder.follow_symlinks(!skip_symlinks); tar_builder.append_dir_all(inner_folder, &src_dir).context( errors::CompressionErrorKind::TarBuildingError { message: format!( -- cgit v1.2.3 From ed705b967a85990b0e0d36187c9d64bba96f8046 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Thu, 14 Mar 2019 20:32:04 +0100 Subject: Cargo fmt --- src/archive.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index b351fb9..c536c2d 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -49,7 +49,7 @@ impl CompressionMethod { pub fn create_archive_file( method: &CompressionMethod, dir: &PathBuf, - skip_symlinks: bool + skip_symlinks: bool, ) -> Result<(String, Bytes), errors::CompressionError> { match method { CompressionMethod::TarGz => tgz_compress(&dir, skip_symlinks), @@ -57,7 +57,10 @@ pub fn create_archive_file( } /// Compresses a given folder in .tar.gz format -fn tgz_compress(dir: &PathBuf, skip_symlinks: bool) -> Result<(String, Bytes), errors::CompressionError> { +fn tgz_compress( + dir: &PathBuf, + skip_symlinks: bool, +) -> Result<(String, Bytes), errors::CompressionError> { let src_dir = dir.display().to_string(); let inner_folder = match dir.file_name() { Some(directory_name) => match directory_name.to_str() { @@ -88,7 +91,11 @@ fn tgz_compress(dir: &PathBuf, skip_symlinks: bool) -> Result<(String, Bytes), e } /// Creates a temporary tar file of a given directory, reads it and returns its content as bytes -fn tar(src_dir: String, inner_folder: String, skip_symlinks: bool) -> Result, errors::CompressionError> { +fn tar( + src_dir: String, + inner_folder: String, + skip_symlinks: bool, +) -> Result, errors::CompressionError> { // Create a TAR file of src_dir let mut tar_builder = Builder::new(Vec::new()); -- cgit v1.2.3 From bc293fa54a612ff7e2d6088ba5d91890cad75b81 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Fri, 15 Mar 2019 17:50:59 +0100 Subject: Renamed create_archive_file function and added documentation --- src/archive.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index c536c2d..bc8ea3b 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -46,7 +46,9 @@ impl CompressionMethod { } } -pub fn create_archive_file( +/// Creates an archive of a folder, using the algorithm the user chose from the web interface +/// This method returns the archive as a stream of bytes +pub fn create_archive( method: &CompressionMethod, dir: &PathBuf, skip_symlinks: bool, @@ -56,7 +58,7 @@ pub fn create_archive_file( } } -/// Compresses a given folder in .tar.gz format +/// Compresses a given folder in .tar.gz format, and returns the result as a stream of bytes fn tgz_compress( dir: &PathBuf, skip_symlinks: bool, @@ -90,7 +92,7 @@ fn tgz_compress( Ok((dst_tgz_filename, data)) } -/// Creates a temporary tar file of a given directory, reads it and returns its content as bytes +/// Creates a TAR archive of a folder, and returns it as a stream of bytes fn tar( src_dir: String, inner_folder: String, @@ -119,7 +121,7 @@ fn tar( Ok(tar_content) } -/// Compresses a stream of bytes using the GZIP algorithm +/// Compresses a stream of bytes using the GZIP algorithm, and returns the resulting stream fn gzip(mut data: &[u8]) -> Result, errors::CompressionError> { let mut encoder = Encoder::new(Vec::new()).context(errors::CompressionErrorKind::GZipBuildingError { -- cgit v1.2.3 From 5b5f599055fb6221936c0985f656d0c4b7b2cb23 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Tue, 19 Mar 2019 20:08:57 +0100 Subject: Added documentation for errors and removed useless errors --- src/archive.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index bc8ea3b..fcf39fd 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -82,9 +82,14 @@ fn tgz_compress( let dst_filename = format!("{}.tar", inner_folder); let dst_tgz_filename = format!("{}.gz", dst_filename); - let tar_content = tar(src_dir, inner_folder.to_string(), skip_symlinks) - .context(errors::CompressionErrorKind::TarContentError)?; - let gz_data = gzip(&tar_content).context(errors::CompressionErrorKind::GZipContentError)?; + let tar_content = tar(src_dir, inner_folder.to_string(), skip_symlinks).context( + errors::CompressionErrorKind::TarBuildingError { + message: "an error occured while writing the TAR archive".to_string(), + }, + )?; + let gz_data = gzip(&tar_content).context(errors::CompressionErrorKind::GZipBuildingError { + message: "an error occured while writing the GZIP archive".to_string(), + })?; let mut data = Bytes::new(); data.extend_from_slice(&gz_data); -- cgit v1.2.3 From b9ee5574f389134ccfbdd970eb9a4a355c4e091b Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Tue, 19 Mar 2019 20:39:43 +0100 Subject: Fixed comments --- src/archive.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/archive.rs') diff --git a/src/archive.rs b/src/archive.rs index fcf39fd..206d252 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -103,10 +103,10 @@ fn tar( inner_folder: String, skip_symlinks: bool, ) -> Result, errors::CompressionError> { - // Create a TAR file of src_dir let mut tar_builder = Builder::new(Vec::new()); tar_builder.follow_symlinks(!skip_symlinks); + // Recursively adds the content of src_dir into the archive stream tar_builder.append_dir_all(inner_folder, &src_dir).context( errors::CompressionErrorKind::TarBuildingError { message: format!( -- cgit v1.2.3