in container/archive.py [0:0]
def add_dir(self,
name,
path,
uid=0,
gid=0,
uname='',
gname='',
mtime=None,
mode=None,
depth=100):
"""Recursively add a directory.
Args:
name: the destination path of the directory to add.
path: the path of the directory to add.
uid: owner user identifier.
gid: owner group identifier.
uname: owner user names.
gname: owner group names.
mtime: modification time to put in the archive.
mode: unix permission mode of the file, default 0644 (0755).
depth: maximum depth to recurse in to avoid infinite loops
with cyclic mounts.
Raises:
TarFileWriter.Error: when the recursion depth has exceeded the
`depth` argument.
"""
if not (name == self.root_directory or name.startswith('/') or
name.startswith(self.root_directory + '/')):
name = posixpath.join(self.root_directory, name)
if mtime is None:
mtime = self.default_mtime
if posixpath.isdir(path):
# Remove trailing '/' (index -1 => last character)
if name[-1] == '/':
name = name[:-1]
# Add the x bit to directories to prevent non-traversable directories.
# The x bit is set only to if the read bit is set.
dirmode = (mode | ((0o444 & mode) >> 2)) if mode else mode
self.add_file(name + '/',
tarfile.DIRTYPE,
uid=uid,
gid=gid,
uname=uname,
gname=gname,
mtime=mtime,
mode=dirmode)
if depth <= 0:
raise self.Error('Recursion depth exceeded, probably in '
'an infinite directory loop.')
# Iterate over the sorted list of file so we get a deterministic result.
filelist = os.listdir(path)
filelist.sort()
for f in filelist:
new_name = posixpath.join(name, f)
new_path = posixpath.join(path, f)
self.add_dir(new_name, new_path, uid, gid, uname, gname, mtime, mode,
depth - 1)
else:
self.add_file(name,
tarfile.REGTYPE,
file_content=path,
uid=uid,
gid=gid,
uname=uname,
gname=gname,
mtime=mtime,
mode=mode)