use std::io::Cursor;
use std::path::Path;
use std::path::PathBuf;
use crate::ArchiveReader;
use crate::ArchiveWriter;
use crate::DecodeReuse;
use crate::EntrySink;
use crate::ExtractionOptions;
use crate::ExtractionReport;
use crate::ListOptions;
use crate::NoopProgress;
use crate::ProgressCallback;
use crate::Result;
use crate::SecurityConfig;
use crate::SinkOptions;
use crate::VerificationOptions;
use crate::api::extract_to_sink_reusing;
use crate::api::extract_validated;
use crate::api::resolve_output_format;
use crate::closed_enum::closed_enum;
use crate::creation::CreationConfig;
use crate::creation::CreationReport;
use crate::creation::progress::check_cancelled;
use crate::creation::staged::StagedOutput;
use crate::creation::tar::write_tar;
use crate::creation::zip::warn_on_extraction_ratio;
use crate::creation::zip::write_zip;
use crate::formats::detect::CreatableFormat;
use crate::formats::detect::CreationFamily;
use crate::inspection::ArchiveManifest;
use crate::inspection::VerificationReport;
use crate::inspection::list::ListPass;
use crate::listed::ListedArchive;
use crate::staging::extract_staged;
struct OptionalProgress<'a>(Option<&'a mut dyn ProgressCallback>);
impl std::fmt::Debug for OptionalProgress<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(if self.0.is_some() { "Some(..)" } else { "None" })
}
}
impl OptionalProgress<'_> {
fn run<R>(self, operation: impl FnOnce(&mut dyn ProgressCallback) -> R) -> R {
match self.0 {
Some(progress) => operation(progress),
None => operation(&mut NoopProgress),
}
}
}
#[derive(Debug)]
struct Carrier<'a, C, O> {
config: &'a C,
options: O,
progress: OptionalProgress<'a>,
}
impl<'a, C, O> Carrier<'a, C, O> {
const fn new(config: &'a C, options: O) -> Self {
Self {
config,
options,
progress: OptionalProgress(None),
}
}
fn with_options(mut self, options: O) -> Self {
self.options = options;
self
}
fn with_progress(mut self, progress: &'a mut dyn ProgressCallback) -> Self {
self.progress = OptionalProgress(Some(progress));
self
}
fn run<R>(self, operation: impl FnOnce(&'a C, O, &mut dyn ProgressCallback) -> R) -> R {
let Self {
config,
options,
progress,
} = self;
progress.run(|progress| operation(config, options, progress))
}
}
#[derive(Debug)]
pub struct ExtractRequest<'a>(Carrier<'a, SecurityConfig, ExtractionOptions>);
impl<'a> ExtractRequest<'a> {
#[must_use]
pub fn new(config: &'a SecurityConfig) -> Self {
Self(Carrier::new(config, ExtractionOptions::default()))
}
#[must_use]
pub fn with_options(self, options: ExtractionOptions) -> Self {
Self(self.0.with_options(options))
}
#[must_use]
pub fn with_progress(self, progress: &'a mut dyn ProgressCallback) -> Self {
Self(self.0.with_progress(progress))
}
pub fn extract(
self,
archive: impl AsRef<Path>,
output_dir: impl AsRef<Path>,
) -> Result<ExtractionReport> {
let Self(request) = self;
request.run(|config, options, progress| {
extract_staged(
output_dir.as_ref(),
&options,
&config.selection,
Some(archive.as_ref()),
progress,
|dir, options, progress| {
extract_validated(archive.as_ref(), dir, config, options, progress)
},
)
})
}
}
#[derive(Debug)]
pub struct ListRequest<'a>(Carrier<'a, SecurityConfig, ListOptions>);
impl<'a> ListRequest<'a> {
#[must_use]
pub fn new(config: &'a SecurityConfig) -> Self {
Self(Carrier::new(config, ListOptions::default()))
}
#[must_use]
pub fn with_options(self, options: ListOptions) -> Self {
Self(self.0.with_options(options))
}
#[must_use]
pub fn with_progress(self, progress: &'a mut dyn ProgressCallback) -> Self {
Self(self.0.with_progress(progress))
}
pub fn list(self, archive: impl AsRef<Path>) -> Result<ArchiveManifest> {
let Self(request) = self;
request.run(|config, options, progress| {
crate::inspection::list_archive_with_progress(archive, config, options, progress)
})
}
pub fn list_reader(self, reader: ArchiveReader<'_>) -> Result<ArchiveManifest> {
let Self(request) = self;
request.run(|config, options, progress| {
let manifest = reader.list(
config,
&mut ListPass::strict_with(options.stored_targets).with_progress(progress),
)?;
progress.on_complete();
Ok(manifest)
})
}
pub fn for_extraction(
self,
archive: impl AsRef<Path>,
reuse: DecodeReuse,
) -> Result<ListedArchive> {
let Self(request) = self;
request.run(|config, options, progress| {
ListedArchive::list(archive.as_ref(), config, options, reuse, Some(progress))
})
}
}
#[derive(Debug)]
pub struct VerifyRequest<'a>(Carrier<'a, SecurityConfig, VerificationOptions>);
impl<'a> VerifyRequest<'a> {
#[must_use]
pub fn new(config: &'a SecurityConfig) -> Self {
Self(Carrier::new(config, VerificationOptions::default()))
}
#[must_use]
pub fn with_options(self, options: VerificationOptions) -> Self {
Self(self.0.with_options(options))
}
#[must_use]
pub fn with_progress(self, progress: &'a mut dyn ProgressCallback) -> Self {
Self(self.0.with_progress(progress))
}
pub fn verify(self, archive: impl AsRef<Path>) -> Result<VerificationReport> {
let Self(request) = self;
request.run(|config, options, progress| {
crate::inspection::verify_archive_with_progress(archive, config, options, progress)
})
}
pub fn verify_reader(self, reader: ArchiveReader<'_>) -> Result<VerificationReport> {
let Self(request) = self;
request.run(|config, options, progress| {
let report = reader.verify(config, options, progress)?;
progress.on_complete();
Ok(report)
})
}
}
#[derive(Debug)]
pub struct SinkExtractRequest<'a>(Carrier<'a, SecurityConfig, SinkOptions>);
impl<'a> SinkExtractRequest<'a> {
#[must_use]
pub fn new(config: &'a SecurityConfig) -> Self {
Self(Carrier::new(config, SinkOptions::default()))
}
#[must_use]
pub fn with_options(self, options: SinkOptions) -> Self {
Self(self.0.with_options(options))
}
#[must_use]
pub fn with_progress(self, progress: &'a mut dyn ProgressCallback) -> Self {
Self(self.0.with_progress(progress))
}
pub fn extract<S: EntrySink>(
self,
reader: ArchiveReader<'_>,
sink: &mut S,
) -> Result<ExtractionReport> {
let Self(request) = self;
request.run(|config, options, progress| {
extract_to_sink_reusing(reader, sink, config, options, progress, None)
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum OutputPolicy {
CreateNew,
Replace,
}
closed_enum! {
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Durability {
#[default]
Synced => "synced",
Unsynced => "unsynced",
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum OutputFormat {
#[default]
FromExtension,
Explicit(CreatableFormat),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OutputFile {
path: PathBuf,
policy: OutputPolicy,
format: OutputFormat,
durability: Durability,
}
impl OutputFile {
#[must_use]
pub fn create_new(path: impl Into<PathBuf>) -> Self {
Self::at(path.into(), OutputPolicy::CreateNew)
}
#[must_use]
pub fn replace(path: impl Into<PathBuf>) -> Self {
Self::at(path.into(), OutputPolicy::Replace)
}
const fn at(path: PathBuf, policy: OutputPolicy) -> Self {
Self {
path,
policy,
format: OutputFormat::FromExtension,
durability: Durability::Synced,
}
}
#[must_use]
pub const fn with_format(mut self, format: CreatableFormat) -> Self {
self.format = OutputFormat::Explicit(format);
self
}
#[must_use]
pub const fn with_durability(mut self, durability: Durability) -> Self {
self.durability = durability;
self
}
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
#[must_use]
pub const fn policy(&self) -> OutputPolicy {
self.policy
}
#[must_use]
pub const fn format(&self) -> OutputFormat {
self.format
}
#[must_use]
pub const fn durability(&self) -> Durability {
self.durability
}
}
#[derive(Debug)]
pub struct CreateRequest<'a>(Carrier<'a, CreationConfig, ()>);
impl<'a> CreateRequest<'a> {
#[must_use]
pub const fn new(config: &'a CreationConfig) -> Self {
Self(Carrier::new(config, ()))
}
#[must_use]
pub fn with_progress(self, progress: &'a mut dyn ProgressCallback) -> Self {
Self(self.0.with_progress(progress))
}
pub fn create<P: AsRef<Path>>(
self,
output: &OutputFile,
sources: &[P],
) -> Result<CreationReport> {
let format = resolve_output_format(output)?;
let mut staged = StagedOutput::begin(output.path(), output.policy())?;
let mut report = self
.0
.run(|config, (), progress| -> Result<CreationReport> {
let report = write_staged(&mut staged, format, sources, config, progress)?;
check_cancelled(progress)?;
Ok(report)
})?;
if let Some(warning) = staged.commit(output.durability())? {
report.add_warning(warning);
}
Ok(report)
}
pub fn write<P: AsRef<Path>>(
self,
writer: ArchiveWriter<'_>,
sources: &[P],
) -> Result<CreationReport> {
self.0
.run(|config, (), progress| writer.write(sources, config, progress))
}
pub fn to_bytes<P: AsRef<Path>>(
self,
format: CreatableFormat,
sources: &[P],
) -> Result<(Vec<u8>, CreationReport)> {
let mut out = Cursor::new(Vec::new());
let mut report = self.write(ArchiveWriter::of_type(&mut out, format), sources)?;
if format == CreatableFormat::Zip {
warn_on_extraction_ratio(&mut out, &mut report);
}
Ok((out.into_inner(), report))
}
}
fn write_staged<P: AsRef<Path>>(
staged: &mut StagedOutput,
format: CreatableFormat,
sources: &[P],
config: &CreationConfig,
progress: &mut dyn ProgressCallback,
) -> Result<CreationReport> {
let (file, excluded) = staged.parts();
match format.family() {
CreationFamily::Tar(codec) => write_tar(file, codec, sources, config, excluded, progress),
CreationFamily::Zip => {
let (mut report, file) = write_zip(file, sources, config, excluded, progress)?;
warn_on_extraction_ratio(file, &mut report);
Ok(report)
}
}
}