From f5de63cb38a53fda51abfd71a94581aff7d20e62 Mon Sep 17 00:00:00 2001
From: Davide Gardenal <davide.gardenal@huawei.com>
Date: Mon, 1 Aug 2022 17:04:37 +0200
Subject: [PATCH] upgrade_oniro: add exception handling when restoring

Now if the metadata file in the backup archive is not found
the tool displays an error message to the user and exits.

Signed-off-by: Davide Gardenal <davide.gardenal@huawei.com>
---
 scripts/upgrade_oniro/upgrade_oniro.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/scripts/upgrade_oniro/upgrade_oniro.py b/scripts/upgrade_oniro/upgrade_oniro.py
index df34cb77..ed79cb39 100755
--- a/scripts/upgrade_oniro/upgrade_oniro.py
+++ b/scripts/upgrade_oniro/upgrade_oniro.py
@@ -229,9 +229,9 @@ def restore_oniro():
         Returns:
             None. Side effects Oniro's backup restore
     """
-    # TODO add an exception handling if the metadata file is not found
     # TODO if something created some files / directories we are not able to
     # delete those and we may end up with a inconsistent state
+
     from upgrade_oniro_utils import find_latest_backup
 
     if backup_archive_arg == "latest":
@@ -249,7 +249,11 @@ def restore_oniro():
     print(f"Restoring {backup_archive_name}")
     with tarfile.open(backup_archive_name) as backup_archive:
         # Extract the metadata file and delete all the old folders
-        backup_folders_file = backup_archive.extract("backup_folders")
+        try:
+            backup_folders_file = backup_archive.extract("backup_folders")
+        except KeyError:
+            print("Metadata file not found in the backup archive, aborting...")
+            return
         with open("backup_folders") as bf:
             for folder in bf.readlines():
                 if os.path.exists(folder):
-- 
GitLab