changeset 1082:ba25ee4ea61d

Added locking to the MetaDict code.
author Alexander Schremmer <alex AT alexanderweb DOT de>
date Fri, 28 Jul 2006 17:02:13 +0200
parents 26ed423681e0
children cf4a2f11c959
files MoinMoin/wikiutil.py
diffstat 1 files changed, 20 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/wikiutil.py	Fri Jul 28 17:02:00 2006 +0200
+++ b/MoinMoin/wikiutil.py	Fri Jul 28 17:02:13 2006 +0200
@@ -10,7 +10,7 @@
 import codecs, types
 
 from MoinMoin import util, version, config
-from MoinMoin.util import pysupport, filesys
+from MoinMoin.util import pysupport, filesys, lock
 
 # Exceptions
 class InvalidFileNameError(Exception):
@@ -410,6 +410,9 @@
         self.metafilename = metafilename
         self.dirty = False
         self.loaded = False
+        lock_dir = os.path.join(self.metafilename, '..', 'cache', '__metalock__')
+        self.rlock = lock.ReadLock(lock_dir, 60.0)
+        self.wlock = lock.WriteLock(lock_dir, 60.0)
 
     def _get_meta(self):
         """ get the meta dict from an arbitrary filename.
@@ -417,11 +420,15 @@
             @param metafilename: the name of the file to read
             @return: dict with all values or {} if empty or error
         """
-        # XXX what does happen if the metafile is being written to in another process?
+
         try:
-            metafile = codecs.open(self.metafilename, "r", "utf-8")
-            meta = metafile.read() # this is much faster than the file's line-by-line iterator
-            metafile.close()
+            self.rlock.acquire(3.0)
+            try:
+                metafile = codecs.open(self.metafilename, "r", "utf-8")
+                meta = metafile.read() # this is much faster than the file's line-by-line iterator
+                metafile.close()
+            finally:
+                self.rlock.release()
         except IOError:
             meta = u''
         for line in meta.splitlines():
@@ -445,9 +452,14 @@
             meta.append("%s: %s" % (key, value))
         meta = '\r\n'.join(meta)
         # XXX what does happen if the metafile is being read or written to in another process?
-        metafile = codecs.open(self.metafilename, "w", "utf-8")
-        metafile.write(meta)
-        metafile.close()
+        # XXX Then it is corrupted. We need locks.
+        self.wlock.aquire(5.0)
+        try:
+            metafile = codecs.open(self.metafilename, "w", "utf-8")
+            metafile.write(meta)
+            metafile.close()
+        finally:
+            self.wlock.release()
         filesys.chmod(self.metafilename, 0666 & config.umask)
         self.dirty = False