From eb8c0af5222efb88e236c9d68b720f1a3a42ada4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Mon, 6 Mar 2023 12:19:07 -0800 Subject: [PATCH] fix(extract): need to rewind NamedTempFile before extraction --- crates/nassun/src/tarball.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/crates/nassun/src/tarball.rs b/crates/nassun/src/tarball.rs index fdf1bfef..b5b35a9b 100644 --- a/crates/nassun/src/tarball.rs +++ b/crates/nassun/src/tarball.rs @@ -1,4 +1,4 @@ -use std::io::Read; +use std::io::{Read, Seek}; #[cfg(not(target_arch = "wasm32"))] use std::io::Write; #[cfg(not(target_arch = "wasm32"))] @@ -185,7 +185,7 @@ pub(crate) enum TempTarball { #[cfg(not(target_arch = "wasm32"))] impl TempTarball { pub(crate) fn extract_to_dir( - self, + mut self, dir: &Path, tarball_integrity: Option, cache: Option<&Path>, @@ -193,6 +193,8 @@ impl TempTarball { let mut file_index = serde_json::Map::new(); let mut drain_buf = [0u8; 1024 * 8]; + self.rewind()?; + let mut reader = std::io::BufReader::new(self); let mut integrity = IntegrityOpts::new().algorithm(ssri::Algorithm::Sha512); let mut tee_reader = io_tee::TeeReader::new(&mut reader, &mut integrity); @@ -212,7 +214,7 @@ impl TempTarball { for file in files { let mut file = file.map_err(|e| { - NassunError::ExtractIoError(e, None, "reading entry from tarball".into()) + NassunError::ExtractIoError(e, Some(PathBuf::from(dir)), "reading entry from tarball".into()) })?; let header = file.header(); let entry_path = header.path().map_err(|e| { @@ -343,6 +345,16 @@ impl std::io::Read for TempTarball { } } +#[cfg(not(target_arch = "wasm32"))] +impl std::io::Seek for TempTarball { + fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result { + match self { + TempTarball::File(f) => f.seek(pos), + TempTarball::Memory(m) => m.seek(pos), + } + } +} + #[cfg(not(target_arch = "wasm32"))] fn strip_one(path: &Path) -> Option<&Path> { let mut comps = path.components();