Skip to content

Commit

Permalink
ZStandard compression, minor fixes, code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
harshdoesdev committed Oct 3, 2023
1 parent b1fe270 commit a999df1
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 35 deletions.
41 changes: 41 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ indicatif = "0.17.7"
dirs = "5.0.1"
sha2 = "0.10.8"
tar = "0.4.40"
zstd = "0.12"
tempfile = "3.8.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
17 changes: 11 additions & 6 deletions src/commands/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ pub async fn download(
return Ok(());
};
let output_dir = tempfile::tempdir()?;

let pb = ProgressBar::new_spinner();
pb.enable_steady_tick(std::time::Duration::new(0, 500));
pb.set_style(ProgressStyle::default_spinner()
.template("[{spinner}] {prefix} {wide_msg}")
.unwrap()
.tick_chars("/|\\- "));
pb.set_style(
ProgressStyle::default_spinner()
.template("[{spinner}] {prefix} {wide_msg}")
.unwrap()
.tick_chars("/|\\- "),
);
pb.set_message("Looking for artifacts...");
let mut output_exists_req = remote_client.exists(
vercel_cache_helper::vercel::constants::FASTN_VERCEL_REMOTE_CACHE_HASH.to_string(),
Expand Down Expand Up @@ -70,7 +72,10 @@ pub async fn download(
.seek(std::io::SeekFrom::Start(0))
.unwrap();

vercel_cache_helper::utils::extract_tar_gz(output_dir_archive, &output_dir.path())?;
vercel_cache_helper::utils::extract_tar_zst(
output_dir_archive,
&output_dir.path().to_path_buf(),
)?;

let temp_build_dir = output_dir.path().join(".build");
let temp_cache_dir = output_dir.path().join("cache");
Expand Down
12 changes: 7 additions & 5 deletions src/commands/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ pub async fn upload(

let pb = ProgressBar::new_spinner();
pb.enable_steady_tick(std::time::Duration::new(0, 500));
pb.set_style(ProgressStyle::default_spinner()
.template("[{spinner}] {prefix} {wide_msg}")
.unwrap()
.tick_chars("/|\\- "));
pb.set_style(
ProgressStyle::default_spinner()
.template("[{spinner}] {prefix} {wide_msg}")
.unwrap()
.tick_chars("/|\\- "),
);
pb.set_message("Preparing to upload artifacts...");

let build_dir = project_dir.join(".build");
Expand All @@ -37,7 +39,7 @@ pub async fn upload(

let mut output_dir_archive = tempfile::tempfile()?;

vercel_cache_helper::utils::create_tar_gz_archive(
vercel_cache_helper::utils::create_tar_zst_archive(
&output_dir.path().to_path_buf(),
&output_dir_archive,
)?;
Expand Down
58 changes: 34 additions & 24 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ pub fn copy_recursively(
}
Ok(())
}

pub fn create_tar_gz_archive(
pub fn create_tar_zst_archive(
src_folder: &std::path::PathBuf,
dest_file: &std::fs::File,
) -> vercel_cache_helper::Result<()> {
) -> std::io::Result<()> {
println!("Creating archive from: {:?}", src_folder);

if !src_folder.exists() {
Expand All @@ -74,35 +73,46 @@ pub fn create_tar_gz_archive(
return Ok(());
}

let gz_encoder = std::io::BufWriter::new(flate2::write::GzEncoder::new(
dest_file,
flate2::Compression::default(),
));
let mut tar_builder = tar::Builder::new(gz_encoder);
// Create the Zstd encoder with proper error handling
let zst_encoder =
match zstd::stream::write::Encoder::new(dest_file, zstd::DEFAULT_COMPRESSION_LEVEL) {
Ok(encoder) => encoder,
Err(err) => {
println!("Error creating Zstd encoder: {:?}", err);
return Err(err);
}
};

tar_builder.append_dir_all("", src_folder).map_err(|e| {
println!("Error creating archive: {:?}", e);
e
})?;
let mut tar_builder = tar::Builder::new(zst_encoder);

println!("Archive created successfully.");
Ok(())
tar_builder.append_dir_all("", src_folder)?;

// Ensure the archive is properly flushed and closed
match tar_builder.into_inner()?.finish() {
Ok(_) => {
println!("Archive created successfully.");
Ok(())
}
Err(err) => {
println!("Error closing the archive: {:?}", err);
Err(err)
}
}
}

pub fn extract_tar_gz(
file: std::fs::File,
dest_path: &std::path::Path,
) -> vercel_cache_helper::Result<()> {
pub fn extract_tar_zst(file: std::fs::File, dest_path: &std::path::PathBuf) -> std::io::Result<()> {
println!(
"Preparing to extract archive in {}...",
dest_path.to_string_lossy()
);
let archive = flate2::read::GzDecoder::new(file);
let mut archive = tar::Archive::new(archive);
if let Err(err) = archive.unpack(dest_path) {
println!("Error extracting archive: {}", err);
return Err(err.into());
}

let zst_decoder = zstd::stream::read::Decoder::new(file)?;

let mut archive = tar::Archive::new(zst_decoder);

// Ensure that directories are created as needed while extracting
archive.unpack(dest_path)?;

println!("Unpacked archive in: {}", &dest_path.to_string_lossy());
Ok(())
}

0 comments on commit a999df1

Please sign in to comment.