scripts/search-index
changeset 99 8719ac564b22
parent 98 8c6e36849f9a
child 100 147be2910b02
--- a/scripts/search-index	Wed Feb 11 03:32:21 2009 +0200
+++ b/scripts/search-index	Wed Feb 11 03:46:59 2009 +0200
@@ -7,7 +7,7 @@
 # XXX: fix path
 import sys; sys.path.insert(0, '.'); sys.path.insert(0, '..')
 
-import os, os.path
+import os, os.path, fcntl
 import datetime, pytz
 
 # configuration and the LogSearchIndex module
@@ -147,7 +147,7 @@
     """
 
     # open index
-    index = _open_index(options, 'c' if not options.force else '*')
+    index = _open_index(options, 'ctrunc' if options.force else 'c')
 
     # that's all
     pass
@@ -261,8 +261,8 @@
         Automatically loads all channel logs that have not been indexed yet (by logfile mtime)
     """
     
-    # open index
-    index = _open_index(options, 'c' if options.create else 'a')
+    # open index, nonblocking
+    index = _open_index(options, 'c?' if options.create else 'a?')
 
     # default to all channels
     if not channel_names :
@@ -284,10 +284,12 @@
         statefile_tmppath = statefile_path + '.tmp'
         
         # do we have a tempfile from a previous crash?
-        # XXX: locking
         if os.path.exists(statefile_tmppath) :
-            # load after from it
+            # first, open it...
             statefile_tmp = open(statefile_tmppath, 'r+')
+
+            # ... then lock it
+            fcntl.lockf(statefile_tmp, fcntl.LOCK_EX | fcntl.LOCK_NB)
             
             # read after timestamp
             after_str = statefile_tmp.read().rstrip()
@@ -307,6 +309,9 @@
         else :
             # open new tempfile
             statefile_tmp = open(statefile_tmppath, 'w')
+            
+            # lock
+            fcntl.lockf(statefile_tmp, fcntl.LOCK_EX | fcntl.LOCK_NB)
 
         # override?
         if options.reload :