use std::fmt;
use std::time::Duration;
use crate::IoKind;
use crate::Skips;
use crate::closed_enum::closed_enum;
closed_enum! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CreationWarningKind {
ZipRatioOverDefaultLimit => "zip_ratio_over_default_limit",
ZipRatioCheckFailed => "zip_ratio_check_failed",
DirectoryNotSynced => "directory_not_synced",
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum CreationWarning {
ZipRatioOverDefaultLimit {
entries: usize,
limit: f64,
needed_ratio: f64,
},
ZipRatioCheckFailed {
reason: String,
},
DirectoryNotSynced {
io_kind: IoKind,
},
}
impl CreationWarning {
#[must_use]
pub const fn kind(&self) -> CreationWarningKind {
match self {
Self::ZipRatioOverDefaultLimit { .. } => CreationWarningKind::ZipRatioOverDefaultLimit,
Self::ZipRatioCheckFailed { .. } => CreationWarningKind::ZipRatioCheckFailed,
Self::DirectoryNotSynced { .. } => CreationWarningKind::DirectoryNotSynced,
}
}
#[must_use]
pub fn redacted(&self) -> Self {
match self {
Self::ZipRatioOverDefaultLimit { .. }
| Self::ZipRatioCheckFailed { .. }
| Self::DirectoryNotSynced { .. } => self.clone(),
}
}
}
impl fmt::Display for CreationWarning {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ZipRatioOverDefaultLimit {
entries,
limit,
needed_ratio,
} => {
let noun = if *entries == 1 { "entry" } else { "entries" };
write!(
f,
"ZIP archive has {entries} {noun} over the default extraction ratio limit of \
{limit}x; default extraction refuses it as a zip bomb unless the maximum \
compression ratio is at least {needed_ratio}"
)
}
Self::ZipRatioCheckFailed { reason } => write!(
f,
"could not check the ZIP entries against the default extraction ratio limit: \
{reason}"
),
Self::DirectoryNotSynced { io_kind } => write!(
f,
"the directory that holds the archive could not be synced ({io_kind}); the \
archive may not survive a crash"
),
}
}
}
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct CreationReport {
pub files_added: usize,
pub directories_added: usize,
pub symlinks_added: usize,
pub bytes_written: u64,
pub bytes_compressed: u64,
pub duration: Duration,
pub skipped: Skips,
pub warnings: Vec<CreationWarning>,
}
impl CreationReport {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn add_warning(&mut self, warning: CreationWarning) {
self.warnings.push(warning);
}
#[must_use]
pub fn files_skipped(&self) -> usize {
self.skipped.total()
}
#[must_use]
pub fn has_warnings(&self) -> bool {
!self.warnings.is_empty()
}
#[must_use]
pub fn compression_ratio(&self) -> f64 {
if self.bytes_compressed == 0 || self.bytes_written == 0 {
return 0.0;
}
self.bytes_written as f64 / self.bytes_compressed as f64
}
#[must_use]
pub fn compression_percentage(&self) -> f64 {
if self.bytes_written == 0 {
return 0.0;
}
if self.bytes_compressed == 0 {
return 100.0;
}
let saved = self.bytes_written.saturating_sub(self.bytes_compressed);
(saved as f64 / self.bytes_written as f64) * 100.0
}
#[must_use]
pub fn total_items(&self) -> usize {
self.files_added
.saturating_add(self.directories_added)
.saturating_add(self.symlinks_added)
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)] mod tests {
use super::*;
use crate::Skip;
use crate::SkipKind;
#[test]
fn test_total_items_saturates_instead_of_overflowing() {
let mut report = CreationReport::new();
report.files_added = usize::MAX;
report.directories_added = 1;
assert_eq!(report.total_items(), usize::MAX);
}
#[test]
fn test_creation_report_default() {
let report = CreationReport::default();
assert_eq!(report.files_added, 0);
assert_eq!(report.directories_added, 0);
assert_eq!(report.symlinks_added, 0);
assert_eq!(report.bytes_written, 0);
assert_eq!(report.bytes_compressed, 0);
assert_eq!(report.duration, Duration::default());
assert_eq!(report.files_skipped(), 0);
assert!(report.warnings.is_empty());
assert!(!report.has_warnings());
}
#[test]
fn test_creation_report_new() {
let report = CreationReport::new();
assert_eq!(report.files_added, 0);
assert!(!report.has_warnings());
}
#[test]
#[allow(clippy::float_cmp)]
fn test_creation_report_compression_ratio() {
let mut report = CreationReport::new();
report.bytes_written = 1000;
report.bytes_compressed = 500;
assert_eq!(report.compression_ratio(), 2.0);
report.bytes_written = 1000;
report.bytes_compressed = 1000;
assert_eq!(report.compression_ratio(), 1.0);
report.bytes_written = 500;
report.bytes_compressed = 1000;
assert_eq!(report.compression_ratio(), 0.5);
report.bytes_written = 1000;
report.bytes_compressed = 0;
assert_eq!(report.compression_ratio(), 0.0);
report.bytes_written = 0;
report.bytes_compressed = 500;
assert_eq!(report.compression_ratio(), 0.0);
report.bytes_written = 0;
report.bytes_compressed = 0;
assert_eq!(report.compression_ratio(), 0.0);
}
#[test]
#[allow(clippy::float_cmp)]
fn test_creation_report_compression_percentage() {
let mut report = CreationReport::new();
report.bytes_written = 1000;
report.bytes_compressed = 500;
assert_eq!(report.compression_percentage(), 50.0);
report.bytes_written = 1000;
report.bytes_compressed = 250;
assert_eq!(report.compression_percentage(), 75.0);
report.bytes_written = 1000;
report.bytes_compressed = 1000;
assert_eq!(report.compression_percentage(), 0.0);
report.bytes_written = 500;
report.bytes_compressed = 1000;
assert_eq!(report.compression_percentage(), 0.0);
report.bytes_written = 1000;
report.bytes_compressed = 0;
assert_eq!(report.compression_percentage(), 100.0);
report.bytes_written = 0;
report.bytes_compressed = 500;
assert_eq!(report.compression_percentage(), 0.0);
report.bytes_written = 0;
report.bytes_compressed = 0;
assert_eq!(report.compression_percentage(), 0.0);
}
#[test]
fn test_creation_report_warnings() {
let mut report = CreationReport::new();
assert!(!report.has_warnings());
let warning = CreationWarning::ZipRatioCheckFailed {
reason: "Warning 1".into(),
};
report.add_warning(warning.clone());
assert!(report.has_warnings());
assert_eq!(report.warnings, vec![warning]);
}
#[test]
fn test_creation_warning_text() {
let over = CreationWarning::ZipRatioOverDefaultLimit {
entries: 2,
limit: 100.0,
needed_ratio: 250.0,
};
assert_eq!(
over.to_string(),
"ZIP archive has 2 entries over the default extraction ratio limit of 100x; default \
extraction refuses it as a zip bomb unless the maximum compression ratio is at \
least 250"
);
}
#[test]
fn test_creation_report_total_items() {
let mut report = CreationReport::new();
assert_eq!(report.total_items(), 0);
report.files_added = 10;
report.directories_added = 5;
report.symlinks_added = 2;
assert_eq!(report.total_items(), 17);
report.files_added = 0;
assert_eq!(report.total_items(), 7);
}
#[test]
fn test_creation_report_real_scenario() {
let mut report = CreationReport::new();
report.files_added = 100;
report.directories_added = 20;
report.symlinks_added = 5;
report.bytes_written = 10 * 1024 * 1024; report.bytes_compressed = 3 * 1024 * 1024; report.duration = Duration::from_secs(2);
for i in 0..3 {
report
.skipped
.record(Skip::new(SkipKind::OverMaxFileSize, format!("big{i}")))
.unwrap();
}
report.add_warning(CreationWarning::ZipRatioCheckFailed {
reason: "unreadable".into(),
});
assert_eq!(report.files_skipped(), 3);
assert_eq!(report.total_items(), 125);
assert!(report.has_warnings());
assert_eq!(report.warnings.len(), 1);
let ratio = report.compression_ratio();
assert!((ratio - 3.333).abs() < 0.01);
let percentage = report.compression_percentage();
assert!((percentage - 70.0).abs() < 0.1);
}
}