summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2012-02-25 16:41:36 +0000
committerLars Wirzenius <liw@liw.fi>2012-02-25 16:41:36 +0000
commit3b1b347bc8e14eb821cf356519494dbc9b3b618d (patch)
treefae9eb6c36f30d4fab1083fe04ce554b7cc2fa05
parent369b6799b31e26478c1f4a922d26b9916bb1721e (diff)
downloadobnam-3b1b347bc8e14eb821cf356519494dbc9b3b618d.tar.gz
If one lock fails, do not lock anything
Or rather, unlock the ones we already locked.
-rw-r--r--obnamlib/lockmgr.py9
-rw-r--r--obnamlib/lockmgr_tests.py7
2 files changed, 15 insertions, 1 deletions
diff --git a/obnamlib/lockmgr.py b/obnamlib/lockmgr.py
index 934f27ab..23c7a594 100644
--- a/obnamlib/lockmgr.py
+++ b/obnamlib/lockmgr.py
@@ -54,8 +54,15 @@ class LockManager(object):
def lock(self, dirnames):
'''Lock ALL the directories.'''
+ we_locked = []
for dirname in dirnames:
- self._lock_one(dirname)
+ try:
+ self._lock_one(dirname)
+ except obnamlib.LockFail:
+ self.unlock(we_locked)
+ raise
+ else:
+ we_locked.append(dirname)
def unlock(self, dirnames):
'''Unlock ALL the directories.'''
diff --git a/obnamlib/lockmgr_tests.py b/obnamlib/lockmgr_tests.py
index 26652899..8fbc52a9 100644
--- a/obnamlib/lockmgr_tests.py
+++ b/obnamlib/lockmgr_tests.py
@@ -84,3 +84,10 @@ class LockManagerTests(unittest.TestCase):
for dirname in self.dirnames:
self.assertFalse(self.locked(dirname))
+ def test_does_not_lock_anything_if_one_lock_fails(self):
+ self.lm.lock([self.dirnames[-1]])
+ self.assertRaises(obnamlib.LockFail, self.lm.lock, self.dirnames)
+ for dirname in self.dirnames[:-1]:
+ self.assertFalse(self.locked(dirname))
+ self.assertTrue(self.locked(self.dirnames[-1]))
+