Repository navigation
Description
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
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsNo status
Activity
encukou commentedon Aug 10, 2023
That Git tarball contains:
mingw64/bin/git-receive-pack.exemingw64/bin/git-upload-archive.exe->mingw64/bin/git-receive-pack.exeYou'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.
encukou commentedon Aug 10, 2023
If you'd like to provide a better error for this, adapt
A small but ugly workaroundfromextractfile(), 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.
mtwilliams commentedon Aug 20, 2023
NTFS supports hard (and soft) links.
pythongh-106980: Give a clear error for link fallback in tarfile streams