Initial copy of RAPI filebase to the trunk
[ganeti-local] / lib / utils.py
index 5e4233a..ceacc9b 100644 (file)
@@ -1063,3 +1063,34 @@ def CheckVolumeGroupSize(vglist, vgname, minsize):
     return ("volume group '%s' too small (%s MiB required, %d MiB found)" %
             (vgname, minsize, vgsize))
   return None
+
+
+def LockedMethod(fn):
+  """Synchronized object access decorator.
+
+  This decorator is intended to protect access to an object using the
+  object's own lock which is hardcoded to '_lock'.
+
+  """
+  def wrapper(self, *args, **kwargs):
+    assert hasattr(self, '_lock')
+    lock = self._lock
+    lock.acquire()
+    try:
+      result = fn(self, *args, **kwargs)
+    finally:
+      lock.release()
+    return result
+  return wrapper
+
+
+def LockFile(fd):
+  """Locks a file using POSIX locks.
+
+  """
+  try:
+    fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
+  except IOError, err:
+    if err.errno == errno.EAGAIN:
+      raise errors.LockError("File already locked")
+    raise