From 06590aee389f4466e02407f39af1674366a74705 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Tue, 15 Jun 2010 00:32:05 +0200 Subject: Reimplemented Lock handling to be conforming to the git lock protocol, which is actually more efficient than the previous implementation Index now locks its file for reading, and properly uses LockedFD when writing --- lib/git/index/base.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'lib/git/index/base.py') diff --git a/lib/git/index/base.py b/lib/git/index/base.py index 165ac09a..02ed78ea 100644 --- a/lib/git/index/base.py +++ b/lib/git/index/base.py @@ -43,7 +43,7 @@ from git.objects.utils import Serializable from git.utils import ( IndexFileSHA1Writer, LazyMixin, - ConcurrentWriteOperation, + LockedFD, join_path_native ) @@ -89,18 +89,18 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): if attr == "entries": # read the current index # try memory map for speed + lfd = LockedFD(self._file_path) try: - fp = open(self._file_path, "rb") - except IOError: + stream = lfd.open(write=False, stream=True) + except OSError: + lfd.rollback() # in new repositories, there may be no index, which means we are empty self.entries = dict() return # END exception handling - stream = fp try: - raise Exception() - stream = mmap.mmap(fp.fileno(), 0, access=mmap.ACCESS_READ) + stream = mmap.mmap(stream.fileno(), 0, access=mmap.ACCESS_READ) except Exception: pass # END memory mapping @@ -108,12 +108,8 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): try: self._deserialize(stream) finally: - pass - # make sure we close the stream ( possibly an mmap ) - # and the file - #stream.close() - #if stream is not fp: - # fp.close() + lfd.rollback() + # The handles will be closed on desctruction # END read from default index on demand else: super(IndexFile, self)._set_cache_(attr) @@ -267,12 +263,12 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): Note Index writing based on the dulwich implementation """ - write_op = ConcurrentWriteOperation(file_path or self._file_path) - stream = write_op._begin_writing() + lfd = LockedFD(file_path or self._file_path) + stream = lfd.open(write=True, stream=True) self._serialize(stream, ignore_tree_extension_data) - write_op._end_writing() + lfd.commit() # make sure we represent what we have written if file_path is not None: -- cgit v1.2.1