Merge pull request #726 from YunoHost/stupid-tar-leading-dot-issue

[fix] Stupid issue with files inside tar : foo is not the same as ./foo
This commit is contained in:
Alexandre Aubin 2019-06-04 00:14:27 +02:00 committed by GitHub
commit 5d88301cd4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1850,11 +1850,19 @@ class TarBackupMethod(BackupMethod):
# Mount the tarball # Mount the tarball
logger.debug(m18n.n("restore_extracting")) logger.debug(m18n.n("restore_extracting"))
tar = tarfile.open(self._archive_file, "r:gz") tar = tarfile.open(self._archive_file, "r:gz")
tar.extract('info.json', path=self.work_dir)
try: if "info.json" in tar.getnames():
leading_dot = ""
tar.extract('info.json', path=self.work_dir)
elif "./info.json" in tar.getnames():
leading_dot = "./"
tar.extract('./info.json', path=self.work_dir)
if "backup.csv" in tar.getnames():
tar.extract('backup.csv', path=self.work_dir) tar.extract('backup.csv', path=self.work_dir)
except KeyError: elif "./backup.csv" in tar.getnames():
tar.extract('./backup.csv', path=self.work_dir)
else:
# Old backup archive have no backup.csv file # Old backup archive have no backup.csv file
pass pass
@ -1876,12 +1884,12 @@ class TarBackupMethod(BackupMethod):
system_part = system_part.replace("_", "/") + "/" system_part = system_part.replace("_", "/") + "/"
subdir_and_files = [ subdir_and_files = [
tarinfo for tarinfo in tar.getmembers() tarinfo for tarinfo in tar.getmembers()
if tarinfo.name.startswith(system_part) if tarinfo.name.startswith(leading_dot+system_part)
] ]
tar.extractall(members=subdir_and_files, path=self.work_dir) tar.extractall(members=subdir_and_files, path=self.work_dir)
subdir_and_files = [ subdir_and_files = [
tarinfo for tarinfo in tar.getmembers() tarinfo for tarinfo in tar.getmembers()
if tarinfo.name.startswith("hooks/restore/") if tarinfo.name.startswith(leading_dot+"hooks/restore/")
] ]
tar.extractall(members=subdir_and_files, path=self.work_dir) tar.extractall(members=subdir_and_files, path=self.work_dir)
@ -1889,7 +1897,7 @@ class TarBackupMethod(BackupMethod):
for app in apps_targets: for app in apps_targets:
subdir_and_files = [ subdir_and_files = [
tarinfo for tarinfo in tar.getmembers() tarinfo for tarinfo in tar.getmembers()
if tarinfo.name.startswith("apps/" + app) if tarinfo.name.startswith(leading_dot+"apps/" + app)
] ]
tar.extractall(members=subdir_and_files, path=self.work_dir) tar.extractall(members=subdir_and_files, path=self.work_dir)
@ -2230,7 +2238,12 @@ def backup_info(name, with_details=False, human_readable=False):
tar = tarfile.open(archive_file, "r:gz") tar = tarfile.open(archive_file, "r:gz")
info_dir = info_file + '.d' info_dir = info_file + '.d'
try: try:
if "info.json" in tar.getnames():
tar.extract('info.json', path=info_dir) tar.extract('info.json', path=info_dir)
elif "./info.json" in tar.getnames():
tar.extract('./info.json', path=info_dir)
else:
raise KeyError
except KeyError: except KeyError:
logger.debug("unable to retrieve '%s' inside the archive", logger.debug("unable to retrieve '%s' inside the archive",
info_file, exc_info=1) info_file, exc_info=1)