-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
break compressed_archive fn out into utils crate
Signed-off-by: Vaughn Dice <[email protected]>
- Loading branch information
Showing
3 changed files
with
55 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
mod auth; | ||
mod client; | ||
mod loader; | ||
mod utils; | ||
|
||
pub use client::Client; | ||
pub use loader::OciLoader; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use anyhow::{Context, Result}; | ||
use async_compression::tokio::write::GzipEncoder; | ||
use std::path::PathBuf; | ||
|
||
/// Create a compressed archive of source, returning its path in working_dir | ||
pub async fn compressed_archive(source: &PathBuf, working_dir: &PathBuf) -> Result<PathBuf> { | ||
// Create tar archive file | ||
let tar_gz_path = working_dir | ||
.join(source.file_name().unwrap()) | ||
.with_extension("tar.gz"); | ||
let tar_gz = tokio::fs::File::create(tar_gz_path.as_path()) | ||
.await | ||
.context(format!( | ||
"Unable to create tar archive for source {:?}", | ||
source.as_path() | ||
))?; | ||
|
||
// Create encoder | ||
// TODO: use zstd? May be more performant | ||
let tar_gz_enc = GzipEncoder::new(tar_gz); | ||
|
||
// Build tar archive | ||
let mut tar_builder = async_tar::Builder::new( | ||
tokio_util::compat::TokioAsyncWriteCompatExt::compat_write(tar_gz_enc), | ||
); | ||
tar_builder | ||
.append_dir_all(".", source.as_path()) | ||
.await | ||
.context(format!( | ||
"Unable to create tar archive for source {:?}", | ||
source.as_path() | ||
))?; | ||
// Finish writing the archive | ||
tar_builder.finish().await?; | ||
// Shutdown the encoder | ||
use tokio::io::AsyncWriteExt; | ||
tar_builder | ||
.into_inner() | ||
.await? | ||
.into_inner() | ||
.shutdown() | ||
.await?; | ||
Ok(tar_gz_path) | ||
} |