diff options
author | boasting-squirrel <boasting.squirrel@gmail.com> | 2019-03-13 17:08:49 +0000 |
---|---|---|
committer | boasting-squirrel <boasting.squirrel@gmail.com> | 2019-03-13 17:08:49 +0000 |
commit | 2723babb9b8ddef120dfeb9b671e18f1a46dfb96 (patch) | |
tree | f05d7c3ce09d6e2a99c1d2e18c3ee5fbd61db959 | |
parent | Added some docs comments to errors.rs (diff) | |
download | miniserve-2723babb9b8ddef120dfeb9b671e18f1a46dfb96.tar.gz miniserve-2723babb9b8ddef120dfeb9b671e18f1a46dfb96.zip |
Build tar in buffer instead of in tempfile
-rw-r--r-- | src/archive.rs | 42 | ||||
-rw-r--r-- | src/errors.rs | 6 |
2 files changed, 10 insertions, 38 deletions
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<Vec<u8>, 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<Vec<u8>, 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) } diff --git a/src/errors.rs b/src/errors.rs index 3fcda8f..8cfedff 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -5,12 +5,6 @@ use yansi::{Color, Paint}; /// Kinds of errors which might happen during the generation of an archive #[derive(Debug, Fail)] pub enum CompressionErrorKind { - #[fail(display = "Could not open file {}", path)] - OpenFileError { path: String }, - #[fail(display = "Could not create temporary file")] - CreateTemporaryFileError, - #[fail(display = "Could not create file {}", path)] - CreateFileError { path: String }, #[fail(display = "Invalid path: directory name terminates in \"..\"")] InvalidDirectoryName, #[fail(display = "Invalid path: directory name contains invalid UTF-8 characters")] |