]> code.delx.au - pymsnt/blobdiff - src/xdb.py
Fixed bug with Twisted 1.3
[pymsnt] / src / xdb.py
index 225e892b33403d76d58a7b4e8b9f12e3d3c5c646..e3caf0e97b50d7ff8b24ddf7b220f0f4e290099c 100644 (file)
@@ -5,8 +5,9 @@ from tlib import xmlw
 from debug import LogEvent, INFO, WARN
 import os
 import os.path
+import shutil
+import md5
 import config
-import legacy
 
 X = os.path.sep
 SPOOL_UMASK = 0077
@@ -18,8 +19,11 @@ def unmangle(file):
        file = "%s@%s" % ("%".join(chunks), end)
        return file
 
-def mangle(jid):
-       return jid.replace("@", "%")
+def mangle(file):
+       return file.replace("@", "%")
+
+def makeHash(file):
+       return md5.md5(file).hexdigest()[0:3]
 
 
 class XDB:
@@ -40,7 +44,7 @@ class XDB:
                if(self.mangle):
                        file = mangle(file)
                
-               hash = file[0:2]
+               hash = makeHash(file)
                document = xmlw.parseFile(self.name + X + hash + X + file + ".xml")
                
                return document
@@ -50,13 +54,18 @@ class XDB:
                        file = mangle(file)
                
                prev_umask = os.umask(SPOOL_UMASK)
-               hash = file[0:2]
+               hash = makeHash(file)
                pre = self.name + X + hash + X
                if not os.path.exists(pre):
                        os.makedirs(pre)
-               f = open(pre + file + ".xml", "w")
-               f.write(text)
-               f.close()
+               try:
+                       f = open(pre + file + ".xml.new", "w")
+                       f.write(text)
+                       f.close()
+                       shutil.move(pre + file + ".xml.new", pre + file + ".xml")
+               except IOError, e:
+                       LogEvent(WARN, "", "IOError " + str(e))
+                       raise
                os.umask(prev_umask)
        
        def files(self):