crates_registry/
pack.rs

1use std::{fs::File, path::Path};
2
3use anyhow::Result;
4use tar::Archive;
5use tempfile::TempDir;
6use tracing::{debug, info};
7
8use crate::{
9    cli::PackArgs,
10    rustup::{download_latest, download_pinned_rust_version},
11};
12
13pub async fn pack(pack_args: PackArgs) -> Result<()> {
14    let root_registry = TempDir::new()?;
15    debug!("Root registry: {}", root_registry.path().display());
16    if !pack_args.rust_versions.is_empty() {
17        download_pinned_rust_version(root_registry.path(), &pack_args).await?;
18    } else {
19        download_latest(root_registry.path(), &pack_args).await?;
20    }
21
22    info!(
23        "Collect file installations to the pack file: {}",
24        pack_args.pack_file.display()
25    );
26
27    let tar_file = File::create(&pack_args.pack_file)?;
28    // let enc = GzEncoder::new(tar_gz, Compression::none());
29    let mut tar = tar::Builder::new(tar_file);
30    tar.append_dir_all(".", root_registry.path())?;
31
32    info!("The packing finished");
33    Ok(())
34}
35
36pub async fn unpack(packed_file: &Path, root_registry: &Path) -> Result<()> {
37    info!(
38        "Unpacking file installations...\n
39        Packed file: {}\n
40        Registry: {}",
41        packed_file.display(),
42        root_registry.display()
43    );
44
45    let tar_file = File::open(packed_file)?;
46    // let enc = GzEncoder::new(tar_gz, Compression::none());
47    let mut archive = Archive::new(tar_file);
48    // TODO: handle history channel files if needed
49    archive.unpack(root_registry)?;
50    info!("The unpacking finished");
51    Ok(())
52}