diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-02-09 17:31:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-09 17:31:12 (GMT) |
commit | 42f87d435ebe4f2834c8fd0b4ce38f0b1627f3ed (patch) | |
tree | ba6d119fa9182fa75f0b447f93e9bff076e52531 /Lib/tarfile.py | |
parent | d29bbc22b08551afce24e933f43bfcb6e980cb08 (diff) | |
download | cpython-42f87d435ebe4f2834c8fd0b4ce38f0b1627f3ed.zip cpython-42f87d435ebe4f2834c8fd0b4ce38f0b1627f3ed.tar.gz cpython-42f87d435ebe4f2834c8fd0b4ce38f0b1627f3ed.tar.bz2 |
bpo-45863: tarfile: don't zero out header fields unnecessarily (GH-29693)
Numeric fields of type float, notably mtime, can't be represented
exactly in the ustar header, so the pax header is used. But it is
helpful to set them to the nearest int (i.e. second rather than
nanosecond precision mtimes) in the ustar header as well, for the
benefit of unarchivers that don't understand the pax header.
Add test for tarfile.TarInfo.create_pax_header to confirm correct
behaviour.
(cherry picked from commit bf2d44ffb06e8f49aacc6b1c140a6717df5cf897)
Co-authored-by: Joshua Root <jmr@macports.org>
Diffstat (limited to 'Lib/tarfile.py')
-rwxr-xr-x | Lib/tarfile.py | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py index e187da2..6ada9a0 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -888,15 +888,24 @@ class TarInfo(object): # Test number fields for values that exceed the field limit or values # that like to be stored as float. for name, digits in (("uid", 8), ("gid", 8), ("size", 12), ("mtime", 12)): - if name in pax_headers: - # The pax header has priority. Avoid overflow. - info[name] = 0 - continue + needs_pax = False val = info[name] - if not 0 <= val < 8 ** (digits - 1) or isinstance(val, float): - pax_headers[name] = str(val) + val_is_float = isinstance(val, float) + val_int = round(val) if val_is_float else val + if not 0 <= val_int < 8 ** (digits - 1): + # Avoid overflow. info[name] = 0 + needs_pax = True + elif val_is_float: + # Put rounded value in ustar header, and full + # precision value in pax header. + info[name] = val_int + needs_pax = True + + # The existing pax header has priority. + if needs_pax and name not in pax_headers: + pax_headers[name] = str(val) # Create a pax extended header if necessary. if pax_headers: |