Eduardo has uploaded a new change for review.

Change subject: Add forceLink() which replaces a link if it already exists.
......................................................................

Add forceLink() which replaces a link if it already exists.

Reduce the number of fileUtils.safeUnlink() callers.

Change-Id: Idea4ab66cc2954fdb11606daa4a73ad3edb0c7ef
Signed-off-by: Eduardo <[email protected]>
---
M vdsm/storage/fileVolume.py
M vdsm/storage/outOfProcess.py
M vdsm/utils.py
3 files changed, 25 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/23/11423/1

diff --git a/vdsm/storage/fileVolume.py b/vdsm/storage/fileVolume.py
index e42fbad..245c189 100644
--- a/vdsm/storage/fileVolume.py
+++ b/vdsm/storage/fileVolume.py
@@ -22,17 +22,18 @@
 import os
 import sanlock
 
-import storage_exception as se
 from vdsm.config import config
+
+from misc import deprecated
 from sdc import sdCache
+from threadLocal import vars
+import storage_exception as se
 import outOfProcess as oop
 import volume
 import image
 import sd
 import misc
-from misc import deprecated
 import task
-from threadLocal import vars
 
 LEASE_FILEEXT = ".lease"
 LEASE_FILEOFFSET = 0
@@ -232,8 +233,7 @@
                        dstImgPath)
         dstLeasePath = self._getLeaseVolumePath(
             os.path.join(dstImgPath, self.volUUID))
-        self.oop.fileUtils.safeUnlink(dstLeasePath)
-        self.oop.os.link(self._getLeaseVolumePath(), dstLeasePath)
+        self.oop.utils.forceLink(self._getLeaseVolumePath(), dstLeasePath)
 
     def _share(self, dstImgPath):
         """
@@ -243,15 +243,11 @@
         dstMetaPath = self._getMetaVolumePath(dstVolPath)
 
         self.log.debug("Share volume %s to %s", self.volUUID, dstImgPath)
-
-        self.oop.fileUtils.safeUnlink(dstVolPath)
-        self.oop.os.link(self.getVolumePath(), dstVolPath)
+        self.oop.utils.forceLink(self.getVolumePath(), dstVolPath)
 
         self.log.debug("Share volume metadata of %s to %s", self.volUUID,
                        dstImgPath)
-
-        self.oop.fileUtils.safeUnlink(dstMetaPath)
-        self.oop.os.link(self._getMetaVolumePath(), dstMetaPath)
+        self.oop.utils.forceLink(self._getMetaVolumePath(), dstMetaPath)
 
         # Link the lease file if the domain uses sanlock
         if sdCache.produce(self.sdUUID).hasVolumeLeases():
diff --git a/vdsm/storage/outOfProcess.py b/vdsm/storage/outOfProcess.py
index 1d78277..a445435 100644
--- a/vdsm/storage/outOfProcess.py
+++ b/vdsm/storage/outOfProcess.py
@@ -82,5 +82,6 @@
     return _ModuleWrapper("oop", procPool, DEFAULT_TIMEOUT,
                           (("os",
                             ("path",)),
+                           ("vdsm", ("utils",)),
                            "glob",
                            "fileUtils"))
diff --git a/vdsm/utils.py b/vdsm/utils.py
index 35338a0..ff25fa3 100644
--- a/vdsm/utils.py
+++ b/vdsm/utils.py
@@ -72,6 +72,23 @@
             raise
 
 
+def forceLink(src, dst):
+    """ Makes or replaces a hard link.
+
+    Like os.link() but replaces the link if it exists.
+    """
+    try:
+        os.link(src, dst)
+    except OSError as e:
+        if e.errno == errno.EEXIST:
+            rmFile(dst)
+            os.link(src, dst)
+        else:
+            logging.error("Linking file: %s to %s failed", src, dst,
+                          exc_info=True)
+            raise
+
+
 def readMemInfo():
     """
     Parse ``/proc/meminfo`` and return its content as a dictionary.


--
To view, visit http://gerrit.ovirt.org/11423
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idea4ab66cc2954fdb11606daa4a73ad3edb0c7ef
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Eduardo <[email protected]>
_______________________________________________
vdsm-patches mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to