cli: determine archive type based on filename instead of path (#224602)

Refs #219632

Seems like PRSS sometimes(?) doesn't return the full archive name in
the response. I don't reproduce this, but others consistently do. This
PR removes the dependency on the URL path and instead checks for the
gzip magic number in the first two bytes of the archive to figure out
what to do.
This commit is contained in:
Connor Peet
2024-08-02 09:34:11 -07:00
committed by GitHub
parent 331b772a5e
commit fcfd4be5b8
3 changed files with 25 additions and 20 deletions

View File

@@ -5,8 +5,8 @@
use crate::util::errors::{wrap, WrappedError};
use flate2::read::GzDecoder;
use std::fs;
use std::io::Seek;
use std::fs::{self, File};
use std::io::{Read, Seek};
use std::path::{Path, PathBuf};
use tar::Archive;
@@ -57,16 +57,13 @@ fn should_skip_first_segment(file: &fs::File) -> Result<(bool, u64), WrappedErro
}
pub fn decompress_tarball<T>(
path: &Path,
mut tar_gz: File,
parent_path: &Path,
mut reporter: T,
) -> Result<(), WrappedError>
where
T: ReportCopyProgress,
{
let mut tar_gz = fs::File::open(path)
.map_err(|e| wrap(e, format!("error opening file {}", path.display())))?;
let (skip_first, num_entries) = should_skip_first_segment(&tar_gz)?;
let report_progress_every = num_entries / 20;
let mut entries_so_far = 0;
@@ -81,7 +78,7 @@ where
let mut archive = Archive::new(tar);
archive
.entries()
.map_err(|e| wrap(e, format!("error opening archive {}", path.display())))?
.map_err(|e| wrap(e, "error opening archive"))?
.filter_map(|e| e.ok())
.try_for_each::<_, Result<_, WrappedError>>(|mut entry| {
// approximate progress based on where we are in the archive:
@@ -118,3 +115,13 @@ where
Ok(())
}
pub fn has_gzip_header(path: &Path) -> std::io::Result<(File, bool)> {
let mut file = fs::File::open(path)?;
let mut header = [0; 2];
let _ = file.read_exact(&mut header);
file.rewind()?;
Ok((file, header[0] == 0x1f && header[1] == 0x8b))
}

View File

@@ -44,15 +44,12 @@ fn should_skip_first_segment(archive: &mut ZipArchive<File>) -> bool {
archive.len() > 1 // prefix removal is invalid if there's only a single file
}
pub fn unzip_file<T>(path: &Path, parent_path: &Path, mut reporter: T) -> Result<(), WrappedError>
pub fn unzip_file<T>(file: File, parent_path: &Path, mut reporter: T) -> Result<(), WrappedError>
where
T: ReportCopyProgress,
{
let file = fs::File::open(path)
.map_err(|e| wrap(e, format!("unable to open file {}", path.display())))?;
let mut archive = zip::ZipArchive::new(file)
.map_err(|e| wrap(e, format!("failed to open zip archive {}", path.display())))?;
let mut archive =
zip::ZipArchive::new(file).map_err(|e| wrap(e, "failed to open zip archive"))?;
let skip_segments_no = usize::from(should_skip_first_segment(&mut archive));
let report_progress_every = archive.len() / 20;