From 2e076a5c75d2cac679a3277a1115c08daec6a8da Mon Sep 17 00:00:00 2001 From: Maximilian Knespel Date: Sun, 13 Jul 2025 12:37:07 +0200 Subject: [PATCH] Always set `TarInfo.size` to the real file size for sparse files --- Lib/tarfile.py | 5 +++++ .../Library/2025-07-13-12-37-49.gh-issue-136601.ZBiMIN.rst | 1 + 2 files changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-07-13-12-37-49.gh-issue-136601.ZBiMIN.rst diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 068aa13ed70356..1fd77786176645 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1616,14 +1616,19 @@ def _apply_pax_info(self, pax_headers, encoding, errors): """Replace fields with supplemental information from a previous pax extended or global header. """ + real_size_updated = False for keyword, value in pax_headers.items(): if keyword == "GNU.sparse.name": setattr(self, "path", value) elif keyword == "GNU.sparse.size": setattr(self, "size", int(value)) + real_size_updated = True elif keyword == "GNU.sparse.realsize": setattr(self, "size", int(value)) + real_size_updated = True elif keyword in PAX_FIELDS: + if keyword == "size" and real_size_updated: + continue if keyword in PAX_NUMBER_FIELDS: try: value = PAX_NUMBER_FIELDS[keyword](value) diff --git a/Misc/NEWS.d/next/Library/2025-07-13-12-37-49.gh-issue-136601.ZBiMIN.rst b/Misc/NEWS.d/next/Library/2025-07-13-12-37-49.gh-issue-136601.ZBiMIN.rst new file mode 100644 index 00000000000000..23080e4c70053f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-07-13-12-37-49.gh-issue-136601.ZBiMIN.rst @@ -0,0 +1 @@ +Always set ``TarInfo.size`` to the real file size for sparse files.