From 4fe199ccc7933f437e503212a25dfb08118a4476 Mon Sep 17 00:00:00 2001 From: Konstantinos Karmas Date: Thu, 24 Jun 2021 17:26:57 +0200 Subject: [PATCH 1/2] Ignore files-in-use while restoring a backup Sometimes, while a backup is being restore, one of the files in the current config folder may be still in use. This means that once the backup tries to replace this file with the one in the backup, the entire backup restoration fails and half the configuration folder has been messed up. To prevent that, we decided to ignore files that are currently in use while restoring a backup. This _may_ lead to a slightly wrong configuration (e.g. a plugin may not be restored properly), but it is an acceptable result, as the rest of the configuration folder is restored properly. CURA-8313 --- cura/Backups/Backup.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/cura/Backups/Backup.py b/cura/Backups/Backup.py index 6128dac320..1f6a961733 100644 --- a/cura/Backups/Backup.py +++ b/cura/Backups/Backup.py @@ -193,14 +193,13 @@ class Backup: Logger.log("d", "Removing current data in location: %s", target_path) Resources.factoryReset() Logger.log("d", "Extracting backup to location: %s", target_path) - try: - name_list = archive.namelist() - for archive_filename in name_list: + name_list = archive.namelist() + for archive_filename in name_list: + try: archive.extract(archive_filename, target_path) - CuraApplication.getInstance().processEvents() - except (PermissionError, EnvironmentError): - Logger.logException("e", "Unable to extract the backup due to permission or file system errors.") - return False + except (PermissionError, EnvironmentError): + Logger.logException("e", f"Unable to extract the file {archive_filename} from the backup due to permission or file system errors.") + CuraApplication.getInstance().processEvents() return True def _obfuscate(self) -> Dict[str, str]: From 584a387debec75023094db6cba84a8bf1781d6ea Mon Sep 17 00:00:00 2001 From: jelle Spijker Date: Fri, 25 Jun 2021 08:24:06 +0200 Subject: [PATCH 2/2] Show warning when restoring backup failed We now allow the backup to fail gracefully when partial files fail to be restored. But the user is not actively informed by these failures. Leaving him/her unaware of the state of his configuration folder. This commit show's a message windows, listing the failed files. Due to a string freeze at the moment we reuse the following i18n message: > The following error occurred while trying to restore a Cura backup: followed by a list of files that fails to be restored. Which is not ideal, but a sufficient warning in my opinion. At least better then being completely uninformed. Contributes to CURA-8313 --- cura/Backups/Backup.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/cura/Backups/Backup.py b/cura/Backups/Backup.py index 1f6a961733..3568369201 100644 --- a/cura/Backups/Backup.py +++ b/cura/Backups/Backup.py @@ -7,7 +7,7 @@ import re import shutil from copy import deepcopy from zipfile import ZipFile, ZIP_DEFLATED, BadZipfile -from typing import Dict, Optional, TYPE_CHECKING, List +from typing import Dict, Optional, TYPE_CHECKING, List, Tuple from UM import i18nCatalog from UM.Logger import Logger @@ -156,7 +156,10 @@ class Backup: Logger.log("d", f"The following error occurred while trying to restore a Cura backup: {str(e)}") self._showMessage(self.catalog.i18nc("@info:backup_failed", "The following error occurred while trying to restore a Cura backup:") + str(e)) return False - extracted = self._extractArchive(archive, version_data_dir) + extracted, failed_files = self._extractArchive(archive, version_data_dir) + self._showMessage( + self.catalog.i18nc("@info:backup_failed", + "The following error occurred while trying to restore a Cura backup:" + "\n{}".format("\n".join(failed_files)))) # Under Linux, preferences are stored elsewhere, so we copy the file to there. if Platform.isLinux(): @@ -175,7 +178,7 @@ class Backup: return extracted @staticmethod - def _extractArchive(archive: "ZipFile", target_path: str) -> bool: + def _extractArchive(archive: "ZipFile", target_path: str) -> Tuple[bool, List[str]]: """Extract the whole archive to the given target path. :param archive: The archive as ZipFile. @@ -188,19 +191,23 @@ class Backup: config_filename = CuraApplication.getInstance().getApplicationName() + ".cfg" # Should be there if valid. if config_filename not in [file.filename for file in archive.filelist]: Logger.logException("e", "Unable to extract the backup due to corruption of compressed file(s).") - return False + return False, [] Logger.log("d", "Removing current data in location: %s", target_path) Resources.factoryReset() Logger.log("d", "Extracting backup to location: %s", target_path) name_list = archive.namelist() + failed_files = [] + full_restore = True for archive_filename in name_list: try: archive.extract(archive_filename, target_path) except (PermissionError, EnvironmentError): Logger.logException("e", f"Unable to extract the file {archive_filename} from the backup due to permission or file system errors.") + failed_files.append(archive_filename) + full_restore = False CuraApplication.getInstance().processEvents() - return True + return full_restore, failed_files def _obfuscate(self) -> Dict[str, str]: """