Blob Blame History Raw
From 8011ea6a741d494c145b4906f7a7865c8b74c6a7 Mon Sep 17 00:00:00 2001
From: Christian Seiler <christian@iwakd.de>
Date: Thu, 23 Mar 2017 19:36:18 +0100
Subject: [PATCH] Properly detect errors when writing backup files. (Closes:
 #80) (#81)

* Properly detect errors when writing backup files. (Closes: #80)

If the backup directory does not exist, properly detect that and show a
warning message to the user, so that they don't think that their
configuration was backed up, when it fact wasn't.

Additionally, try to automatically create the backup directory if it
does not exist.

Signed-off-by: Christian Seiler <christian@iwakd.de>

* Don't automatically create backup directory if it doesn't exist.

After discussion on the issue tracker, it was decided to not
auto-create the backup-directory if it doesn't exist yet.

If the directory doesn't exist, the user will now see the warning.

Signed-off-by: Christian Seiler <christian@iwakd.de>
---
 targetcli/ui_root.py | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/targetcli/ui_root.py b/targetcli/ui_root.py
index 2d8989c..c3a8483 100644
--- a/targetcli/ui_root.py
+++ b/targetcli/ui_root.py
@@ -75,17 +75,25 @@ class UIRoot(UINode):
             backup_name = "saveconfig-" + \
                 datetime.now().strftime("%Y%m%d-%H:%M:%S") + ".json"
             backupfile = backup_dir + "/" + backup_name
-            with ignored(IOError):
+            backup_error = None
+            try:
                 shutil.copy(savefile, backupfile)
-
-            # Kill excess backups
-            backups = sorted(glob(os.path.dirname(savefile) + "/backup/*.json"))
-            files_to_unlink = list(reversed(backups))[kept_backups:]
-            for f in files_to_unlink:
-                os.unlink(f)
-
-            self.shell.log.info("Last %d configs saved in %s." % \
-                                    (kept_backups, backup_dir))
+            except IOError as ioe:
+                backup_error = ioe.strerror or "Unknown error"
+
+            if backup_error == None:
+                # Kill excess backups
+                backups = sorted(glob(os.path.dirname(savefile) + "/backup/*.json"))
+                files_to_unlink = list(reversed(backups))[kept_backups:]
+                for f in files_to_unlink:
+                    with ignored(IOError):
+                        os.unlink(f)
+
+                self.shell.log.info("Last %d configs saved in %s." % \
+                                        (kept_backups, backup_dir))
+            else:
+                self.shell.log.warning("Could not create backup file %s: %s." % \
+                                           (backupfile, backup_error))
 
         self.rtsroot.save_to_file(savefile)
 
-- 
1.8.3.1