Skip to content

Commit

Permalink
fix: remove the need for tmp folder in tarball
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jul 21, 2023
1 parent 5561160 commit 521ef4b
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions crates/tarball/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
mod symlink;

use std::{
env,
fs::{self},
io::Cursor,
io::{Cursor, Read, Write},
path::PathBuf,
};

use libdeflater::{DecompressionError, Decompressor};
use ssri::{Algorithm, IntegrityOpts};
use tar::Archive;
use thiserror::Error;
use tracing::{event, instrument, Level};
use tracing::instrument;

use crate::symlink::symlink_dir;

Expand Down Expand Up @@ -54,13 +53,26 @@ impl TarballManager {
integrity: &str,
extract_path: &PathBuf,
) -> Result<(), TarballError> {
let unpack_path = env::temp_dir().join(integrity);
event!(Level::DEBUG, "unpacking tarball to {}", unpack_path.display());
let mut archive = Archive::new(Cursor::new(data));
archive.unpack(&unpack_path)?;
fs::create_dir_all(extract_path)?;
fs::rename(unpack_path.join("package"), extract_path)?;
fs::remove_dir_all(&unpack_path)?;

for entry in archive.entries()? {
let mut entry = entry?;

// Read the contents of the entry
let mut buffer = Vec::with_capacity(entry.size() as usize);
entry.read_to_end(&mut buffer)?;

let entry_path = entry.path().unwrap();
let cleaned_entry_path: PathBuf = entry_path.components().skip(1).collect();
let file_path = extract_path.join(cleaned_entry_path);

if !file_path.exists() {
let parent_dir = file_path.parent().unwrap();
fs::create_dir_all(parent_dir)?;
let mut file = fs::File::create(&file_path)?;
file.write_all(&buffer)?;
}
}
Ok(())
}

Expand Down

0 comments on commit 521ef4b

Please sign in to comment.