]> git.neil.brown.name Git - plato.git/commitdiff
storesms: get shared lock when reading from message file.
authorNeilBrown <neilb@suse.de>
Mon, 16 Dec 2013 03:52:28 +0000 (14:52 +1100)
committerNeilBrown <neilb@suse.de>
Mon, 16 Dec 2013 03:52:28 +0000 (14:52 +1100)
This might fix some strange behaviour I've seen..

sms/storesms.py

index f04a47fcdbfa10d1ac116266b28851f5ce462abf..5b63280ad025621a1dcf9f9d552f03890e3d4342 100644 (file)
@@ -181,7 +181,6 @@ class SMSmesg:
             if self.source == None:
                 self.source = 'LOCAL'
             if part:
-                print 'part', part[0], part[1]
                 self.parts = [None for x in range(part[1])]
                 self.parts[part[0]-1] = self.text
                 self.reduce_parts()
@@ -247,6 +246,8 @@ class SMSstore:
 
         if update:
             fcntl.lockf(f, fcntl.LOCK_EX)
+        else:
+            fcntl.lockf(f, fcntl.LOCK_SH)
         for ln in f:
             l.append(parse_time(ln.strip()))
         l.sort()
@@ -262,9 +263,11 @@ class SMSstore:
         f.close()
         return l
 
-    def load_month(self, f):
+    def load_month(self, f, do_lock):
         # load the messages from f, which is open for read
         rv = {}
+        if do_lock:
+            fcntl.lockf(f, do_lock)
         for l in f:
             l.strip()
             m = SMSmesg(line=l)
@@ -321,7 +324,7 @@ class SMSstore:
                 sms.parts = None
 
         fcntl.lockf(f, fcntl.LOCK_EX)
-        l = self.load_month(f)
+        l = self.load_month(f, 0)
         while sms.stamp in l:
             sms.stamp += 1
         l[sms.stamp] = sms
@@ -370,7 +373,7 @@ class SMSstore:
 
     def lookup(self, lasttime = None, state = None):
         if lasttime == None:
-            lasttime = int(time.time())
+            lasttime = int(time.time() + 3600)
         if state == None:
             return self.getmesgs(lasttime)
         if state == 'DRAFT':
@@ -401,7 +404,7 @@ class SMSstore:
             t = parse_time(m + '01-000000')
             if t > last:
                 continue
-            mon = self.load_month(open(self.dirname + '/' + m))
+            mon = self.load_month(open(self.dirname + '/' + m), fcntl.LOCK_SH)
             for mt in mon:
                 if mt <= last:
                     rv.append(mon[mt])
@@ -416,7 +419,7 @@ class SMSstore:
             return None
         if self.cached_month != m:
             self.cached_month = m
-            self.cache = self.load_month(open(self.dirname + '/' + m))
+            self.cache = self.load_month(open(self.dirname + '/' + m), fcntl.LOCK_SH)
         if t in self.cache:
             return self.cache[t]
         return None
@@ -433,7 +436,7 @@ class SMSstore:
             return
 
         fcntl.lockf(f, fcntl.LOCK_EX)
-        l = self.load_month(f)
+        l = self.load_month(f, 0)
         if tm in l:
             del l[tm]
         self.store_month(l, m);