return False
filter = partial(filter, self, list_func)
- tarobj.extractall(filter=filter)
+ tarobj.extractall(filter=filter, unlink=True)
tarobj.close()
def restore_backup(self, target_path, backup_indexes_paths=[],
self._deltatar.logger.warning("Ignoring symlink %s" % member.name)
# finally, restore the file
- index_data['tarobj'].extract(member, symlink_cb=ignore_symlink)
+ index_data['tarobj'].extract(member, symlink_cb=ignore_symlink,
+ unlink=True)
def add_member_dir(self, member):
'''
self.closed = True
raise
- def extractall(self, path=".", members=None, filter=None):
+ def extractall(self, path=".", members=None, filter=None, unlink=False):
"""Extract all members from the archive to the current working
directory and set owner, modification time and permissions on
directories afterwards. `path' specifies a different directory
tarinfo = copy.copy(tarinfo)
tarinfo.mode = 0o0700
# Do not set_attrs directories, as we will do that further down
- self.extract(tarinfo, path, set_attrs=not tarinfo.isdir())
+ self.extract(tarinfo, path, set_attrs=not tarinfo.isdir(), unlink=unlink)
# Reverse sort directories.
directories.sort(key=lambda a: a.name)
else:
self._dbg(1, "tarfile: %s" % e)
- def extract(self, member, path="", set_attrs=True, symlink_cb=None):
+ def extract(self, member, path="", set_attrs=True, symlink_cb=None,
+ unlink=False):
"""Extract a member from the archive to the current working directory,
using its full name. Its file information is extracted as accurately
as possible. `member' may be a filename or a TarInfo object. You can
try:
self._extract_member(tarinfo, os.path.join(path, tarinfo.name),
- set_attrs=set_attrs)
+ set_attrs=set_attrs, unlink=unlink)
except EnvironmentError as e:
if self.errorlevel > 0:
raise
# blkdev, etc.), return None instead of a file object.
return None
- def _extract_member(self, tarinfo, targetpath, set_attrs=True):
+ def _extract_member(self, tarinfo, targetpath, set_attrs=True, unlink=False):
"""Extract the TarInfo object tarinfo to a physical
file called targetpath.
"""
else:
self._dbg(1, tarinfo.name)
+ if unlink is True:
+ _unlinkfirst(targetpath)
+
if tarinfo.isreg():
self.makefile(tarinfo, targetpath)
elif tarinfo.isdir():
if not self._extfileobj:
self.fileobj.close()
self.closed = True
+
+def _unlinkfirst(targetpath):
+ try:
+ os.unlink(targetpath)
+ except OSError as e:
+ if e.errno == errno.ENOENT or e.errno == errno.EISDIR:
+ pass
+
+
# class TarFile
class TarIter: