Skip to content

Commit 927617c

Browse files
committed
tarfile: Make it possible to extract nested tarfiles in memory
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 <[email protected]>
1 parent f95568d commit 927617c

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

python-stdlib/tarfile/tarfile/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ def read(self, sz=65536):
4141
self.content_len -= sz
4242
return data
4343

44-
def readinto(self, buf):
44+
def readinto(self, buf, size=None):
4545
if self.content_len == 0:
4646
return 0
4747
if len(buf) > self.content_len:
4848
buf = memoryview(buf)[: self.content_len]
49+
if size is not None and len(buf) > size:
50+
buf = memoryview(buf)[:size]
4951
sz = self.f.readinto(buf)
5052
self.content_len -= sz
5153
return sz

0 commit comments

Comments
 (0)