[PATCH 2/3] Perform legacy global lock in addition to new per-account lock

Sebastian Spaeth Sebastian at SSpaeth.de
Mon Aug 22 11:12:06 BST 2011


We simply lock OfflineImap the same global way that we have always done
in addition to the previously implemented per-account lock. We can keep
both systems in parallel and then after a few stable releases, drop the
old-style global lock. by reverting this patch

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
 offlineimap/accounts.py |    1 +
 offlineimap/init.py     |   19 ++++++++++++++++++-
 2 files changed, 19 insertions(+), 1 deletions(-)

diff --git a/offlineimap/accounts.py b/offlineimap/accounts.py
index e3dad0e..67194f4 100644
--- a/offlineimap/accounts.py
+++ b/offlineimap/accounts.py
@@ -172,6 +172,7 @@ class SyncableAccount(Account):
 
     def lock(self):
         """Lock the account, throwing an exception if it is locked already"""
+        # Take a new-style per-account lock
         self._lockfd = open(self._lockfilepath, 'w')
         try:
             fcntl.lockf(self._lockfd, fcntl.LOCK_EX|fcntl.LOCK_NB)
diff --git a/offlineimap/init.py b/offlineimap/init.py
index f7b2eef..e5560a9 100644
--- a/offlineimap/init.py
+++ b/offlineimap/init.py
@@ -24,8 +24,13 @@ import signal
 import socket
 import logging
 from optparse import OptionParser
+try:
+    import fcntl
+except ImportError:
+    pass #it's OK
 import offlineimap
 from offlineimap import accounts, threadutil, syncmaster
+from offlineimap.error import OfflineImapError
 from offlineimap.ui import UI_LIST, setglobalui, getglobalui
 from offlineimap.CustomConfig import CustomConfigParser
 
@@ -310,6 +315,18 @@ class OfflineImap:
             #various initializations that need to be performed:
             offlineimap.mbnames.init(config, syncaccounts)
 
+            #TODO: keep legacy lock for a few versions, then remove.
+            self._legacy_lock = open(self.config.getmetadatadir() + "/lock",
+                                       'w')
+            try:
+                fcntl.lockf(self._legacy_lock, fcntl.LOCK_EX|fcntl.LOCK_NB)
+            except NameError:
+                #fcntl not available (Windows), disable file locking... :(
+                pass
+            except IOError:
+                raise OfflineImapError("Could not take global lock.",
+                                       OfflineImapError.ERROR.REPO)
+
             if options.singlethreading:
                 #singlethreaded
                 self.sync_singlethreaded(syncaccounts, config)
@@ -329,7 +346,7 @@ class OfflineImap:
             return
         except (SystemExit):
             raise
-        except:
+        except Exception, e:
             ui.mainException()
 
     def sync_singlethreaded(self, accs, config):
-- 
1.7.4.1





More information about the OfflineIMAP-project mailing list