Skip to content

feat(extra_fields): add type-safe constructors and FileOptions builders for ExtendedTimestamp and NTFS - #1296

Merged
Pr0methean merged 3 commits into
zip-rs:masterfrom
ByGh00st:feat/type-safe-extra-fields
Nov 14, 2026
Merged

Pr0methean merged 3 commits into
zip-rs:masterfrom
ByGh00st:feat/type-safe-extra-fields

Conversation

@ByGh00st

@ByGh00st ByGh00st commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1285.

Summary of Changes:

  1. Type-Safe Constructors:
    • Added ExtendedTimestamp::new(modified, accessed, created) and ExtendedTimestamp::from_mod_time(modified).
    • Added Ntfs::new(mtime, atime, ctime) and #[cfg(feature = "nt-time")] Ntfs::from_file_times(...).
  2. Serialization & Raw Copy Preservation:
  3. Fluent FileOptions Builders:
    • Added .with_extended_timestamp(...) and .with_ntfs_times(...) to ExtendedFileOptions, FullFileOptions, and SimpleFileOptions. Users no longer need to manually craft raw binary byte vectors when creating entries with high-precision timestamps.
  4. Verification:
    • Added tests/type_safe_extra_fields.rs testing creation via fluent builders, validation via ZipArchive, and preservation across raw_copy_file.
    • All 207 library tests and cargo clippy --all-targets pass with zero warnings.

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…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.
@ByGh00st
ByGh00st requested a review from Pr0methean November 11, 2026 18:22

@amazon-q-developer amazon-q-developer Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/write/options.rs Outdated
) -> ZipResult<()> {
let ts = ExtendedTimestamp::new(modified, accessed, created);
let mut buf = Vec::new();
ts.write(&mut buf, false)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛑 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.

Suggested change
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.
@Its-Just-Nans

Its-Just-Nans commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Hi @ByGh00st

This one is quite similar to #1291 and #1293 no ?

@ByGh00st

Copy link
Copy Markdown
Contributor Author

Hi @Its-Just-Nans,

They intersect on the underlying serialization (ExtraField::write and full_size), but they serve different purposes:

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.

@Pr0methean
Pr0methean added this pull request to the merge queue Nov 14, 2026
Merged via the queue into zip-rs:master with commit fcc4f78 Nov 14, 2026
356 of 359 checks passed
@ByGh00st
ByGh00st deleted the feat/type-safe-extra-fields branch November 14, 2026 13:24

@Its-Just-Nans Its-Just-Nans left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of #1310

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

raw_copy_file silently drops the extended timestamp (0x5455) extra field

3 participants