Skip to content

Commit

Permalink
refactor: Change type of last_modified_time to Option<DateTime>
Browse files Browse the repository at this point in the history
  • Loading branch information
Pr0methean committed May 23, 2024
1 parent e085483 commit 7d61377
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
17 changes: 9 additions & 8 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub(crate) fn find_content<'a>(
pub(crate) fn make_crypto_reader<'a>(
compression_method: CompressionMethod,
crc32: u32,
last_modified_time: DateTime,
mut last_modified_time: Option<DateTime>,
using_data_descriptor: bool,
reader: io::Take<&'a mut dyn Read>,
password: Option<&[u8]>,
Expand All @@ -269,7 +269,10 @@ pub(crate) fn make_crypto_reader<'a>(
vendor_version,
},
(Some(password), None) => {
let validator = if using_data_descriptor {
if !using_data_descriptor {
last_modified_time = None;
}
let validator = if let Some(last_modified_time) = last_modified_time {
ZipCryptoValidator::InfoZipMsdosTime(last_modified_time.timepart())
} else {
ZipCryptoValidator::PkzipCrc32(crc32)
Expand Down Expand Up @@ -1009,8 +1012,7 @@ 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)
.unwrap_or_else(|_| DateTime::default()),
last_modified_time: DateTime::try_from_msdos(last_mod_date, last_mod_time).ok(),
crc32,
compressed_size: compressed_size as u64,
uncompressed_size: uncompressed_size as u64,
Expand Down Expand Up @@ -1252,7 +1254,7 @@ impl<'a> ZipFile<'a> {
}

/// Get the time the file was last modified
pub fn last_modified(&self) -> DateTime {
pub fn last_modified(&self) -> Option<DateTime> {
self.data.last_modified_time
}
/// Returns whether the file is actually a directory
Expand Down Expand Up @@ -1397,8 +1399,7 @@ 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)
.unwrap_or_else(|_| DateTime::default()),
last_modified_time: DateTime::try_from_msdos(last_mod_date, last_mod_time).ok(),
crc32,
compressed_size: compressed_size as u64,
uncompressed_size: uncompressed_size as u64,
Expand Down Expand Up @@ -1525,7 +1526,7 @@ mod test {
let mut file1 = reader1.by_index(0).unwrap();
let mut file2 = reader2.by_index(0).unwrap();

let t = file1.last_modified();
let t = file1.last_modified().unwrap();
assert_eq!(
(
t.year(),
Expand Down
4 changes: 2 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ pub struct ZipFileData {
/// Compression level to store the file
pub compression_level: Option<i64>,
/// Last modified time. This will only have a 2 second precision.
pub last_modified_time: DateTime,
pub last_modified_time: Option<DateTime>,
/// CRC32 checksum
pub crc32: u32,
/// Size of the file in the ZIP
Expand Down Expand Up @@ -615,7 +615,7 @@ mod test {
using_data_descriptor: false,
compression_method: crate::compression::CompressionMethod::Stored,
compression_level: None,
last_modified_time: DateTime::default(),
last_modified_time: None,
crc32: 0,
compressed_size: 0,
uncompressed_size: 0,
Expand Down
18 changes: 10 additions & 8 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ impl<A: Read + Write + Seek> ZipWriter<A> {
let mut options = FileOptions::<ExtendedFileOptions> {
compression_method: src_data.compression_method,
compression_level: src_data.compression_level,
last_modified_time: src_data.last_modified_time,
last_modified_time: src_data.last_modified_time.unwrap_or_else(DateTime::default_for_write),
permissions: src_data.unix_mode(),
large_file: src_data.large_file,
encrypt_with: None,
Expand All @@ -594,7 +594,7 @@ impl<A: Read + Write + Seek> ZipWriter<A> {
let mut options = FileOptions::<()> {
compression_method: src_data.compression_method,
compression_level: src_data.compression_level,
last_modified_time: src_data.last_modified_time,
last_modified_time: src_data.last_modified_time.unwrap_or_else(DateTime::default_for_write),
permissions: src_data.unix_mode(),
large_file: src_data.large_file,
encrypt_with: None,
Expand Down Expand Up @@ -782,14 +782,15 @@ impl<W: Write + Seek> ZipWriter<W> {
),
_ => (options.compression_method, None),
};
let last_modified_time = options.last_modified_time;
let file = ZipFileData {
system: System::Unix,
version_made_by: DEFAULT_VERSION,
encrypted: options.encrypt_with.is_some(),
using_data_descriptor: false,
compression_method,
compression_level: options.compression_level,
last_modified_time: options.last_modified_time,
last_modified_time: Some(options.last_modified_time),
crc32: raw_values.crc32,
compressed_size: raw_values.compressed_size,
uncompressed_size: raw_values.uncompressed_size,
Expand Down Expand Up @@ -826,8 +827,8 @@ impl<W: Write + Seek> ZipWriter<W> {
#[allow(deprecated)]
writer.write_u16_le(file.compression_method.to_u16())?;
// last mod file time and last mod file date
writer.write_u16_le(file.last_modified_time.timepart())?;
writer.write_u16_le(file.last_modified_time.datepart())?;
writer.write_u16_le(last_modified_time.timepart())?;
writer.write_u16_le(last_modified_time.datepart())?;
// crc-32
writer.write_u32_le(file.crc32)?;
// compressed size and uncompressed size
Expand Down Expand Up @@ -1193,7 +1194,7 @@ impl<W: Write + Seek> ZipWriter<W> {
{
let mut options = SimpleFileOptions::default()
.large_file(file.compressed_size().max(file.size()) > spec::ZIP64_BYTES_THR)
.last_modified_time(file.last_modified())
.last_modified_time(file.last_modified().unwrap_or_else(DateTime::default_for_write))
.compression_method(file.compression());
if let Some(perms) = file.unix_mode() {
options = options.unix_permissions(perms);
Expand Down Expand Up @@ -1813,9 +1814,10 @@ fn write_central_directory_header<T: Write>(writer: &mut T, file: &ZipFileData)
// compression method
#[allow(deprecated)]
writer.write_u16_le(file.compression_method.to_u16())?;
let last_modified_time = file.last_modified_time.unwrap_or_else(DateTime::default_for_write);
// last mod file time + date
writer.write_u16_le(file.last_modified_time.timepart())?;
writer.write_u16_le(file.last_modified_time.datepart())?;
writer.write_u16_le(last_modified_time.timepart())?;
writer.write_u16_le(last_modified_time.datepart())?;
// crc-32
writer.write_u32_le(file.crc32)?;
// compressed size
Expand Down

0 comments on commit 7d61377

Please sign in to comment.