##// END OF EJS Templates
Create an atomic opener that does not automatically rename on close...
mason@suse.com -
r2076:d007df6d default
parent child Browse files
Show More
@@ -694,7 +694,7 b' class revlog(object):'
694 df.write(d)
694 df.write(d)
695 fp.close()
695 fp.close()
696 df.close()
696 df.close()
697 fp = self.opener(self.indexfile, 'w', atomic=True)
697 fp = self.opener(self.indexfile, 'w', atomictemp=True)
698 self.version &= ~(REVLOGNGINLINEDATA)
698 self.version &= ~(REVLOGNGINLINEDATA)
699 if self.count():
699 if self.count():
700 x = self.index[0]
700 x = self.index[0]
@@ -708,7 +708,9 b' class revlog(object):'
708 e = struct.pack(self.indexformat, *x)
708 e = struct.pack(self.indexformat, *x)
709 fp.write(e)
709 fp.write(e)
710
710
711 fp.close()
711 # if we don't call rename, the temp file will never replace the
712 # real index
713 fp.rename()
712 self.chunkcache = None
714 self.chunkcache = None
713
715
714 def addrevision(self, text, transaction, link, p1=None, p2=None, d=None):
716 def addrevision(self, text, transaction, link, p1=None, p2=None, d=None):
@@ -431,20 +431,33 b' def opener(base, audit=True):'
431 os.chmod(temp, st.st_mode)
431 os.chmod(temp, st.st_mode)
432 return temp
432 return temp
433
433
434 class atomicfile(file):
434 class atomictempfile(file):
435 """the file will only be copied on close"""
435 """the file will only be copied when rename is called"""
436 def __init__(self, name, mode, atomic=False):
436 def __init__(self, name, mode):
437 self.__name = name
437 self.__name = name
438 self.temp = mktempcopy(name)
438 self.temp = mktempcopy(name)
439 file.__init__(self, self.temp, mode)
439 file.__init__(self, self.temp, mode)
440 def close(self):
440 def rename(self):
441 if not self.closed:
441 if not self.closed:
442 file.close(self)
442 file.close(self)
443 rename(self.temp, self.__name)
443 rename(self.temp, self.__name)
444 def __del__(self):
444 def __del__(self):
445 self.close()
445 if not self.closed:
446 try:
447 os.unlink(self.temp)
448 except: pass
449 file.close(self)
446
450
447 def o(path, mode="r", text=False, atomic=False):
451 class atomicfile(atomictempfile):
452 """the file will only be copied on close"""
453 def __init__(self, name, mode):
454 atomictempfile.__init__(self, name, mode)
455 def close(self):
456 self.rename()
457 def __del__(self):
458 self.rename()
459
460 def o(path, mode="r", text=False, atomic=False, atomictemp=False):
448 if audit_p:
461 if audit_p:
449 audit_path(path)
462 audit_path(path)
450 f = os.path.join(p, path)
463 f = os.path.join(p, path)
@@ -462,6 +475,8 b' def opener(base, audit=True):'
462 else:
475 else:
463 if atomic:
476 if atomic:
464 return atomicfile(f, mode)
477 return atomicfile(f, mode)
478 elif atomictemp:
479 return atomictempfile(f, mode)
465 if nlink > 1:
480 if nlink > 1:
466 rename(mktempcopy(f), f)
481 rename(mktempcopy(f), f)
467 return file(f, mode)
482 return file(f, mode)
General Comments 0
You need to be logged in to leave comments. Login now