Blame SOURCES/0004-save_to_file-function-breaks-symbolic-link-when-savi.patch

b6e9cf
From 5626a4d593df9384fff5129e7bdb6c909f5f2b11 Mon Sep 17 00:00:00 2001
b6e9cf
From: Maurizio Lombardi <mlombard@redhat.com>
b6e9cf
Date: Fri, 8 Feb 2019 14:36:34 +0100
b6e9cf
Subject: [PATCH] save_to_file() function breaks symbolic link when saving
b6e9cf
 configuration
b6e9cf
b6e9cf
Currently the location of the targetcli configuration file is not
b6e9cf
configurable (saveconfig.json). The location is : /etc/target
b6e9cf
Some users use stateless VMs, with just a persistent device
b6e9cf
mounted under /var/lib/diskless.
b6e9cf
b6e9cf
In order to circumvent the non configurable location of
b6e9cf
saveconfig.json, they create a symbolic link in /etc/target
b6e9cf
pointing to /var/lib/diskless
b6e9cf
b6e9cf
Unfortunately, the rtslib save_to_file() function breaks
b6e9cf
this symbolic link and creates a new regular file in place of it,
b6e9cf
due to the creation of a saveconfig.json.temp file which is
b6e9cf
then renamed to saveconfig.json using the os.rename() function.
b6e9cf
b6e9cf
This patch replace os.rename() with shutil.copyfile() + os.remove(),
b6e9cf
using copyfile() instead of rename() will preserve the symbolic link
b6e9cf
b6e9cf
Signed-off-by: Maurizio Lombardi <mlombard@redhat.com>
b6e9cf
---
b6e9cf
 rtslib/root.py | 8 ++++++--
b6e9cf
 1 file changed, 6 insertions(+), 2 deletions(-)
b6e9cf
b6e9cf
diff --git a/rtslib/root.py b/rtslib/root.py
b6e9cf
index 5c0a98c..c93e65b 100644
b6e9cf
--- a/rtslib/root.py
b6e9cf
+++ b/rtslib/root.py
b6e9cf
@@ -23,6 +23,7 @@ import stat
b6e9cf
 import json
b6e9cf
 import glob
b6e9cf
 import errno
b6e9cf
+import shutil
b6e9cf
 
b6e9cf
 from .node import CFSNode
b6e9cf
 from .target import Target
b6e9cf
@@ -386,7 +387,9 @@ class RTSRoot(CFSNode):
b6e9cf
         else:
b6e9cf
             saveconf = self.dump()
b6e9cf
 
b6e9cf
-        with open(save_file+".temp", "w+") as f:
b6e9cf
+        tmp_file = save_file + ".temp"
b6e9cf
+
b6e9cf
+        with open(tmp_file, "w+") as f:
b6e9cf
             os.fchmod(f.fileno(), stat.S_IRUSR | stat.S_IWUSR)
b6e9cf
             f.write(json.dumps(saveconf, sort_keys=True, indent=2))
b6e9cf
             f.write("\n")
b6e9cf
@@ -394,7 +397,8 @@ class RTSRoot(CFSNode):
b6e9cf
             os.fsync(f.fileno())
b6e9cf
             f.close()
b6e9cf
 
b6e9cf
-        os.rename(save_file+".temp", save_file)
b6e9cf
+        shutil.copyfile(tmp_file, save_file)
b6e9cf
+        os.remove(tmp_file)
b6e9cf
 
b6e9cf
     def restore_from_file(self, restore_file=None, clear_existing=True, abort_on_error=False):
b6e9cf
         '''
b6e9cf
-- 
b6e9cf
2.21.0
b6e9cf