Skip to content

TarFile.extractall seeks backwards #106980

Open

Description

@mtwilliams

Bug report

Using tarfile.TarFile.extractall on a compressed tarball opened in a streaming mode, like r|bz2, results in a tarfile.StreamError:

Traceback (most recent call last):
  File "C:\<redacted>\git.py", line 22, in install
    tar.extractall(_PATH_TO_GIT_FOR_WINDOWS)
  File "tarfile.py", line 2257, in extractall
  File "tarfile.py", line 2320, in _extract_one
  File "tarfile.py", line 2403, in _extract_member
  File "tarfile.py", line 2446, in makefile
  File "tarfile.py", line 520, in seek
tarfile.StreamError: seeking backwards is not allowed

I would expect this to work as the seeks should only ever move forward past headers and never backwards: read a header, create the file/directory, and repeat.

Background

I have a little script that shims calls to git. If it can't find git in the path, it downloads a portal version of Git for Windows and uses that. Conveniently, Git for Windows distributes bzip2 compressed tarballs, which can be decompressed and extracted with a few short lines. However, doing so in streaming mode (r|bz2) results in a tarfile.StreamError.

Repro

Here's a minimal script that reproduces the error.

import urllib.request
import tarfile

url = "https://github.com/git-for-windows/git/releases/download/v2.41.0.windows.1/Git-2.41.0-64-bit.tar.bz2"

with urllib.request.urlopen(url) as response:
  with tarfile.open(fileobj=response, mode='r|bz2') as tar:
    tar.extractall("git-for-windows")

As a workaround, I currently download the tarball to a temporary file and extract from there.

Your environment

I'm running into this on Windows 10, Version 10.0.19045 Build 19045, using a portable installation of Python.

Python 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32

Activity

  1. added
    type-bug
    on Jul 21, 2023
  2. encukou commented on Aug 10, 2023

    @encukou
    Member

    That Git tarball contains:

    • at position 31053312, the regular file mingw64/bin/git-receive-pack.exe
    • at position 31110656, the hard link mingw64/bin/git-upload-archive.exe -> mingw64/bin/git-receive-pack.exe

    You're on Windows, so I assume theOS/filesystem doesn't support hardlinks. In this case, tarfile falls back to reading the linked file from the archive. For that, it needs to seek backwards.

    I guess it could be modified to copy the file that was already extracted to disk, but that would be a new code path we could only use for extractall, not for extracting individual files out-of-order (or for files that appear after the link -- not sure if that's suppoted). I don't think we could commit to maintaining that.

    Your workaround is correct.

  3. encukou commented on Aug 10, 2023

    @encukou
    Member

    If you'd like to provide a better error for this, adapt A small but ugly workaround from extractfile(), put it in an appropriate place, and add a test for Windows.

    I am not on Windows at the moment so I cannot do that easily.

  4. mtwilliams commented on Aug 20, 2023

    @mtwilliams
    Author
  5. added
    stdlib
    on Nov 26, 2023
  6. added a commit that references this issue on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibtype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions