in container/archive.py [0:0]
def add_tar(self,
tar,
rootuid=None,
rootgid=None,
numeric=False,
name_filter=None,
root=None):
"""Merge a tar content into the current tar, stripping timestamp.
Args:
tar: the name of tar to extract and put content into the current tar.
rootuid: user id that we will pretend is root (replaced by uid 0).
rootgid: group id that we will pretend is root (replaced by gid 0).
numeric: set to true to strip out name of owners (and just use the
numeric values).
name_filter: filter out file by names. If not none, this method will be
called for each file to add, given the name and should return true if
the file is to be added to the final tar and false otherwise.
root: place all non-absolute content under given root directory, if not
None.
Raises:
TarFileWriter.Error: if an error happens when uncompressing the tar file.
"""
if root and root[0] not in ['/', '.']:
# Root prefix should start with a '/', adds it if missing
root = '/' + root
compression = posixpath.splitext(tar)[-1][1:]
if compression == 'tgz':
compression = 'gz'
elif compression == 'bzip2':
compression = 'bz2'
elif compression == 'lzma':
compression = 'xz'
elif compression not in ['gz', 'bz2', 'xz']:
compression = ''
if compression == 'xz':
# Python 2 does not support lzma, our py3 support is terrible so let's
# just hack around.
# Note that we buffer the file in memory and it can have an important
# memory footprint but it's probably fine as we don't use them for really
# large files.
# TODO(dmarting): once our py3 support gets better, compile this tools
# with py3 for proper lzma support.
if subprocess.call('which xzcat', shell=True, stdout=subprocess.PIPE):
raise self.Error('Cannot handle .xz and .lzma compression: '
'xzcat not found.')
p = subprocess.Popen('cat %s | xzcat' % tar,
shell=True,
stdout=subprocess.PIPE)
f = io.BytesIO(p.stdout.read())
p.wait()
intar = tarfile.open(fileobj=f, mode='r:')
else:
if compression in ['gz', 'bz2']:
# prevent performance issues due to accidentally-introduced seeks
# during intar traversal by opening in "streaming" mode. gz, bz2
# are supported natively by python 2.7 and 3.x
inmode = 'r|' + compression
else:
inmode = 'r:' + compression
intar = tarfile.open(name=tar, mode=inmode)
for tarinfo in intar:
if name_filter is None or name_filter(tarinfo.name):
if not self.preserve_mtime:
tarinfo.mtime = self.default_mtime
if rootuid is not None and tarinfo.uid == rootuid:
tarinfo.uid = 0
tarinfo.uname = 'root'
if rootgid is not None and tarinfo.gid == rootgid:
tarinfo.gid = 0
tarinfo.gname = 'root'
if numeric:
tarinfo.uname = ''
tarinfo.gname = ''
name = tarinfo.name
if (not name.startswith('/') and
not name.startswith(self.root_directory)):
name = posixpath.join(self.root_directory, name)
if root is not None:
if name.startswith('.'):
name = '.' + root + name.lstrip('.')
# Add root dir with same permissions if missing. Note that
# add_file deduplicates directories and is safe to call here.
self.add_file('.' + root,
tarfile.DIRTYPE,
uid=tarinfo.uid,
gid=tarinfo.gid,
uname=tarinfo.uname,
gname=tarinfo.gname,
mtime=tarinfo.mtime,
mode=0o755)
# Relocate internal hardlinks as well to avoid breaking them.
link = tarinfo.linkname
if link.startswith('.') and tarinfo.type == tarfile.LNKTYPE:
tarinfo.linkname = '.' + root + link.lstrip('.')
tarinfo.name = name
if 'path' in tarinfo.pax_headers:
# Modify the TarInfo's PAX header for the path name. These headers are used to define "long" path names for
# files within a tar file. This header is defined within this spec:
# https://en.wikipedia.org/wiki/Tar_(computing)#POSIX.1-2001/pax
# When we read a tar file with this path type the tarfile module sets both the TarInfo.name and
# pax_headers['path'] so we need to manually update both.
tarinfo.pax_headers['path'] = name
if tarinfo.isfile():
# use extractfile(tarinfo) instead of tarinfo.name to preserve
# seek position in intar
self._addfile(tarinfo, intar.extractfile(tarinfo))
else:
self._addfile(tarinfo)
intar.close()