tarfile: Make it possible to extract nested tarfiles in memory#1032
tarfile: Make it possible to extract nested tarfiles in memory#1032woju wants to merge 1 commit intomicropython:masterfrom
Conversation
FileSection.skip() (see below the diff) uses 2-argument readinto, so
attempting to recursively extract archives throws an error. This commit
adds optional second argument to fix this problem. After this commit, it
is possible to extract nested archives in roughly this fashion:
with open(path, 'rb') as file:
tar_outer = tarfile.TarFile(fileobj=file)
for ti_outer in tar_outer:
tar_inner = tarfile.TarFile(
fileobj=tar_outer.extractfile(ti_outer))
for ti_inner in tar_inner:
...
Nested archives are used in some embedded contexts, for example Mender
artifacts.
Signed-off-by: Wojciech Porczyk <wojciech.porczyk@connectpoint.pl>
|
Thanks for the patch. I can see why it's needed. But, this is not CPython compatible, and we strive to retain compatibility where possible. Now, So I suggest to fix it by changing how --- a/python-stdlib/tarfile/tarfile/__init__.py
+++ b/python-stdlib/tarfile/tarfile/__init__.py
@@ -55,9 +55,12 @@ class FileSection:
if sz:
buf = bytearray(16)
while sz:
- s = min(sz, 16)
- self.f.readinto(buf, s)
- sz -= s
+ if sz >= 16:
+ self.f.readinto(buf)
+ sz -= 16
+ else:
+ self.f.read(sz)
+ sz = 0
class TarInfo: |
|
For reference, this used to work, but commit 2ca1527 optimised |
I thought this was already the case, because as it is, it doesn't work without 2-argument
Sure, wilco. I'll also change the title of the PR |
Yes, you're right, all fileobj's that it uses must support 2-arg For example, if there's a tar file on the host PC and you use So, instead of trying to add the 2-arg form to all streams/files, better to fix it once here so that it doesn't use the 2-arg form. |
|
I agree, yes, this sounds like the correct fix. I'll do that the week after; next week I'm on vacation. |
Make it possible to extract nested archives, which are used in e.g. Mender artifacts. See commit message for details and a (simplified) example.