Repository navigation
feat(extra_fields): add type-safe constructors and FileOptions builders for ExtendedTimestamp and NTFS - #1296
Conversation
…rs for ExtendedTimestamp and NTFS Add public const constructors for ExtendedTimestamp and Ntfs extra fields, along with serialization (write) and size (full_size) implementations. Provide fluent builder methods on ExtendedFileOptions, FullFileOptions, and SimpleFileOptions (.with_extended_timestamp and .with_ntfs_times) to configure high-precision timestamps without manual byte packing. Fixes zip-rs#1285 by ensuring ExtendedTimestamp and Ntfs extra fields are preserved during raw copies.
There was a problem hiding this comment.
Review Summary
This PR adds type-safe constructors for ExtendedTimestamp and Ntfs extra fields, serialization (full_size / write) for both, fixes silent field loss during raw_copy_file by wiring up ExtraField::size and ExtraField::write, and exposes fluent builders on FileOptions.
The fix to ExtraField::write / ExtraField::size (correctly dispatching to ntfs.write / ntfs.full_size() and extended_timestamp.write / extended_timestamp.full_size() instead of the former no-ops) is the critical bug fix for #1285 and is correct.
Critical Issue Found
add_extended_timestamp silently drops accessed and created timestamps (see inline comment on src/write/options.rs line 264). The field is serialized with is_local_header=false, producing the central-directory form that contains only modified. The resulting bytes are then stored verbatim as a CustomExtraField and written identically to both the local and central headers — so the local header never receives the access or creation times. The fix is to serialize with is_local_header=true; CustomExtraField::write and the existing ExtraField::write dispatch will handle the correct truncation for the central-directory copy at write time.
The integration test in tests/type_safe_extra_fields.rs passes None for accessed and created, so this bug is not covered by the new test suite.
You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.
| ) -> ZipResult<()> { | ||
| let ts = ExtendedTimestamp::new(modified, accessed, created); | ||
| let mut buf = Vec::new(); | ||
| ts.write(&mut buf, false)?; |
There was a problem hiding this comment.
🛑 Logic Error: add_extended_timestamp serializes the field using is_local_header=false, which produces the central-directory form containing only the modification time. The resulting bytes are then stored as a CustomExtraField and written verbatim to both the local and central headers. This silently drops accessed and created from the local header, even when the caller explicitly provides them.
The serialization must use is_local_header=true so the payload stored in the CustomExtraField contains all three timestamps. The central-directory write path (ExtraField::write with is_local_header=false) will truncate to just the modification time at write time, which is correct.
| ts.write(&mut buf, false)?; | |
| ts.write(&mut buf, true)?; |
…tamp Serialize with is_local_header=true in add_extended_timestamp so the CustomExtraField payload retains access and creation times for the local file header. Update tests to verify that local header carries all three timestamps.
|
Hi @Its-Just-Nans, They intersect on the underlying serialization (
The serialization logic in this PR was implemented as the necessary foundation so that newly created typed fields actually get written to the archive. If either #1291 or #1293 is merged first to fix raw copying, I will happily rebase #1296 on top of it so this PR strictly focuses on the type-safe public builder API and tests. |
Its-Just-Nans
left a comment
There was a problem hiding this comment.
The flags variable in the central header refers to the local header, not to himself
| if self.created.is_some() { | ||
| flags |= u8::from(ExtendedTimestampFlags::Created); | ||
| } | ||
| } |
There was a problem hiding this comment.
@ByGh00st
Are we sure about this variable flags ?
Central-header version:
Value Size Description
----- ---- -----------
(time) 0x5455 Short tag for this extra block type ("UT")
TSize Short total data size for this block
Flags Byte info bits (refers to local header!)
(ModTime) Long time of last modification (UTC/GMT)
There was a problem hiding this comment.
Good catch! You're completely right about extrafld.txt -- the Flags byte in the central directory 0x5455 block refers to what is present in the local header, not the truncated central-header payload.
PR #1310 looks good to me, and raw_copy_file stays consistent too since ZipArchive parses from the central directory (so accessed and created are None there anyway). Left a couple of tiny comment/follow-up notes over on #1310!
Fixes #1285.
Summary of Changes:
ExtendedTimestamp::new(modified, accessed, created)andExtendedTimestamp::from_mod_time(modified).Ntfs::new(mtime, atime, ctime)and#[cfg(feature = "nt-time")] Ntfs::from_file_times(...).full_sizeandwritefor bothExtendedTimestamp(0x5455) andNtfs(0x000a), properly handling local vs. central directory flags for extended timestamps.ExtraField::sizeandExtraField::writearms, resolving silent field loss duringraw_copy_file(raw_copy_file silently drops the extended timestamp (0x5455) extra field #1285).FileOptionsBuilders:.with_extended_timestamp(...)and.with_ntfs_times(...)toExtendedFileOptions,FullFileOptions, andSimpleFileOptions. Users no longer need to manually craft raw binary byte vectors when creating entries with high-precision timestamps.tests/type_safe_extra_fields.rstesting creation via fluent builders, validation viaZipArchive, and preservation acrossraw_copy_file.cargo clippy --all-targetspass with zero warnings.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.