|
1 | 1 | //! Utilities related to distributing Spin apps via OCI registries |
2 | 2 |
|
3 | 3 | use anyhow::{Context, Result}; |
4 | | -use async_compression::tokio::bufread::GzipDecoder; |
5 | | -use async_compression::tokio::write::GzipEncoder; |
6 | | -use async_tar::Archive; |
| 4 | +use flate2::read::GzDecoder; |
| 5 | +use flate2::write::GzEncoder; |
7 | 6 | use spin_common::ui::quoted_path; |
8 | 7 | use std::path::{Path, PathBuf}; |
| 8 | +use tar::Archive; |
9 | 9 |
|
10 | 10 | /// Create a compressed archive of source, returning its path in working_dir |
11 | 11 | pub async fn archive(source: &Path, working_dir: &Path) -> Result<PathBuf> { |
12 | | - // Create tar archive file |
13 | | - let tar_gz_path = working_dir |
14 | | - .join(source.file_name().unwrap()) |
15 | | - .with_extension("tar.gz"); |
16 | | - let tar_gz = tokio::fs::File::create(tar_gz_path.as_path()) |
17 | | - .await |
18 | | - .context(format!( |
| 12 | + let source = source.to_owned(); |
| 13 | + let working_dir = working_dir.to_owned(); |
| 14 | + |
| 15 | + tokio::task::spawn_blocking(move || { |
| 16 | + // Create tar archive file |
| 17 | + let tar_gz_path = working_dir |
| 18 | + .join(source.file_name().unwrap()) |
| 19 | + .with_extension("tar.gz"); |
| 20 | + let tar_gz = std::fs::File::create(tar_gz_path.as_path()).context(format!( |
19 | 21 | "Unable to create tar archive for source {}", |
20 | | - quoted_path(source) |
| 22 | + quoted_path(&source) |
21 | 23 | ))?; |
22 | 24 |
|
23 | | - // Create encoder |
24 | | - // TODO: use zstd? May be more performant |
25 | | - let tar_gz_enc = GzipEncoder::new(tar_gz); |
26 | | - |
27 | | - // Build tar archive |
28 | | - let mut tar_builder = async_tar::Builder::new( |
29 | | - tokio_util::compat::TokioAsyncWriteCompatExt::compat_write(tar_gz_enc), |
30 | | - ); |
31 | | - tar_builder |
32 | | - .append_dir_all(".", source) |
33 | | - .await |
34 | | - .context(format!( |
| 25 | + // Create encoder |
| 26 | + // TODO: use zstd? May be more performant |
| 27 | + let tar_gz_enc = GzEncoder::new(tar_gz, flate2::Compression::default()); |
| 28 | + |
| 29 | + // Build tar archive |
| 30 | + let mut tar_builder = tar::Builder::new(tar_gz_enc); |
| 31 | + tar_builder.append_dir_all(".", &source).context(format!( |
35 | 32 | "Unable to create tar archive for source {}", |
36 | | - quoted_path(source) |
| 33 | + quoted_path(&source) |
37 | 34 | ))?; |
38 | | - // Finish writing the archive |
39 | | - tar_builder.finish().await?; |
40 | | - // Shutdown the encoder |
41 | | - use tokio::io::AsyncWriteExt; |
42 | | - tar_builder |
43 | | - .into_inner() |
44 | | - .await? |
45 | | - .into_inner() |
46 | | - .shutdown() |
47 | | - .await?; |
48 | | - Ok(tar_gz_path) |
| 35 | + |
| 36 | + // Finish writing the archive and shut down the encoder. |
| 37 | + let inner_enc = tar_builder.into_inner()?; |
| 38 | + inner_enc.finish()?; |
| 39 | + |
| 40 | + Ok(tar_gz_path) |
| 41 | + }) |
| 42 | + .await? |
49 | 43 | } |
50 | 44 |
|
51 | 45 | /// Unpack a compressed archive existing at source into dest |
52 | 46 | pub async fn unarchive(source: &Path, dest: &Path) -> Result<()> { |
53 | | - let decoder = GzipDecoder::new(tokio::io::BufReader::new( |
54 | | - tokio::fs::File::open(source).await?, |
55 | | - )); |
56 | | - let archive = Archive::new(tokio_util::compat::TokioAsyncReadCompatExt::compat(decoder)); |
57 | | - if let Err(e) = archive.unpack(dest).await { |
58 | | - return Err(e.into()); |
59 | | - }; |
60 | | - Ok(()) |
| 47 | + let source = source.to_owned(); |
| 48 | + let dest = dest.to_owned(); |
| 49 | + |
| 50 | + tokio::task::spawn_blocking(move || { |
| 51 | + let decoder = GzDecoder::new(std::fs::File::open(&source)?); |
| 52 | + let mut archive = Archive::new(decoder); |
| 53 | + if let Err(e) = archive.unpack(&dest) { |
| 54 | + return Err(e.into()); |
| 55 | + }; |
| 56 | + Ok(()) |
| 57 | + }) |
| 58 | + .await? |
61 | 59 | } |
0 commit comments