chore: Continue to accept archives with invalid DateTime, and use now_utc() as default only when writing, not reading

This commit is contained in:
Chris Hennick 2024-05-23 09:44:06 -07:00
parent 7a34aa5f41
commit b0c666aa0c
No known key found for this signature in database
GPG key ID: DA47AABA4961C509
3 changed files with 21 additions and 9 deletions

View file

@ -1009,7 +1009,8 @@ fn central_header_to_zip_file_inner<R: Read>(
CompressionMethod::from_u16(compression_method)
},
compression_level: None,
last_modified_time: DateTime::try_from_msdos(last_mod_date, last_mod_time)?,
last_modified_time: DateTime::try_from_msdos(last_mod_date, last_mod_time)
.unwrap_or_else(|_| DateTime::default()),
crc32,
compressed_size: compressed_size as u64,
uncompressed_size: uncompressed_size as u64,
@ -1396,7 +1397,8 @@ pub fn read_zipfile_from_stream<'a, R: Read>(reader: &'a mut R) -> ZipResult<Opt
using_data_descriptor: false,
compression_method,
compression_level: None,
last_modified_time: DateTime::try_from_msdos(last_mod_date, last_mod_time)?,
last_modified_time: DateTime::try_from_msdos(last_mod_date, last_mod_time)
.unwrap_or_else(|_| DateTime::default()),
crc32,
compressed_size: compressed_size as u64,
uncompressed_size: uncompressed_size as u64,

View file

@ -77,6 +77,22 @@ pub struct DateTime {
second: u8,
}
impl DateTime {
/// Returns the current time if possible, otherwise the default of 1980-01-01.
#[cfg(feature = "time")]
pub fn default_for_write() -> Self {
OffsetDateTime::now_utc()
.try_into()
.unwrap_or_else(|_| DateTime::default())
}
/// Returns the current time if possible, otherwise the default of 1980-01-01.
#[cfg(not(feature = "time"))]
pub fn default_for_write() -> Self {
DateTime::default()
}
}
#[cfg(fuzzing)]
impl arbitrary::Arbitrary<'_> for DateTime {
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<Self> {

View file

@ -34,9 +34,6 @@ use flate2::{write::DeflateEncoder, Compression};
#[cfg(feature = "bzip2")]
use bzip2::write::BzEncoder;
#[cfg(feature = "time")]
use time::OffsetDateTime;
#[cfg(feature = "deflate-zopfli")]
use zopfli::Options;
@ -444,10 +441,7 @@ impl<'k, T: FileOptionExtension> Default for FileOptions<'k, T> {
Self {
compression_method: Default::default(),
compression_level: None,
#[cfg(feature = "time")]
last_modified_time: OffsetDateTime::now_utc().try_into().unwrap_or_default(),
#[cfg(not(feature = "time"))]
last_modified_time: DateTime::default(),
last_modified_time: DateTime::default_for_write(),
permissions: None,
large_file: false,
encrypt_with: None,